use ip rather than ii for funct
This commit is contained in:
parent
71a3d6ad45
commit
f665efaeed
|
@ -5,7 +5,7 @@ import types/value
|
|||
|
||||
type
|
||||
OpCode* = enum
|
||||
opReturn, opCall, opCheckArity # functions
|
||||
opReturn, opCall, opCheckArity, opFunctionDef, # functions
|
||||
opPop, opPopSA, opPopA # pop
|
||||
opPrint, # print
|
||||
opNegate, opNot # unary
|
||||
|
@ -93,6 +93,7 @@ const argInstructions = {
|
|||
opPopA,
|
||||
opGetLocal, opSetLocal,
|
||||
opJumpIfFalse, opJump, opLoop, opJumpIfFalsePop,
|
||||
opFunctionDef,
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -599,7 +599,7 @@ tkWhile.genRule(parseWhile, nop, pcNone)
|
|||
proc parseFunct(comp: Compiler) =
|
||||
|
||||
# jump over
|
||||
let jumpOverBody = comp.emitJump(0, opJump)
|
||||
let jumpOverBody = comp.emitJump(0, opFunctionDef)
|
||||
|
||||
comp.consume(tkLeftParen, "Expected '(' after keyword 'funct'.")
|
||||
|
||||
|
@ -641,13 +641,8 @@ proc parseFunct(comp: Compiler) =
|
|||
comp.endScope()
|
||||
dec comp.stackIndex # the previous end scope did not put anything on the stack, it is jumped over
|
||||
|
||||
# get ndvalue functions:
|
||||
let ntFunct = functII.fromFunct()
|
||||
|
||||
# end of function declaration:
|
||||
comp.patchJump(jumpOverBody)
|
||||
# put the fn object on the stack
|
||||
comp.writeConstant(ntFunct)
|
||||
|
||||
tkFunct.genRule(parseFunct, nop, pcNone)
|
||||
|
||||
|
|
|
@ -63,8 +63,8 @@ template asFloat*(val: NdValue): float =
|
|||
template asString*(val: NdValue): NdString =
|
||||
cast[NdString](val.bitand(mask48.bitnot()))
|
||||
|
||||
template asFunct*(val: NdValue): int =
|
||||
(val.bitand(mask48.bitnot())).int
|
||||
template asFunct*(val: NdValue): ptr uint8 =
|
||||
cast[ptr uint8](val.bitand(mask48.bitnot()))
|
||||
|
||||
template fromNil*(): NdValue =
|
||||
ndNil
|
||||
|
@ -81,7 +81,7 @@ template fromNdString*(val: NdString): NdValue =
|
|||
template fromNimString*(sval: string): NdValue =
|
||||
fromNdString(newString(sval))
|
||||
|
||||
template fromFunct*(val: int): NdValue =
|
||||
template fromFunct*(val: ptr uint8): NdValue =
|
||||
cast[uint](val).bitor(tagFunct)
|
||||
|
||||
|
||||
|
@ -97,7 +97,7 @@ proc `$`*(val: NdValue): string =
|
|||
elif val.isFloat():
|
||||
return $val.asFloat()
|
||||
elif val.isFunct():
|
||||
return &"Function object {val.asFunct()}"
|
||||
return &"Function object {cast[uint](val.asFunct())}"
|
||||
elif val.isString():
|
||||
return $val.asString()
|
||||
|
||||
|
|
7
vm.nim
7
vm.nim
|
@ -199,6 +199,11 @@ proc run*(chunk: Chunk): InterpretResult =
|
|||
of opLoop:
|
||||
let offset = readDU8()
|
||||
ip = ip.psub(offset)
|
||||
of opFunctionDef:
|
||||
let offset = readDU8()
|
||||
let faddr: ptr uint8 = ip
|
||||
ip = ip.padd(offset)
|
||||
stack.push(faddr.fromFunct())
|
||||
of opCheckArity:
|
||||
let arity = readUI8()
|
||||
let argcount = stack.high() - frameBottom
|
||||
|
@ -222,7 +227,7 @@ proc run*(chunk: Chunk): InterpretResult =
|
|||
# create new frame
|
||||
frames.add(Frame(stackBottom: stack.high - argcount, returnIp: ip))
|
||||
|
||||
ip = chunk.code[0].unsafeAddr.padd(funct.asFunct()) # jump to the entry point
|
||||
ip = funct.asFunct() # jump to the entry point
|
||||
|
||||
when profileInstructions:
|
||||
durations[ins] += getMonoTime() - startTime
|
||||
|
|
Loading…
Reference in New Issue