From 5963a25f3756e2452cada74ee7ed8718bd92a55e Mon Sep 17 00:00:00 2001 From: prod2 <95874442+prod2@users.noreply.github.com> Date: Thu, 10 Feb 2022 00:35:29 +0100 Subject: [PATCH] def declaration of procedures --- src/ndspkg/compiler/functions.nim | 2 +- src/ndspkg/compiler/statement.nim | 3 +++ src/ndspkg/compiler/variables.nim | 10 +++++++++- src/ndspkg/scanner.nim | 4 ++-- tests/sugar.nds | 20 +++++++++++++++++++- 5 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/ndspkg/compiler/functions.nim b/src/ndspkg/compiler/functions.nim index f0347be..0c80b9d 100644 --- a/src/ndspkg/compiler/functions.nim +++ b/src/ndspkg/compiler/functions.nim @@ -89,7 +89,7 @@ proc parseArrowCall(comp: Compiler) = tkArrow.genRule(nop, parseArrowCall, pcIndex) -proc parseFunct(comp: Compiler) = +proc parseFunct*(comp: Compiler) = # jump over var jumpStacklen = 0 diff --git a/src/ndspkg/compiler/statement.nim b/src/ndspkg/compiler/statement.nim index be16a6b..fb5b5ac 100644 --- a/src/ndspkg/compiler/statement.nim +++ b/src/ndspkg/compiler/statement.nim @@ -47,6 +47,9 @@ proc statement*(comp: Compiler) = if comp.match(tkVar): comp.varStatement() comp.consume(tkSemicolon, "Semicolon expected after expression statement.") + elif comp.match(tkDef): + comp.procStatement() + comp.consume(tkSemicolon, "Semicolon expected after procedure declaration.") elif comp.match(tkBreak): comp.breakStatement() else: diff --git a/src/ndspkg/compiler/variables.nim b/src/ndspkg/compiler/variables.nim index 574cf43..be6a0f9 100644 --- a/src/ndspkg/compiler/variables.nim +++ b/src/ndspkg/compiler/variables.nim @@ -9,6 +9,7 @@ import ../types/value import types import utils import precedence +import functions # UTILS @@ -110,7 +111,7 @@ proc variable(comp: Compiler) = tkIdentifier.genRule(variable, nop, pcNone) -# VAR STATEMENT +# VAR STATEMENT, PROC STATEMENT proc parseVariable(comp: Compiler, msg: string): int = ## Parses variable declarations @@ -167,3 +168,10 @@ proc varStatement*(comp: Compiler) = comp.writeChunk(1, opNil) comp.defineVariable(globalIndex) + +proc procStatement*(comp: Compiler) = + let globalIndex = comp.parseVariable("Expect procedure name.") + + comp.parseFunct() + + comp.defineVariable(globalIndex) \ No newline at end of file diff --git a/src/ndspkg/scanner.nim b/src/ndspkg/scanner.nim index a07f1af..a3f62e3 100644 --- a/src/ndspkg/scanner.nim +++ b/src/ndspkg/scanner.nim @@ -19,7 +19,7 @@ type tkStartList, tkStartTable, tkLeftBracket, tkRightBracket, tkHashtag, tkAmpersand, tkIdentifier, tkString, - tkNumber, tkAnd, tkElse, tkFalse, tkFor, tkFunct, tkGoto, tkIf, tkNil, + tkNumber, tkAnd, tkDef, tkElse, tkFalse, tkFor, tkFunct, tkGoto, tkIf, tkNil, tkOr, tkLabel, tkBreak, tkTrue, tkVar, tkWhile, tkError, tkEof @@ -142,11 +142,11 @@ proc scanNumber(scanner: Scanner): Token = const keywords = { "and": tkAnd, "break": tkBreak, + "def": tkDef, "else": tkElse, "false": tkFalse, "for": tkFor, "proc": tkFunct, - # here's a language that uses funct... still waiting for the day when a good de-funct joke comes to my mind that I can abuse "goto": tkGoto, "if": tkIf, "nil": tkNil, diff --git a/tests/sugar.nds b/tests/sugar.nds index 893ad2a..0dc8789 100644 --- a/tests/sugar.nds +++ b/tests/sugar.nds @@ -81,4 +81,22 @@ var returner = @{ //expect:3.4 print("finish"); -//expect:finish \ No newline at end of file +//expect:finish + +// proc declaration through def + +def globalProc(a, b, c) + a + b * c +; + +{ + def localProc(d, e, f) + 2 * d + 3 * e + 4 * f + ; + + print(globalProc(2, 3, 4)); + //expect:14.0 + + print(localProc(0.2, 0.3, 0.5)); + //expect:3.3 +}; \ No newline at end of file