switched opCall to a short arg op
This commit is contained in:
parent
84081074cc
commit
d69decebab
|
@ -28,11 +28,11 @@ type
|
|||
|
||||
# WARNING! short args can safely assumed to be 1 byte long outside of chunk.nim
|
||||
const shortArgSize* = 1
|
||||
const shortArgMax* = 256 # despite the name, this is one larger than the max
|
||||
const shortArgMax* = 256 - 1
|
||||
|
||||
# modules outside chunk.nim should not assume any length of argSize, however code inside chunk.nim can make such assumptions
|
||||
const argSize* = 2
|
||||
const argMax*: int = 256*256 # despite the name, this is one larger than the max
|
||||
const argMax*: int = 256*256 - 1
|
||||
|
||||
proc initChunk*(name: string): Chunk =
|
||||
Chunk(code: @[], name: name, lines: @[], constants: @[])
|
||||
|
@ -86,10 +86,10 @@ const constantInstructions = {
|
|||
}
|
||||
const shortArgInstructions = {
|
||||
opPopSA,
|
||||
opCall,
|
||||
}
|
||||
const argInstructions = {
|
||||
opPopA,
|
||||
opCall,
|
||||
opGetLocal, opSetLocal,
|
||||
opJumpIfFalse, opJump, opLoop, opJumpIfFalsePop,
|
||||
}
|
||||
|
|
|
@ -452,7 +452,7 @@ proc parseCall(comp: Compiler) =
|
|||
|
||||
# emit call
|
||||
comp.writeChunk(-argcount, opCall)
|
||||
comp.writeChunk(0, argcount.toDU8)
|
||||
comp.writeChunk(0, argcount.uint8)
|
||||
|
||||
|
||||
tkLeftParen.genRule(grouping, parseCall, pcCall)
|
||||
|
@ -630,6 +630,9 @@ proc parseFunct(comp: Compiler) =
|
|||
# [2] = arg #2
|
||||
# [3] = arg #3
|
||||
|
||||
if params.len > shortArgMax:
|
||||
comp.error("Too many parameters.")
|
||||
|
||||
for i in countup(1, params.len):
|
||||
comp.stackIndex = i
|
||||
comp.addLocal(params[i-1], 0)
|
||||
|
@ -642,7 +645,7 @@ proc parseFunct(comp: Compiler) =
|
|||
dec comp.stackIndex # the previous end scope did not put anything on the stack, it is jumped over
|
||||
|
||||
# get konvalue functions:
|
||||
let konFunct = newKonFunction(functII, params.len)
|
||||
let konFunct = newKonFunction(functII, params.len.uint8)
|
||||
|
||||
# end of function declaration:
|
||||
comp.patchJump(jumpOverBody)
|
||||
|
|
|
@ -20,9 +20,9 @@ type
|
|||
of ktString:
|
||||
stringValue*: string
|
||||
of ktFunct:
|
||||
module*: string
|
||||
sdfga*: string # WHY IS THIS STRING WHEN HERE EVEN THO USELESS SPEEDING THE BENCHMARK UP
|
||||
arity*: uint8 # number of arguments
|
||||
entryII*: int # entry instruction index
|
||||
arity*: int # number of arguments
|
||||
of errorTypes:
|
||||
message*: string
|
||||
|
||||
|
@ -87,7 +87,7 @@ proc toKonValue*(val: bool): KonValue =
|
|||
proc toKonValue*(val: string): KonValue =
|
||||
KonValue(konType: ktString, stringValue: val)
|
||||
|
||||
proc newKonFunction*(ii: int, arity: int): KonValue =
|
||||
proc newKonFunction*(ii: int, arity: uint8): KonValue =
|
||||
KonValue(konType: ktFunct, entryII: ii, arity: arity)
|
||||
|
||||
proc toKonValue*: KonValue =
|
||||
|
|
11
vm.nim
11
vm.nim
|
@ -47,6 +47,7 @@ proc run*(chunk: Chunk): InterpretResult =
|
|||
discard stack.pop()
|
||||
|
||||
proc pushSafe(stack: var seq[KonValue], val: KonValue): bool =
|
||||
## DEPRECATED
|
||||
## returns if the value is not a runtime error
|
||||
## prints the error if it is a runtime error
|
||||
## pushes it to the stack if it is not an error
|
||||
|
@ -60,6 +61,10 @@ proc run*(chunk: Chunk): InterpretResult =
|
|||
proc readUI8(): int =
|
||||
result = ip[].int
|
||||
ip = ip.padd(1)
|
||||
|
||||
proc readUI8Noconv(): uint8 =
|
||||
result = ip[]
|
||||
ip = ip.padd(1)
|
||||
|
||||
proc readDU8(): int =
|
||||
result = ip.DU8ptrToInt
|
||||
|
@ -205,15 +210,15 @@ proc run*(chunk: Chunk): InterpretResult =
|
|||
# ... <funct obj> <arg1> <arg2> <arg3>
|
||||
# opCall converts it to this
|
||||
# ... <ret val> <arg1> <arg2> <arg3>
|
||||
let argcount = readDU8()
|
||||
let argcountUint8 = readUI8Noconv()
|
||||
let argcount = argcountUint8.int
|
||||
let funct = stack[stack.high - argcount]
|
||||
if funct.konType != ktFunct:
|
||||
runtimeError("Attempt to call a non-funct (a defunct?).") # here is a bad defunct joke
|
||||
break
|
||||
if funct.arity != argcount:
|
||||
if funct.arity != argcountUint8:
|
||||
runtimeError(&"Wrong number of arguments, expected {funct.arity}, got {argcount}.")
|
||||
break
|
||||
# if funct.module ... TODO
|
||||
|
||||
stack[stack.high - argcount] = toKonValue() # replace the function with nil: this is the return value slot
|
||||
|
||||
|
|
Loading…
Reference in New Issue