Simplify config
- made assertions always enabled, since they already pretty much were - removed debug compiler/scanner since the whole thing was messy, and not present in compv2
This commit is contained in:
parent
3405c10b08
commit
04dd2bf73f
|
@ -3,6 +3,7 @@ import ndspkg/compv2/parser
|
||||||
import ndspkg/compv2/node
|
import ndspkg/compv2/node
|
||||||
import ndspkg/compv2/emitter
|
import ndspkg/compv2/emitter
|
||||||
import ndspkg/config
|
import ndspkg/config
|
||||||
|
import ndspkg/chunk
|
||||||
|
|
||||||
when compilerChoice == cmOne:
|
when compilerChoice == cmOne:
|
||||||
import ndspkg/compiler/compiler
|
import ndspkg/compiler/compiler
|
||||||
|
@ -27,6 +28,11 @@ proc interpret(name: string, source: string): Result =
|
||||||
return rsCompileError
|
return rsCompileError
|
||||||
let emitter = newEmitter(name, node)
|
let emitter = newEmitter(name, node)
|
||||||
emitter.emit()
|
emitter.emit()
|
||||||
|
if emitter.hadError:
|
||||||
|
return rsCompileError
|
||||||
|
when debugDumpChunk:
|
||||||
|
emitter.chunk.disassembleChunk()
|
||||||
|
|
||||||
case emitter.chunk.run():
|
case emitter.chunk.run():
|
||||||
of irOK:
|
of irOK:
|
||||||
rsOK
|
rsOK
|
||||||
|
|
|
@ -18,8 +18,6 @@ proc number(comp: Compiler) =
|
||||||
# assume the number is already advanced through
|
# assume the number is already advanced through
|
||||||
let value = comp.previous.text.parseFloat.fromFloat()
|
let value = comp.previous.text.parseFloat.fromFloat()
|
||||||
comp.writeConstant(value)
|
comp.writeConstant(value)
|
||||||
when debugCompiler:
|
|
||||||
debugEcho &"Written constant (type: {value.ndType}, str repr: {$value}) to chunk"
|
|
||||||
|
|
||||||
tkNumber.genRule(number, nop, pcNone)
|
tkNumber.genRule(number, nop, pcNone)
|
||||||
|
|
||||||
|
@ -41,8 +39,6 @@ tkNil.genRule(expNil, nop, pcNone)
|
||||||
proc expString(comp: Compiler) =
|
proc expString(comp: Compiler) =
|
||||||
let value = comp.previous.text[1..^2].fromNimString()
|
let value = comp.previous.text[1..^2].fromNimString()
|
||||||
comp.writeConstant(value)
|
comp.writeConstant(value)
|
||||||
when debugCompiler:
|
|
||||||
debugEcho &"Written constant (type: {value.ndType}, str repr: {$value}) to chunk"
|
|
||||||
|
|
||||||
tkString.genRule(expString, nop, pcNone)
|
tkString.genRule(expString, nop, pcNone)
|
||||||
|
|
||||||
|
|
|
@ -128,10 +128,9 @@ proc parseFunct*(comp: Compiler) =
|
||||||
comp.stackIndex = i
|
comp.stackIndex = i
|
||||||
comp.addLocal(params[i-1], 0)
|
comp.addLocal(params[i-1], 0)
|
||||||
comp.expression()
|
comp.expression()
|
||||||
when assertionsCompiler:
|
let shouldbeStackIndex = params.len + 1
|
||||||
let shouldbeStackIndex = params.len + 1
|
if shouldbeStackIndex != comp.stackIndex:
|
||||||
if shouldbeStackIndex != comp.stackIndex:
|
comp.error(&"Assertion failed: wrong stackindex ({comp.stackIndex}) in function declaration (should be {shouldbeStackIndex}).")
|
||||||
comp.error(&"Assertion failed: wrong stackindex ({comp.stackIndex}) in function declaration (should be {shouldbeStackIndex}).")
|
|
||||||
let f = comp.endScope()
|
let f = comp.endScope()
|
||||||
dec comp.stackIndex # the previous end scope did not put anything on the stack, it is jumped over
|
dec comp.stackIndex # the previous end scope did not put anything on the stack, it is jumped over
|
||||||
|
|
||||||
|
|
|
@ -11,14 +11,12 @@ proc emitJump*(comp: Compiler, delta: int, op: OpCode, stacklen: var int): int =
|
||||||
# delta -> -1 if the jump pops the condition from the stack
|
# delta -> -1 if the jump pops the condition from the stack
|
||||||
comp.writeChunk(delta, op)
|
comp.writeChunk(delta, op)
|
||||||
comp.writeChunk(0, 0xffffff.toDU8)
|
comp.writeChunk(0, 0xffffff.toDU8)
|
||||||
when assertionsCompiler:
|
stacklen = comp.stackIndex
|
||||||
stacklen = comp.stackIndex
|
|
||||||
comp.chunk.len - argSize
|
comp.chunk.len - argSize
|
||||||
|
|
||||||
proc patchJump*(comp: Compiler, offset: int, stacklen: int) =
|
proc patchJump*(comp: Compiler, offset: int, stacklen: int) =
|
||||||
when assertionsCompiler:
|
if comp.stackIndex != stacklen:
|
||||||
if comp.stackIndex != stacklen:
|
comp.error("Assertion failed: loop doesn't preserve stackindex.")
|
||||||
comp.error("Assertion failed: loop doesn't preserve stackindex.")
|
|
||||||
let jump = (comp.chunk.len - offset - argSize)
|
let jump = (comp.chunk.len - offset - argSize)
|
||||||
|
|
||||||
if (jump > argMax):
|
if (jump > argMax):
|
||||||
|
@ -30,9 +28,8 @@ proc patchJump*(comp: Compiler, offset: int, stacklen: int) =
|
||||||
comp.chunk.code[offset + 1] = jumpt[1]
|
comp.chunk.code[offset + 1] = jumpt[1]
|
||||||
|
|
||||||
proc emitLoop*(comp: Compiler, loopstart: int, delta: int, op: OpCode, stacklen: int) =
|
proc emitLoop*(comp: Compiler, loopstart: int, delta: int, op: OpCode, stacklen: int) =
|
||||||
when assertionsCompiler:
|
if comp.stackIndex != stacklen:
|
||||||
if comp.stackIndex != stacklen:
|
comp.error("Assertion failed: loop doesn't preserve stackindex.")
|
||||||
comp.error("Assertion failed: loop doesn't preserve stackindex.")
|
|
||||||
comp.writeChunk(delta, op)
|
comp.writeChunk(delta, op)
|
||||||
|
|
||||||
let offset = comp.chunk.len - loopstart + argSize
|
let offset = comp.chunk.len - loopstart + argSize
|
||||||
|
|
|
@ -50,9 +50,6 @@ proc parsePrecedence*(comp: Compiler, prec: Precedence) =
|
||||||
if rule.prefix != nop:
|
if rule.prefix != nop:
|
||||||
comp.canAssign = prec <= pcAssignment
|
comp.canAssign = prec <= pcAssignment
|
||||||
|
|
||||||
when debugCompiler:
|
|
||||||
debugEcho &"parsePrecedence call, valid prefix op found, rule used: {rule.name}, precedence: {prec}"
|
|
||||||
|
|
||||||
rule.prefix(comp)
|
rule.prefix(comp)
|
||||||
|
|
||||||
while prec <= comp.current.tokenType.getRule().prec:
|
while prec <= comp.current.tokenType.getRule().prec:
|
||||||
|
|
|
@ -15,9 +15,6 @@ import jumps
|
||||||
proc beginScope*(comp: Compiler, function: bool = false) =
|
proc beginScope*(comp: Compiler, function: bool = false) =
|
||||||
let scope = comp.newScope(function)
|
let scope = comp.newScope(function)
|
||||||
|
|
||||||
when debugCompiler:
|
|
||||||
debugEcho &"Begin scope called for depth {comp.scopes.len} function? {function}"
|
|
||||||
|
|
||||||
if not function:
|
if not function:
|
||||||
while comp.match(tkLabel):
|
while comp.match(tkLabel):
|
||||||
let label = comp.previous.text[1..^1]
|
let label = comp.previous.text[1..^1]
|
||||||
|
@ -35,20 +32,16 @@ proc beginScope*(comp: Compiler, function: bool = false) =
|
||||||
|
|
||||||
proc scopeRetIndex*(comp: Compiler): int =
|
proc scopeRetIndex*(comp: Compiler): int =
|
||||||
let scope = comp.scopes[comp.scopes.high()]
|
let scope = comp.scopes[comp.scopes.high()]
|
||||||
when assertionsCompiler:
|
# this is an illegal operation for function scopes, as they work differently
|
||||||
# this is an illegal operation for function scopes, as they work differently
|
if scope.function:
|
||||||
if scope.function:
|
comp.error("Assertion failed, Internal error, scopeRetIndex calculation for a function scope.")
|
||||||
comp.error("Assertion failed, Internal error, scopeRetIndex calculation for a function scope.")
|
|
||||||
return scope.goalStackIndex
|
return scope.goalStackIndex
|
||||||
|
|
||||||
proc restore*(comp: Compiler, scope: Scope) =
|
proc restore*(comp: Compiler, scope: Scope) =
|
||||||
let delta = comp.stackIndex - scope.goalStackIndex
|
let delta = comp.stackIndex - scope.goalStackIndex
|
||||||
comp.writePops(delta)
|
comp.writePops(delta)
|
||||||
when assertionsCompiler:
|
if not comp.stackIndex == scope.goalStackIndex:
|
||||||
if not comp.stackIndex == scope.goalStackIndex:
|
comp.error("Assertion failed in restore")
|
||||||
comp.error("Assertion failed in restore")
|
|
||||||
when debugCompiler:
|
|
||||||
debugEcho &"Restored scope: delta {delta}"
|
|
||||||
|
|
||||||
proc restoreInFunct*(comp: Compiler, scope: Scope) =
|
proc restoreInFunct*(comp: Compiler, scope: Scope) =
|
||||||
#let pops = comp.stackIndex
|
#let pops = comp.stackIndex
|
||||||
|
@ -88,18 +81,15 @@ proc endScope*(comp: Compiler): Scope =
|
||||||
i.dec()
|
i.dec()
|
||||||
|
|
||||||
# restore the stackIndex, emit pops
|
# restore the stackIndex, emit pops
|
||||||
when debugCompiler:
|
|
||||||
debugEcho &"End scope called for depth {comp.scopes.len} function? {function}"
|
|
||||||
if function:
|
if function:
|
||||||
comp.restoreInFunct(popped)
|
comp.restoreInFunct(popped)
|
||||||
else:
|
else:
|
||||||
comp.restore(popped)
|
comp.restore(popped)
|
||||||
# patch jumps to after the scope (such jumps from breaks emit the pops before jumping)
|
# patch jumps to after the scope (such jumps from breaks emit the pops before jumping)
|
||||||
for jump in popped.jumps:
|
for jump in popped.jumps:
|
||||||
when assertionsCompiler:
|
if function:
|
||||||
if function:
|
|
||||||
# all jumps should only happen to named block expressions
|
# all jumps should only happen to named block expressions
|
||||||
comp.error("Assertion failed: jump attempt to end function.")
|
comp.error("Assertion failed: jump attempt to end function.")
|
||||||
comp.patchJump(jump, popped.goalStackIndex)
|
comp.patchJump(jump, popped.goalStackIndex)
|
||||||
if function:
|
if function:
|
||||||
comp.writeChunk(0, opReturn)
|
comp.writeChunk(0, opReturn)
|
||||||
|
|
|
@ -29,8 +29,6 @@ proc advance*(comp: Compiler) =
|
||||||
comp.previous = comp.current
|
comp.previous = comp.current
|
||||||
while true:
|
while true:
|
||||||
comp.current = comp.scanner.scanToken()
|
comp.current = comp.scanner.scanToken()
|
||||||
when debugScanner:
|
|
||||||
comp.current.debugPrint()
|
|
||||||
if (comp.current.tokenType != tkError):
|
if (comp.current.tokenType != tkError):
|
||||||
break
|
break
|
||||||
comp.errorAtCurrent(comp.current.text)
|
comp.errorAtCurrent(comp.current.text)
|
||||||
|
@ -59,8 +57,6 @@ proc synchronize*(comp: Compiler) =
|
||||||
|
|
||||||
proc writeChunk*(comp: Compiler, dStackIndex: int, ch: OpCode | DoubleUint8 | uint8) =
|
proc writeChunk*(comp: Compiler, dStackIndex: int, ch: OpCode | DoubleUint8 | uint8) =
|
||||||
comp.stackIndex += dStackIndex
|
comp.stackIndex += dStackIndex
|
||||||
when debugCompiler:
|
|
||||||
debugEcho &"new stackindex: {comp.stackIndex}, delta: {dStackIndex} due to {ch.repr}"
|
|
||||||
comp.chunk.writeChunk(ch, comp.previous.line)
|
comp.chunk.writeChunk(ch, comp.previous.line)
|
||||||
|
|
||||||
proc writePops*(comp: Compiler, count: int) =
|
proc writePops*(comp: Compiler, count: int) =
|
||||||
|
@ -68,8 +64,6 @@ proc writePops*(comp: Compiler, count: int) =
|
||||||
comp.error("Too many local variables in block.")
|
comp.error("Too many local variables in block.")
|
||||||
if count == 0:
|
if count == 0:
|
||||||
return
|
return
|
||||||
when debugCompiler:
|
|
||||||
debugEcho &"Emitting {count}xPop."
|
|
||||||
|
|
||||||
if count == 1:
|
if count == 1:
|
||||||
comp.writeChunk(-1, opPop)
|
comp.writeChunk(-1, opPop)
|
||||||
|
|
|
@ -67,8 +67,6 @@ proc advance(parser: Parser) =
|
||||||
parser.next = none[Token]()
|
parser.next = none[Token]()
|
||||||
else:
|
else:
|
||||||
parser.current = parser.scanner.scanToken()
|
parser.current = parser.scanner.scanToken()
|
||||||
when debugScanner:
|
|
||||||
parser.current.debugPrint()
|
|
||||||
if (parser.current.tokenType != tkError):
|
if (parser.current.tokenType != tkError):
|
||||||
break
|
break
|
||||||
parser.errorAtCurrent(parser.current.text)
|
parser.errorAtCurrent(parser.current.text)
|
||||||
|
|
|
@ -3,11 +3,7 @@ type
|
||||||
leBasic, leRdstdin
|
leBasic, leRdstdin
|
||||||
ReadlineInterruptedException* = object of CatchableError
|
ReadlineInterruptedException* = object of CatchableError
|
||||||
|
|
||||||
# compiler debug options
|
|
||||||
const debugScanner* = false
|
|
||||||
const debugCompiler* = false
|
|
||||||
const debugDumpChunk* = defined(debug)
|
const debugDumpChunk* = defined(debug)
|
||||||
const assertionsCompiler* = true # sanity checks in the compiler
|
|
||||||
# vm debug options (setting any to true will slow runtime down!)
|
# vm debug options (setting any to true will slow runtime down!)
|
||||||
const debugVM* = defined(debug)
|
const debugVM* = defined(debug)
|
||||||
const assertionsVM* = defined(debug) or defined(release) # sanity checks in the VM, such as the stack being empty at the end
|
const assertionsVM* = defined(debug) or defined(release) # sanity checks in the VM, such as the stack being empty at the end
|
||||||
|
|
Loading…
Reference in New Issue