From 6b3fccfc159cee77e8113ac084d27f522e7fa49c Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Sat, 14 Jan 2023 12:39:37 +0200 Subject: [PATCH 1/2] Types - 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` --- source/tlang/compiler/codegen/emit/dgen.d | 3 ++- source/tlang/compiler/symbols/typing/core.d | 5 +++++ source/tlang/compiler/typecheck/core.d | 22 ++++++++++++++++++++- source/tlang/testing/simple_pointer.t | 5 ++++- 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/source/tlang/compiler/codegen/emit/dgen.d b/source/tlang/compiler/codegen/emit/dgen.d index 40c7459c..6a96c6d1 100644 --- a/source/tlang/compiler/codegen/emit/dgen.d +++ b/source/tlang/compiler/codegen/emit/dgen.d @@ -671,8 +671,9 @@ int main() #include int main() { - thing(); + int retValue = thing(); assert(t_87bc875d0b65f741b69fb100a0edebc7 == 4); + assert(retValue == 6); return 0; }`); diff --git a/source/tlang/compiler/symbols/typing/core.d b/source/tlang/compiler/symbols/typing/core.d index b46303c5..7c50ffb5 100644 --- a/source/tlang/compiler/symbols/typing/core.d +++ b/source/tlang/compiler/symbols/typing/core.d @@ -105,6 +105,11 @@ public class Pointer : Integer super(name, 8); this.dataType = dataType; } + + public Type getReferredType() + { + return dataType; + } } /** diff --git a/source/tlang/compiler/typecheck/core.d b/source/tlang/compiler/typecheck/core.d index c9658b16..46101181 100644 --- a/source/tlang/compiler/typecheck/core.d +++ b/source/tlang/compiler/typecheck/core.d @@ -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 `` 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 */ diff --git a/source/tlang/testing/simple_pointer.t b/source/tlang/testing/simple_pointer.t index 0724201c..93bbe3a3 100644 --- a/source/tlang/testing/simple_pointer.t +++ b/source/tlang/testing/simple_pointer.t @@ -5,12 +5,15 @@ int j; int function(int* ptr) { *ptr = 2+2; + return (*ptr)+1*2; - return 0; + } int thing() { int discardExpr = function(&j); int** l; + + return discardExpr; } \ No newline at end of file From 2c912132492b4e3674dd44b2f128fd84727e9ebe Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Sat, 14 Jan 2023 12:43:21 +0200 Subject: [PATCH 2/2] - Cleaned up test case whitespace --- source/tlang/testing/simple_pointer.t | 2 -- 1 file changed, 2 deletions(-) diff --git a/source/tlang/testing/simple_pointer.t b/source/tlang/testing/simple_pointer.t index 93bbe3a3..69db5af0 100644 --- a/source/tlang/testing/simple_pointer.t +++ b/source/tlang/testing/simple_pointer.t @@ -6,8 +6,6 @@ int function(int* ptr) { *ptr = 2+2; return (*ptr)+1*2; - - } int thing()