///
The `ewor_brainfuck` project includes a higher-level, Brainfuck-like Language (BFL) designed to simplify the development of complex Brainfuck programs, particularly those leveraging [BFA Mode](/wiki/l
52 views
~52 views from guests
Guest views are estimated from total page views. These include anonymous visitors and users who weren't logged in when they viewed the page.
The ewor_brainfuck project includes a higher-level, Brainfuck-like Language (BFL) designed to simplify the development of complex Brainfuck programs, particularly those leveraging BFA Mode for system calls. BFL acts as an abstract syntax tree (AST), allowing developers to write programs using more conventional programming constructs like variables, assignments, arithmetic operations, conditional statements, and loops, which are then compiled down to pure Brainfuck.
The structure of BFL is defined by the BFLNode enum in src/bfl.rs. Each variant represents a distinct element or operation within the BFL language. The BFL Compiler Architecture and Internals page provides details on how the BFLCompiler processes these nodes to generate Brainfuck code.
Here's a breakdown of each BFLNode variant, its purpose, and how it conceptually translates into Brainfuck operations.
Assign(String, Box<BFLNode>)variable_name = expressionvariable_name.expression is evaluated, and its result is placed into the allocated cell for the variable_name. This typically involves clearing the target cell ([-]) and then using + operations to set its value, or using copy/move patterns if the expression is a variable.This would assign the value
42 to a variable named x.Variable(String)variable_nameAdd or Sub will typically copy the variable's value to a scratch cell before performing arithmetic, to preserve the original variable's value.This would represent the value stored at the memory location associated with "my_var".
String(String)"Hello, World!"+ operations.Assign statement) to store the starting memory address (a "pointer") of this string data.This would store the bytes for "Hello, BFL!" in memory and return the starting address.
Number(i32)123[-]) and then incremented (+) n times to set its value.This represents the numeric value 10.
Bytes(Vec<u8>)String but takes a Vec<u8>, useful for binary data like network addresses.[0x02, 0x00, ...]String, a pointer to the start of this byte data is stored in a designated variable cell.This would store the raw byte sequence in memory and return its starting address.
Add(Box<BFLNode>, Box<BFLNode>)expression_a + expression_blhs) expression is evaluated into the target cell (e.g., the cell assigned to a variable).rhs) expression is evaluated into a temporary "scratch" cell (e.g., SCRATCH_1 in src/bfl.rs).[ ]) is used to destructively move the value from the scratch cell to the target cell, effectively adding them: [<+>-] (assuming scratch is to the right of target, this pattern moves value from current to left, then decrements current).This would add 1 to the value of the variable
count.Sub(Box<BFLNode>, Box<BFLNode>)expression_a - expression_blhs) expression is evaluated into the target cell.rhs) expression is evaluated into a temporary "scratch" cell.[ ]) is used to destructively move the value from the scratch cell, decrementing the target cell for each unit moved: [<-+>] (assuming scratch is to the right of target). The operation implicitly clamps at 0 for unsigned 8-bit cells.This would subtract 1 from the value of the variable
counter.If(Box<BFLNode>, Vec<BFLNode>)if (condition) { statements }condition expression is evaluated into a temporary "condition" cell (e.g., SCRATCH_2 in src/bfl.rs).[ ... ] is wrapped around the body statements. This loop will execute only if the condition cell is non-zero.body statements, the condition cell is cleared ([-]). This ensures the if block executes only once, as Brainfuck loops repeat until the current cell is zero.If
flag is non-zero, result will be set to 1.While(Box<BFLNode>, Vec<BFLNode>)while (condition) { statements }condition expression is initially evaluated into a temporary "condition" cell.[ ... ] is wrapped around the body statements.body statements, the condition expression is re-evaluated and its value placed back into the condition cell. This allows the loop to check the updated condition before the next iteration.This would decrement
i until it reaches zero.Syscall(Box<BFLNode>, Vec<BFLNode>)syscall(syscall_number, arg1, arg2, ...)syscall_number expression is evaluated and its result placed into Brainfuck cell 7.args is evaluated and its result placed into Brainfuck cells 1 through 6, respectively.. (dot) instruction is appended to the Brainfuck output. In BFA Mode, this . instruction triggers the system call defined by cells 1-7._syscall_result variable.This would write 13 bytes from the
message variable to standard output. For a complete list of supported syscalls and their conventions, refer to the Brainfuck with Syscall Extensions (BFA Mode) page.Block(Vec<BFLNode>)If and While bodies or as the top-level program.{ statement1; statement2; ... }This represents a sequence of operations.
By providing these structured constructs, BFL significantly simplifies the process of writing complex Brainfuck programs. You can explore Practical Examples like examples/hello_bfl.rs and examples/ping_pong_server.rs to see BFL in action.