From 7ef5b4dfbfd063f2e93a02a47c5a3e2313a02c0d Mon Sep 17 00:00:00 2001 From: Mattia Giambirtone Date: Mon, 15 Aug 2022 17:20:09 +0200 Subject: [PATCH] Fixed some issues with strings and added debug print to fibonacci test --- src/backend/vm.nim | 2 +- src/config.nim | 4 ++-- src/frontend/compiler.nim | 4 ++-- src/frontend/lexer.nim | 2 +- tests/import/fib.pn | 2 ++ tests/import/lib.pn | 5 +++++ 6 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/backend/vm.nim b/src/backend/vm.nim index 5afaf4c..8df9989 100644 --- a/src/backend/vm.nim +++ b/src/backend/vm.nim @@ -268,7 +268,7 @@ proc constReadString(self: PeonVM, size: int, idx: int): PeonObject = ## chunk's constant table and ## returns a Peon object. Assumes ## the constant is a string - result = PeonObject(kind: String, str: self.chunk.consts[idx.. 255: {.fatal: "The git branch name's length must be less than or equal to 255 characters".} -const DEBUG_TRACE_VM* = false # Traces VM execution +const DEBUG_TRACE_VM* = debugVM # Traces VM execution const DEBUG_TRACE_GC* = false # Traces the garbage collector (TODO) const DEBUG_TRACE_ALLOCATION* = false # Traces memory allocation/deallocation const DEBUG_TRACE_COMPILER* = false # Traces the compiler diff --git a/src/frontend/compiler.nim b/src/frontend/compiler.nim index c7693f5..67e9bc6 100644 --- a/src/frontend/compiler.nim +++ b/src/frontend/compiler.nim @@ -305,7 +305,7 @@ proc makeConstant(self: Compiler, val: Expression, typ: Type): array[3, uint8] = of Int64, UInt64: result = self.chunk.writeConstant(v.toLong()) of String: - result = self.chunk.writeConstant(v.toBytes()) + result = self.chunk.writeConstant(val.token.lexeme[1..^1].toBytes()) of Float32: var f: float = 0.0 discard parseFloat(val.token.lexeme, f) @@ -343,7 +343,7 @@ proc emitConstant(self: Compiler, obj: Expression, kind: Type) = let str = LiteralExpr(obj).literal.lexeme if str.len() >= 16777216: self.error("string constants cannot be larger than 16777215 bytes") - self.emitBytes(str.len().toTriple()) + self.emitBytes((str.len() - 2).toTriple()) of Float32: self.emitByte(LoadFloat32) of Float64: diff --git a/src/frontend/lexer.nim b/src/frontend/lexer.nim index d6218ad..c72cb89 100644 --- a/src/frontend/lexer.nim +++ b/src/frontend/lexer.nim @@ -195,7 +195,7 @@ proc incLine(self: Lexer) = ## Increments the lexer's line ## counter and updates internal ## line metadata - self.lines.add((self.lastLine, self.current - 1)) + self.lines.add((self.lastLine, self.current)) self.lastLine = self.current self.line += 1 diff --git a/tests/import/fib.pn b/tests/import/fib.pn index 7e08723..b44f054 100644 --- a/tests/import/fib.pn +++ b/tests/import/fib.pn @@ -9,6 +9,8 @@ fn fib(n: int): int { } +print("Computing the value of fib(25)"); var x = clock(); print(fib(25)); print(clock() - x); +print("Done!"); \ No newline at end of file diff --git a/tests/import/lib.pn b/tests/import/lib.pn index 45393f4..bcb3b10 100644 --- a/tests/import/lib.pn +++ b/tests/import/lib.pn @@ -36,3 +36,8 @@ fn print*(x: float) { fn print*(x: int) { #pragma[magic: "GenericPrint"] } + + +fn print*(x: string) { + #pragma[magic: "GenericPrint"] +}