From 408d8cbeaac0950904970f7f85e1a41b6df97632 Mon Sep 17 00:00:00 2001 From: nocturn9x Date: Thu, 30 Sep 2021 19:38:35 +0200 Subject: [PATCH] The REPL no longer shows the last pop value if an error occurs --- src/japl.nim | 5 ++-- src/types/closure.nim | 63 ------------------------------------------ src/types/function.nim | 14 ---------- src/vm.nim | 2 +- 4 files changed, 4 insertions(+), 80 deletions(-) delete mode 100644 src/types/closure.nim diff --git a/src/japl.nim b/src/japl.nim index 7900c49..bafa6ff 100644 --- a/src/japl.nim +++ b/src/japl.nim @@ -52,6 +52,7 @@ proc repl(vmObj: Option[VM]) = var source = "" let lineEditor = getLineEditor() var keep = true + var result: InterpretResult lineEditor.bindEvent(jeQuit): keep = false while keep: @@ -64,8 +65,8 @@ proc repl(vmObj: Option[VM]) = echo "Goodbye!" break elif source != "": - discard bytecodeVM.interpret(source, "stdin") - if not bytecodeVM.lastPop.isJaplNil(): + result = bytecodeVM.interpret(source, "stdin") + if not bytecodeVM.lastPop.isJaplNil() and result notin {CompileError, RuntimeError}: echo stringify(bytecodeVM.lastPop) bytecodeVM.lastPop = cast[ptr Nil](bytecodeVM.cached[2]) bytecodeVM.freeVM() diff --git a/src/types/closure.nim b/src/types/closure.nim deleted file mode 100644 index aaa662c..0000000 --- a/src/types/closure.nim +++ /dev/null @@ -1,63 +0,0 @@ -# Copyright 2020 Mattia Giambirtone -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -## Implements closures - -import baseObject -import function -import ../memory - - -type Closure* = object of Obj - ## Represents a function - ## closing over values outside - ## of its lexical scope - function*: ptr Function - - -proc newClosure(function: ptr Function): ptr Closure = - ## Initializes a new closure from a function - ## object - result = allocateObject(Closure, ObjectType.Closure) - result.function = function - - -proc typeName*(self: ptr Closure): string = - result = "function" - - -proc stringify*(self: ptr Closure): string = - if self.function.name != nil: - if self.function.name.toStr() == "": - result = self.function.name.toStr() - else: - result = "" - else: - result = "" - - -proc isFalsey*(self: ptr Closure): bool = - result = false - - -proc hash*(self: ptr Closure): uint64 = - result = uint64(393027534) # Arbitrary hash because ¯\_(ツ)_/¯ - - -proc eq*(self, other: ptr Closure): bool = - result = self.function == other.function # Pointer equality - - -proc eq*(self: ptr Closure, other: ptr Function): bool = - result = self.function == other diff --git a/src/types/function.nim b/src/types/function.nim index 4ccbdc8..ef24dbc 100644 --- a/src/types/function.nim +++ b/src/types/function.nim @@ -1,17 +1,3 @@ -# Copyright 2020 Mattia Giambirtone -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - import baseObject import ../meta/opcode import japlString diff --git a/src/vm.nim b/src/vm.nim index d0a98ae..acce138 100644 --- a/src/vm.nim +++ b/src/vm.nim @@ -46,7 +46,7 @@ when DEBUG_TRACE_VM: type KeyboardInterrupt* = object of CatchableError ## Custom exception to handle Ctrl+C - InterpretResult = enum + InterpretResult* = enum ## All possible interpretation results Ok, CompileError,