From c957845c66fc5cb2674b78717c08fc103b537f04 Mon Sep 17 00:00:00 2001 From: "Tristan B. Kildaire" Date: Tue, 15 Jun 2021 14:27:32 +0200 Subject: [PATCH] Made `new` expression support more explicit to return a `NewExpression` with an embedded FunctionCall rather than a FunctionCall alone --- source/tlang/compiler/parsing/core.d | 25 ++++++++++++++++--- source/tlang/compiler/symbols/check.d | 7 ++++++ source/tlang/compiler/symbols/expressions.d | 15 +++++++++++ .../typecheck/basic_dependence_correct2.t | 2 +- 4 files changed, 45 insertions(+), 4 deletions(-) diff --git a/source/tlang/compiler/parsing/core.d b/source/tlang/compiler/parsing/core.d index 7bdacc2d..baa2527e 100644 --- a/source/tlang/compiler/parsing/core.d +++ b/source/tlang/compiler/parsing/core.d @@ -995,14 +995,15 @@ public final class Parser nextToken(); - Expression toAdd; + NewExpression toAdd; + FunctionCall functionCallPart; /* If the symbol is `(` then function call */ if (getSymbolType(getCurrentToken()) == SymbolType.LBRACE) { /* TODO: Implement function call parsing */ previousToken(); - toAdd = parseFuncCall(); + functionCallPart = parseFuncCall(); } /* If not an `(` */ else @@ -1011,9 +1012,27 @@ public final class Parser expect(SymbolType.LBRACE, getCurrentToken()); } + /* Create a NewExpression with the associated FunctionCall */ + toAdd = new NewExpression(functionCallPart); + /* Add the expression */ addRetExp(toAdd); + } + /* TODO: New addition (UNTESTED, remove if problem causer) */ + else if(symbol == SymbolType.DOT) + { + /* Pop the previous expression */ + Expression previousExpression = removeExp(); + /* TODO: Get next expression */ + nextToken(); + Expression item = parseExpression(); + + /* TODO: Construct accessor expression from both and addRetExp */ + + BinaryOperatorExpression binOp = new BinaryOperatorExpression(SymbolType.DOT, previousExpression, item); + + addRetExp(binOp); } else { @@ -1324,7 +1343,7 @@ public final class Parser gprintln("parseStatement(): Leave", DebugType.WARNING); } - private Expression parseFuncCall() + private FunctionCall parseFuncCall() { gprintln("parseFuncCall(): Enter", DebugType.WARNING); diff --git a/source/tlang/compiler/symbols/check.d b/source/tlang/compiler/symbols/check.d index 9e477198..876f7341 100644 --- a/source/tlang/compiler/symbols/check.d +++ b/source/tlang/compiler/symbols/check.d @@ -44,6 +44,7 @@ public enum SymbolType CASE, GOTO, DO, + DOT, DELETE, STRUCT, UNKNOWN @@ -369,6 +370,12 @@ public SymbolType getSymbolType(Token tokenIn) { return SymbolType.TILDE; } + /* Dot operator check */ + else if (token[0] == '.') + { + return SymbolType.DOT; + } + diff --git a/source/tlang/compiler/symbols/expressions.d b/source/tlang/compiler/symbols/expressions.d index aca97c15..c7dca03d 100644 --- a/source/tlang/compiler/symbols/expressions.d +++ b/source/tlang/compiler/symbols/expressions.d @@ -86,4 +86,19 @@ public class Expression : Statement } /* TODO: Evalute this expression's type */ +} + +public final class NewExpression : Expression +{ + private FunctionCall funcCall; + + this(FunctionCall funcCall) + { + this.funcCall = funcCall; + } + + public FunctionCall getFuncCall() + { + return funcCall; + } } \ No newline at end of file diff --git a/source/tlang/testing/typecheck/basic_dependence_correct2.t b/source/tlang/testing/typecheck/basic_dependence_correct2.t index 6cdb1fc5..8056db8a 100644 --- a/source/tlang/testing/typecheck/basic_dependence_correct2.t +++ b/source/tlang/testing/typecheck/basic_dependence_correct2.t @@ -4,7 +4,7 @@ A aInstance; B bInstance; int p = p+p*2; int k =1+p+l; -int o = new A().l; +int o = new A().l.p.p; class A {