From 04dd2bf73f35f6eba92fa5bd70015da322e75c63 Mon Sep 17 00:00:00 2001 From: prod2 <95874442+prod2@users.noreply.github.com> Date: Sat, 3 Dec 2022 12:08:41 +0100 Subject: [PATCH] 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 --- src/nds.nim | 6 ++++++ src/ndspkg/compiler/expressions.nim | 4 ---- src/ndspkg/compiler/functions.nim | 7 +++---- src/ndspkg/compiler/jumps.nim | 13 +++++-------- src/ndspkg/compiler/precedence.nim | 3 --- src/ndspkg/compiler/scope.nim | 24 +++++++----------------- src/ndspkg/compiler/utils.nim | 6 ------ src/ndspkg/compv2/parser.nim | 2 -- src/ndspkg/config.nim | 4 ---- 9 files changed, 21 insertions(+), 48 deletions(-) diff --git a/src/nds.nim b/src/nds.nim index 2feb436..8a81229 100644 --- a/src/nds.nim +++ b/src/nds.nim @@ -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 diff --git a/src/ndspkg/compiler/expressions.nim b/src/ndspkg/compiler/expressions.nim index 9676034..15eb483 100644 --- a/src/ndspkg/compiler/expressions.nim +++ b/src/ndspkg/compiler/expressions.nim @@ -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) diff --git a/src/ndspkg/compiler/functions.nim b/src/ndspkg/compiler/functions.nim index eddcf4b..7159c11 100644 --- a/src/ndspkg/compiler/functions.nim +++ b/src/ndspkg/compiler/functions.nim @@ -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 diff --git a/src/ndspkg/compiler/jumps.nim b/src/ndspkg/compiler/jumps.nim index 8f76065..c719171 100644 --- a/src/ndspkg/compiler/jumps.nim +++ b/src/ndspkg/compiler/jumps.nim @@ -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 diff --git a/src/ndspkg/compiler/precedence.nim b/src/ndspkg/compiler/precedence.nim index 395fc9b..d79668f 100644 --- a/src/ndspkg/compiler/precedence.nim +++ b/src/ndspkg/compiler/precedence.nim @@ -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: diff --git a/src/ndspkg/compiler/scope.nim b/src/ndspkg/compiler/scope.nim index 4398a16..f34576d 100644 --- a/src/ndspkg/compiler/scope.nim +++ b/src/ndspkg/compiler/scope.nim @@ -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) diff --git a/src/ndspkg/compiler/utils.nim b/src/ndspkg/compiler/utils.nim index f8b211b..0fd5c82 100644 --- a/src/ndspkg/compiler/utils.nim +++ b/src/ndspkg/compiler/utils.nim @@ -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) diff --git a/src/ndspkg/compv2/parser.nim b/src/ndspkg/compv2/parser.nim index 4e9274a..dacf8e9 100644 --- a/src/ndspkg/compv2/parser.nim +++ b/src/ndspkg/compv2/parser.nim @@ -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) diff --git a/src/ndspkg/config.nim b/src/ndspkg/config.nim index b0b663d..5eca083 100644 --- a/src/ndspkg/config.nim +++ b/src/ndspkg/config.nim @@ -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