BasicRS is a BASIC interpreter written in Rust that supports a subset of the BASIC programming language. The project includes both an interpreter and a compiler that generates LLVM IR code.
The project is organized into several key modules:
Defines fundamental types used throughout the interpreter
Tokenenum: Represents lexical tokens (keywords, operators, literals, identifiers)Statementenum: Represents parsed BASIC statements (LET, PRINT, IF, etc.)Expressionstruct: Represents expressions with various types (numbers, strings, variables, arrays, operations)Programstruct: Contains the parsed program with line numbers and statementsSymbolValueenum: Represents runtime values (numbers, strings, arrays)BasicErrorenum: Error types (Syntax, Runtime, Internal, Type)RunStatusenum: Program execution status
Lexer struct: Tokenizes BASIC source code
- Processes line numbers, keywords, operators, identifiers, and literals
- Handles BASIC's space-free syntax (e.g.,
LETX=5) - Supports both explicit and implicit LET statements
Parser struct: Converts tokens into an Abstract Syntax Tree (AST)
- Implements recursive descent parsing
- Handles operator precedence and associativity
- Parses all BASIC statements and expressions
Interpreter struct: Executes parsed BASIC programs
- Implements control flow (GOTO, GOSUB, FOR/NEXT, IF/THEN)
- Manages symbol tables and variable scope
- Handles arrays, functions, and built-in functions
- Supports debugging features (breakpoints, tracing, coverage)
SymbolTable struct: Manages variables, functions, and arrays
- Handles variable scoping and lifetime
- Supports both numeric and string variables
- Implements arithmetic, comparison, and logical operators
- Handles type coercion and error checking
Registry of built-in functions (SIN, COS, RND, etc.)
- Function registration and lookup system
Registry of BASIC keywords
- Keyword recognition and classification
Defines BASIC dialect-specific features
- Handles case sensitivity and input formatting
- Code coverage tracking and reporting
- HTML coverage report generation
- Coverage data serialization
LLVMCodeGeneratorstruct: Generates LLVM IR from BASIC programs- Converts BASIC statements to LLVM instructions
- Handles variable allocation and memory management
- Supports debugging and tracing in generated code
LLVMIRBuilderstruct: Low-level LLVM IR generation- Manages LLVM module, function, and basic block creation
- Handles LLVM instruction generation
The project provides several executables:
- Main BASIC interpreter executable
- Command-line interface for running BASIC programs
- Supports coverage tracking with
--coverage-fileand--reset-coverageoptions - Returns appropriate exit codes based on program completion status
- Interactive BASIC development environment
- Commands: load, run, step, continue, break, symbols, coverage
- Debugging features: breakpoints, variable inspection, stack inspection
- Program formatting and line renumbering
- Compiles BASIC programs to LLVM IR
- Generates optimized native code
- Supports debug and trace modes
- Coverage analysis and reporting tool
- Generates HTML coverage reports
- Analyzes coverage data from multiple runs
- Located in each module with
#[cfg(test)]sections - Test individual components (lexer, parser, interpreter)
- Use
cargo test --libto run
- Located in
test_suite/directory - Contains
.basfiles with BASIC programs - Automatically generated test functions via
build.rs - Use
cargo test --test run_teststo run
- Located in
tests/run_tests.rs - Tests complete program execution
- Validates exit codes and program behavior
./run_all_tests.shcargo test --libcargo test --test run_testscargo test test_nameBASIC test files can include expected exit codes:
10 @EXPECT_EXIT_CODE=0
20 PRINT "Hello, World!"
30 END- anyhow, thiserror: Error handling
- regex, lazy_static: Text processing
- clap: Command-line interface
- pretty_assertions: Enhanced test assertions
- tracing: Debugging and logging
- rand: Random number generation
- serde: Serialization
- chrono: Date/time handling
build.rsautomatically discovers.basfiles intest_suite/- Generates test functions in
target/out/generated_tests.rs - Compiles with LLVM support for code generation
BasicRS/
├── src/
│ ├── lib.rs # Library entry point
│ ├── main.rs # Main executable
│ ├── basic_types.rs # Core data structures
│ ├── basic_lexer.rs # Lexical analysis
│ ├── basic_parser.rs # Syntax analysis
│ ├── basic_interpreter.rs # Execution engine
│ ├── basic_symbols.rs # Symbol management
│ ├── basic_operators.rs # Operator implementation
│ ├── basic_function_registry.rs # Built-in functions
│ ├── basic_keyword_registry.rs # Keyword management
│ ├── basic_dialect.rs # Language dialect
│ ├── basic_reports.rs # Coverage and reporting
│ ├── llvm_codegen.rs # LLVM code generation
│ ├── llvm_ir_builder.rs # LLVM IR construction
│ └── bin/ # Executables
│ ├── basic_shell.rs # Interactive shell
│ ├── basic-compiler.rs # LLVM compiler
│ └── basic_coverage.rs # Coverage tool
├── test_suite/ # BASIC test programs
├── tests/ # Integration tests
├── build.rs # Build script
├── Cargo.toml # Project configuration
└── run_all_tests.sh # Test runner script
cargo run -- program.bascargo run -- program.bas --coverage-file coverage.jsoncargo run --bin basic_shell -- program.bascargo run --bin basic-compiler -- program.bascargo run --bin basic_coverage -- coverage.jsonThe project uses a comprehensive error handling system:
- Syntax errors: Invalid program structure
- Runtime errors: Execution-time problems
- Type errors: Type mismatches
- Internal errors: Interpreter implementation issues
All errors include line number information for debugging.
The project is actively developed with several planned improvements:
- Enhanced LLVM optimization
- Additional BASIC dialect support
- Improved debugging tools
- Performance optimizations
- Extended test coverage