1 files changed,
18 insertions(+),
19 deletions(-)
Author:
Oleksandr Smirnov
olexsmir@gmail.com
Committed at:
2025-10-17 16:29:09 +0300
Change ID:
sywkzukyqwypwwwtwszyynmwvkqmxpyo
Parent:
dc12bd2
M
src/gbf/eval.gleam
@@ -11,7 +11,7 @@ /// An unexpected command was encountered at the given position.
UnexpectedCommand(pos: lexer.Position) /// An error occurred in the virtual machine - VmError(reason: vm.Error) + VmError(reason: vm.Error, pos: lexer.Position) } /// Evaluates an AST node against the virtual machine.@@ -28,20 +28,22 @@ command: Command,
vm: VirtualMachine, ) -> Result(VirtualMachine, Error) { case command { - #(token.IncrementPointer, _) -> - vm.set_pointer(vm, vm.pointer + 1) |> wrap_vm_error() - #(token.DecrementPointer, _) -> - vm.set_pointer(vm, vm.pointer - 1) |> wrap_vm_error() + #(token.Comment(_), _) -> Ok(vm) + + #(token.IncrementPointer, p) -> + vm.set_pointer(vm, vm.pointer + 1) |> wrap_vm_error(p) + #(token.DecrementPointer, p) -> + vm.set_pointer(vm, vm.pointer - 1) |> wrap_vm_error(p) - #(token.IncrementByte, _) -> mut_byte(vm, int.add) - #(token.DecrementByte, _) -> mut_byte(vm, int.subtract) + #(token.IncrementByte, p) -> mut_byte(vm, int.add) |> wrap_vm_error(p) + #(token.DecrementByte, p) -> mut_byte(vm, int.subtract) |> wrap_vm_error(p) - #(token.InputByte, _) -> vm.input_byte(vm) |> wrap_vm_error - #(token.OutputByte, _) -> vm.output_byte(vm) |> wrap_vm_error + #(token.InputByte, p) -> vm.input_byte(vm) |> wrap_vm_error(p) + #(token.OutputByte, p) -> vm.output_byte(vm) |> wrap_vm_error(p) - #(token.StartBlock, pos) -> Error(UnexpectedCommand(pos)) - #(token.EndBlock, pos) -> Error(UnexpectedCommand(pos)) - #(_, pos) -> Error(UnexpectedCommand(pos)) + #(token.StartBlock, p) -> Error(UnexpectedCommand(p)) + #(token.EndBlock, p) -> Error(UnexpectedCommand(p)) + #(_, p) -> Error(UnexpectedCommand(p)) } }@@ -57,7 +59,7 @@
fn eval_child_block(vm: VirtualMachine, child_block: Block) { use cell_value <- result.try( vm.get_cell(vm, vm.pointer) - |> result.map_error(VmError), + |> result.map_error(VmError(_, pos: child_block.position)), ) case cell_value > 0 {@@ -70,18 +72,15 @@ }
} fn mut_byte(vm: VirtualMachine, op: fn(Int, Int) -> Int) { - use cell_value <- result.try( - vm.get_cell(vm, vm.pointer) - |> result.map_error(VmError), - ) + use cell_value <- result.try(vm.get_cell(vm, vm.pointer)) let cell_value = op(cell_value, 1) vm.set_cell(vm, vm.pointer, cell_value) - |> result.map_error(VmError) } fn wrap_vm_error( r: Result(VirtualMachine, vm.Error), + pos: lexer.Position, ) -> Result(VirtualMachine, Error) { - result.map_error(r, VmError) + result.map_error(r, VmError(_, pos)) }