def declaration of procedures
This commit is contained in:
parent
f86cce7977
commit
5963a25f37
|
@ -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
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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)
|
|
@ -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,
|
||||
|
|
|
@ -82,3 +82,21 @@ var returner = @{
|
|||
|
||||
print("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
|
||||
};
|
Loading…
Reference in New Issue