Some stuff, working on getting function calls working

This commit is contained in:
Tristan B. Velloza Kildaire 2022-02-18 14:32:45 +02:00
parent 71efb7ae8f
commit a84e0dfe20
5 changed files with 174 additions and 6 deletions

View File

@ -135,12 +135,29 @@ public class BinOpInstr : Instruction
*
*/
public class CallInstr : Instruction
//public class CallInstr : Instruction
public class CallInstr : Value
{
}
public class FuncCallInstr : CallInstr
{
/* Per-argument instrructions */
private Instruction[] evaluationInstructions;
private string functionName;
this(string functionName, ulong argEvalInstrsSize)
{
this.functionName = functionName;
evaluationInstructions.length = argEvalInstrsSize;
addInfo = "FunctionName: "~functionName;
}
public void setEvalInstr(ulong argPos, Instruction instr)
{
evaluationInstructions[argPos] = instr;
}
}

View File

@ -248,6 +248,11 @@ public class Function : TypedEntity
weight = 1;
}
public Variable[] getParams()
{
return params;
}
public Statement[] getStatements()
{
import compiler.symbols.containers : weightReorder;

View File

@ -52,6 +52,11 @@ public class Number : Primitive
super(name);
this.width = width;
}
public final ubyte getSize()
{
return width;
}
}
public class Integer : Number
@ -64,6 +69,17 @@ public class Integer : Number
super(name, width);
this.signed = signed;
}
public final bool isSigned()
{
return signed;
}
/* TODO: Remove ig */
public override string toString()
{
return name;
}
}
public class Float : Number

View File

@ -159,6 +159,50 @@ public final class TypeChecker
}
}
/*
* Prints the current contents of the code-queue
*/
public void printTypeQueue()
{
import std.range : walkLength;
ulong i = 0;
foreach(Type instruction; typeStack)
{
gprintln("TypeQueue: "~to!(string)(i+1)~"/"~to!(string)(walkLength(typeStack[]))~": "~instruction.toString());
i++;
}
}
/**
* There are several types and comparing them differs
*/
private bool isSameType(Type type1, Type type2)
{
bool same = false;
/* Handling for Integers */
if(typeid(type1) == typeid(type2) && cast(Integer)type1 !is null)
{
Integer i1 = cast(Integer)type1, i2 = cast(Integer)type2;
/* Both same size? */
if(i1.getSize() == i2.getSize())
{
/* Matching signedness ? */
return i1.isSigned() == i2.isSigned();
}
/* Size mismatch */
else
{
return false;
}
}
return same;
}
private SList!(Type) typeStack;
@ -213,6 +257,7 @@ public final class TypeChecker
/* TODO: For now */
// gprintln("STRING LIT");
// addType(getType(modulle, "int"));
//addType();
}
else if(cast(VariableExpression)statement)
{
@ -265,6 +310,84 @@ public final class TypeChecker
{
/* TODO: Implement me */
gprintln("FuncCall hehe (REMOVE AFTER DONE)");
printTypeQueue();
FunctionCall funcCall = cast(FunctionCall)statement;
/* TODO: Look up func def to know when popping stops (types-based delimiting) */
Function func = cast(Function)resolver.resolveBest(modulle, funcCall.getName());
assert(func);
Variable[] paremeters = func.getParams();
gprintln(func.getParams());
ulong parmCount = paremeters.length-1;
/* TODO: Current bug is that the instructions are byte then int but types are popping int then byte
due to the insertion being puishing infront, I recommend we do an initial run through first */
/* If there are paremeters for this function (as per definition) */
if(!paremeters.length)
{
}
/* Pop all args per type */
else
{
while(!isInstrEmpty())
{
Instruction instr = popInstr();
Value valueInstr = cast(Value)instr;
/* Must be a value instruction */
if(valueInstr && parmCount!=-1)
{
/* TODO: Determine type and match up */
gprintln("Yeah");
gprintln(valueInstr);
Type argType = popType();
gprintln(argType);
Variable parameter = paremeters[parmCount];
gprintln(parameter);
parmCount--;
Type parmType = getType(func.parentOf(), parameter.getType());
gprintln("FuncCall(Actual): "~argType.getName());
gprintln("FuncCall(Formal): "~parmType.getName());
gprintln("FuncCall(Actual): "~valueInstr.toString());
printTypeQueue();
/* Match up types */
//if(argType == parmType)
if(isSameType(argType, parmType))
{
gprintln("Match type");
}
else
{
printCodeQueue();
gprintln("Wrong actual argument type for function call", DebugType.ERROR);
gprintln("Cannot pass value of type '"~argType.getName()~"' to function accepting '"~parmType.getName()~"'", DebugType.ERROR);
assert(false);
}
}
else
{
/* Push it back */
addInstr(instr);
break;
}
}
}
/* TODO: Pass in FUnction, so we get function's body for calling too */
FuncCallInstr funcCallInstr = new FuncCallInstr(func.getName(), paremeters.length);
/**
* TODO:
@ -275,7 +398,10 @@ public final class TypeChecker
* 4. AddInstr(combining those args)
* 5. DOne
*/
addInstr(new FuncCallInstr());
addInstr(funcCallInstr);
addType(getType(func.parentOf(), func.getType()));
gprintln("Genaai");
gprintln(getType(func.parentOf(), func.getType()));
}
}
/* VariableAssigbmentDNode */

View File

@ -3,10 +3,14 @@ module simple;
discard "return 1;";
int j = 1+func(2,test());
int j = func(1,0);
int func()
int func(int x1, byte x2)
{
}
byte test()
{
}