2026-02-22 12:18:17 -05:00
|
|
|
# GW-BASIC 2026
|
2026-02-10 15:53:57 -05:00
|
|
|
|
|
|
|
|
A portable C reimplementation of Microsoft GW-BASIC, using the
|
|
|
|
|
[original 8088 assembly source](https://github.com/microsoft/GW-BASIC)
|
|
|
|
|
(released by Microsoft in 2020) as the authoritative reference.
|
|
|
|
|
|
2026-03-30 16:50:43 -04:00
|
|
|
This is not a transpilation -- it reimplements the algorithms in clean C11
|
2026-02-10 15:53:57 -05:00
|
|
|
with modern data structures while targeting bug-compatible behavior.
|
|
|
|
|
|
|
|
|
|
## Building
|
|
|
|
|
|
|
|
|
|
```bash
|
2026-03-30 16:45:41 -04:00
|
|
|
cmake -B build && cmake --build build
|
2026-02-10 15:53:57 -05:00
|
|
|
```
|
|
|
|
|
|
2026-02-15 16:28:44 -05:00
|
|
|
Requires a C11 compiler and CMake 3.10+. PulseAudio (`libpulse-simple`)
|
2026-03-30 16:50:43 -04:00
|
|
|
is optional -- detected at build time for `SOUND`/`BEEP`/`PLAY` support.
|
2026-02-10 15:53:57 -05:00
|
|
|
|
2026-03-30 16:45:41 -04:00
|
|
|
Builds three targets:
|
2026-03-30 16:50:43 -04:00
|
|
|
- `gwbasic` -- the interpreter
|
|
|
|
|
- `gwbasic-compile` -- the ahead-of-time compiler
|
|
|
|
|
- `libgwrt.a` -- runtime library for compiled programs
|
2026-03-30 16:45:41 -04:00
|
|
|
|
2026-02-10 15:53:57 -05:00
|
|
|
## Usage
|
|
|
|
|
|
2026-02-22 12:18:17 -05:00
|
|
|
Interactive mode launches the authentic GW-BASIC full-screen editor:
|
2026-02-10 15:53:57 -05:00
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ ./gwbasic
|
2026-05-03 12:25:41 -04:00
|
|
|
GW-BASIC 2026 0.17.0
|
2026-02-10 15:53:57 -05:00
|
|
|
(C) Eremey Valetov 2026. MIT License.
|
|
|
|
|
Based on Microsoft GW-BASIC assembly source.
|
|
|
|
|
Ok
|
|
|
|
|
PRINT 2+2
|
|
|
|
|
4
|
|
|
|
|
Ok
|
|
|
|
|
```
|
|
|
|
|
|
2026-03-01 12:25:47 -05:00
|
|
|
Run a program file (ASCII or binary tokenized):
|
2026-02-10 15:53:57 -05:00
|
|
|
|
|
|
|
|
```bash
|
2026-02-10 17:01:50 -05:00
|
|
|
./gwbasic tests/programs/prime_sieve.bas
|
2026-02-10 15:53:57 -05:00
|
|
|
```
|
|
|
|
|
|
2026-03-30 16:45:41 -04:00
|
|
|
Compile to a native executable:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
./gwbasic-compile tests/programs/fibonacci.bas -c --runtime .
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Ahead-of-Time Compiler
|
|
|
|
|
|
|
|
|
|
`gwbasic-compile` translates BASIC programs to C source, then invokes GCC
|
|
|
|
|
to produce native executables linked against `libgwrt.a`.
|
|
|
|
|
|
|
|
|
|
Pipeline: `.bas` → tokenizer → analysis → C codegen → `gcc` → native binary.
|
|
|
|
|
|
2026-05-04 16:32:09 -04:00
|
|
|
63 of 63 eligible test programs compile via `gwbasic-compile` and produce
|
2026-05-03 12:25:41 -04:00
|
|
|
output matching the interpreter's golden files. Run `bash
|
|
|
|
|
tests/run_compiler_tests.sh` to verify.
|
2026-03-30 16:45:41 -04:00
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
# Generate C source
|
|
|
|
|
./gwbasic-compile myprog.bas -o myprog.c
|
|
|
|
|
|
|
|
|
|
# Compile to executable (automatic)
|
|
|
|
|
./gwbasic-compile myprog.bas -c --runtime /path/to/gw-basic-2026
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Jupyter Kernel
|
|
|
|
|
|
|
|
|
|
A Jupyter notebook kernel for GW-BASIC with inline Sixel graphics
|
|
|
|
|
rendering, INPUT support, and Pygments syntax highlighting.
|
2026-02-10 15:53:57 -05:00
|
|
|
|
|
|
|
|
```bash
|
2026-03-30 16:45:41 -04:00
|
|
|
pip install -e .
|
|
|
|
|
gwbasickernel-install --user
|
|
|
|
|
jupyter notebook # select "GW-BASIC 2026" kernel
|
2026-02-10 15:53:57 -05:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## What Works
|
|
|
|
|
|
2026-03-30 16:50:43 -04:00
|
|
|
100% token coverage -- all 144 GW-BASIC tokens are implemented.
|
2026-03-30 16:45:41 -04:00
|
|
|
|
2026-02-10 15:53:57 -05:00
|
|
|
**Data types:** INTEGER (%), SINGLE (!), DOUBLE (#), STRING ($)
|
|
|
|
|
|
|
|
|
|
**Operators:** `+ - * / ^ \ MOD AND OR XOR EQV IMP NOT < = > <= >= <>`
|
|
|
|
|
|
|
|
|
|
**Statements:**
|
|
|
|
|
|
|
|
|
|
| Category | Statements |
|
|
|
|
|
|----------|------------|
|
2026-02-22 13:55:27 -05:00
|
|
|
| Output | PRINT, LPRINT, LLIST, PRINT USING, WRITE, CLS |
|
2026-02-10 15:53:57 -05:00
|
|
|
| Variables | LET, DIM, ERASE, SWAP, DEFINT/SNG/DBL/STR |
|
|
|
|
|
| Control flow | GOTO, GOSUB/RETURN, FOR/NEXT, IF/THEN/ELSE, WHILE/WEND, ON...GOTO/GOSUB |
|
2026-02-10 17:01:50 -05:00
|
|
|
| Input | INPUT, LINE INPUT, DATA/READ/RESTORE, INKEY$ |
|
2026-03-30 16:45:41 -04:00
|
|
|
| Program | RUN, CONT, STOP, END, NEW, LIST, CLEAR, AUTO, RENUM, DELETE, EDIT |
|
2026-02-10 15:53:57 -05:00
|
|
|
| Sequential I/O | OPEN, CLOSE, PRINT#, WRITE#, INPUT#, LINE INPUT# |
|
|
|
|
|
| Random-access I/O | FIELD, LSET, RSET, PUT, GET, CVI/CVS/CVD, MKI$/MKS$/MKD$ |
|
2026-03-30 16:45:41 -04:00
|
|
|
| Program I/O | SAVE (binary/ASCII), LOAD (auto-detects), MERGE, CHAIN, COMMON |
|
|
|
|
|
| Event trapping | ON TIMER(n) GOSUB, TIMER ON/OFF/STOP, ON KEY(n) GOSUB |
|
2026-02-10 17:01:50 -05:00
|
|
|
| Error handling | ON ERROR GOTO, RESUME, ERROR, ERR, ERL |
|
2026-02-10 15:53:57 -05:00
|
|
|
| User functions | DEF FN, RANDOMIZE |
|
2026-03-30 16:45:41 -04:00
|
|
|
| File management | KILL, NAME, FILES, MKDIR, RMDIR, CHDIR, SHELL, ENVIRON |
|
2026-02-22 12:40:18 -05:00
|
|
|
| Date/time | DATE$, TIME$, TIMER |
|
2026-02-22 12:18:17 -05:00
|
|
|
| Screen | LOCATE, COLOR, WIDTH, SCREEN, KEY ON/OFF/LIST |
|
2026-03-30 16:45:41 -04:00
|
|
|
| Graphics | PSET, PRESET, LINE, CIRCLE, DRAW, PAINT, GET/PUT (sprites), VIEW, WINDOW, PALETTE, PMAP |
|
2026-02-15 16:28:44 -05:00
|
|
|
| Sound | SOUND, BEEP, PLAY (MML) |
|
2026-03-30 16:45:41 -04:00
|
|
|
| Memory | DEF SEG, PEEK, POKE, BSAVE, BLOAD |
|
|
|
|
|
| Hardware I/O | OUT, INP, WAIT, MOTOR, STICK, STRIG |
|
2026-03-01 12:25:47 -05:00
|
|
|
|
2026-02-10 15:53:57 -05:00
|
|
|
## Tests
|
|
|
|
|
|
2026-05-04 16:32:09 -04:00
|
|
|
72 interpreter tests, 14 kernel tests, 63 compiler tests -- all passing.
|
2026-02-10 15:53:57 -05:00
|
|
|
|
|
|
|
|
```bash
|
2026-03-30 16:45:41 -04:00
|
|
|
bash tests/run_tests.sh # interpreter
|
|
|
|
|
python -m gwbasickernel.test_kernel # Jupyter kernel
|
2026-02-10 15:53:57 -05:00
|
|
|
```
|
|
|
|
|
|
2026-04-10 18:14:41 -04:00
|
|
|
## DOS / FreeDOS
|
|
|
|
|
|
|
|
|
|
Cross-compiles to DOS with OpenWatcom V2:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
wmake -f Makefile.dos16 # 16-bit real-mode (128KB standalone, no extender)
|
|
|
|
|
wmake -f Makefile.dos # 32-bit DOS/4GW (175KB, requires DOS4GW.EXE)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
The 16-bit build runs on FreeDOS, MS-DOS, and compatible systems without
|
|
|
|
|
a DOS extender. See [Getting Started](docs/getting-started.md) for details.
|
|
|
|
|
|
2026-03-30 16:45:41 -04:00
|
|
|
## Documentation
|
|
|
|
|
|
|
|
|
|
Full Sphinx documentation in `docs/`:
|
2026-02-22 12:18:17 -05:00
|
|
|
|
|
|
|
|
```bash
|
2026-03-30 16:45:41 -04:00
|
|
|
cd docs && pip install -r requirements.txt && make html
|
2026-02-22 12:18:17 -05:00
|
|
|
```
|
|
|
|
|
|
2026-02-10 15:53:57 -05:00
|
|
|
## License
|
|
|
|
|
|
|
|
|
|
MIT License. See [LICENSE](LICENSE).
|