⭐ gleaming brainfuck

some refactoring

- rename char module to ascii
- make code more better looking imo

olexsmir.xyz e9c84a09 6862d63b

verified
src/char.gleam src/ascii.gleam
+2 -2
src/gbf.gleam
··· 1 - import char 1 + import ascii 2 2 import gbf/eval 3 3 import gbf/lexer 4 4 import gbf/parser ··· 16 16 let bvm = 17 17 source 18 18 |> string.split(on: "") 19 - |> list.map(char.to_code) 19 + |> list.map(ascii.to_code) 20 20 |> vm.new 21 21 22 22 use ast <- result.try(parse_ast(source))
+8 -9
src/gbf/eval.gleam
··· 18 18 /// 19 19 pub fn eval(vm: VirtualMachine, node: AST) -> Result(VirtualMachine, Error) { 20 20 case node { 21 - parser.Leaf(command) -> eval_command(command, vm) 21 + parser.Leaf(command) -> eval_command(vm, command) 22 22 parser.Node(block) -> eval_block(vm, block) 23 23 } 24 24 } 25 25 26 26 fn eval_command( 27 - command: Command, 28 27 vm: VirtualMachine, 28 + command: Command, 29 29 ) -> Result(VirtualMachine, Error) { 30 30 case command { 31 31 #(token.Comment(_), _) -> Ok(vm) ··· 50 50 fn eval_block(vm: VirtualMachine, block: Block) -> Result(VirtualMachine, Error) { 51 51 use acc_vm, child <- list.fold(block.children, Ok(vm)) 52 52 case child { 53 - parser.Leaf(command) -> result.try(acc_vm, eval_command(command, _)) 53 + parser.Leaf(command) -> result.try(acc_vm, eval_command(_, command)) 54 54 parser.Node(child_block) -> 55 55 result.try(acc_vm, eval_child_block(_, child_block)) 56 56 } ··· 65 65 case cell_value > 0 { 66 66 False -> Ok(vm) 67 67 True -> { 68 - let new_acc = eval_block(vm, child_block) 69 - result.try(new_acc, eval_child_block(_, child_block)) 68 + let acc = eval_block(vm, child_block) 69 + result.try(acc, eval_child_block(_, child_block)) 70 70 } 71 71 } 72 72 } 73 73 74 74 fn mut_byte(vm: VirtualMachine, op: fn(Int, Int) -> Int) { 75 - use cell_value <- result.try(vm.get_cell(vm, vm.pointer)) 76 - 77 - let cell_value = op(cell_value, 1) 78 - vm.set_cell(vm, vm.pointer, cell_value) 75 + use cell <- result.try(vm.get_cell(vm, vm.pointer)) 76 + let cell = op(cell, 1) 77 + vm.set_cell(vm, vm.pointer, cell) 79 78 } 80 79 81 80 fn wrap_vm_error(
+3 -5
src/gbf/parser.gleam
··· 35 35 /// 36 36 pub fn parse(tokens: List(#(Token, Position))) -> Result(AST, Error) { 37 37 let root = Node(Block(children: [], position: Position(0))) 38 - 39 38 use #(ast, remaining_tokens) <- result.try(parse_tokens(tokens, root)) 40 39 41 40 case remaining_tokens { ··· 71 70 child_block, 72 71 )) 73 72 74 - let new_children = list.append(block.children, [parsed_child_block]) 75 - let new_node = 76 - Node(Block(children: new_children, position: block.position)) 73 + let children = list.append(block.children, [parsed_child_block]) 74 + let node = Node(Block(children: children, position: block.position)) 77 75 78 - parse_tokens(remaining_tokens, new_node) 76 + parse_tokens(remaining_tokens, node) 79 77 } 80 78 } 81 79 }
+4 -4
src/gbf/vm.gleam
··· 1 - import char 1 + import ascii 2 2 import gleam/dict.{type Dict} 3 3 import gleam/list 4 4 import gleam/result ··· 79 79 ) -> Result(VirtualMachine, Error) { 80 80 use pointer <- result.try(validate_tape_size(pointer)) 81 81 use value <- result.try(validate_cell_size(value)) 82 + let cells = dict.insert(vm.cells, pointer, value) 82 83 83 - let new_cells = dict.insert(vm.cells, pointer, value) 84 - VirtualMachine(..vm, cells: new_cells) 84 + VirtualMachine(..vm, cells:) 85 85 |> Ok 86 86 } 87 87 ··· 132 132 pub fn output_byte(vm: VirtualMachine) -> Result(VirtualMachine, Error) { 133 133 use cell_value <- result.try(get_cell(vm, vm.pointer)) 134 134 135 - case char.from_code(cell_value) { 135 + case ascii.from_code(cell_value) { 136 136 "" -> Error(InvalidChar(cell_value)) 137 137 c -> 138 138 VirtualMachine(..vm, output: vm.output <> c)
+4 -4
test/gbf_vm_test.gleam
··· 1 - import char 1 + import ascii 2 2 import gbf/vm.{type VirtualMachine} 3 3 import gleam/result 4 4 import gleeunit/should ··· 53 53 } 54 54 55 55 pub fn output_byte_test() { 56 - let vm = setup([char.to_code("a")]) 56 + let vm = setup([ascii.to_code("a")]) 57 57 use vm <- result.try(vm.output_byte(vm)) 58 58 59 59 should.equal(vm.output, "a") ··· 68 68 } 69 69 70 70 pub fn input_byte_test() { 71 - let vm = setup([char.to_code("a"), char.to_code("b")]) 71 + let vm = setup([ascii.to_code("a"), ascii.to_code("b")]) 72 72 use vm <- result.try(vm.input_byte(vm)) 73 73 74 - should.equal(vm.input, [char.to_code("b")]) 74 + should.equal(vm.input, [ascii.to_code("b")]) 75 75 Ok("") 76 76 }