The REPL no longer shows the last pop value if an error occurs

This commit is contained in:
nocturn9x 2021-09-30 19:38:35 +02:00
parent b9b5301ad4
commit 408d8cbeaa
4 changed files with 4 additions and 80 deletions

View File

@ -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()

View File

@ -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() == "<lambda function>":
result = self.function.name.toStr()
else:
result = "<function '" & self.function.name.toStr() & "'>"
else:
result = "<code object>"
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

View File

@ -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

View File

@ -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,