///
This page provides a collection of practical examples demonstrating how to use the `ewor_brainfuck` project, showcasing both the Brainfuck interpreter with syscall extensions ([BFA Mode](/wiki/lantos1
47 views
~47 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.
This page provides a collection of practical examples demonstrating how to use the ewor_brainfuck project, showcasing both the Brainfuck interpreter with syscall extensions (BFA Mode) and the Brainfuck-like Language (BFL) compiler. These examples range from simple "Hello, World!" programs to a fully functional network server, illustrating the power and capabilities of the custom Brainfuck environment.
To learn how to run these examples, refer to the Getting Started: Running Brainfuck and BFL page.
examples/hello_bfl.rs)This example demonstrates how to generate and execute a "Hello, World!" program using the Brainfuck-like Language (BFL) and its compiler. It leverages the SYS_WRITE syscall in BFA mode to print the message to standard output. The BFL code assigns individual characters of the string to distinct memory locations and then performs a single SYS_WRITE syscall, passing the starting address and length of the message.
examples/bfa_hello_world_gen.rs)This example provides an alternative "Hello World!" implementation. Instead of creating a contiguous string, it generates a BFL program that assigns each character of "Hello World!\n" to a separate variable and then prints each character individually using distinct SYS_WRITE syscalls. The Rust code then compiles this BFL program to Brainfuck and executes it in BFA Mode to verify the output. This highlights how the BFL Compiler can handle numerous assignments and syscalls.
examples/syscall_print.rs)This example demonstrates a fundamental SYS_WRITE syscall through BFL. It defines a simple BFL program that assigns a string literal to a variable and then uses the Syscall node to write that string to standard output. This is a clearer and more direct way to interact with output compared to the character-by-character approach. The output also includes debugging information about the generated Brainfuck code and memory state.
examples/simple_socket_test.rs)This example illustrates the fundamental steps of network programming in Brainfuck using BFL and BFA Mode. It demonstrates how to create a basic network socket using SYS_SOCKET and then immediately close it using SYS_CLOSE. The file descriptor returned by SYS_SOCKET is captured into a BFL variable, showcasing variable assignment for syscall results. A confirmation message is printed to stdout using SYS_WRITE.
examples/ping_pong_server.rs)This example is a core component for the EWOR builder case study, demonstrating the advanced networking capabilities enabled by BFL and BFA Mode. It implements a basic TCP echo server that listens on port 8080, accepts a single client connection, and then continuously echoes back any data received from that client until the connection is closed.
The server's lifecycle is orchestrated entirely through BFL nodes, which the BFL Compiler translates into Brainfuck code suitable for BFA Mode.
Key operations demonstrated:
SYS_SOCKET to create a new TCP socket.sockaddr_in structure (using BFLNode::Bytes) and uses SYS_BIND to assign the socket to a local address (127.0.0.1) and port (8080).SYS_LISTEN.SYS_ACCEPT, obtaining a new file descriptor for the client.BFLNode::While loop to handle data from the connected client.
SYS_READ is used to read data into a pre-allocated buffer (BFLNode::Bytes).BFLNode::If checks if data was successfully read (bytes_read > 0).SYS_WRITE.SYS_CLOSE calls for both the client and server sockets (the server close is practically unreachable in an infinite loop).This example highlights the comprehensive integration of system calls into Brainfuck, enabling complex network applications to be conceptualized and compiled from a more manageable high-level language.
examples/efficiency_test.rs)This example is designed to evaluate the length and efficiency of the Brainfuck code generated by the BFL Compiler for various common BFL constructs. It compiles simple BFL programs involving assignments (numbers, strings) and If statements, then prints the length of the resulting Brainfuck code. This helps in assessing the compiler's performance and the effectiveness of its optimization passes.
examples/test_codegen_debug.rs)This example focuses on debugging and analyzing the Brainfuck code generated by the BFL Compiler for different BFL statements and control flow constructs. It compiles various simple programs (number assignment, string assignment, loops with numbers, loops with strings) and prints both the raw and "optimized" Brainfuck output, along with their respective lengths. This is invaluable for understanding how BFL translates to Brainfuck and for assessing the effectiveness of the compiler's peephole optimization pass.
examples/test_loop_debug.rs)This example specifically focuses on testing the compilation of a BFL While loop. It defines a simple program that initializes a variable i to 3 and then decrements it within a loop until i becomes 0. After compilation and execution in BFA Mode, the program prints the generated Brainfuck code and the final value of i in memory, allowing for verification of correct loop behavior and variable updates.