Merge branch 'pointers' into vardec_varass_dependency

This commit is contained in:
Tristan B. Velloza Kildaire 2023-01-15 12:59:41 +02:00
commit d3e15e7a2f
4 changed files with 28 additions and 6 deletions

View File

@ -63,7 +63,7 @@ public final class DCodeEmitter : CodeEmitter
{
/* Extract type being pointed to */
Pointer pointerType = cast(Pointer)typeIn;
Type referType = pointerType.getReferType();
Type referType = pointerType.getReferredType();
/* The type is then `transform(<refertype>)*` */
return typeTransform(referType)~"*";
@ -774,8 +774,9 @@ int main()
#include<assert.h>
int main()
{
thing();
int retValue = thing();
assert(t_87bc875d0b65f741b69fb100a0edebc7 == 4);
assert(retValue == 6);
return 0;
}`);

View File

@ -106,7 +106,7 @@ public class Pointer : Integer
this.dataType = dataType;
}
public Type getReferType()
public Type getReferredType()
{
return dataType;
}

View File

@ -598,7 +598,26 @@ public final class TypeChecker
/* If pointer dereference */
else if(unaryOperator == SymbolType.STAR)
{
/* TODO: Add support */
gprintln("Type popped: "~to!(string)(expType));
// Okay, so yes, we would pop `ptr`'s type as `int*` which is correct
// but now, we must a.) ensure that IS the case and b.)
// push the type of `<type>` with one star less on as we are derefrencing `ptr`
Type derefPointerType;
if(cast(Pointer)expType)
{
Pointer pointerType = cast(Pointer)expType;
// Get the type being referred to
Type referredType = pointerType.getReferredType();
addType(referredType);
}
else
{
gprintln("You cannot dereference a type that is not a pointer type!", DebugType.ERROR);
assert(false);
}
}
/* If pointer create `&` */
else if(unaryOperator == SymbolType.AMPERSAND)
@ -632,6 +651,7 @@ public final class TypeChecker
UnaryOpInstr addInst = new UnaryOpInstr(expInstr, unaryOperator);
gprintln("Made unaryop instr: "~to!(string)(addInst));
addInstr(addInst);
}
/* Function calls */

View File

@ -5,12 +5,13 @@ int j;
int function(int* ptr)
{
*ptr = 2+2;
return 0;
return (*ptr)+1*2;
}
int thing()
{
int discardExpr = function(&j);
int** l;
return discardExpr;
}