all repos

gbf @ e9c84a0

⭐ gleaming brainfuck
6 files changed, 21 insertions(+), 24 deletions(-)
some refactoring

- rename char module to ascii
- make code more better looking imo
Author: Oleksandr Smirnov olexsmir@gmail.com
Committed at: 2025-10-21 13:36:47 +0300
Parent: 6862d63
M src/gbf.gleam

@@ -1,4 +1,4 @@

-import char +import ascii import gbf/eval import gbf/lexer import gbf/parser

@@ -16,7 +16,7 @@ pub fn run(source: String) -> Result(VirtualMachine, Error) {

let bvm = source |> string.split(on: "") - |> list.map(char.to_code) + |> list.map(ascii.to_code) |> vm.new use ast <- result.try(parse_ast(source))
M src/gbf/eval.gleam

@@ -18,14 +18,14 @@ /// Evaluates an AST node against the virtual machine.

/// pub fn eval(vm: VirtualMachine, node: AST) -> Result(VirtualMachine, Error) { case node { - parser.Leaf(command) -> eval_command(command, vm) + parser.Leaf(command) -> eval_command(vm, command) parser.Node(block) -> eval_block(vm, block) } } fn eval_command( - command: Command, vm: VirtualMachine, + command: Command, ) -> Result(VirtualMachine, Error) { case command { #(token.Comment(_), _) -> Ok(vm)

@@ -50,7 +50,7 @@

fn eval_block(vm: VirtualMachine, block: Block) -> Result(VirtualMachine, Error) { use acc_vm, child <- list.fold(block.children, Ok(vm)) case child { - parser.Leaf(command) -> result.try(acc_vm, eval_command(command, _)) + parser.Leaf(command) -> result.try(acc_vm, eval_command(_, command)) parser.Node(child_block) -> result.try(acc_vm, eval_child_block(_, child_block)) }

@@ -65,17 +65,16 @@

case cell_value > 0 { False -> Ok(vm) True -> { - let new_acc = eval_block(vm, child_block) - result.try(new_acc, eval_child_block(_, child_block)) + let acc = eval_block(vm, child_block) + result.try(acc, eval_child_block(_, child_block)) } } } fn mut_byte(vm: VirtualMachine, op: fn(Int, Int) -> Int) { - 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) + use cell <- result.try(vm.get_cell(vm, vm.pointer)) + let cell = op(cell, 1) + vm.set_cell(vm, vm.pointer, cell) } fn wrap_vm_error(
M src/gbf/parser.gleam

@@ -35,7 +35,6 @@ /// All tokens must be consumed for successful parsing.

/// pub fn parse(tokens: List(#(Token, Position))) -> Result(AST, Error) { let root = Node(Block(children: [], position: Position(0))) - use #(ast, remaining_tokens) <- result.try(parse_tokens(tokens, root)) case remaining_tokens {

@@ -71,11 +70,10 @@ tokens,

child_block, )) - let new_children = list.append(block.children, [parsed_child_block]) - let new_node = - Node(Block(children: new_children, position: block.position)) + let children = list.append(block.children, [parsed_child_block]) + let node = Node(Block(children: children, position: block.position)) - parse_tokens(remaining_tokens, new_node) + parse_tokens(remaining_tokens, node) } } }
M src/gbf/vm.gleam

@@ -1,4 +1,4 @@

-import char +import ascii import gleam/dict.{type Dict} import gleam/list import gleam/result

@@ -79,9 +79,9 @@ value: Int,

) -> Result(VirtualMachine, Error) { use pointer <- result.try(validate_tape_size(pointer)) use value <- result.try(validate_cell_size(value)) + let cells = dict.insert(vm.cells, pointer, value) - let new_cells = dict.insert(vm.cells, pointer, value) - VirtualMachine(..vm, cells: new_cells) + VirtualMachine(..vm, cells:) |> Ok }

@@ -132,7 +132,7 @@ ///

pub fn output_byte(vm: VirtualMachine) -> Result(VirtualMachine, Error) { use cell_value <- result.try(get_cell(vm, vm.pointer)) - case char.from_code(cell_value) { + case ascii.from_code(cell_value) { "" -> Error(InvalidChar(cell_value)) c -> VirtualMachine(..vm, output: vm.output <> c)
M test/gbf_vm_test.gleam

@@ -1,4 +1,4 @@

-import char +import ascii import gbf/vm.{type VirtualMachine} import gleam/result import gleeunit/should

@@ -53,7 +53,7 @@ |> should.equal(Error(vm.InvalidChar(0)))

} pub fn output_byte_test() { - let vm = setup([char.to_code("a")]) + let vm = setup([ascii.to_code("a")]) use vm <- result.try(vm.output_byte(vm)) should.equal(vm.output, "a")

@@ -68,9 +68,9 @@ |> should.equal(Error(vm.EmptyInput))

} pub fn input_byte_test() { - let vm = setup([char.to_code("a"), char.to_code("b")]) + let vm = setup([ascii.to_code("a"), ascii.to_code("b")]) use vm <- result.try(vm.input_byte(vm)) - should.equal(vm.input, [char.to_code("b")]) + should.equal(vm.input, [ascii.to_code("b")]) Ok("") }