From 87e4cd2fe6b7cfb1baab00868f0467232a49ce2a Mon Sep 17 00:00:00 2001 From: "Tristan B. Kildaire" Date: Thu, 1 Apr 2021 15:49:02 +0200 Subject: [PATCH] Added support for variable assignments --- source/tlang/compiler/parsing/core.d | 30 ++++++++++++++++++++++++++ source/tlang/compiler/symbols/data.d | 12 +++++++++++ source/tlang/compiler/typecheck/core.d | 7 +++++- source/tlang/testing/basic1.t | 2 ++ 4 files changed, 50 insertions(+), 1 deletion(-) diff --git a/source/tlang/compiler/parsing/core.d b/source/tlang/compiler/parsing/core.d index c6573ff..70c686a 100644 --- a/source/tlang/compiler/parsing/core.d +++ b/source/tlang/compiler/parsing/core.d @@ -216,6 +216,30 @@ public final class Parser tokenPtr--; } + public Assignment parseAssignment() + { + /* Generated Assignment statement */ + Assignment assignment; + + /* The identifier being assigned to */ + string identifier = getCurrentToken().getToken(); + nextToken(); + nextToken(); + + /* Expression */ + Expression assignmentExpression = parseExpression(); + + assignment = new Assignment(identifier, assignmentExpression); + + /* TODO: Support for (a=1)? */ + /* Expect a semicolon */ + expect(SymbolType.SEMICOLON, getCurrentToken()); + nextToken(); + + + return assignment; + } + public Statement parseName() { Statement ret; @@ -246,6 +270,12 @@ public final class Parser previousToken(); ret = parseTypedDeclaration(); } + /* Assignment */ + else if(type == SymbolType.ASSIGN) + { + previousToken(); + ret = parseAssignment(); + } /* Any other case */ else { diff --git a/source/tlang/compiler/symbols/data.d b/source/tlang/compiler/symbols/data.d index 86de50b..3bcc0d5 100644 --- a/source/tlang/compiler/symbols/data.d +++ b/source/tlang/compiler/symbols/data.d @@ -92,6 +92,18 @@ public enum FunctionType STATIC, VIRTUAL } +public class Assignment : Statement +{ + private string identifier; + private Expression assignmentExpression; + + this(string identifier, Expression assignmentExpression) + { + this.identifier = identifier; + this.assignmentExpression = assignmentExpression; + } +} + /* Declared variables, defined classes and fucntions */ public class Entity : Statement { diff --git a/source/tlang/compiler/typecheck/core.d b/source/tlang/compiler/typecheck/core.d index 85de0af..c4b315b 100644 --- a/source/tlang/compiler/typecheck/core.d +++ b/source/tlang/compiler/typecheck/core.d @@ -91,7 +91,7 @@ public final class TypeChecker * Classes being declared before Functions and * Functions before Variables */ - checkContainer(modulle); + checkContainer(modulle); /* TODO: Rename checkContainerCollision */ checkIt(modulle); } @@ -401,6 +401,11 @@ public final class TypeChecker } + /** + * Finds the first occurring Entity with the provided + * name based on Classes being searched, then Functions + * and lastly Variables + */ public Entity findPrecedence(Container c, string name) { foreach(Entity entity; getContainerMembers(c)) diff --git a/source/tlang/testing/basic1.t b/source/tlang/testing/basic1.t index ecf639d..ccd4395 100644 --- a/source/tlang/testing/basic1.t +++ b/source/tlang/testing/basic1.t @@ -9,6 +9,8 @@ int b = a; int c = myModule.x; int l; +l=2; + int o = myModule.l;