all repos

gbf @ cccbd0e

⭐ gleaming brainfuck
3 files changed, 34 insertions(+), 212 deletions(-)
handle chars better
Author: Oleksandr Smirnov olexsmir@gmail.com
Committed at: 2025-10-16 23:57:44 +0300
Change ID: svxkttxnwsxzvouuqrxpqvynpxklksmt
Parent: 0ca503c
A src/char.gleam

@@ -0,0 +1,25 @@

+import gleam/string + +pub fn to_code(s: String) { + case <<s:utf8>> { + // lowercase a(ansii 97) to z(ansii 122) + <<char:int>> if char >= 97 -> char - 96 + + // uppercase A(ansii 65) to Z(ansii 90), and special symbols + <<char:int>> -> char - 38 + + _ -> 0 + } +} + +pub fn from_code(code: Int) { + case code { + c if c == 0x0A || c >= 0x20 && c <= 0x7E -> { + case string.utf_codepoint(code) { + Ok(codepoint) -> string.from_utf_codepoints([codepoint]) + Error(_) -> "" + } + } + _ -> "" + } +}
M src/gbf.gleam

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

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

@@ -14,7 +15,7 @@

let vm = input |> string.split(on: "") - |> list.map(char_to_code) + |> list.map(char.to_code) |> vm.new let ast = input |> lexer.new() |> lexer.lex |> parser.parse()

@@ -29,105 +30,3 @@ Ok("")

} } } - -fn char_to_code(s: String) -> Int { - case s { - "\n" -> 0x0A - " " -> 0x20 - "!" -> 0x21 - "\"" -> 0x22 - "#" -> 0x23 - "$" -> 0x24 - "%" -> 0x25 - "&" -> 0x26 - "'" -> 0x27 - "(" -> 0x28 - ")" -> 0x29 - "*" -> 0x2A - "+" -> 0x2B - "," -> 0x2C - "-" -> 0x2D - "." -> 0x2E - "/" -> 0x2F - "0" -> 0x30 - "1" -> 0x31 - "2" -> 0x32 - "3" -> 0x33 - "4" -> 0x34 - "5" -> 0x35 - "6" -> 0x36 - "7" -> 0x37 - "8" -> 0x38 - "9" -> 0x39 - ":" -> 0x3A - ";" -> 0x3B - "<" -> 0x3C - "=" -> 0x3D - ">" -> 0x3E - "?" -> 0x3F - "@" -> 0x40 - "A" -> 0x41 - "B" -> 0x42 - "C" -> 0x43 - "D" -> 0x44 - "E" -> 0x45 - "F" -> 0x46 - "G" -> 0x47 - "H" -> 0x48 - "I" -> 0x49 - "J" -> 0x4A - "K" -> 0x4B - "L" -> 0x4C - "M" -> 0x4D - "N" -> 0x4E - "O" -> 0x4F - "P" -> 0x50 - "Q" -> 0x51 - "R" -> 0x52 - "S" -> 0x53 - "T" -> 0x54 - "U" -> 0x55 - "V" -> 0x56 - "W" -> 0x57 - "X" -> 0x58 - "Y" -> 0x59 - "Z" -> 0x5A - "[" -> 0x5B - "\\" -> 0x5C - "]" -> 0x5D - "^" -> 0x5E - "_" -> 0x5F - "`" -> 0x60 - "a" -> 0x61 - "b" -> 0x62 - "c" -> 0x63 - "d" -> 0x64 - "e" -> 0x65 - "f" -> 0x66 - "g" -> 0x67 - "h" -> 0x68 - "i" -> 0x69 - "j" -> 0x6A - "k" -> 0x6B - "l" -> 0x6C - "m" -> 0x6D - "n" -> 0x6E - "o" -> 0x6F - "p" -> 0x70 - "q" -> 0x71 - "r" -> 0x72 - "s" -> 0x73 - "t" -> 0x74 - "u" -> 0x75 - "v" -> 0x76 - "w" -> 0x77 - "x" -> 0x78 - "y" -> 0x79 - "z" -> 0x7A - "{" -> 0x7B - "|" -> 0x7C - "}" -> 0x7D - "~" -> 0x7E - _ -> 0x3F - } -}
M src/gbf/eval.gleam

@@ -1,9 +1,10 @@

+import char import gbf/lexer import gbf/parser.{type AST, type Block, type Command} import gbf/token import gbf/vm.{type VirtualMachine} import gleam/list -import gleam/option.{type Option, None, Some} +import gleam/option import gleam/result pub type Error {

@@ -119,113 +120,10 @@ let cell_value =

vm.get_cell(vm, vm.pointer) |> option.unwrap(0) - case to_char(cell_value) { - None -> InvalidChar(cell_value) |> Error - Some(char) -> { - let new_output = vm.output <> char - Ok(vm.VirtualMachine(..vm, output: new_output)) - } - } -} - -fn to_char(code: Int) -> Option(String) { - case code { - 0x0A -> Some("\n") - 0x20 -> Some(" ") - 0x21 -> Some("!") - 0x22 -> Some("\"") - 0x23 -> Some("#") - 0x24 -> Some("$") - 0x25 -> Some("%") - 0x26 -> Some("&") - 0x27 -> Some("'") - 0x28 -> Some("(") - 0x29 -> Some(")") - 0x2A -> Some("*") - 0x2B -> Some("+") - 0x2C -> Some(",") - 0x2D -> Some("-") - 0x2E -> Some(".") - 0x2F -> Some("/") - 0x30 -> Some("0") - 0x31 -> Some("1") - 0x32 -> Some("2") - 0x33 -> Some("3") - 0x34 -> Some("4") - 0x35 -> Some("5") - 0x36 -> Some("6") - 0x37 -> Some("7") - 0x38 -> Some("8") - 0x39 -> Some("9") - 0x3A -> Some(":") - 0x3B -> Some(";") - 0x3C -> Some("<") - 0x3D -> Some("=") - 0x3E -> Some(">") - 0x3F -> Some("?") - 0x40 -> Some("@") - 0x41 -> Some("A") - 0x42 -> Some("B") - 0x43 -> Some("C") - 0x44 -> Some("D") - 0x45 -> Some("E") - 0x46 -> Some("F") - 0x47 -> Some("G") - 0x48 -> Some("H") - 0x49 -> Some("I") - 0x4A -> Some("J") - 0x4B -> Some("K") - 0x4C -> Some("L") - 0x4D -> Some("M") - 0x4E -> Some("N") - 0x4F -> Some("O") - 0x50 -> Some("P") - 0x51 -> Some("Q") - 0x52 -> Some("R") - 0x53 -> Some("S") - 0x54 -> Some("T") - 0x55 -> Some("U") - 0x56 -> Some("V") - 0x57 -> Some("W") - 0x58 -> Some("X") - 0x59 -> Some("Y") - 0x5A -> Some("Z") - 0x5B -> Some("[") - 0x5C -> Some("\\") - 0x5D -> Some("]") - 0x5E -> Some("^") - 0x5F -> Some("_") - 0x60 -> Some("`") - 0x61 -> Some("a") - 0x62 -> Some("b") - 0x63 -> Some("c") - 0x64 -> Some("d") - 0x65 -> Some("e") - 0x66 -> Some("f") - 0x67 -> Some("g") - 0x68 -> Some("h") - 0x69 -> Some("i") - 0x6A -> Some("j") - 0x6B -> Some("k") - 0x6C -> Some("l") - 0x6D -> Some("m") - 0x6E -> Some("n") - 0x6F -> Some("o") - 0x70 -> Some("p") - 0x71 -> Some("q") - 0x72 -> Some("r") - 0x73 -> Some("s") - 0x74 -> Some("t") - 0x75 -> Some("u") - 0x76 -> Some("v") - 0x77 -> Some("w") - 0x78 -> Some("x") - 0x79 -> Some("y") - 0x7A -> Some("z") - 0x7B -> Some("{") - 0x7C -> Some("|") - 0x7D -> Some("}") - 0x7E -> Some("~") - _ -> None + case char.from_code(cell_value) { + "" -> Error(InvalidChar(cell_value)) + c -> + vm.VirtualMachine(..vm, output: vm.output <> c) + |> Ok } }