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:
prod2 2022-12-03 12:08:41 +01:00
parent 3405c10b08
commit 04dd2bf73f
9 changed files with 21 additions and 48 deletions

View File

@ -3,6 +3,7 @@ import ndspkg/compv2/parser
import ndspkg/compv2/node
import ndspkg/compv2/emitter
import ndspkg/config
import ndspkg/chunk
when compilerChoice == cmOne:
import ndspkg/compiler/compiler
@ -27,6 +28,11 @@ proc interpret(name: string, source: string): Result =
return rsCompileError
let emitter = newEmitter(name, node)
emitter.emit()
if emitter.hadError:
return rsCompileError
when debugDumpChunk:
emitter.chunk.disassembleChunk()
case emitter.chunk.run():
of irOK:
rsOK

View File

@ -18,8 +18,6 @@ proc number(comp: Compiler) =
# assume the number is already advanced through
let value = comp.previous.text.parseFloat.fromFloat()
comp.writeConstant(value)
when debugCompiler:
debugEcho &"Written constant (type: {value.ndType}, str repr: {$value}) to chunk"
tkNumber.genRule(number, nop, pcNone)
@ -41,8 +39,6 @@ tkNil.genRule(expNil, nop, pcNone)
proc expString(comp: Compiler) =
let value = comp.previous.text[1..^2].fromNimString()
comp.writeConstant(value)
when debugCompiler:
debugEcho &"Written constant (type: {value.ndType}, str repr: {$value}) to chunk"
tkString.genRule(expString, nop, pcNone)

View File

@ -128,10 +128,9 @@ proc parseFunct*(comp: Compiler) =
comp.stackIndex = i
comp.addLocal(params[i-1], 0)
comp.expression()
when assertionsCompiler:
let shouldbeStackIndex = params.len + 1
if shouldbeStackIndex != comp.stackIndex:
comp.error(&"Assertion failed: wrong stackindex ({comp.stackIndex}) in function declaration (should be {shouldbeStackIndex}).")
let shouldbeStackIndex = params.len + 1
if shouldbeStackIndex != comp.stackIndex:
comp.error(&"Assertion failed: wrong stackindex ({comp.stackIndex}) in function declaration (should be {shouldbeStackIndex}).")
let f = comp.endScope()
dec comp.stackIndex # the previous end scope did not put anything on the stack, it is jumped over

View File

@ -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
comp.writeChunk(delta, op)
comp.writeChunk(0, 0xffffff.toDU8)
when assertionsCompiler:
stacklen = comp.stackIndex
stacklen = comp.stackIndex
comp.chunk.len - argSize
proc patchJump*(comp: Compiler, offset: int, stacklen: int) =
when assertionsCompiler:
if comp.stackIndex != stacklen:
comp.error("Assertion failed: loop doesn't preserve stackindex.")
if comp.stackIndex != stacklen:
comp.error("Assertion failed: loop doesn't preserve stackindex.")
let jump = (comp.chunk.len - offset - argSize)
if (jump > argMax):
@ -30,9 +28,8 @@ proc patchJump*(comp: Compiler, offset: int, stacklen: int) =
comp.chunk.code[offset + 1] = jumpt[1]
proc emitLoop*(comp: Compiler, loopstart: int, delta: int, op: OpCode, stacklen: int) =
when assertionsCompiler:
if comp.stackIndex != stacklen:
comp.error("Assertion failed: loop doesn't preserve stackindex.")
if comp.stackIndex != stacklen:
comp.error("Assertion failed: loop doesn't preserve stackindex.")
comp.writeChunk(delta, op)
let offset = comp.chunk.len - loopstart + argSize

View File

@ -50,9 +50,6 @@ proc parsePrecedence*(comp: Compiler, prec: Precedence) =
if rule.prefix != nop:
comp.canAssign = prec <= pcAssignment
when debugCompiler:
debugEcho &"parsePrecedence call, valid prefix op found, rule used: {rule.name}, precedence: {prec}"
rule.prefix(comp)
while prec <= comp.current.tokenType.getRule().prec:

View File

@ -15,9 +15,6 @@ import jumps
proc beginScope*(comp: Compiler, function: bool = false) =
let scope = comp.newScope(function)
when debugCompiler:
debugEcho &"Begin scope called for depth {comp.scopes.len} function? {function}"
if not function:
while comp.match(tkLabel):
let label = comp.previous.text[1..^1]
@ -35,20 +32,16 @@ proc beginScope*(comp: Compiler, function: bool = false) =
proc scopeRetIndex*(comp: Compiler): int =
let scope = comp.scopes[comp.scopes.high()]
when assertionsCompiler:
# this is an illegal operation for function scopes, as they work differently
if scope.function:
comp.error("Assertion failed, Internal error, scopeRetIndex calculation for a function scope.")
# this is an illegal operation for function scopes, as they work differently
if scope.function:
comp.error("Assertion failed, Internal error, scopeRetIndex calculation for a function scope.")
return scope.goalStackIndex
proc restore*(comp: Compiler, scope: Scope) =
let delta = comp.stackIndex - scope.goalStackIndex
comp.writePops(delta)
when assertionsCompiler:
if not comp.stackIndex == scope.goalStackIndex:
comp.error("Assertion failed in restore")
when debugCompiler:
debugEcho &"Restored scope: delta {delta}"
if not comp.stackIndex == scope.goalStackIndex:
comp.error("Assertion failed in restore")
proc restoreInFunct*(comp: Compiler, scope: Scope) =
#let pops = comp.stackIndex
@ -88,18 +81,15 @@ proc endScope*(comp: Compiler): Scope =
i.dec()
# restore the stackIndex, emit pops
when debugCompiler:
debugEcho &"End scope called for depth {comp.scopes.len} function? {function}"
if function:
comp.restoreInFunct(popped)
else:
comp.restore(popped)
# patch jumps to after the scope (such jumps from breaks emit the pops before jumping)
for jump in popped.jumps:
when assertionsCompiler:
if function:
if function:
# 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)
if function:
comp.writeChunk(0, opReturn)

View File

@ -29,8 +29,6 @@ proc advance*(comp: Compiler) =
comp.previous = comp.current
while true:
comp.current = comp.scanner.scanToken()
when debugScanner:
comp.current.debugPrint()
if (comp.current.tokenType != tkError):
break
comp.errorAtCurrent(comp.current.text)
@ -59,8 +57,6 @@ proc synchronize*(comp: Compiler) =
proc writeChunk*(comp: Compiler, dStackIndex: int, ch: OpCode | DoubleUint8 | uint8) =
comp.stackIndex += dStackIndex
when debugCompiler:
debugEcho &"new stackindex: {comp.stackIndex}, delta: {dStackIndex} due to {ch.repr}"
comp.chunk.writeChunk(ch, comp.previous.line)
proc writePops*(comp: Compiler, count: int) =
@ -68,8 +64,6 @@ proc writePops*(comp: Compiler, count: int) =
comp.error("Too many local variables in block.")
if count == 0:
return
when debugCompiler:
debugEcho &"Emitting {count}xPop."
if count == 1:
comp.writeChunk(-1, opPop)

View File

@ -67,8 +67,6 @@ proc advance(parser: Parser) =
parser.next = none[Token]()
else:
parser.current = parser.scanner.scanToken()
when debugScanner:
parser.current.debugPrint()
if (parser.current.tokenType != tkError):
break
parser.errorAtCurrent(parser.current.text)

View File

@ -3,11 +3,7 @@ type
leBasic, leRdstdin
ReadlineInterruptedException* = object of CatchableError
# compiler debug options
const debugScanner* = false
const debugCompiler* = false
const debugDumpChunk* = defined(debug)
const assertionsCompiler* = true # sanity checks in the compiler
# vm debug options (setting any to true will slow runtime down!)
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