71 lines
1.8 KiB
Nim
71 lines
1.8 KiB
Nim
{.used.}
|
|
|
|
import ../scanner
|
|
import ../chunk
|
|
|
|
# the following order of imports here is important
|
|
# it defines the allowed dependency precedence between the compiler files
|
|
import types
|
|
import utils
|
|
import precedence
|
|
|
|
# lists
|
|
|
|
proc parseList(comp: Compiler) =
|
|
var count: int
|
|
while comp.current.tokenType != tkRightBracket:
|
|
comp.expression()
|
|
count.inc()
|
|
if comp.current.tokenType != tkRightBracket:
|
|
comp.consume(tkComma, "Comma expected after list member.")
|
|
comp.consume(tkRightBracket, "Right bracket expected after list members.")
|
|
if count > argMax:
|
|
comp.error("Maximum list length exceeded.")
|
|
comp.writeChunk(1 - count, opCreateList)
|
|
comp.writeChunk(0, count.toDU8())
|
|
|
|
|
|
tkStartList.genRule(parseList, nop, pcNone)
|
|
|
|
# tables
|
|
|
|
proc parseTable(comp: Compiler) =
|
|
var count: int
|
|
while comp.current.tokenType != tkRightBrace:
|
|
comp.expression()
|
|
comp.consume(tkEqual, "Equal sign expected after key.")
|
|
comp.expression()
|
|
count.inc()
|
|
if comp.current.tokenType != tkRightBrace:
|
|
comp.consume(tkComma, "Comma expected after key-value pair.")
|
|
|
|
comp.consume(tkRightBrace, "Right brace expected after table members.")
|
|
if count > argMax:
|
|
comp.error("Maximum table length exceeded.")
|
|
comp.writeChunk(1 - 2 * count, opCreateTable)
|
|
comp.writeChunk(0, count.toDU8())
|
|
|
|
tkStartTable.genRule(parseTable, nop, pcNone)
|
|
|
|
# len op
|
|
|
|
proc parseLen(comp: Compiler) =
|
|
comp.expression()
|
|
comp.writeChunk(0, opLen)
|
|
|
|
tkHashtag.genRule(parseLen, nop, pcNone)
|
|
|
|
# get/set index
|
|
|
|
proc parseIndex(comp: Compiler) =
|
|
# the index
|
|
comp.expression()
|
|
comp.consume(tkRightBracket, "Right bracket expected after index.")
|
|
if comp.match(tkEqual):
|
|
comp.parsePrecedence(pcNonAssignTop)
|
|
comp.writeChunk(-2, opSetIndex)
|
|
else:
|
|
comp.writeChunk(-1, opGetIndex)
|
|
|
|
tkLeftBracket.genRule(nop, parseIndex, pcIndex)
|