- Added `getReferredType()` to `Pointer` to fetch the type of the data being referred to

Typechecker

- Unary operator `STAR` now will check popped type, ensure it is a pointer, then push the type of the referred-to data

Test cases

- Updated the `simple_pointer.t` test case to do pointer dereferencing

DGen

- Updated entry point testing code for the pointer test `simple_pointer.t`
This commit is contained in:
Tristan B. Velloza Kildaire 2023-01-14 12:39:37 +02:00
parent c1ba609609
commit 6b3fccfc15
4 changed files with 32 additions and 3 deletions

View File

@ -671,8 +671,9 @@ int main()
#include<assert.h> #include<assert.h>
int main() int main()
{ {
thing(); int retValue = thing();
assert(t_87bc875d0b65f741b69fb100a0edebc7 == 4); assert(t_87bc875d0b65f741b69fb100a0edebc7 == 4);
assert(retValue == 6);
return 0; return 0;
}`); }`);

View File

@ -105,6 +105,11 @@ public class Pointer : Integer
super(name, 8); super(name, 8);
this.dataType = dataType; this.dataType = dataType;
} }
public Type getReferredType()
{
return dataType;
}
} }
/** /**

View File

@ -598,7 +598,26 @@ public final class TypeChecker
/* If pointer dereference */ /* If pointer dereference */
else if(unaryOperator == SymbolType.STAR) 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 `&` */ /* If pointer create `&` */
else if(unaryOperator == SymbolType.AMPERSAND) else if(unaryOperator == SymbolType.AMPERSAND)
@ -632,6 +651,7 @@ public final class TypeChecker
UnaryOpInstr addInst = new UnaryOpInstr(expInstr, unaryOperator); UnaryOpInstr addInst = new UnaryOpInstr(expInstr, unaryOperator);
gprintln("Made unaryop instr: "~to!(string)(addInst));
addInstr(addInst); addInstr(addInst);
} }
/* Function calls */ /* Function calls */

View File

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