From 8fe1737433fe370b680f5a877784eea473d4207d Mon Sep 17 00:00:00 2001 From: "Tristan B. Kildaire" Date: Sun, 21 Mar 2021 14:03:13 +0200 Subject: [PATCH] Added support for accessors to nested classes, classes and variables Updated test case respectively --- source/tlang/compiler/parser.d | 40 +++++++++++++++++++++++++-------- source/tlang/compiler/symbols.d | 5 +++++ source/tlang/testing/basic1.t | 13 ++++++----- 3 files changed, 44 insertions(+), 14 deletions(-) diff --git a/source/tlang/compiler/parser.d b/source/tlang/compiler/parser.d index 73e4855..09f3519 100644 --- a/source/tlang/compiler/parser.d +++ b/source/tlang/compiler/parser.d @@ -15,7 +15,7 @@ import misc.exceptions : TError; public enum AccessorType { - PUBLIC, PRIVATE, PROTECTED + PUBLIC, PRIVATE, PROTECTED, UNKNOWN } public enum FunctionType @@ -239,6 +239,11 @@ public final class Parser /* Might be a function, might be a variable */ parseTypedDeclaration(); } + /* If it is an accessor */ + else if (isAccessor(tok)) + { + parseAccessor(); + } /* If it is a branch */ else if (symbol == SymbolType.IF) { @@ -286,11 +291,31 @@ public final class Parser gprintln("parseBody(): Leave", DebugType.WARNING); } + private AccessorType getAccessorType(Token token) + { + if(getSymbolType(token) == SymbolType.PUBLIC) + { + return AccessorType.PUBLIC; + } + else if(getSymbolType(token) == SymbolType.PROTECTED) + { + return AccessorType.PROTECTED; + } + else if(getSymbolType(token) == SymbolType.PRIVATE) + { + return AccessorType.PRIVATE; + } + else + { + return AccessorType.UNKNOWN; + } + } + /* STATUS: Not being used yet */ private void parseAccessor() { /* Save and consume the accessor */ - string accessor = getCurrentToken().getToken(); + AccessorType accessorType = getAccessorType(getCurrentToken()); nextToken(); /* TODO: Only allow, private, public, protected */ @@ -299,18 +324,15 @@ public final class Parser /* Get the current token's symbol type */ SymbolType symbolType = getSymbolType(getCurrentToken()); - /* Before calling any, consume the token (accessor) */ - nextToken(); - /* If class */ if(symbolType == SymbolType.CLASS) { - + parseClass(accessorType); } /* If typed-definition (function or variable) */ else if(symbolType == SymbolType.TYPE) { - /* TODO: Call parseClass with parseClass(View=) */ + parseTypedDeclaration(accessorType); } /* Error out */ else @@ -497,7 +519,7 @@ public final class Parser gprintln("parseExpression(): Leave", DebugType.WARNING); } - private void parseTypedDeclaration() + private void parseTypedDeclaration(AccessorType accessorType = AccessorType.PUBLIC) { gprintln("parseTypedDeclaration(): Enter", DebugType.WARNING); @@ -563,7 +585,7 @@ public final class Parser * This is called when there is an occurrence of * a token `class` */ - private void parseClass() + private void parseClass(AccessorType accessorType = AccessorType.PUBLIC) { gprintln("parseClass(): Enter", DebugType.WARNING); diff --git a/source/tlang/compiler/symbols.d b/source/tlang/compiler/symbols.d index d7a9365..b35bdbb 100644 --- a/source/tlang/compiler/symbols.d +++ b/source/tlang/compiler/symbols.d @@ -193,6 +193,11 @@ public SymbolType getSymbolType(Token tokenIn) { return SymbolType.PUBLIC; } + /* protected keyword */ + else if(cmp(token, "protected") == 0) + { + return SymbolType.PROTECTED; + } /* return keyword */ else if(cmp(token, "return") == 0) { diff --git a/source/tlang/testing/basic1.t b/source/tlang/testing/basic1.t index ffaacea..3998b3b 100644 --- a/source/tlang/testing/basic1.t +++ b/source/tlang/testing/basic1.t @@ -4,9 +4,12 @@ module bababoioey; int x; ubyte y; -ubyte k = 1; +public ubyte k = 1; +private ubyte k = 1; +protected ubyte k = 1; -class clazz1 + +public class clazz1 { print("Hello world"); } @@ -15,13 +18,13 @@ class clazz_2_1 : bruh { class clazz_2_2 { - class clazz_2_2_1 : bruh, bruh2 + private class clazz_2_2_1 : bruh, bruh2 { } } - class clazz_2_3 + protected class clazz_2_3 { class clazz_2_3_1 { @@ -89,7 +92,7 @@ class clazz_2_1 : bruh } -void main(int hello, byte d) +public void main(int hello, byte d) { void bruh()