tlang/source/tlang/compiler/codegen/instruction.d

482 lines
10 KiB
D
Raw Normal View History

2021-10-26 16:17:53 +01:00
module compiler.codegen.instruction;
import std.conv : to;
import compiler.typecheck.dependency.core : Context;
import std.string : cmp;
import compiler.symbols.data : SymbolType;
import compiler.symbols.check : getCharacter;
import gogga;
import compiler.symbols.typing.core : Type;
2021-10-26 16:17:53 +01:00
public class Instruction
{
2021-11-09 13:49:59 +00:00
/* Context for the Instruction (used in emitter for name resolution) */
2021-11-09 17:00:23 +00:00
public Context context;
2021-11-09 13:49:59 +00:00
2021-10-26 16:17:53 +01:00
protected string addInfo;
this()
{
// this.instructionName = instructionName;
}
public final override string toString()
{
return "[Instruction: "~this.classinfo.name~":"~addInfo~"]";
}
public final Context getContext()
{
return context;
}
2021-10-26 16:17:53 +01:00
}
public class FetchInst : Instruction
{
}
public class Value : Instruction
{
}
public class StorageDeclaration : Instruction
2021-10-26 16:17:53 +01:00
{
}
public class ClassStaticInitAllocate : Instruction
{
this(string className)
{
addInfo = "classStaticInitAllocate: "~className;
}
}
2021-10-26 19:32:47 +01:00
public class VariableAssignmentInstr : Instruction
{
/* Name of variable being declared */
public string varName; /*TODO: Might not be needed */
public const Instruction data;
2021-10-26 19:32:47 +01:00
this(string varName, Instruction data)
{
this.varName = varName;
this.data = data;
addInfo = "assignTo: "~varName~", valInstr: "~data.toString();
2021-10-26 19:32:47 +01:00
}
}
public final class VariableDeclaration : StorageDeclaration
2021-10-26 16:17:53 +01:00
{
/* Name of variable being declared */
public const string varName;
2021-10-26 16:17:53 +01:00
/* Length */
public const byte length;
2021-10-26 16:17:53 +01:00
/* Type of the variable being declared */
public const Type varType;
Lexer - Fixed missing flushing for issue #65 (see "Flushing fix ✅") - Added unit test for flushing fix VariableDeclaration (Instruction) - Added support for the embedding of a VariableAssignmentInstr inside (added a getter too) (a part of issue #66) - Conditional support for if statements: Added two new instructions (IfStatementInstruction and BranchInstruction). See issue #64 DGen - Added depth increment/decrement on enter/leave scope of `transform()` - Correct tabbing for nested if-statements using new method `genTabs(ulong)` (which uses the above mechanism). Makes code emitted for if statements (issue #64) look nicer. - Updated VariableDeclarations (with assignments) handling in `transform()` in the manner similar to BinOpInstr (see issue #66) - Added a TODO for formatting BinOpInstr's `transform()` a little more aesthetically nicer - Added code emitting support for if statements (the `IfStatementInstruction` instruction) (see issue #64) - Updated `emitEntryPoint()` to only emit testing C code for the correct input test file Parser - `parseIf()` now returns an instance of IfStatement which couples multiple `Branch` objects consisting of `Statement[]` and `Expression` - Ensured that each `Statement` of the generated `Statement[]` from `parseBody()` for a given `Branch` is parented to said Branch using `parentToContainer()` - Ensured each generated `Branch` in `Branch[]` is parented to the generated `IfStatement` using `parentToContainer()` - `parseBody()` now adds to its `Statement[]` build-up array the generated `IfStatement` from the call to `parseIf()` Check - Added support for back-mapping `SymbolType.EQUALS` to `getCharacter(SymbolType)` Data - Added `Branch` parser node which is a Container for body statements (`Statement[]`) - Added `IfStatement` parser node which is a Container of `Statement[]` which are actually `Branch[]` TypeChecker - Moved import for `reverse` to top of module - Implemented `tailPopInstr()` method which will pop from the back of the `codeQueue` "scratchpad" - Fixes handling of `StaticVariableDeclaration` and `VariableAssignmentNode` (fixes issue #66) - Added handling for IfStatement entities (if statement support #64) Resolution - Added a debug statement to `resolveUp(Container, string)` to print out the container to lookup from and the name being looked up Dependency - Added a default `toString()` to the DNode class which prints `[DNode: <entity toString()]` - Added a TODO and debug print related to issues #9 - Disabled InitScope.STATIC check for now as it caused issues with if statement parsing (probably due to VIRTUAL being default and therefore skipping if statment processing) - issue #69 - Cleaned up handling of Entity type `Variable` (variable declarations) - removed repeated code - Undid the VarAss->(depends on)->VarDec, reverted back to VarDec->(depends on)->VarAss, fixed by #66 (and closes it and #11) - Added support for `IfStatement` (if statements) in `generalPass(Container, Context)` Test cases - Added new test case testing nested if statements (`nested_conditions.t`) - Added another test case for if statements, `simple_conditions.t`
2022-12-19 13:37:55 +00:00
/* VariableAssignmentInstr-instruction to be assigned */
private VariableAssignmentInstr varAssInstr;
//TODO: This must take in type information
this(string varName, byte len, Type varType, VariableAssignmentInstr varAssInstr)
2021-10-26 16:17:53 +01:00
{
this.varName = varName;
this.length = len;
this.varType = varType;
2021-10-26 19:32:47 +01:00
Lexer - Fixed missing flushing for issue #65 (see "Flushing fix ✅") - Added unit test for flushing fix VariableDeclaration (Instruction) - Added support for the embedding of a VariableAssignmentInstr inside (added a getter too) (a part of issue #66) - Conditional support for if statements: Added two new instructions (IfStatementInstruction and BranchInstruction). See issue #64 DGen - Added depth increment/decrement on enter/leave scope of `transform()` - Correct tabbing for nested if-statements using new method `genTabs(ulong)` (which uses the above mechanism). Makes code emitted for if statements (issue #64) look nicer. - Updated VariableDeclarations (with assignments) handling in `transform()` in the manner similar to BinOpInstr (see issue #66) - Added a TODO for formatting BinOpInstr's `transform()` a little more aesthetically nicer - Added code emitting support for if statements (the `IfStatementInstruction` instruction) (see issue #64) - Updated `emitEntryPoint()` to only emit testing C code for the correct input test file Parser - `parseIf()` now returns an instance of IfStatement which couples multiple `Branch` objects consisting of `Statement[]` and `Expression` - Ensured that each `Statement` of the generated `Statement[]` from `parseBody()` for a given `Branch` is parented to said Branch using `parentToContainer()` - Ensured each generated `Branch` in `Branch[]` is parented to the generated `IfStatement` using `parentToContainer()` - `parseBody()` now adds to its `Statement[]` build-up array the generated `IfStatement` from the call to `parseIf()` Check - Added support for back-mapping `SymbolType.EQUALS` to `getCharacter(SymbolType)` Data - Added `Branch` parser node which is a Container for body statements (`Statement[]`) - Added `IfStatement` parser node which is a Container of `Statement[]` which are actually `Branch[]` TypeChecker - Moved import for `reverse` to top of module - Implemented `tailPopInstr()` method which will pop from the back of the `codeQueue` "scratchpad" - Fixes handling of `StaticVariableDeclaration` and `VariableAssignmentNode` (fixes issue #66) - Added handling for IfStatement entities (if statement support #64) Resolution - Added a debug statement to `resolveUp(Container, string)` to print out the container to lookup from and the name being looked up Dependency - Added a default `toString()` to the DNode class which prints `[DNode: <entity toString()]` - Added a TODO and debug print related to issues #9 - Disabled InitScope.STATIC check for now as it caused issues with if statement parsing (probably due to VIRTUAL being default and therefore skipping if statment processing) - issue #69 - Cleaned up handling of Entity type `Variable` (variable declarations) - removed repeated code - Undid the VarAss->(depends on)->VarDec, reverted back to VarDec->(depends on)->VarAss, fixed by #66 (and closes it and #11) - Added support for `IfStatement` (if statements) in `generalPass(Container, Context)` Test cases - Added new test case testing nested if statements (`nested_conditions.t`) - Added another test case for if statements, `simple_conditions.t`
2022-12-19 13:37:55 +00:00
this.varAssInstr = varAssInstr;
2021-10-26 19:32:47 +01:00
addInfo = "varName: "~varName;
2021-10-26 16:17:53 +01:00
}
Lexer - Fixed missing flushing for issue #65 (see "Flushing fix ✅") - Added unit test for flushing fix VariableDeclaration (Instruction) - Added support for the embedding of a VariableAssignmentInstr inside (added a getter too) (a part of issue #66) - Conditional support for if statements: Added two new instructions (IfStatementInstruction and BranchInstruction). See issue #64 DGen - Added depth increment/decrement on enter/leave scope of `transform()` - Correct tabbing for nested if-statements using new method `genTabs(ulong)` (which uses the above mechanism). Makes code emitted for if statements (issue #64) look nicer. - Updated VariableDeclarations (with assignments) handling in `transform()` in the manner similar to BinOpInstr (see issue #66) - Added a TODO for formatting BinOpInstr's `transform()` a little more aesthetically nicer - Added code emitting support for if statements (the `IfStatementInstruction` instruction) (see issue #64) - Updated `emitEntryPoint()` to only emit testing C code for the correct input test file Parser - `parseIf()` now returns an instance of IfStatement which couples multiple `Branch` objects consisting of `Statement[]` and `Expression` - Ensured that each `Statement` of the generated `Statement[]` from `parseBody()` for a given `Branch` is parented to said Branch using `parentToContainer()` - Ensured each generated `Branch` in `Branch[]` is parented to the generated `IfStatement` using `parentToContainer()` - `parseBody()` now adds to its `Statement[]` build-up array the generated `IfStatement` from the call to `parseIf()` Check - Added support for back-mapping `SymbolType.EQUALS` to `getCharacter(SymbolType)` Data - Added `Branch` parser node which is a Container for body statements (`Statement[]`) - Added `IfStatement` parser node which is a Container of `Statement[]` which are actually `Branch[]` TypeChecker - Moved import for `reverse` to top of module - Implemented `tailPopInstr()` method which will pop from the back of the `codeQueue` "scratchpad" - Fixes handling of `StaticVariableDeclaration` and `VariableAssignmentNode` (fixes issue #66) - Added handling for IfStatement entities (if statement support #64) Resolution - Added a debug statement to `resolveUp(Container, string)` to print out the container to lookup from and the name being looked up Dependency - Added a default `toString()` to the DNode class which prints `[DNode: <entity toString()]` - Added a TODO and debug print related to issues #9 - Disabled InitScope.STATIC check for now as it caused issues with if statement parsing (probably due to VIRTUAL being default and therefore skipping if statment processing) - issue #69 - Cleaned up handling of Entity type `Variable` (variable declarations) - removed repeated code - Undid the VarAss->(depends on)->VarDec, reverted back to VarDec->(depends on)->VarAss, fixed by #66 (and closes it and #11) - Added support for `IfStatement` (if statements) in `generalPass(Container, Context)` Test cases - Added new test case testing nested if statements (`nested_conditions.t`) - Added another test case for if statements, `simple_conditions.t`
2022-12-19 13:37:55 +00:00
public VariableAssignmentInstr getAssignmentInstr()
{
return varAssInstr;
}
2021-10-26 16:17:53 +01:00
}
public final class FetchValueVar : Value
{
/* Name of variable to fetch from */
public string varName;
/* Length */
public byte length;
this(string varName, byte len)
{
this.varName = varName;
this.length = len;
addInfo = "fetchVarValName: "~varName~", VarLen: "~to!(string)(length);
2021-10-26 16:17:53 +01:00
}
}
/* Used for integers */
2021-10-26 16:17:53 +01:00
public final class LiteralValue : Value
{
/* Data */
public ulong data;
public byte len;
this(ulong data, byte len)
{
this.data = data;
this.len = len;
addInfo = "Data: "~to!(string)(data)~", Length: "~to!(string)(len);
}
}
public final class LiteralValueFloat : Value
{
/* Data */
public double data; /* TODO: Is this best way to store? Consirring floats/doubles */
public byte len;
this(double data, byte len)
{
this.data = data;
this.len = len;
2021-10-26 16:17:53 +01:00
addInfo = "Data: "~to!(string)(data)~", Length: "~to!(string)(len);
}
}
2022-07-25 18:30:07 +01:00
/* FIXME: Implement this */
/**
* TODO: This should take in:
*
* 1. The string literal
* 2. It should assign it to an interning pool and get the ID (associate one with the string literal if equal/in-the-pool)
*/
public final class StringLiteral : Value
{
/* String interning pool */
private static int[string] internmentCamp;
private static int rollCount = 0;
private string stringLiteral;
this(string stringLiteral)
2022-07-25 18:30:07 +01:00
{
this.stringLiteral = stringLiteral;
/* Intern the string */
intern(stringLiteral);
addInfo = "StrLit: `"~stringLiteral~"`, InternID: "~to!(string)(intern(stringLiteral));
}
2022-07-25 18:30:07 +01:00
public static int intern(string strLit)
{
/* Search for the string (if it exists return it's pool ID) */
foreach(string curStrLit; internmentCamp.keys())
{
if(cmp(strLit, curStrLit) == 0)
{
return internmentCamp[strLit];
}
}
/* If not, create a new entry (pool it) and return */
internmentCamp[strLit] = rollCount;
rollCount++; /* TODO: Overflow check */
return rollCount-1;
}
public string getStringLiteral()
{
return stringLiteral;
2022-07-25 18:30:07 +01:00
}
}
2021-10-26 16:17:53 +01:00
/**
2021-10-26 21:03:48 +01:00
* BinOpInstr instruction
2021-10-26 16:17:53 +01:00
*
2021-10-26 21:03:48 +01:00
* Any sort of Binary Operator
2021-10-26 16:17:53 +01:00
*/
public class BinOpInstr : Value
2021-10-26 16:17:53 +01:00
{
public const Instruction lhs;
public const Instruction rhs;
public const SymbolType operator;
2021-10-26 16:17:53 +01:00
2021-10-26 21:03:48 +01:00
this(Instruction lhs, Instruction rhs, SymbolType operator)
2021-10-26 16:17:53 +01:00
{
this.lhs = lhs;
this.rhs = rhs;
this.operator = operator;
2021-10-26 21:03:48 +01:00
addInfo = "BinOpType: "~to!(string)(operator)~", LhsValInstr: "~lhs.toString()~", RhsValInstr: "~rhs.toString();
2021-10-26 16:17:53 +01:00
}
}
2022-04-12 09:52:18 +01:00
/**
* UnaryOpInstr instruction
*
* Any sort of Unary Operator
*/
public class UnaryOpInstr : Value
{
private Instruction exp;
private SymbolType operator;
this(Instruction exp, SymbolType operator)
{
this.exp = exp;
this.operator = operator;
2022-04-12 09:52:18 +01:00
addInfo = "UnaryOpType: "~to!(string)(operator)~", Instr: "~exp.toString();
}
Instruction - Added `getOperator()` and `getOperand()` methods to `UnaryOpInstr` - Added new instruction `PointerDereferenceAssignmentInstruction` for pointer support DGen - Updated `transform()` to emit code for instruction type `UnaryOpInstr` - Updated `transform()` to emit code for instruction type `PointerDereferenceAssignmentInstruction` - Added testing emit code in `emitEntryPoint()` for pointer testing Parser - Updated `parseName()` to trigger `parseTypedDeclaration()` on occurene of `SymbolType.STAR` (for pointer type declarations) - Added pointer-type support for function parameters (so far only single) in `parseFuncDef()` - `parseExpression()` terminates on occurence of a single `=` (ASSIGN) operator - Declaring of pointers of any depth implemented in `parseTypedDeclaration()` - Added support for pointer dereferncing assignments with the addition of `parseDerefAssignment()` - `parseStatement()` will now call `parseDerefAssignment()` on occurence of a `SymbolType.STAR` - Added a unittest for testing pointers - Finished unittest for for loops Check - Added backmapping for `SymbolType.ASSIGN` -> `&` Data - Added new parser node type `PointerDereferenceAssignment` for pointer support in the parser TypeChecker - Because function parameters are type che cked upon function call I had to add typechecking code for pointer support in the `UnaryOperatorExpression` case - Added code generation support for `PointerDereferenceAssignment` type Dependency - Added support for `PointerDereferenceAssignment` type (pointer support) to `generalStatement()` Tests - Added pointer test `simple_pointer.t`
2023-01-12 08:53:48 +00:00
public SymbolType getOperator()
{
return operator;
}
public Instruction getOperand()
{
return exp;
}
2022-04-12 09:52:18 +01:00
}
/**
* 2022 New things
*
*/
//public class CallInstr : Instruction
public class CallInstr : Value
{
}
public class FuncCallInstr : CallInstr
{
/* Per-argument instrructions */
private Value[] evaluationInstructions;
public const string functionName;
this(string functionName, ulong argEvalInstrsSize)
{
this.functionName = functionName;
evaluationInstructions.length = argEvalInstrsSize;
updateAddInfo();
}
/**
* FuncCallInstr is built-bit-by-bit so toString information will change
*/
private void updateAddInfo()
{
addInfo = "FunctionName: "~functionName ~" EvalInstrs: "~ to!(string)(getEvaluationInstructions());
}
public void setEvalInstr(ulong argPos, Value instr)
{
evaluationInstructions[argPos] = instr;
updateAddInfo();
}
public Value[] getEvaluationInstructions()
{
return evaluationInstructions;
}
}
public final class ReturnInstruction : Instruction
{
private Value returnExprInstr;
this(Value returnExprInstr)
{
this.returnExprInstr = returnExprInstr;
}
public Value getReturnExpInstr()
{
return returnExprInstr;
}
Lexer - Fixed missing flushing for issue #65 (see "Flushing fix ✅") - Added unit test for flushing fix VariableDeclaration (Instruction) - Added support for the embedding of a VariableAssignmentInstr inside (added a getter too) (a part of issue #66) - Conditional support for if statements: Added two new instructions (IfStatementInstruction and BranchInstruction). See issue #64 DGen - Added depth increment/decrement on enter/leave scope of `transform()` - Correct tabbing for nested if-statements using new method `genTabs(ulong)` (which uses the above mechanism). Makes code emitted for if statements (issue #64) look nicer. - Updated VariableDeclarations (with assignments) handling in `transform()` in the manner similar to BinOpInstr (see issue #66) - Added a TODO for formatting BinOpInstr's `transform()` a little more aesthetically nicer - Added code emitting support for if statements (the `IfStatementInstruction` instruction) (see issue #64) - Updated `emitEntryPoint()` to only emit testing C code for the correct input test file Parser - `parseIf()` now returns an instance of IfStatement which couples multiple `Branch` objects consisting of `Statement[]` and `Expression` - Ensured that each `Statement` of the generated `Statement[]` from `parseBody()` for a given `Branch` is parented to said Branch using `parentToContainer()` - Ensured each generated `Branch` in `Branch[]` is parented to the generated `IfStatement` using `parentToContainer()` - `parseBody()` now adds to its `Statement[]` build-up array the generated `IfStatement` from the call to `parseIf()` Check - Added support for back-mapping `SymbolType.EQUALS` to `getCharacter(SymbolType)` Data - Added `Branch` parser node which is a Container for body statements (`Statement[]`) - Added `IfStatement` parser node which is a Container of `Statement[]` which are actually `Branch[]` TypeChecker - Moved import for `reverse` to top of module - Implemented `tailPopInstr()` method which will pop from the back of the `codeQueue` "scratchpad" - Fixes handling of `StaticVariableDeclaration` and `VariableAssignmentNode` (fixes issue #66) - Added handling for IfStatement entities (if statement support #64) Resolution - Added a debug statement to `resolveUp(Container, string)` to print out the container to lookup from and the name being looked up Dependency - Added a default `toString()` to the DNode class which prints `[DNode: <entity toString()]` - Added a TODO and debug print related to issues #9 - Disabled InitScope.STATIC check for now as it caused issues with if statement parsing (probably due to VIRTUAL being default and therefore skipping if statment processing) - issue #69 - Cleaned up handling of Entity type `Variable` (variable declarations) - removed repeated code - Undid the VarAss->(depends on)->VarDec, reverted back to VarDec->(depends on)->VarAss, fixed by #66 (and closes it and #11) - Added support for `IfStatement` (if statements) in `generalPass(Container, Context)` Test cases - Added new test case testing nested if statements (`nested_conditions.t`) - Added another test case for if statements, `simple_conditions.t`
2022-12-19 13:37:55 +00:00
}
public final class IfStatementInstruction : Instruction
{
private BranchInstruction[] branchInstructions;
this(BranchInstruction[] branchInstructions)
{
this.branchInstructions = branchInstructions;
addInfo = "Branches: "~to!(string)(branchInstructions);
}
public BranchInstruction[] getBranchInstructions()
{
return branchInstructions;
}
}
public final class WhileLoopInstruction : Instruction
{
private BranchInstruction branchInstruction;
this(BranchInstruction branchInstruction)
{
this.branchInstruction = branchInstruction;
addInfo = "Branch: "~to!(string)(branchInstruction);
}
public BranchInstruction getBranchInstruction()
{
return branchInstruction;
}
}
Instruction - Added a new instruction, `ForLoop`, which contains a pre-run Instruction and a `Branch` instruction, coupled with some flags DGen - Added a TODO for WhileLoops (we need to implement do-while loops) - Implemented C code emitting in `emit()` for `ForLoop` instruction Check - Added missing back-mapping for `SymbolType.SMALLER_THAN` Data - Added new parser node type `ForLoop` Parser - Fixed typo in `parseWhile()` - Implemented `parseDoWhile()` for do-while loops - Implemented `parseFor()` for for-loops - Implemented `parseStatement()` for singular statement parsing - `parseStatement()` can now have the terminating symbol specified, defaults to `SymbolType.SEMICOLON` - `parseName()` and `parseAssignment()` now also accept a terminating symbol parameter as per `parseStatement()`'s behavior - `parseBody()` now makes multiple calls to `parseStatement()` for singular Statement parsing (dead code below still to be removed) - Removed commented-out unittests - Unittests that read from files now have the file source code embedded - Added unit test for while loops, for-loops (unfinished) and some other smaller language constructs (roughly 70% coverage) TypeChecker (CodeGen) - Do-while loops will fail if used (for now) - Added for-loop code generation Dependency - Implemented `generalStatement()` for statement processing - `generalPass()` now makes calls to `generalStatement()` Tests - Added `simple_for_loops.t` to test for-loops - Added `simple_do_while.t` to test do-while loops
2023-01-11 08:43:29 +00:00
public final class ForLoopInstruction : Instruction
{
private Instruction preRunInstruction;
private BranchInstruction branchInstruction;
private bool hasPostIterate;
this(BranchInstruction branchInstruction, Instruction preRunInstruction = null, bool hasPostIterate = false)
{
this.branchInstruction = branchInstruction;
this.preRunInstruction = preRunInstruction;
addInfo = (hasPreRunInstruction() ? "PreRun: "~to!(string)(preRunInstruction)~", " : "")~"Branch: "~to!(string)(branchInstruction);
this.hasPostIterate = hasPostIterate;
}
public bool hasPostIterationInstruction()
{
return hasPostIterate;
}
public Instruction getPreRunInstruction()
{
return preRunInstruction;
}
public bool hasPreRunInstruction()
{
return !(preRunInstruction is null);
}
public BranchInstruction getBranchInstruction()
{
return branchInstruction;
}
}
Lexer - Fixed missing flushing for issue #65 (see "Flushing fix ✅") - Added unit test for flushing fix VariableDeclaration (Instruction) - Added support for the embedding of a VariableAssignmentInstr inside (added a getter too) (a part of issue #66) - Conditional support for if statements: Added two new instructions (IfStatementInstruction and BranchInstruction). See issue #64 DGen - Added depth increment/decrement on enter/leave scope of `transform()` - Correct tabbing for nested if-statements using new method `genTabs(ulong)` (which uses the above mechanism). Makes code emitted for if statements (issue #64) look nicer. - Updated VariableDeclarations (with assignments) handling in `transform()` in the manner similar to BinOpInstr (see issue #66) - Added a TODO for formatting BinOpInstr's `transform()` a little more aesthetically nicer - Added code emitting support for if statements (the `IfStatementInstruction` instruction) (see issue #64) - Updated `emitEntryPoint()` to only emit testing C code for the correct input test file Parser - `parseIf()` now returns an instance of IfStatement which couples multiple `Branch` objects consisting of `Statement[]` and `Expression` - Ensured that each `Statement` of the generated `Statement[]` from `parseBody()` for a given `Branch` is parented to said Branch using `parentToContainer()` - Ensured each generated `Branch` in `Branch[]` is parented to the generated `IfStatement` using `parentToContainer()` - `parseBody()` now adds to its `Statement[]` build-up array the generated `IfStatement` from the call to `parseIf()` Check - Added support for back-mapping `SymbolType.EQUALS` to `getCharacter(SymbolType)` Data - Added `Branch` parser node which is a Container for body statements (`Statement[]`) - Added `IfStatement` parser node which is a Container of `Statement[]` which are actually `Branch[]` TypeChecker - Moved import for `reverse` to top of module - Implemented `tailPopInstr()` method which will pop from the back of the `codeQueue` "scratchpad" - Fixes handling of `StaticVariableDeclaration` and `VariableAssignmentNode` (fixes issue #66) - Added handling for IfStatement entities (if statement support #64) Resolution - Added a debug statement to `resolveUp(Container, string)` to print out the container to lookup from and the name being looked up Dependency - Added a default `toString()` to the DNode class which prints `[DNode: <entity toString()]` - Added a TODO and debug print related to issues #9 - Disabled InitScope.STATIC check for now as it caused issues with if statement parsing (probably due to VIRTUAL being default and therefore skipping if statment processing) - issue #69 - Cleaned up handling of Entity type `Variable` (variable declarations) - removed repeated code - Undid the VarAss->(depends on)->VarDec, reverted back to VarDec->(depends on)->VarAss, fixed by #66 (and closes it and #11) - Added support for `IfStatement` (if statements) in `generalPass(Container, Context)` Test cases - Added new test case testing nested if statements (`nested_conditions.t`) - Added another test case for if statements, `simple_conditions.t`
2022-12-19 13:37:55 +00:00
public final class BranchInstruction : Instruction
{
private Value branchConditionInstr;
private Instruction[] bodyInstructions;
this(Value conditionInstr, Instruction[] bodyInstructions)
{
this.branchConditionInstr = conditionInstr;
this.bodyInstructions = bodyInstructions;
addInfo = "CondInstr: "~to!(string)(branchConditionInstr)~", BBodyInstrs: "~to!(string)(bodyInstructions);
}
public bool hasConditionInstr()
{
return !(branchConditionInstr is null);
}
public Value getConditionInstr()
{
return branchConditionInstr;
}
public Instruction[] getBodyInstructions()
{
return bodyInstructions;
}
Instruction - Added `getOperator()` and `getOperand()` methods to `UnaryOpInstr` - Added new instruction `PointerDereferenceAssignmentInstruction` for pointer support DGen - Updated `transform()` to emit code for instruction type `UnaryOpInstr` - Updated `transform()` to emit code for instruction type `PointerDereferenceAssignmentInstruction` - Added testing emit code in `emitEntryPoint()` for pointer testing Parser - Updated `parseName()` to trigger `parseTypedDeclaration()` on occurene of `SymbolType.STAR` (for pointer type declarations) - Added pointer-type support for function parameters (so far only single) in `parseFuncDef()` - `parseExpression()` terminates on occurence of a single `=` (ASSIGN) operator - Declaring of pointers of any depth implemented in `parseTypedDeclaration()` - Added support for pointer dereferncing assignments with the addition of `parseDerefAssignment()` - `parseStatement()` will now call `parseDerefAssignment()` on occurence of a `SymbolType.STAR` - Added a unittest for testing pointers - Finished unittest for for loops Check - Added backmapping for `SymbolType.ASSIGN` -> `&` Data - Added new parser node type `PointerDereferenceAssignment` for pointer support in the parser TypeChecker - Because function parameters are type che cked upon function call I had to add typechecking code for pointer support in the `UnaryOperatorExpression` case - Added code generation support for `PointerDereferenceAssignment` type Dependency - Added support for `PointerDereferenceAssignment` type (pointer support) to `generalStatement()` Tests - Added pointer test `simple_pointer.t`
2023-01-12 08:53:48 +00:00
}
public final class PointerDereferenceAssignmentInstruction : Instruction
{
private Value pointerEvalInstr;
private Value assigmnetExprInstr;
private ulong derefCount;
this(Value pointerEvalInstr, Value assigmnetExprInstr, ulong derefCount)
{
this.pointerEvalInstr = pointerEvalInstr;
this.assigmnetExprInstr = assigmnetExprInstr;
this.derefCount = derefCount;
}
public Value getPointerEvalInstr()
{
return pointerEvalInstr;
}
public Value getAssExprInstr()
{
return assigmnetExprInstr;
}
public ulong getDerefCount()
{
return derefCount;
}
}
public final class DiscardInstruction : Instruction
{
private Value exprInstr;
this(Value exprInstr)
{
this.exprInstr = exprInstr;
}
public Value getExpressionInstruction()
{
return exprInstr;
}
}
public final class CastedValueInstruction : Value
{
/* The uncasted original instruction that must be executed-then-trimmed (casted) */
private Value uncastedValue;
private Type castToType;
this(Value uncastedValue, Type castToType)
{
this.uncastedValue = uncastedValue;
this.castToType = castToType;
}
public Value getEmbeddedInstruction()
{
return uncastedValue;
}
public Type getCastToType()
{
return castToType;
}
2021-10-26 16:17:53 +01:00
}