Skip to content

dagrigorev/gw-basic-cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

5 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

GW-BASIC (Modern Cross-Platform Rewrite)

A modern, cross-platform rewrite of the classic GW-BASIC interpreter, implemented in C++20 with a real-time execution model and hardware-accelerated graphics.

This project aims to preserve the simplicity and spirit of GW-BASIC while providing a clean, extensible architecture suitable for modern systems.


โœจ Features

๐Ÿง  Language Support

  • Line-numbered BASIC programs

  • Immediate mode (REPL) + file execution

  • Control flow:

    • IF ... THEN ... ELSE
    • GOTO, GOSUB, RETURN
    • FOR/NEXT
    • WHILE/WEND
  • Variables:

    • Numeric + string
    • Arrays (DIM)
    • Default typing (DEFINT, DEFSTR, etc.)
  • Expressions:

    • Arithmetic
    • Logical (AND, OR, NOT)
  • Functions:

    • ABS, INT, LEN, VAL
    • SQR, SIN, COS, TAN, ATN
    • RND
    • String functions (LEFT$, RIGHT$, MID$, etc.)

๐ŸŽฎ Real-Time Engine

  • Frame-based execution model
  • Non-blocking input (INKEY$)
  • Engine tick integration
  • Batch rendering for high-performance drawing
  • Suitable for games and simulations

๐Ÿ–ฅ Graphics

Rendering Backends

  • Windows โ†’ Direct3D 11
  • Linux โ†’ OpenGL (X11)
  • Headless mode for testing / CI

Graphics API

  • SCREEN
  • CLS
  • COLOR
  • LOCATE
  • PSET, LINE, CIRCLE
  • PAINT, DRAW
  • GET / PUT (graphics blocks)
  • VIEW, WINDOW, PMAP
  • PALETTE, PALETTE USING

Features

  • Pixel canvas abstraction
  • Palette remapping
  • Viewport and world-coordinate transforms
  • Hardware-accelerated presentation
  • Automatic batching for performance

Examples

Here is example of graphics

tan(x^2 + y^2) = 1 graph example:

tan

snake game example:

snake

easter card example:

easter

y=sin(x), y=cos(x) graphs:

sincos

norton commaner inspired example:

commander


๐Ÿ“ File I/O

  • OPEN, CLOSE
  • INPUT#, PRINT#, WRITE#
  • LINE INPUT#
  • EOF, LOF, LOC
  • KILL, NAME, MKDIR, RMDIR

๐ŸŽน Input

  • INKEY$ (non-blocking)

  • Keyboard input from:

    • Console
    • Native graphics window (Win32 / X11)

๐Ÿš€ Getting Started

Build

Requirements

  • C++20 compiler
  • CMake โ‰ฅ 3.16

Linux

mkdir build
cd build
cmake ..
cmake --build .

Windows (MSVC)

mkdir build
cd build
cmake ..
cmake --build . --config Debug

โ–ถ๏ธ Usage

Run interpreter (REPL)

gwbasic

Run BASIC file

gwbasic --file ./examples/snake.bas

Headless mode (no graphics window)

gwbasic --headless --file ./examples/snake.bas

๐Ÿงช Example

Snake Game

gwbasic --file ./examples/snake.bas

Features:

  • real-time gameplay
  • keyboard control via graphics window
  • hardware-accelerated rendering

Math Plot

10 SCREEN 2
20 CLS
30 COLOR 15,0
40 CX = 320: CY = 100
50 SX = 0.03: SY = 0.03
60 FOR PY = 0 TO 199
70 FOR PX = 0 TO 639
80 X = (PX - CX) * SX
90 Y = (PY - CY) * SY
100 IF ABS(TAN(X*X+Y*Y)-1) < 0.05 THEN PSET (PX,PY), 10
110 NEXT PX
120 NEXT PY
130 END

๐Ÿ— Architecture

Core Components

Interpreter

  • Lexer โ†’ Parser โ†’ AST โ†’ Execution
  • Line-numbered program storage
  • Immediate + stored execution modes

Runtime

  • Variable storage
  • Array memory
  • File handles
  • Graphics state
  • Engine tick integration

Graphics

  • In-memory pixel canvas

  • Platform-specific presenter:

    • D3D11 (Windows)
    • OpenGL (Linux)
  • Batch rendering system

Engine Loop

  • Cooperative execution
  • Frame pacing (~60 FPS)
  • Event pumping
  • Input polling

โšก Performance Notes

  • Rendering is batched, not per-pixel immediate

  • Large plots are now significantly faster than naive implementations

  • Still an interpreter โ†’ heavy numeric loops can be slow

  • Prefer:

    • coarse stepping
    • analytical drawing (e.g., circles instead of brute-force)

โš ๏ธ Limitations

  • Not a 100% GW-BASIC clone
  • Graphics are portable abstractions, not exact hardware emulation
  • Some legacy quirks are not implemented
  • Floating-point math is modernized (not 8087-compatible)
  • Performance depends on interpreter speed

๐Ÿ›  Roadmap

  • Fixed-timestep VM scheduler
  • More BASIC compatibility edge cases
  • Improved parser tolerance
  • Sound subsystem (PLAY, SOUND) improvements
  • Better console emulation
  • Optional SDL backend
  • Debug / trace mode

๐Ÿค Contributing

Contributions are welcome.

Focus areas:

  • language compatibility
  • performance improvements
  • platform backends
  • test coverage

๐Ÿ“œ License

Under MIT license


๐ŸŽฏ Vision

This project is not just a clone.

Itโ€™s a modern BASIC runtime:

  • simple like the original
  • powerful enough for real-time programs
  • portable across platforms
  • clean and extensible in design

About

Rewritten version of GW-BASIC in C++20

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors