- Instead of returning the generated C emit code, set it in a local scope but at the highest scope variable `emmmmit`
This commit is contained in:
Tristan B. Velloza Kildaire 2023-08-17 08:46:59 +02:00
parent fd31024a07
commit ef0c817217
1 changed files with 55 additions and 42 deletions

View File

@ -141,6 +141,9 @@ public final class DCodeEmitter : CodeEmitter
gprintln("transform(): "~to!(string)(instruction)); gprintln("transform(): "~to!(string)(instruction));
transformDepth++; transformDepth++;
// The data to emit
string emmmmit;
// At any return decrement the depth // At any return decrement the depth
scope(exit) scope(exit)
{ {
@ -172,12 +175,12 @@ public final class DCodeEmitter : CodeEmitter
{ {
string renamedSymbol = mapper.symbolLookup(typedEntityVariable); string renamedSymbol = mapper.symbolLookup(typedEntityVariable);
return renamedSymbol~" = "~transform(varAs.data)~";"; emmmmit = renamedSymbol~" = "~transform(varAs.data)~";";
} }
/* If it is external */ /* If it is external */
else else
{ {
return typedEntityVariable.getName()~" = "~transform(varAs.data)~";"; emmmmit = typedEntityVariable.getName()~" = "~transform(varAs.data)~";";
} }
} }
/* VariableDeclaration */ /* VariableDeclaration */
@ -218,17 +221,18 @@ public final class DCodeEmitter : CodeEmitter
gprintln("VarDec(with assignment): My assignment type is: "~varAssInstr.getInstrType().getName()); gprintln("VarDec(with assignment): My assignment type is: "~varAssInstr.getInstrType().getName());
// Generate the code to emit // 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 */ /* If the variable is external */
else else
{ {
return "extern "~typeTransform(cast(Type)varDecInstr.varType)~" "~typedEntityVariable.getName()~";"; emmmmit = "extern "~typeTransform(cast(Type)varDecInstr.varType)~" "~typedEntityVariable.getName()~";";
} }
} }
/* LiteralValue */ /* LiteralValue */
else if(cast(LiteralValue)instruction) else if(cast(LiteralValue)instruction)
@ -237,7 +241,7 @@ public final class DCodeEmitter : CodeEmitter
LiteralValue literalValueInstr = cast(LiteralValue)instruction; LiteralValue literalValueInstr = cast(LiteralValue)instruction;
return to!(string)(literalValueInstr.getLiteralValue()); emmmmit = to!(string)(literalValueInstr.getLiteralValue());
} }
/* FetchValueVar */ /* FetchValueVar */
else if(cast(FetchValueVar)instruction) else if(cast(FetchValueVar)instruction)
@ -257,12 +261,12 @@ public final class DCodeEmitter : CodeEmitter
string renamedSymbol = mapper.symbolLookup(typedEntityVariable); string renamedSymbol = mapper.symbolLookup(typedEntityVariable);
return renamedSymbol; emmmmit = renamedSymbol;
} }
/* If it is external */ /* If it is external */
else else
{ {
return typedEntityVariable.getName(); emmmmit = typedEntityVariable.getName();
} }
} }
/* BinOpInstr */ /* BinOpInstr */
@ -320,7 +324,7 @@ public final class DCodeEmitter : CodeEmitter
cvInstr.setRelax(true); 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 */ /* FuncCallInstr */
else if(cast(FuncCallInstr)instruction) else if(cast(FuncCallInstr)instruction)
@ -370,7 +374,7 @@ public final class DCodeEmitter : CodeEmitter
emit ~= ";"; emit ~= ";";
} }
return emit; emmmmit = emit;
} }
/* ReturnInstruction */ /* ReturnInstruction */
else if(cast(ReturnInstruction)instruction) else if(cast(ReturnInstruction)instruction)
@ -384,7 +388,7 @@ public final class DCodeEmitter : CodeEmitter
/* Get the return expression instruction */ /* Get the return expression instruction */
Value returnExpressionInstr = returnInstruction.getReturnExpInstr(); Value returnExpressionInstr = returnInstruction.getReturnExpInstr();
return "return "~transform(returnExpressionInstr)~";"; emmmmit = "return "~transform(returnExpressionInstr)~";";
} }
/** /**
* If statements (IfStatementInstruction) * If statements (IfStatementInstruction)
@ -434,7 +438,7 @@ public final class DCodeEmitter : CodeEmitter
} }
} }
return emit; emmmmit = emit;
} }
/** /**
* While loops (WhileLoopInstruction) * While loops (WhileLoopInstruction)
@ -464,7 +468,7 @@ public final class DCodeEmitter : CodeEmitter
/* Closing curly brace */ /* Closing curly brace */
emit~=genTabs(transformDepth)~"}"; emit~=genTabs(transformDepth)~"}";
return emit; emmmmit = emit;
} }
/** /**
* For loops (ForLoopInstruction) * For loops (ForLoopInstruction)
@ -502,7 +506,7 @@ public final class DCodeEmitter : CodeEmitter
// Close curly (body end) // Close curly (body end)
emit~=genTabs(transformDepth)~"}"; emit~=genTabs(transformDepth)~"}";
return emit; emmmmit = emit;
} }
/** /**
* Unary operators (UnaryOpInstr) * Unary operators (UnaryOpInstr)
@ -521,7 +525,7 @@ public final class DCodeEmitter : CodeEmitter
/* Transform the operand */ /* Transform the operand */
emit ~= transform(operandInstruction); emit ~= transform(operandInstruction);
return emit; emmmmit = emit;
} }
/** /**
* Pointer dereference assignment (PointerDereferenceAssignmentInstruction) * Pointer dereference assignment (PointerDereferenceAssignmentInstruction)
@ -551,7 +555,7 @@ public final class DCodeEmitter : CodeEmitter
emit ~= transform(rhsAssExprInstr)~";"; emit ~= transform(rhsAssExprInstr)~";";
return emit; emmmmit = emit;
} }
/** /**
* Discard instruction (DiscardInstruction) * Discard instruction (DiscardInstruction)
@ -566,7 +570,7 @@ public final class DCodeEmitter : CodeEmitter
/* Transform the expression */ /* Transform the expression */
emit ~= transform(valueInstruction)~";"; emit ~= transform(valueInstruction)~";";
return emit; emmmmit = emit;
} }
/** /**
* Type casting instruction (CastedValueInstruction) * Type casting instruction (CastedValueInstruction)
@ -593,29 +597,27 @@ public final class DCodeEmitter : CodeEmitter
{ {
/* The original expression */ /* The original expression */
emit ~= transform(uncastedInstruction); 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 else
{ {
// TODO: Implement this /* Handling of primitive types */
gprintln("Non-primitive type casting not yet implemented", DebugType.ERROR); if(cast(Primitive)castingTo)
assert(false); {
/* 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);
}
} }
emmmmit = emit;
return emit;
} }
/** /**
* Array indexing (pointer-based arrays) * Array indexing (pointer-based arrays)
@ -655,7 +657,7 @@ public final class DCodeEmitter : CodeEmitter
// return "*("~transform(indexed)~"+"~transform(index)~")"; // return "*("~transform(indexed)~"+"~transform(index)~")";
return emit; emmmmit = emit;
} }
/** /**
* Array assignments (pointer-based arrays) * Array assignments (pointer-based arrays)
@ -701,7 +703,7 @@ public final class DCodeEmitter : CodeEmitter
emit ~= ";"; emit ~= ";";
return emit; emmmmit = emit;
} }
/** /**
* Array indexing (stack-based arrays) * Array indexing (stack-based arrays)
@ -741,7 +743,7 @@ public final class DCodeEmitter : CodeEmitter
// return "(TODO: Stack-array index emit)"; // return "(TODO: Stack-array index emit)";
return emit; emmmmit = emit;
} }
/** /**
* Array assignments (stack-based arrays) * Array assignments (stack-based arrays)
@ -785,12 +787,23 @@ public final class DCodeEmitter : CodeEmitter
// return "(StackArrAssignmentInstr: TODO)"; // return "(StackArrAssignmentInstr: TODO)";
return emit; emmmmit = emit;
} }
// TODO: MAAAAN we don't even have this yet // TODO: MAAAAN we don't even have this yet
// else if(cast(StringExpression)) // 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 = "<TODO: Base emit: "~to!(string)(instruction)~">";
}
return "<TODO: Base emit: "~to!(string)(instruction)~">"; return emmmmit;
} }