def declaration of procedures

This commit is contained in:
prod2 2022-02-10 00:35:29 +01:00
parent f86cce7977
commit 5963a25f37
5 changed files with 34 additions and 5 deletions

View File

@ -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

View File

@ -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:

View File

@ -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)

View File

@ -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,

View File

@ -81,4 +81,22 @@ var returner = @{
//expect:3.4
print("finish");
//expect:finish
//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
};