From ef0c8172177dbd9e93e4a4a4201b7e48836703ff Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Thu, 17 Aug 2023 08:46:59 +0200 Subject: [PATCH] DGen - Instead of returning the generated C emit code, set it in a local scope but at the highest scope variable `emmmmit` --- source/tlang/compiler/codegen/emit/dgen.d | 97 +++++++++++++---------- 1 file changed, 55 insertions(+), 42 deletions(-) diff --git a/source/tlang/compiler/codegen/emit/dgen.d b/source/tlang/compiler/codegen/emit/dgen.d index d568986..449ea8b 100644 --- a/source/tlang/compiler/codegen/emit/dgen.d +++ b/source/tlang/compiler/codegen/emit/dgen.d @@ -141,6 +141,9 @@ public final class DCodeEmitter : CodeEmitter gprintln("transform(): "~to!(string)(instruction)); transformDepth++; + // The data to emit + string emmmmit; + // At any return decrement the depth scope(exit) { @@ -172,12 +175,12 @@ public final class DCodeEmitter : CodeEmitter { string renamedSymbol = mapper.symbolLookup(typedEntityVariable); - return renamedSymbol~" = "~transform(varAs.data)~";"; + emmmmit = renamedSymbol~" = "~transform(varAs.data)~";"; } /* If it is external */ else { - return typedEntityVariable.getName()~" = "~transform(varAs.data)~";"; + emmmmit = typedEntityVariable.getName()~" = "~transform(varAs.data)~";"; } } /* VariableDeclaration */ @@ -218,17 +221,18 @@ public final class DCodeEmitter : CodeEmitter gprintln("VarDec(with assignment): My assignment type is: "~varAssInstr.getInstrType().getName()); // Generate the code to emit - return typeTransform(cast(Type)varDecInstr.varType)~" "~renamedSymbol~" = "~transform(varAssInstr)~";"; + emmmmit = typeTransform(cast(Type)varDecInstr.varType)~" "~renamedSymbol~" = "~transform(varAssInstr)~";"; + } + else + { + emmmmit = typeTransform(cast(Type)varDecInstr.varType)~" "~renamedSymbol~";"; } - - return typeTransform(cast(Type)varDecInstr.varType)~" "~renamedSymbol~";"; } /* If the variable is external */ else { - return "extern "~typeTransform(cast(Type)varDecInstr.varType)~" "~typedEntityVariable.getName()~";"; + emmmmit = "extern "~typeTransform(cast(Type)varDecInstr.varType)~" "~typedEntityVariable.getName()~";"; } - } /* LiteralValue */ else if(cast(LiteralValue)instruction) @@ -237,7 +241,7 @@ public final class DCodeEmitter : CodeEmitter LiteralValue literalValueInstr = cast(LiteralValue)instruction; - return to!(string)(literalValueInstr.getLiteralValue()); + emmmmit = to!(string)(literalValueInstr.getLiteralValue()); } /* FetchValueVar */ else if(cast(FetchValueVar)instruction) @@ -257,12 +261,12 @@ public final class DCodeEmitter : CodeEmitter string renamedSymbol = mapper.symbolLookup(typedEntityVariable); - return renamedSymbol; + emmmmit = renamedSymbol; } /* If it is external */ else { - return typedEntityVariable.getName(); + emmmmit = typedEntityVariable.getName(); } } /* BinOpInstr */ @@ -320,7 +324,7 @@ public final class DCodeEmitter : CodeEmitter cvInstr.setRelax(true); } - return transform(binOpInstr.lhs)~to!(string)(getCharacter(binOpInstr.operator))~transform(binOpInstr.rhs); + emmmmit = transform(binOpInstr.lhs)~to!(string)(getCharacter(binOpInstr.operator))~transform(binOpInstr.rhs); } /* FuncCallInstr */ else if(cast(FuncCallInstr)instruction) @@ -370,7 +374,7 @@ public final class DCodeEmitter : CodeEmitter emit ~= ";"; } - return emit; + emmmmit = emit; } /* ReturnInstruction */ else if(cast(ReturnInstruction)instruction) @@ -384,7 +388,7 @@ public final class DCodeEmitter : CodeEmitter /* Get the return expression instruction */ Value returnExpressionInstr = returnInstruction.getReturnExpInstr(); - return "return "~transform(returnExpressionInstr)~";"; + emmmmit = "return "~transform(returnExpressionInstr)~";"; } /** * If statements (IfStatementInstruction) @@ -434,7 +438,7 @@ public final class DCodeEmitter : CodeEmitter } } - return emit; + emmmmit = emit; } /** * While loops (WhileLoopInstruction) @@ -464,7 +468,7 @@ public final class DCodeEmitter : CodeEmitter /* Closing curly brace */ emit~=genTabs(transformDepth)~"}"; - return emit; + emmmmit = emit; } /** * For loops (ForLoopInstruction) @@ -502,7 +506,7 @@ public final class DCodeEmitter : CodeEmitter // Close curly (body end) emit~=genTabs(transformDepth)~"}"; - return emit; + emmmmit = emit; } /** * Unary operators (UnaryOpInstr) @@ -521,7 +525,7 @@ public final class DCodeEmitter : CodeEmitter /* Transform the operand */ emit ~= transform(operandInstruction); - return emit; + emmmmit = emit; } /** * Pointer dereference assignment (PointerDereferenceAssignmentInstruction) @@ -551,7 +555,7 @@ public final class DCodeEmitter : CodeEmitter emit ~= transform(rhsAssExprInstr)~";"; - return emit; + emmmmit = emit; } /** * Discard instruction (DiscardInstruction) @@ -566,7 +570,7 @@ public final class DCodeEmitter : CodeEmitter /* Transform the expression */ emit ~= transform(valueInstruction)~";"; - return emit; + emmmmit = emit; } /** * Type casting instruction (CastedValueInstruction) @@ -593,29 +597,27 @@ public final class DCodeEmitter : CodeEmitter { /* The original expression */ emit ~= transform(uncastedInstruction); - return emit; - } - - /* Handling of primitive types */ - if(cast(Primitive)castingTo) - { - /* Add the actual cast */ - emit ~= "("~typeTransform(castingTo)~")"; - - /* The expression being casted */ - emit ~= transform(uncastedInstruction); } else { - // TODO: Implement this - gprintln("Non-primitive type casting not yet implemented", DebugType.ERROR); - assert(false); + /* Handling of primitive types */ + if(cast(Primitive)castingTo) + { + /* Add the actual cast */ + emit ~= "("~typeTransform(castingTo)~")"; + + /* The expression being casted */ + emit ~= transform(uncastedInstruction); + } + else + { + // TODO: Implement this + gprintln("Non-primitive type casting not yet implemented", DebugType.ERROR); + assert(false); + } } - - - - return emit; + emmmmit = emit; } /** * Array indexing (pointer-based arrays) @@ -655,7 +657,7 @@ public final class DCodeEmitter : CodeEmitter // return "*("~transform(indexed)~"+"~transform(index)~")"; - return emit; + emmmmit = emit; } /** * Array assignments (pointer-based arrays) @@ -701,7 +703,7 @@ public final class DCodeEmitter : CodeEmitter emit ~= ";"; - return emit; + emmmmit = emit; } /** * Array indexing (stack-based arrays) @@ -741,7 +743,7 @@ public final class DCodeEmitter : CodeEmitter // return "(TODO: Stack-array index emit)"; - return emit; + emmmmit = emit; } /** * Array assignments (stack-based arrays) @@ -785,12 +787,23 @@ public final class DCodeEmitter : CodeEmitter // return "(StackArrAssignmentInstr: TODO)"; - return emit; + emmmmit = emit; } // TODO: MAAAAN we don't even have this yet // else if(cast(StringExpression)) + /** + * Unsupported instruction + * + * If you get here then normally it's because + * you didn't implement a transformation for + * an instruction yet. + */ + else + { + emmmmit = ""; + } - return ""; + return emmmmit; }