This commit is contained in:
Tristan B. Velloza Kildaire 2021-10-26 17:17:53 +02:00
parent b9adbc6663
commit 129860fc37
4 changed files with 203 additions and 2 deletions

View File

@ -0,0 +1,96 @@
module compiler.codegen.instruction;
import std.conv : to;
public class Instruction
{
protected string addInfo;
this()
{
// this.instructionName = instructionName;
}
public final override string toString()
{
return "[Instruction: "~this.classinfo.name~":"~addInfo~"]";
}
}
public class FetchInst : Instruction
{
}
public class Value : Instruction
{
}
public class StorageDeclaratio : Instruction
{
}
public final class VariableDeclaration : StorageDeclaratio
{
/* Name of variable being declared */
public string varName;
/* Length */
public byte length;
this(string varName, byte len)
{
this.varName = varName;
this.length = len;
}
}
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;
}
}
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);
}
}
/**
* Addition instruction
*
* Type: integers
* Signedness: unsigned/signed (two's complement)
*/
public class AddInstr : Instruction
{
private Instruction lhs;
private Instruction rhs;
this(Instruction lhs, Instruction rhs)
{
this.lhs = lhs;
this.rhs = rhs;
}
}

View File

@ -61,6 +61,12 @@ public class BinaryOperatorExpression : OperatorExpression
{
return rhs;
}
public override string toString()
{
/* TODO: FIll in */
return "[BinOpExp: "~"TODO"~"]";
}
}
public class NumberLiteral : Expression
@ -73,6 +79,11 @@ public class NumberLiteral : Expression
this.numberLiteral = numberLiteral;
}
public string getNumber()
{
return numberLiteral;
}
public override string toString()
{
return "[numberLiteral: "~numberLiteral~"]";

View File

@ -125,9 +125,39 @@ public final class TypeChecker
import compiler.typecheck.dependency;
import std.container.slist;
import compiler.codegen.instruction;
private SList!(Instruction) codeQueue;
public void addInstr(Instruction inst)
{
codeQueue.insertAfter(codeQueue[], inst);
}
public Instruction popInstr()
{
Instruction poppedInstr = codeQueue.front();
codeQueue.removeFront();
return poppedInstr;
}
/*
* Prints the current contents of the code-queue
*/
public void printCodeQueue()
{
import std.range : walkLength;
ulong i = 0;
foreach(Instruction instruction; codeQueue)
{
gprintln(to!(string)(i)~"/"~to!(string)(walkLength(codeQueue[]))~": "~instruction.toString());
i++;
}
}
private SList!(Type) typeStack;
private void addType(Type typeName)
{
typeStack.insertAfter(typeStack[], typeName);
@ -144,6 +174,8 @@ public final class TypeChecker
public void typeCheckThing(DNode dnode)
{
gprintln("typeCheckThing(): "~dnode.toString());
/* ExpressionDNodes */
if(cast(compiler.typecheck.expression.ExpressionDNode)dnode)
{
@ -157,8 +189,19 @@ public final class TypeChecker
if(cast(NumberLiteral)statement)
{
/* TODO: For now */
/**
* Typechecking
*/
gprintln("NUMBER LIT");
addType(getType(modulle, "int"));
/**
* Codegen
*/
ulong i = to!(ulong)((cast(NumberLiteral)statement).getNumber());
LiteralValue litValInstr = new LiteralValue(i, 4);
addInstr(litValInstr);
}
else if(cast(StringExpression)statement)
{
@ -170,12 +213,44 @@ public final class TypeChecker
{
auto g = cast(VariableExpression)statement;
auto gVar = cast(TypedEntity)resolver.resolveBest(g.getContext().getContainer(), g.getName());
string variableName = resolver.generateName(modulle, gVar);
/* TODO: Above TYpedEntity check */
/* TODO: still wip the expresison parser */
/* TODO: TYpe needs ansatz too `.updateName()` call */
addType(getType(gVar.getContext().getContainer(), gVar.getType()));
/**
* Codegen
*
* FIXME: Add type info, length
*/
FetchValueVar fVV = new FetchValueVar(variableName, 4);
addInstr(fVV);
}
// else if(cast()) !!!! Continue here
else if(cast(BinaryOperatorExpression)statement)
{
/**
* Typechecking (TODO)
*/
/**
* Codegen
*
* Retrieve the two Value Instructions
*/
Instruction vLhsInstr = popInstr();
printCodeQueue();
Instruction vRhsInstr = popInstr();
AddInstr addInst = new AddInstr(vLhsInstr, vRhsInstr);
addInstr(addInst);
}
}
@ -195,6 +270,9 @@ public final class TypeChecker
{
gprintln("Process: "~to!(string)(node));
/* Print the code queue each time */
printCodeQueue();
/**
* Now depending on thr DNode type we should
* place ambiguous intems on stack then
@ -221,7 +299,16 @@ public final class TypeChecker
/* Non-ambigous ModuleVarDev */
else if(cast(compiler.typecheck.variables.ModuleVariableDeclaration)node)
{
gprintln("fdsfds");
/**
* Codegen
*
* Emit a variable declaration instruction
*/
Variable variablePNode = cast(Variable)node.getEntity();
string variableName = resolver.generateName(modulle, variablePNode);
VariableDeclaration varDecInstr = new VariableDeclaration(variableName, 4);
addInstr(varDecInstr);
/* The stack can be empty */
if(resStack.empty)
{
@ -257,6 +344,9 @@ public final class TypeChecker
/* Get final type */
Type typeCur = popType();
/* Only one type before previous call should be on type-stack */
//assert(typeStack.empty);
/* Var type */
auto varDNode = cast(compiler.typecheck.variables.ModuleVariableDeclaration)node;
Type varType = getType((cast(Variable)varDNode.getEntity()).getContext().getContainer(), (cast(Variable)varDNode.getEntity()).getType());
@ -276,6 +366,8 @@ public final class TypeChecker
{
Parser.expect("Type mismatch between "~to!(string)(varType.classinfo)~" and "~to!(string)(typeCur.classinfo));
}
/* TODO: Emit var assign */
// /* Get the VarAssign */
@ -314,6 +406,8 @@ public final class TypeChecker
gprintln("fdsfd");
}
}
}
/* TODO: typecheck(node) */

View File

@ -6,5 +6,5 @@ class F
}
int p = 21;
F k = 2+p+p;
int k = 2+p+p;
int l = p;