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) tkArrow.genRule(nop, parseArrowCall, pcIndex)
proc parseFunct(comp: Compiler) = proc parseFunct*(comp: Compiler) =
# jump over # jump over
var jumpStacklen = 0 var jumpStacklen = 0

View File

@ -47,6 +47,9 @@ proc statement*(comp: Compiler) =
if comp.match(tkVar): if comp.match(tkVar):
comp.varStatement() comp.varStatement()
comp.consume(tkSemicolon, "Semicolon expected after expression statement.") 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): elif comp.match(tkBreak):
comp.breakStatement() comp.breakStatement()
else: else:

View File

@ -9,6 +9,7 @@ import ../types/value
import types import types
import utils import utils
import precedence import precedence
import functions
# UTILS # UTILS
@ -110,7 +111,7 @@ proc variable(comp: Compiler) =
tkIdentifier.genRule(variable, nop, pcNone) tkIdentifier.genRule(variable, nop, pcNone)
# VAR STATEMENT # VAR STATEMENT, PROC STATEMENT
proc parseVariable(comp: Compiler, msg: string): int = proc parseVariable(comp: Compiler, msg: string): int =
## Parses variable declarations ## Parses variable declarations
@ -167,3 +168,10 @@ proc varStatement*(comp: Compiler) =
comp.writeChunk(1, opNil) comp.writeChunk(1, opNil)
comp.defineVariable(globalIndex) 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, tkStartList, tkStartTable, tkLeftBracket, tkRightBracket,
tkHashtag, tkAmpersand, tkHashtag, tkAmpersand,
tkIdentifier, tkString, 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, tkOr, tkLabel, tkBreak, tkTrue, tkVar, tkWhile,
tkError, tkEof tkError, tkEof
@ -142,11 +142,11 @@ proc scanNumber(scanner: Scanner): Token =
const keywords = { const keywords = {
"and": tkAnd, "and": tkAnd,
"break": tkBreak, "break": tkBreak,
"def": tkDef,
"else": tkElse, "else": tkElse,
"false": tkFalse, "false": tkFalse,
"for": tkFor, "for": tkFor,
"proc": tkFunct, "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, "goto": tkGoto,
"if": tkIf, "if": tkIf,
"nil": tkNil, "nil": tkNil,

View File

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