BASICCompiler
A BASIC compiler, written in Java, compiles BASIC programs into Java bytecode
Install / Use
/learn @lwiest/BASICCompilerREADME
BASIC Compiler
BASIC Compiler is an open-source BASIC compiler, written in Java.
It compiles a BASIC program into Java bytecode, which can be executed with any Java Virtual Machine 1.5 or higher.
Originally, I started this project to learn about Java bytecodes, hacking together an empty Java class file, adding bits and pieces. Soon this grew into implementing a BASIC compiler complete enough to compile and play classic BASIC games like, for example, StarTrek.
BASIC Compiler is self-contained. It uses only a minimum set of Java Virtual Machine methods and – by intention – no other frameworks as I wanted to write the compiler from scratch.
The functionality of BASIC Compiler is backed by over 1500 test programs in BASIC.
The implemented BASIC language is oriented at Microsoft BASIC.
Enjoy! — Lorenz
Table of Contents
Getting Started
Prerequisites
- You have installed Java JDK (or SDK) 5 or higher on your system.
Instructions
-
Download BASICCompiler.jar to a folder.
-
Open a command prompt in that folder and enter
java -jar BASICCompiler.jarThis runs BASIC Compiler and lists command-line options:
___ _ ___ _ ___ ___ _ _ | _ ) /_\ / __|| |/ __| / __|___ _ __ _ __(_) |___ _ _ | _ \/ _ \\__ \| | (__ | (__/ _ \ ' \| '_ \ | | -_) '_| |___/_/ \_\___/|_|\___| \___\___/_|_|_| .__/_|_|___|_| |_| Version 1.6 (22-DEC-2019) by Lorenz Wiest Usage: java BASICCompiler <BASIC source filename> <Java class filename> [<options>] Options: -formatted=<filename> | Writes a formatted BASIC source file -optimize | Applies compiler optimizationsOption
-formatted=<filename>writes a pretty-printed version of the BASIC program to<filename>. The line numbers of the BASIC program are renumbered from 1000 on in increments of 10.Option
-optimizeapplies compiler optimizations. As of now, the only implemented optimization is the folding of constant string arguments of one or morePRINTstatements. -
To compile a BASIC program, for example STARTREK.BAS in folder samples, enter
java -jar BASICCompiler.jar samples/STARTREK.BAS StarTrek -
To run the compiled BASIC program, enter
java StarTrek
Build Instructions
Prerequisites
- You have installed Java SDK 5 or higher on your system.
Instructions
-
Download this project’s ZIP file from GitHub.
-
Unzip it to a temporary folder.
-
To work with the BASIC Compiler source code in your Eclipse IDE, import the
BASICCompilerproject from the temporary folder into your Eclipse IDE as an import source General > Existing Projects into Workspace. -
To compile BASIC Compiler into a convenient JAR file (Windows only), open a command prompt in the temporary folder and enter
makejarThis produces the
BASICCompiler.jarfile, containing the compiled BASIC compiler.(Note that the environment variable
JAVA_HOMEmust point to the installation folder of your Java SDK.)
Language Specification
Find more information about the implemented BASIC language in the language specification.
Samples
Find sample BASIC programs from David Ahl’s classic BASIC Computer Games books, original (used with permission) and modified, in folder samples. Below are transcripts of runs from some compiled sample BASIC programs:
F:\BASICCompiler>java 3dplot
3D-PLOT
CREATIVE COMPUTING MORRISTOWN, NEW JERSEY
*
* * *
* * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * *
* * * * * * ** * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * ** *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * ** *
* * * * * * * * *
* * * * * * * * * *
* * * * * * ** * * *
* * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * *
* * *
*
F:\BASICCompiler>java Amazing
AMAZING PROGRAM
CREATIVE COMPUTING MORRISTOWN, NEW JERSEY
WHAT ARE YOUR WIDTH AND LENGTH?22,15
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ +--+--+--+--+--+
| | | | | | |
| +--+--+--+--+--| | | | | | +--+--+--| | | | | +--| +
| | | | | | | | | | | |
+--+--+--| | +--+--+--+--+--+--+--+--+--| | | | +--| +--| +
| | | | | | | | | |
+--+--| +--+--+--+--| +--| | | | | +--| +--+--| | | +--+
| | | | | | | | | | | |
| +--+--| +--+--| +--+--| +--| | +--+--+--| | | +--+--| +
| | | | | | | | | | | |
| | | | | | | | | +--| +--+--| +--+--+--+--| +--+--| +
| | | | | | | | | | | | | |
+--+--| | | | +--+--+--+--+--| +--+--| | +--| | | | | +
| | | | | | | | | | | |
| | | +--| | +--+--+--+--+--+--| +--+--| | +--+--| | +--+
| | | | | | | | | | |
| +--+--| +--+--| +--+--| | | +--+--| +--| | +--+--+--| +
| | | | | | | | | |
+--+--+--+--| | | | +--+--| +--+--| +--+--+--+--| | +--| +
| | | | | | | | | | |
| | | | | +--| +--+--| +--+--+--+--| | | +--+--+--| | +
| | | | | | | | | | | |
+--+--+--+--+--| +--| | | | +--+--| | +--+--+--+--+--| | +
| | | | | | | | | | | |
+--+--| +--| +--| +--+--| | | | | +--| | +--+--+--+--+--+
| | | | | | | | | | | | |
+--| | +--+--| | | +--+--+--+--| +--| | +--| | | | | +
| | | | | | | | | | | | |
| +--+--+--+--+--+--+--| +--+--| | | | | +--+--| | +--| +
| | | | | | | |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--| +--+--+--+
F:\BASICCompiler>java Camel
CAMEL
CREATIVE COMPUTING
MORRISTOWN, NEW JERSEY
WOULD YOU LIKE INSTRUCTIONS?Y
WELCOME TO CAMEL. THE OBJECT IS TO TRAVEL
200 MILES ACROSS THE GREAT GOBI DESERT.
A TRIBE OF KNOCKED-KNEED PYGMIES WILL BE CHASING YOU.
YOU WILL BE ASKED FOR COMMANDS EVERY SO OFTEN.
C O M M A N D S :
#1 DRINK FROM YOUR CANTEEN
#2 AHEAD MODERATE SPEED
#3 AHEAD FULL SPEED
#4 STOP FOR THE NIGHT
#5 STATUS CHECK
#6 HOPE FOR HELP
YOU HAVE ONE QUART OF WATER WHICH WILL LAST YOU SIX DRINKS.
YOU MAY RENEW YOUR WATER SUPPLY COMPLETELY AT AN OASIS.
YOU GET A HALF A QUART IF FOUND BY HELP.
IF HELP DOES NOT FIND YOU AFTER COMMAND SIX, YOU LOSE.
GOOD LUCK AND GOOD CAMELING!
YOU ARE IN THE MIDDLE OF THE DESERT AT AN OASIS.
YOU HAVE TRAVELLED 0 MILES ALLTOGETHER.
WHAT IS YOUR COMMAND?3
YOUR CAMEL IS BURNING ACROSS THE DESERT SANDS.
YOU HAVE TRAVELLED 18 MILES ALLTOGETHER.
WHAT IS YOUR COMMAND?3
YOUR CAMEL IS BURNING ACROSS THE DESERT SANDS.
----------W A R N I N G---------- GET A DRINK
YOU HAVE TRAVELLED 18 MILES ALLTOGETHER.
WHAT IS YOUR COMMAND?4
YOUR CAMEL THANKS YOU!
THE PYGMIES ARE 6 MILES BEHIND YOU.
YOU HAVE TRAVELLED 18 MILES ALLTOGETHER.
WHAT IS YOUR COMMAND?1
BETTER WATCH FOR AN OASIS!
WHAT IS YOUR COMMAND?3
YOUR CAMEL IS BURNING ACROSS THE DESERT SANDS.
THE PYGMIES HAVE CAPTURED YOU. CAMEL AND PEOPLE SOUP IS
THEIR FAVORITE DISH!!!
WANT A NEW CAMEL AND A NEW GAME?
-----------------
CHICKEN
-----------------
F:\BASICCompiler>java Chase
CHASE
CREATIVE COMPUTING
MORISTOWN, NEW JERSEY
YOU ARE WITHIN THE WALLS OF A HIGH VOLTAGE MAZE.
THERE ARE FIVE SECURITY MACHINES TRYING TO DESTROY YOU.
YOU ARE THE '*', THE INTERCEPTORS ARE THE '+'.
THE AREAS MARKED 'X' ARE HIGH VOLTAGE.
YOUR ONLY CHANCE FOR SURVIVAL IS TO MANEUVER EACH
INTERCEPTOR INTO AN 'X'. GOOD LUCK!
MOVES ARE 7.8.9
4.*.6
1.2.3
10 = NO MOVE FOR THE RE
