From 44aab6f5fc1b33f5a6870482f7537fe03e69a091 Mon Sep 17 00:00:00 2001 From: Productive2 <48047721+Productive2@users.noreply.github.com> Date: Sun, 25 Oct 2020 17:47:53 +0100 Subject: [PATCH] a few minor changes, so it compiles --- src/compiler.nim | 16 +++++++++++++--- src/config.nim | 4 ++-- src/meta/opcode.nim | 5 ----- src/types/jobject.nim | 40 ++++++++++++++++++++++++++++++++++++---- src/vm.nim | 11 ++++------- 5 files changed, 55 insertions(+), 21 deletions(-) diff --git a/src/compiler.nim b/src/compiler.nim index 0a1c20a..8249723 100644 --- a/src/compiler.nim +++ b/src/compiler.nim @@ -24,8 +24,6 @@ import meta/opcode import meta/token import meta/looptype import types/jobject -import types/jstring -import types/function import tables import config when isMainModule: @@ -1081,6 +1079,9 @@ proc getRule(kind: TokenType): ParseRule = proc compile*(self: ref Compiler, source: string): ptr Function = + when DEBUG_TRACE_COMPILER: + echo "==== COMPILER debugger starts ====" + echo "" ## Compiles a source string into a function ## object. This wires up all the code ## inside the parser and the lexer @@ -1090,10 +1091,19 @@ proc compile*(self: ref Compiler, source: string): ptr Function = self.parser = initParser(tokens, self.file) while not self.parser.match(EOF): self.declaration() + var function = self.endCompiler() + when DEBUG_TRACE_COMPILER: + echo "==== COMPILER debugger ends ====" + echo "" + if not self.parser.hadError: + when DEBUG_TRACE_COMPILER: + echo "Result: Ok" return function else: + when DEBUG_TRACE_COMPILER: + echo "Result: Fail" return nil else: return nil @@ -1126,10 +1136,10 @@ proc initCompiler*(context: FunctionType, enclosing: ref Compiler = nil, parser: # This way the compiler can be executed on its own # without the VM when isMainModule: - var compiler: ref Compiler = initCompiler(SCRIPT, file="test") echo "JAPL Compiler REPL" while true: try: + var compiler: ref Compiler = initCompiler(SCRIPT, file="test") stdout.write("=> ") var compiled = compiler.compile(stdin.readLine()) if compiled != nil: diff --git a/src/config.nim b/src/config.nim index e9fb284..b79fa8f 100644 --- a/src/config.nim +++ b/src/config.nim @@ -17,8 +17,8 @@ const FRAMES_MAX* = 400 # TODO: Inspect why the VM crashes if this exceeds 400 const JAPL_VERSION* = "0.2.0" const JAPL_RELEASE* = "alpha" -const DEBUG_TRACE_VM* = false # Traces VM execution +const DEBUG_TRACE_VM* = false # Traces VM execution const DEBUG_TRACE_GC* = true # Traces the garbage collector (TODO) const DEBUG_TRACE_ALLOCATION* = true # Traces memory allocation/deallocation (WIP) -const DEBUG_TRACE_COMPILER* = true # Traces the compiler (TODO) +const DEBUG_TRACE_COMPILER* = false # Traces the compiler (WIP) diff --git a/src/meta/opcode.nim b/src/meta/opcode.nim index 85e5fa1..1c853ad 100644 --- a/src/meta/opcode.nim +++ b/src/meta/opcode.nim @@ -80,11 +80,6 @@ const byteInstructions* = {OpCode.SetLocal, OpCode.GetLocal, OpCode.DeleteLocal, const jumpInstructions* = {OpCode.JumpIfFalse, OpCode.Jump, OpCode.Loop} -proc newChunk*(): Chunk = - ## The constructor for the type Chunk - result = Chunk(consts: @[], code: @[], lines: @[]) - - proc writeChunk*(self: Chunk, newByte: uint8, line: int) = ## Appends newByte at line to a chunk. self.code.add(newByte) diff --git a/src/types/jobject.nim b/src/types/jobject.nim index 00378d4..49ba516 100644 --- a/src/types/jobject.nim +++ b/src/types/jobject.nim @@ -16,7 +16,6 @@ ## types inherit from this simple structure import ../memory -import ../meta/opcode import strformat @@ -65,6 +64,11 @@ type message*: ptr String +proc newChunk*(): Chunk = + ## The constructor for the type Chunk + result = Chunk(consts: @[], code: @[], lines: @[]) + + proc allocateObject*(size: int, kind: ObjectType): ptr Obj = ## Wrapper around reallocate to create a new generic JAPL object result = cast[ptr Obj](reallocate(nil, 0, size)) @@ -152,8 +156,8 @@ proc stringify(self: ptr Float): string = # Function object methods type - FunctionType* = enum - FUNC, SCRIPT + FunctionType* {.pure.} = enum + Func, Script proc newFunction*(name: string = "", chunk: Chunk = newChunk(), arity: int = 0): ptr Function = @@ -394,7 +398,7 @@ proc asNan*(): ptr NotANumber = result = allocateObj(NotANumber, ObjectType.Nan) -proc asInf*(): ptr Infinity = +proc asInf*(): ptr Infinity = ## Creates a nil object result = allocateObj(Infinity, ObjectType.Infinity) @@ -403,4 +407,32 @@ proc asObj*(obj: ptr Obj): ptr Obj = ## Creates a generic JAPL object result = allocateObj(Obj, ObjectType.BaseObject) +proc newIndexError*(message: string): ptr JAPLException = + result = allocateObj(JAPLException, ObjectType.Exception) + result.errName = newString("IndexError") + result.message = newString(message) + +proc newReferenceError*(message: string): ptr JAPLException = + result = allocateObj(JAPLException, ObjectType.Exception) + result.errName = newString("ReferenceError") + result.message = newString(message) + + +proc newInterruptedError*(message: string): ptr JAPLException = + result = allocateObj(JAPLException, ObjectType.Exception) + result.errName = newString("InterruptedError") + result.message = newString(message) + + +proc newRecursionError*(message: string): ptr JAPLException = + result = allocateObj(JAPLException, ObjectType.Exception) + result.errName = newString("RecursionError") + result.message = newString(message) + + + +proc newTypeError*(message: string): ptr JAPLException = + result = allocateObj(JAPLException, ObjectType.Exception) + result.errName = newString("TypeError") + result.message = newString(message) diff --git a/src/vm.nim b/src/vm.nim index 6be0edd..7988b5d 100644 --- a/src/vm.nim +++ b/src/vm.nim @@ -24,10 +24,7 @@ import compiler import tables import meta/opcode import meta/frame -import types/exceptions import types/jobject -import types/jstring -import types/function import memory when DEBUG_TRACE_VM: import util/debug @@ -41,10 +38,10 @@ proc `**`(a, b: float): float = pow(a, b) type KeyboardInterrupt* = object of CatchableError - InterpretResult = enum - OK, - COMPILE_ERROR, - RUNTIME_ERROR + InterpretResult {.pure.} = enum + Ok, + CompileError, + RuntimeError VM* = ref object # The VM object lastPop*: ptr Obj frameCount*: int