A line-by-line interpreter for a classic BASIC dialect, implemented in C.
- Line-Oriented Programming: Traditional BASIC style with line numbers
- Variable Types: Numbers and strings
- Control Structures: GOTO, GOSUB/RETURN, IF/THEN, FOR/NEXT
- Expressions: Arithmetic and string operations with operator precedence
- Built-in Functions: RND, LEN, LEFT$, RIGHT$, MID$
- File I/O: OPEN, CLOSE, INPUT#, PRINT#
- Sound & Graphics: Basic placeholder implementations
- Garbage Collection: Integrated mark-and-sweep memory management
- Debugger: BREAK, STEP, WATCH, LIST commands
- REPL Environment: Interactive command-line interface
- State Serialization: Save/load program state to resume later
make./basic./basic my_program.bas./basic --test-tokenizer # Test the tokenizer
./basic --test-line-manager # Test the line manager
./basic --test-symbol-table # Test variable management
./basic --test-execution # Test execution loop
./basic --test-control # Test control structures
./basic --test-expressions # Test expressions and functions
./basic --test-file-io # Test file I/O
./basic --test-sound-graphics # Test sound and graphics
./basic --test-gc # Test garbage collection
./basic --test-debugger # Test debugger commands
./basic --test-debug-integration # Test debugger integration
./basic --test-repl # Test REPL functionality
./basic --test-state # Test state save/load
./basic --test-comprehensive # Run comprehensive test suite
./basic --test-final # Run final integration test suite
./basic --help # Show usage informationThe interpreter supports classic BASIC commands:
RUN- Run the programLIST- List the programNEW- Clear program from memoryCLEAR- Clear variables
GOTO <line>- Jump to lineGOSUB <line>- Call subroutineRETURN- Return from subroutineFOR <var>=<start> TO <end> [STEP <step>]- Begin loopNEXT <var>- End loopIF <expr> THEN <line>- Conditional branchEND- End program
PRINT <expr>[;<expr>...]- Print to screenINPUT <var>- Get input from userOPEN "<filename>" FOR <mode> AS #<n>- Open fileCLOSE #<n>- Close filePRINT #<n>, <expr>- Print to fileINPUT #<n>, <var>- Input from file
BEEP- Make a beep soundSOUND <freq>, <duration>- Play a toneLINE (<x1>,<y1>)-(<x2>,<y2>)- Draw a lineCIRCLE (<x>,<y>), <radius>- Draw a circlePLOT <x>, <y>- Draw a point
SAVE <filename>- Save programLOAD <filename>- Load programSAVESTATE <filename>- Save program stateLOADSTATE <filename>- Load program state
DEBUG- Toggle debug modeBREAK <line>- Set breakpointSTEP- Execute one lineWATCH <var>- Watch variableCONTINUE- Resume execution
10 REM Simple BASIC program
20 PRINT "Hello, BASIC!"
30 INPUT "What is your name? ", name$
40 PRINT "Nice to meet you, "; name$
50 FOR i = 1 TO 5
60 PRINT "Counting: "; i
70 NEXT i
80 END- main.c: Entry point and test functions
- basic.h: Core data structures
- token.h/tokenizer.c: Lexical analysis
- line_manager.h/c: Line storage and management
- execution.h/c: Execution loop and command handlers
- expression.h/c: Expression parsing and evaluation
- symboltable.c: Variable storage and retrieval
- memory_manager.h/c: Garbage collection
- debugger.h/c: Debugging functionality
- repl.h/c: Read-Eval-Print Loop
- error_handler.h/c: Centralized error handling
The interpreter follows a traditional line-by-line execution model:
- Tokenization: Source lines are broken into tokens
- Line Storage: Program lines are stored by line numbers
- Execution: Lines execute sequentially unless redirected by control structures
- Expression Evaluation: Recursive descent parsing with operator precedence
- Memory Management: Custom mark-and-sweep garbage collection
- Error Handling: Centralized error reporting with file/line information
- Makefile: Automatic dependency generation with -MMD -MP flags
The following features are currently implemented as stubs or placeholders:
-
Parser Module (
parser.c): Currently just a stub implementation. The tokenizer is used directly rather than a proper parser. -
Lexer Module (
lexer.c): Currently just a stub implementation. The tokenizer (tokenizer.c) is used instead. -
Graphics Commands: The following commands are stub implementations that don't actually render graphics:
LINE (<x1>,<y1>)-(<x2>,<y2>)- Just prints a placeholder messageCIRCLE (<x>,<y>), <radius>- Just prints a placeholder messagePLOT <x>, <y>- Just prints a placeholder message
-
Sound Commands: The following commands have minimal implementations:
BEEP- Uses the terminal bell character rather than a proper sound librarySOUND <freq>, <duration>- Parses parameters but doesn't actually generate sounds
These stubs are marked with #WARNING comments in the code to make them easily findable for future developers.
This project is free to use and distribute.