### Run All Examples Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/firstlang/README.md Iterates through all .fl files in the examples directory and executes them. ```bash for file in examples/*.fl; do echo "Running $file..." cargo run -- "$file" done ``` -------------------------------- ### Execute example programs Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/02_firstlang/repl.md Run specific example files or iterate through all available examples in the examples directory. ```bash cargo run -- examples/fibonacci.fl ``` ```bash cargo run -- examples/factorial.fl ``` ```bash cargo run -- examples/basics.fl ``` ```bash for file in examples/*.fl; do echo "Running $file..." cargo run -- "$file" done ``` -------------------------------- ### Run All Thirdlang Examples Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/04_thirdlang/running.md Commands to run individual Thirdlang example files and a loop to execute all .tl files in the examples directory. ```bash cd thirdlang # Point: distance calculation rustup run nightly cargo run --bin thirdlang -- examples/point.tl # Counter: stateful object rustup run nightly cargo run --bin thirdlang -- examples/counter.tl # Destructor: cleanup demo rustup run nightly cargo run --bin thirdlang -- examples/destructor.tl # Linked node: object references rustup run nightly cargo run --bin thirdlang -- examples/linked_node.tl ``` ```bash for file in examples/*.tl; do echo "=== $file ===" rustup run nightly cargo run --bin thirdlang -q -- "$file" done ``` -------------------------------- ### Run Firstlang Programs from Command Line Source: https://context7.com/ehsanmok/create-your-own-lang-with-rust/llms.txt Provides instructions for running Firstlang programs from the command line, including executing example files and starting the interactive REPL. ```bash # Run Firstlang programs from command line cd firstlang # Run the Fibonacci example cargo run -- examples/fibonacci.fl # Output: 55 # Run the Factorial example cargo run -- examples/factorial.fl # Output: 120 # Start the interactive REPL cargo run # >>> x = 10 # >>> def double(n) { return n * 2 } # >>> double(x) # 20 ``` -------------------------------- ### Batch Execution of Examples Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/03_secondlang/jit_fibonacci.md Shell loop to execute all .sl files in the examples directory. ```bash for file in examples/*.sl; do echo "Running $file..." rustup run nightly cargo run -- "$file" done ``` -------------------------------- ### Install GCC JIT Development Libraries Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/01_calculator/exercise.md Install the necessary `libgccjit` development libraries for your system to use the GCC JIT backend. This example shows commands for Ubuntu/Debian and macOS. ```bash # Ubuntu/Debian sudo apt install libgccjit-10-dev # macOS brew install gcc ``` -------------------------------- ### Run Basics Example Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/firstlang/README.md Executes the basics example program demonstrating variables, functions, conditionals, and loops. ```bash cargo run -- examples/basics.fl ``` -------------------------------- ### Install Prerequisites Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/04_thirdlang/intro.md Commands to set up the nightly Rust toolchain and verify LLVM installation. ```bash # Install nightly Rust rustup toolchain install nightly # Check LLVM version llvm-config --version ``` -------------------------------- ### Run Factorial Example Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/firstlang/README.md Executes the factorial example program. ```bash cargo run -- examples/factorial.fl ``` -------------------------------- ### Run VM Example Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/calculator/README.md Execute an example expression using the VM backend. Requires stable Rust and the 'vm' feature. ```bash cargo run --bin main --no-default-features --features vm examples/simple.calc ``` -------------------------------- ### Running LLVM Example with Nightly Rust Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/01_calculator/basic_llvm.md Execute the LLVM example using the nightly Rust toolchain. This command navigates to the example's sub-crate and runs the application. ```bash rustup run nightly cargo run ``` -------------------------------- ### Run Secondlang Examples Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/secondlang/README.md Commands to execute various example programs using the nightly Rust toolchain. ```bash rustup run nightly cargo run -- examples/fibonacci.sl ``` ```bash rustup run nightly cargo run -- examples/factorial.sl ``` ```bash rustup run nightly cargo run -- examples/inference.sl ``` ```bash rustup run nightly cargo run -- examples/basics.sl ``` ```bash for file in examples/*.sl; do echo "Running $file..." rustup run nightly cargo run -- "$file" done ``` -------------------------------- ### Fuzzing Setup: Cargo Fuzz Commands Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/testing.md Commands to install and initialize cargo-fuzz for fuzz testing. This includes installing the tool, initializing a fuzz target, and running the fuzz target. ```bash cargo install cargo-fuzz cargo fuzz init cargo fuzz run parse_fuzz ``` -------------------------------- ### Run Thirdlang Examples Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/thirdlang/README.md Execute example programs using the cargo run command. ```bash # Point distance calculation rustup run nightly cargo run -- examples/point.tl # Output: 25 # Counter with increment rustup run nightly cargo run -- examples/counter.tl # Output: 18 # Destructor demonstration rustup run nightly cargo run -- examples/destructor.tl # Output: 1 # Multiple nodes rustup run nightly cargo run -- examples/linked_node.tl # Output: 60 ``` -------------------------------- ### Run Thirdlang Point Example Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/04_thirdlang/running.md Command to compile and run the Point example file using the Thirdlang binary. ```bash cd thirdlang rustup run nightly cargo run --bin thirdlang -- examples/point.tl ``` -------------------------------- ### Run Thirdlang (Counter Example) Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/intro.md Execute the thirdlang project with the counter example, utilizing LLVM JIT compilation. ```bash # Thirdlang cd thirdlang rustup run nightly cargo run --bin thirdlang -- examples/counter.tl ``` -------------------------------- ### Run Thirdlang Examples and Tests Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/04_thirdlang/intro.md Commands to execute the language runtime on provided examples and run the test suite. ```bash cd thirdlang # Run Point example rustup run nightly cargo run --bin thirdlang -- examples/point.tl # Run Counter example rustup run nightly cargo run --bin thirdlang -- examples/counter.tl # Run all tests rustup run nightly cargo test ``` -------------------------------- ### Run Inference Example Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/03_secondlang/inference.md Command to execute the inference example using the Rust toolchain. ```bash rustup run nightly cargo run -- examples/inference.sl ``` -------------------------------- ### Install Secondlang Dependencies Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/secondlang/README.md Commands to install the required Rust nightly toolchain and LLVM 20. ```bash # Install nightly Rust rustup toolchain install nightly # Install LLVM 20 # macOS: brew install llvm # Debian/Ubuntu: wget https://apt.llvm.org/llvm.sh chmod +x llvm.sh sudo ./llvm.sh 20 ``` -------------------------------- ### Bytecode VM Opcodes Example Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/calculator/README.md Example bytecode sequence for the expression '1 + 2' in the stack-based VM. Demonstrates pushing constants and performing addition. ```text OpConstant(0) # Push 1 OpConstant(1) # Push 2 OpAdd # Pop 2, 1, push 3 ``` -------------------------------- ### Run Thirdlang Destructor Example Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/04_thirdlang/running.md Command to compile and run the Destructor example file using the Thirdlang binary. ```bash rustup run nightly cargo run --bin thirdlang -- examples/destructor.tl ``` -------------------------------- ### Install Nightly Rust Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/intro.md Install the nightly toolchain for Rust, required for projects using LLVM. ```bash # Install nightly Rust rustup toolchain install nightly ``` -------------------------------- ### Install LLVM (Debian/Ubuntu) Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/intro.md Instructions for installing LLVM on Debian/Ubuntu systems. Refer to the provided link for specific commands. ```bash # Install LLVM (Debian/Ubuntu) - see https://apt.llvm.org/ ``` -------------------------------- ### Interpreter Output Example Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/01_calculator/repl.md Example output showing AST structure and evaluation for a simple addition expression. ```text Calculator prompt. Expressions are line evaluated. >> 1 + 2 Compiling the source: 1 + 2 [BinaryExpr { op: Plus, lhs: Int(1), rhs: Int(2) }] 3 ``` -------------------------------- ### Function Definition Example Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/02_firstlang/syntax.md Demonstrates defining and calling functions. ```text def greet() { return 42 } def add(a, b) { return a + b } add(3, 4) # = 7 ``` -------------------------------- ### Example Calculator Session Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/calculator/README.md Demonstrates typical interactions within the calculator's REPL, showing input expressions and their evaluated output. ```text Calculator prompt. Expressions are line evaluated. >> 1 + 2 3 >> -2 + 5 3 >> (10 - 3) + 5 12 >> -(5 - 2) -3 >> CTRL-C ``` -------------------------------- ### Install Nightly Rust Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/README.md Installs the nightly Rust toolchain, which is required for the Calculator JIT, Secondlang, and Thirdlang. ```bash rustup toolchain install nightly ``` -------------------------------- ### Continuous Integration: Run Tests and Examples Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/testing.md GitHub Actions workflow configuration to automatically run all tests (`cargo test --all`) and execute example programs (`cargo run -- examples/fibonacci.sl`) on every commit. ```yaml # .github/workflows/ci.yml - name: Run tests run: cargo test --all - name: Run examples run: | cargo run -- examples/fibonacci.sl cargo run -- examples/factorial.sl ``` -------------------------------- ### Use Classes as Parameters Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/04_thirdlang/intro.md Example showing how classes can be passed as arguments to methods. ```text class Point { x: int y: int def __init__(self, x: int, y: int) { self.x = x self.y = y } def distance_squared(self, other: Point) -> int { dx = other.x - self.x dy = other.y - self.y return dx * dx + dy * dy } } p1 = new Point(0, 0) p2 = new Point(3, 4) p1.distance_squared(p2) # returns 25 ``` -------------------------------- ### AOT Compilation Example Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/whats_next.md Command-line example for Ahead-of-Time compilation using a custom compiler to produce a standalone executable. ```bash ./mycompiler program.tl -o program ./program # Run without compiler ``` -------------------------------- ### Run Firstlang (REPL) Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/intro.md Start the Read-Eval-Print Loop for the firstlang project. ```bash cargo run # REPL ``` -------------------------------- ### Install Rust using rustup Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/README.md Installs the Rust toolchain, including cargo, rustc, and rustup. This is required for the Calculator interpreter/VM and Firstlang. ```bash curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh ``` -------------------------------- ### VM Bytecode Generation Example Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/01_calculator/repl.md Shows the step-by-step compilation of a simple expression to bytecode, including constant pushing and arithmetic operations. ```text Calculator prompt. Expressions are line evaluated. >> 1 + 2 Compiling the source: 1 + 2 [BinaryExpr { op: Plus, lhs: Int(1), rhs: Int(2) }] compiling node BinaryExpr { op: Plus, lhs: Int(1), rhs: Int(2) } added instructions [1, 0, 0] from opcode OpConstant(0) added instructions [1, 0, 0, 1, 0, 1] from opcode OpConstant(1) added instructions [1, 0, 0, 1, 0, 1, 3] from opcode OpAdd added instructions [1, 0, 0, 1, 0, 1, 3, 2] from opcode OpPop byte code: Bytecode { instructions: [1, 0, 0, 1, 0, 1, 3, 2], constants: [Int(1), Int(2)] } 3 ``` -------------------------------- ### Simple Arithmetic Examples Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/02_firstlang/syntax.md Demonstrates basic arithmetic operations and operator precedence. ```text 1 + 2 * 3 # = 7 (multiplication first) (1 + 2) * 3 # = 9 (parentheses override) -5 + 3 # = -2 (unary minus) ``` -------------------------------- ### Run Thirdlang via CLI Source: https://context7.com/ehsanmok/create-your-own-lang-with-rust/llms.txt Commands for executing Thirdlang example files. ```bash # Run Thirdlang from command line (requires nightly Rust + LLVM) cd thirdlang # Run the Point example rustup run nightly cargo run -- examples/point.tl # Output: 25 # Run the Counter example rustup run nightly cargo run -- examples/counter.tl # Output: 18 ``` -------------------------------- ### Implement a counter class with lifecycle methods Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/transition_3_to_4.md A comprehensive example showing class state, initialization, method logic, and destructor cleanup. ```text class Counter { value: int def __init__(self, start: int) { self.value = start } def increment(self) -> int { self.value = self.value + 1 return self.value } def __del__(self) { # Cleanup when deleted } } c = new Counter(0) c.increment() # 1 c.increment() # 2 delete c ``` -------------------------------- ### Recursion Example Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/02_firstlang/syntax.md Demonstrates a recursive factorial function. ```text def factorial(n) { if (n <= 1) { return 1 } else { return n * factorial(n - 1) } } factorial(5) # = 120 ``` -------------------------------- ### Run Calculator (Interpreter Mode) Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/intro.md Execute the calculator project in interpreter mode with a simple example file. ```bash # Calculator - interpreter mode cd calculator cargo run --bin main examples/simple.calc ``` -------------------------------- ### While Loop Example Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/02_firstlang/syntax.md Demonstrates using a while loop to increment a variable. ```text x = 0 while (x < 5) { x = x + 1 } x # = 5 ``` -------------------------------- ### Variable Assignment Example Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/02_firstlang/syntax.md Demonstrates assigning values to variables. ```text x = 10 y = x + 5 # = 15 ``` -------------------------------- ### Add Array Type Example Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/03_secondlang/exercises.md Demonstrates the syntax for defining a function that accepts a fixed-size array. ```text def sum(arr: [int; 3]) -> int { return arr[0] + arr[1] + arr[2] } ``` -------------------------------- ### Start Firstlang REPL Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/README.md Launch the Read-Eval-Print Loop (REPL) for the Firstlang interpreter. Ensure you are in the 'firstlang' directory. ```bash cd firstlang cargo run ``` -------------------------------- ### Type Inference Example Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/03_secondlang/jit_fibonacci.md Demonstration of automatic type inference in Secondlang. ```text def complex_calc(x: int) -> int { a = x + 1 # int (inferred from x + literal) b = a * a # int (inferred from int * int) c = b - x # int (inferred from int - int) flag = c > 100 # bool (inferred from comparison) if (flag) { return c } else { return b } } complex_calc(10) ``` -------------------------------- ### Debugger Usage Example Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/whats_next.md Demonstrates basic debugger commands for setting breakpoints, running code, inspecting variables, and stepping through execution. ```plaintext (debug) break main.tl:10 (debug) run Breakpoint hit at main.tl:10 (debug) print x x = 42 (debug) step ``` -------------------------------- ### Initialize and Run VM Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/01_calculator/vm.md Demonstrates how to compile source code into bytecode and execute it using the VM instance. ```rust let byte_code = Interpreter::from_source(source); println!("byte code: {:?}", byte_code); let mut vm = VM::new(byte_code); vm.run(); println!("{}", vm.pop_last()); ``` -------------------------------- ### While Loop Syntax Example Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/02_firstlang/control_flow.md Basic structure of a while loop incrementing a variable. ```text x = 0 while (x < 5) { x = x + 1 } x # 5 ``` -------------------------------- ### Variables and Arithmetic Example Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/02_firstlang/variables.md A simple script demonstrating variable assignment and basic arithmetic operations. ```text # Variables and arithmetic a = 5 b = 3 sum = a + b # 8 diff = a - b # 2 prod = a * b # 15 ``` -------------------------------- ### Build and serve documentation Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/README.md Commands to preview or build the static site documentation using mdbook. ```bash cd book mdbook serve # Local preview at http://localhost:3000 mdbook build # Build static site ``` -------------------------------- ### Add Modulo Operator Example Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/03_secondlang/exercises.md Demonstrates the use of the modulo operator within a function. ```text def is_even(n: int) -> bool { return n % 2 == 0 } ``` -------------------------------- ### REPL Usage Examples Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/firstlang/README.md Demonstrates variable assignment, function definition, and recursive function calls within the REPL. ```python >>> x = 10 >>> x + 5 15 >>> def double(n) { return n * 2 } >>> double(21) 42 >>> def fib(n) { ... if (n <= 1) { ... return n ... } else { ... return fib(n - 1) + fib(n - 2) ... } ... } >>> fib(10) 55 ``` -------------------------------- ### Full Fibonacci Example Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/secondlang/README.md A complete implementation of the Fibonacci sequence using type annotations. ```python def fibonacci(n: int) -> int { if (n <= 1) { return n } else { return fibonacci(n - 1) + fibonacci(n - 2) } } result = fibonacci(10) ``` -------------------------------- ### Heap Allocation in Thirdlang Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/04_thirdlang/memory.md Example of manual heap allocation and deallocation using the 'new' and 'delete' operators. ```text p = new Point(1, 2) # Allocate on heap # ... use p ... delete p # Must free manually! ``` -------------------------------- ### Add Short-Circuit Operators Example Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/03_secondlang/exercises.md Demonstrates short-circuit evaluation using the logical AND operator. ```text def safe_div(a: int, b: int) -> int { if (b != 0 && a / b > 0) { return a / b } return 0 } ``` -------------------------------- ### Add Type Aliases Example Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/03_secondlang/exercises.md Demonstrates defining and using a type alias for an existing integer type. ```text type Distance = int def manhattan(x: Distance, y: Distance) -> Distance { return x + y } ``` -------------------------------- ### Add Float Type Example Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/03_secondlang/exercises.md Demonstrates the syntax for a function using 64-bit floating point types. ```text def area(radius: float) -> float { return 3.14159 * radius * radius } area(2.0) ``` -------------------------------- ### Clone Repository and Navigate Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/intro.md Clone the project repository and navigate to the root directory to begin. ```bash git clone https://github.com/ehsanmok/create-your-own-lang-with-rust cd create-your-own-lang-with-rust ``` -------------------------------- ### Local and Global Scoping Example Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/02_firstlang/variables.md Illustrates how variables are scoped within functions versus global scope. ```text x = 10 # global - accessible everywhere def foo() { y = 20 # local to foo - only accessible inside foo return x + y # can access global x } foo() # = 30 # y # ERROR: y is not defined here ``` -------------------------------- ### Function AST Node Example Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/02_firstlang/functions.md An example of how the 'add' function is represented as a Function statement in the AST. ```rust Stmt::Function { name: "add".to_string(), params: vec!["a".to_string(), "b".to_string()], body: vec![ Stmt::Return(Expr::Binary { op: BinaryOp::Add, lhs: Box::new(Expr::Var("a".to_string())), rhs: Box::new(Expr::Var("b".to_string())), }) ], } ``` -------------------------------- ### Code Formatting Command Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/whats_next.md Example of using a command-line tool to automatically format code, similar to tools like rustfmt or prettier. ```bash ./myfmt program.tl # Like rustfmt or prettier ``` -------------------------------- ### Serve and Build mdBook Locally Source: https://context7.com/ehsanmok/create-your-own-lang-with-rust/llms.txt Commands to serve the documentation book locally for preview or to build a static site. ```bash cd book mdbook serve # http://localhost:3000 ``` ```bash mdbook build # Static site in book/book/ ``` -------------------------------- ### Complete JIT Pipeline Setup Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/01_calculator/ast_traversal.md This Rust code outlines the complete Just-In-Time (JIT) compilation pipeline, including parsing the source, setting up LLVM, compiling the AST using a recursive builder, and returning the result. ```rust pub fn jit_ast(source: &str) -> Result { let ast = parser::parse(source).map_err(|e| e.to_string())?; let context = Context::new(); let builder = Builder::new(&context); let module = Module::new("jit", &context, None); let fn_type = context.i64_type().fn_type(&[], false); let function = module.add_function(fn_type, "__jit"); let basic_block = context.append_basic_block(function, "entry"); builder.position_at_end(basic_block); let result_val = Self::jit_recursive_builder(&context, &builder, &module, &ast)?; builder.build_return(Some(&result_val)); let ee = ExecutionEngine::new(&module)?; // Use new for LLVM 17+ let mut execution_engine = ee; let mut args: Vec = Vec::new(); let ret_val = execution_engine.run_function(function, &args); Ok(ret_val.to_int(false) as i64) } ``` -------------------------------- ### Define Function Type Inference Example Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/03_secondlang/inference.md An example of how function parameters and local variables are inferred within a function body. ```rust def compute(a: int, b: int) -> int { temp = a + b # What type is temp? doubled = temp * 2 # What type is doubled? return doubled + 1 } ``` -------------------------------- ### Perform Object Creation and Method Calls Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/04_thirdlang/intro.md Demonstrates instantiating a class, calling methods, and freeing memory. ```text c = new Counter() c.increment() # returns 1 c.increment() # returns 2 c.get() # returns 2 delete c # free memory ``` -------------------------------- ### Build and run the calculator Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/01_calculator/calc_intro.md Combines the build and execution steps into a single command. ```bash cargo run --bin main examples/simple.calc ``` -------------------------------- ### Install LLVM on Debian/Ubuntu Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/README.md Installs LLVM on Debian/Ubuntu systems. This is a dependency for nightly Rust features used in JIT compilation and advanced languages. ```bash # Debian/Ubuntu: ``` -------------------------------- ### Run Calculator from Command Line Source: https://context7.com/ehsanmok/create-your-own-lang-with-rust/llms.txt Shows how to execute Calculator language programs from the command line using different backends (interpreter, VM, LLVM JIT) and interact with the REPL. ```bash # Run Calculator from command line # Using the interpreter (stable Rust) cd calculator cargo run --bin main examples/simple.calc # Output: 2 # Using the VM backend cargo run --bin main --no-default-features --features vm examples/simple.calc # Using LLVM JIT (requires nightly Rust + LLVM) rustup run nightly cargo run --bin main --no-default-features --features jit examples/simple.calc # Interactive REPL cargo run --bin repl --features interpreter # >> 1 + 2 # 3 # >> (10 - 3) + 5 # 12 ``` -------------------------------- ### Define and instantiate a class Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/transition_3_to_4.md Shows the syntax for class definition, constructor implementation, method definition, and manual heap memory management. ```text class Point { x: int # Fields - data stored in the object y: int def __init__(self, x: int, y: int) { # Constructor self.x = x self.y = y } def move(self, dx: int, dy: int) { # Method self.x = self.x + dx self.y = self.y + dy } } p = new Point(10, 20) # Heap allocation p.move(5, 5) # Method call delete p # Manual deallocation ``` -------------------------------- ### Install LLVM on macOS Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/README.md Installs LLVM using Homebrew on macOS. This is a dependency for nightly Rust features used in JIT compilation and advanced languages. ```bash brew install llvm ``` -------------------------------- ### Thirdlang Integration Test Example Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/04_thirdlang/running.md An example of an integration test written in Rust for the Thirdlang compiler, demonstrating how to test features like simple class syntax and method calls. ```rust,ignore {{#include ../../../thirdlang/tests/integration_tests.rs:test_simple_class}} ``` -------------------------------- ### LLVM Field Access using getelementptr Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/04_thirdlang/codegen_classes.md Demonstrates the LLVM IR for accessing the 'x' field of a 'Point' object. It uses `getelementptr` with indices 0 (struct) and 0 (field) to get the address, then `load` to get the value. ```llvm %x_ptr = getelementptr %Point, ptr %self, i32 0, i32 0 %x = load i64, ptr %x_ptr ret i64 %x ``` -------------------------------- ### Run Calculator (VM Mode) Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/intro.md Execute the calculator project using the virtual machine backend. ```bash # Calculator - VM mode cargo run --bin main --features vm examples/simple.calc ``` -------------------------------- ### Define Constructor and Destructor Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/04_thirdlang/methods.md Shows the syntax for class initialization and cleanup methods. ```text def __init__(self, x: int) { self.x = x } ``` ```text def __del__(self) { # Cleanup code (if any) } ``` -------------------------------- ### Algebraic Simplification Optimization Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/secondlang/README.md Examples of algebraic simplifications performed by the compiler. ```python # Before: x + 0 # After: x # Before: x * 1 # After: x # Before: x - x # After: 0 ``` -------------------------------- ### Type Inference Example Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/secondlang/README.md Demonstration of local type inference with unification. ```python # All types are inferred automatically: def is_even(n: int) { return n % 2 == 0 # return type: bool (inferred) } x = 10 # int (inferred) result = is_even(x) # bool (inferred) ``` -------------------------------- ### Build the calculator project Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/01_calculator/calc_intro.md Compiles the calculator binary using Cargo. ```bash cargo build --bin main ``` -------------------------------- ### Define a Counter Class Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/04_thirdlang/intro.md Example of a class definition with a constructor and methods. ```text class Counter { count: int def __init__(self) { self.count = 0 } def increment(self) -> int { self.count = self.count + 1 return self.count } def get(self) -> int { return self.count } } ``` -------------------------------- ### Iterative Factorial Function Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/02_firstlang/control_flow.md Example of using a while loop to compute a factorial. ```text def factorial(n) { result = 1 while (n > 1) { result = result * n n = n - 1 } return result } factorial(5) # 120 ``` -------------------------------- ### If/Else Syntax Example Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/02_firstlang/control_flow.md Basic structure of an if/else conditional expression. ```text if (x > 0) { return 1 } else { return 0 } ``` -------------------------------- ### JIT Compilation LLVM IR Example Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/01_calculator/repl.md Displays the generated LLVM Intermediate Representation (IR) for a simple expression, demonstrating constant folding. ```text Calculator prompt. Expressions are line evaluated. >> 1 + 2 Compiling the source: 1 + 2 [BinaryExpr { op: Plus, lhs: Int(1), rhs: Int(2) }] Generated LLVM IR: define i32 @jit() { entry: ret i32 3 } 3 ``` -------------------------------- ### Run Calculator VM Backend Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/README.md Execute a simple calculator program using the Virtual Machine (VM) backend. Ensure you are in the 'calculator' directory. ```bash cd calculator cargo run --bin main --no-default-features --features vm examples/simple.calc ``` -------------------------------- ### Type Inference Syntax Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/secondlang/README.md Examples demonstrating how the compiler infers types automatically. ```python # Type is inferred from the right-hand side x = 42 # inferred as int y = x + 10 # inferred as int flag = x > 0 # inferred as bool # Function with inferred return type def double(n: int) { return n * 2 # return type inferred as int } ``` -------------------------------- ### Type Annotations Syntax Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/secondlang/README.md Examples of explicit type annotations for variables and functions. ```python # Variable with type x: int = 42 # Function with parameter and return types def add(a: int, b: int) -> int { return a + b } ``` -------------------------------- ### Example Calculation Checkpoint Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/01_calculator/exercise.md Verify successful implementation of Exercise 1 by running a calculation involving floating-point numbers and operators. This checkpoint confirms basic functionality. ```plaintext 3.14 * 2.0 + 1.5 / 3.0 ``` -------------------------------- ### Thirdlang Project Directory Structure Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/04_thirdlang/intro.md Layout of the Thirdlang source files and examples. ```text thirdlang/ ├── Cargo.toml ├── src/ │ ├── lib.rs # Library exports │ ├── main.rs # CLI entry point │ ├── grammar.pest # PEG grammar with classes │ ├── parser.rs # Parser → Typed AST │ ├── ast.rs # Typed AST with class nodes │ ├── types.rs # Type system with ClassInfo │ ├── typeck.rs # Type checker for classes │ ├── visitor.rs # AST visitors │ └── codegen.rs # LLVM code generation with malloc/free ├── examples/ │ ├── point.tl # Point class with methods │ ├── counter.tl # Counter with state │ ├── destructor.tl # Destructor example │ └── linked_node.tl # Linked data structure └── tests/ └── integration_tests.rs ``` -------------------------------- ### Conditional Statement Example Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/02_firstlang/syntax.md Demonstrates using if-else blocks to return values. ```text def max(a, b) { if (a > b) { return a } else { return b } } max(10, 20) # = 20 ``` -------------------------------- ### Manage Object Lifecycle Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/thirdlang/README.md Demonstration of object creation, field access, method invocation, and manual deletion. ```python # Create object (calls __init__) p = new Point(3, 4) # Use object x = p.x # Field access d = p.distance_squared(other) # Method call # Delete object (calls __del__, then frees memory) delete p ``` -------------------------------- ### Constant Folding Optimization Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/secondlang/README.md Example of constant folding optimization performed at the AST level. ```python # Before: 1 + 2 * 3 # After: 7 ``` -------------------------------- ### Project Directory Structures Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/03_secondlang/intro.md Comparison of the file structures for Secondlang and Firstlang projects. ```text secondlang/ ├── Cargo.toml ├── src/ │ ├── lib.rs # Library exports │ ├── main.rs # CLI entry point │ ├── grammar.pest # PEG grammar with types │ ├── parser.rs # Parser → Typed AST │ ├── ast.rs # Typed AST definitions │ ├── types.rs # Type system │ ├── typeck.rs # Type checker and inference │ ├── visitor.rs # AST visitors and optimizations │ └── codegen.rs # LLVM code generation ├── examples/ │ ├── fibonacci.sl │ └── factorial.sl └── tests/ └── integration_tests.rs ``` ```text firstlang/ ├── src/ │ ├── grammar.pest # Same structure, fewer rules │ ├── parser.rs # Simpler: no type handling │ ├── ast.rs # Simpler: no TypedExpr │ └── interpreter.rs # Tree-walking, no codegen ``` -------------------------------- ### Project Build and Test Commands Source: https://context7.com/ehsanmok/create-your-own-lang-with-rust/llms.txt Commands for building the workspace and running tests across different language crates. ```bash # Project structure create-your-own-lang-with-rust/ ├── calculator/ # Simple arithmetic (interpreter, VM, JIT backends) ├── firstlang/ # Interpreted language with recursion ├── secondlang/ # Compiled language with type inference ├── thirdlang/ # Object-oriented with classes and methods ├── book/ # mdbook source for createlang.rs ├── Cargo.toml # Workspace configuration └── pixi.toml # Pixi environment manager config # Build all projects (stable Rust only) cargo build -p calculator -p firstlang # Build with nightly for LLVM features rustup run nightly cargo build -p secondlang -p thirdlang # Run all tests cargo test -p calculator -p firstlang rustup run nightly cargo test -p secondlang -p thirdlang ``` -------------------------------- ### Use After Free Bug Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/04_thirdlang/memory.md Example of undefined behavior resulting from accessing memory after it has been freed. ```text p = new Point(1, 2) delete p p.x # BUG! Memory already freed ``` -------------------------------- ### Initialize LLVM Context, Module, and Builder in Rust Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/01_calculator/basic_llvm.md Set up the foundational LLVM structures in Rust using the inkwell library. This includes creating a context, a module, defining integer types, and initializing an instruction builder. ```rust use inkwell::context::Context; let context = Context::create(); let module = context.create_module("addition"); let i32_type = context.i32_type(); let builder = context.create_builder(); ``` -------------------------------- ### Memory Leak Example Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/04_thirdlang/memory.md Demonstration of a memory leak caused by failing to call delete. ```text def leak() { p = new Point(1, 2) # Oops, forgot delete p! } # Memory is lost forever ``` -------------------------------- ### Point Class Size Calculation Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/04_thirdlang/memory.md Example of calculating memory size for a class structure. ```text class Point { x: int # 8 bytes (i64) y: int # 8 bytes (i64) } # Total: 16 bytes ``` -------------------------------- ### Compare Project Structure with Secondlang Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/04_thirdlang/intro.md Simplified structure comparison highlighting the absence of class-specific modules in Secondlang. ```text secondlang/ ├── src/ │ ├── types.rs # Simpler: no ClassInfo │ ├── typeck.rs # Simpler: no class type checking │ └── codegen.rs # Simpler: no malloc/free ``` -------------------------------- ### Example Class Definition Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/04_thirdlang/classes_syntax.md A sample class definition used to trace the parsing process. ```text class Point { x: int y: int def __init__(self, x: int, y: int) { self.x = x self.y = y } def get_x(self) -> int { return self.x } } ``` -------------------------------- ### Recursive Factorial Function Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/02_firstlang/control_flow.md Example of a recursive implementation of factorial for comparison with the iterative approach. ```text def factorial_recursive(n) { if (n <= 1) { return 1 } else { return n * factorial_recursive(n - 1) } } ``` -------------------------------- ### Run Calculator JIT Backend Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/README.md Execute a simple calculator program using the Just-In-Time (JIT) compilation backend. Requires nightly Rust and LLVM installed. Ensure you are in the 'calculator' directory. ```bash rustup run nightly cargo run --bin main --no-default-features --features jit examples/simple.calc ``` -------------------------------- ### Double Free Bug Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/04_thirdlang/memory.md Example of undefined behavior resulting from calling delete on the same pointer twice. ```text p = new Point(1, 2) delete p delete p # BUG! Already freed ``` -------------------------------- ### Perform Explicit Memory Deletion Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/04_thirdlang/intro.md Example of manual memory management using the delete keyword. ```text p = new Point(1, 2) ``` -------------------------------- ### Define Python-like Constructor and Destructor Source: https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/book/src/04_thirdlang/intro.md Syntax for class initialization and cleanup methods. ```text def __init__(self, x: int) { self.x = x } ``` ```text def __del__(self) { # cleanup code } ```