From 8efeb9a3fb46cfe1b1370d5513e3799e97933841 Mon Sep 17 00:00:00 2001 From: "Tristan B. Kildaire" Date: Sun, 21 Mar 2021 10:02:23 +0200 Subject: [PATCH] Fixed function argument parsing --- source/tlang/compiler/parser.d | 72 +++++++++++++++++++++------------- source/tlang/testing/basic1.t | 8 ++++ 2 files changed, 52 insertions(+), 28 deletions(-) diff --git a/source/tlang/compiler/parser.d b/source/tlang/compiler/parser.d index 4f7ab1a..f3afa26 100644 --- a/source/tlang/compiler/parser.d +++ b/source/tlang/compiler/parser.d @@ -286,47 +286,65 @@ public final class Parser /* Count for number of parameters processed */ ulong parameterCount; + /* Expecting more arguments */ + bool moreArgs; + /* Get command-line arguments */ while (hasTokens()) { - /* Expect a type */ - string type = getCurrentToken().getToken(); - expect(SymbolType.TYPE, getCurrentToken()); - nextToken(); - - /* Expect an identifier */ - expect(SymbolType.IDENTIFIER, getCurrentToken()); - string identifier = getCurrentToken().getToken(); - nextToken(); - - parameterCount++; - - /* Check if RBRACE/ `)` */ - if (getSymbolType(getCurrentToken()) == SymbolType.RBRACE) + /* Check if the first thing is a type */ + if(getSymbolType(getCurrentToken()) == SymbolType.TYPE) { - nextToken(); - expect(SymbolType.OCURLY, getCurrentToken()); - - /* Parse the body (and it leaves ONLY when it gets the correct symbol, no expect needed) */ - parseBody(); + /* Get the type */ + string type = getCurrentToken().getToken(); nextToken(); - break; + /* Get the identifier */ + expect(SymbolType.IDENTIFIER, getCurrentToken()); + string identifier = getCurrentToken().getToken(); + nextToken(); + + moreArgs = false; + + parameterCount++; } - else if (getSymbolType(getCurrentToken()) == SymbolType.COMMA) + /* If we get a comma */ + else if(getSymbolType(getCurrentToken()) == SymbolType.COMMA) { + /* Consume the `,` */ nextToken(); + + moreArgs = true; } + /* Check if it is a closing brace */ + else if(getSymbolType(getCurrentToken()) == SymbolType.RBRACE) + { + /* Make sure we were not expecting more arguments */ + if(!moreArgs) + { + /* Consume the `)` */ + nextToken(); + break; + } + /* Error out if we were and we prematurely ended */ + else + { + expect(SymbolType.IDENTIFIER, getCurrentToken()); + } + } + /* Error out */ else { - /* TODO: Error */ - expect("Expecting either ) or , but got " ~ getCurrentToken().getToken()); + expect("Expected either type or )"); } - - gprintln("ParseFuncDef: ParameterDec: (Type: " ~ type ~ ", Identifier: " ~ identifier ~ ")", - DebugType.WARNING); } + expect(SymbolType.OCURLY, getCurrentToken()); + + /* Parse the body (and it leaves ONLY when it gets the correct symbol, no expect needed) */ + parseBody(); + nextToken(); + gprintln("ParseFuncDef: Parameter count: " ~ to!(string)(parameterCount)); gprintln("parseFuncDef(): Leave", DebugType.WARNING); } @@ -543,8 +561,6 @@ public final class Parser } } } - - /* TODO: Go through all classes comma seperated */ /* Parse a body */ parseBody(); diff --git a/source/tlang/testing/basic1.t b/source/tlang/testing/basic1.t index fabb9bf..57a780f 100644 --- a/source/tlang/testing/basic1.t +++ b/source/tlang/testing/basic1.t @@ -126,8 +126,16 @@ void main(int hello, byte d) } } +void pdsjhfjdsf(int j) +{ + +} + void k(int j, int k) { + + + ubyte thing = "Hello"; print("Hello world"); print(1+1);