DCPU-Bas is a simple QuickBASIC-like compiler for virtual DCPU in Notch's 0x10c Game, written in Go language.
Compiler structure and engine is heavily inspired by Let's Build a Compiler, by Jack Crenshaw.
- Arithmetics: + - * / %
- Boolean operators: & ~ !
- Bit shifts: << >>
- Relational operators: == <> < > <= >=
- Loading custom fonts
- Control structures: IF, LOOP
- Variables (both integer and string)
- Statements: CLS, LOCATE, PRINT, COLOR, KEY, INPUT, POKE, GOTO, RND, FONT
- Functions: STR, CHR, VAL, LEN, PEEK, SQR
Here's a sample program (you can find it in samples/input.bas) that asks for your name and then displays it back to you:
DIM MyName
PRINT "Enter your name: "; CONTINUE
MyName = INPUT
PRINT " "
PRINT "Hello, "; MyName; "!"
END
Easiest way is to get the binaries from the downloads section.
You can also build it from the latest sources. In that case, you need to:
- Get and setup Go
- Get the latest sources and unpack them to a directory
- On the command line, within that directory, type:
go build
That should do it.
Below are language statements and functions explained:
Usage:
IF condition THEN
...
[ELSE
...]
END IF
Executes a code block if condition is met. Optional ELSE block executed if condition is NOT met.
Usage:
LOOP [WHILE condition]
...
END WHILE
Loops through a code block. Whe condition is supplied, loops while the condition is met.
Usage:
CLS
Clears whole 32x16 screen (video buffer at 0x8000)
Usage:
PRINT expression [; expression][; CONTINUE]
PRINT
Prints expression(s) at current screen cursor location. Multiple expressions can be joined with semi-colon (;).
After printing all expressions cursor position will be set to next row, column 1, unless CONTINUE keyword is given at the end of expression list.
Example:
A = "World"
PRINT "Hello "; A
PRINT "A sentence within "; CONTINUE
PRINT "the same line."
Usage:
PUTCHAR "c"
PUTCHAR expression
Fast way (5x faster than PRINT "c") to output a single character to video buffer at current cursor position. It can be either a string literal c in double quotes, or a math expression (which could be a single number) returning character ascii code.
Usage:
LOCATE Y[, X]
Sets current cursor location to X, Y. Set's only Y if X is not provided.
Usage:
COLOR FOREGROUND, BACKGROUND
Sets current output color to FOREGROUND and BACKGROUND. Both these values can be 0 to 15.
Usage:
DIM Code
Code = KEY
Used in an expression, it returns character code of last pressed key.
Usage:
DIM Char
Char = STR(expression)
Returns an ASCII character from given character code.
Usage:
DIM Code
Code = STR(expression)
Returns a character code from first character of an ASCII string (opposite to STR)
Usage:
DIM Number
Number = VAL(expression)
Returns an integer representation of a number in string variable (e.g. input from user)
Usage:
DIM Length
Length = LEN(expression)
Returns length (how many letters) of a string.
Usage:
DIM YourName
YourName = INPUT
Waits for user to enter a string followed by ENTER key, and returns this string as expression. User input is displayed on the screen.
Usage:
DIM MemoryValue
MemoryValue = PEEK(address)
Reads directly the memory and returns a number representing word at given memory address.
Usage:
POKE address, value
Writes directly to memory, sets word at address to given value.
Usage:
:label
GOTO label
An unconditional jump into another place in the program code, defined by a label.
Label definitions must start with a colon (:).
Use it with caution, there are no checks performed, so you can break your code easily by abusing this statement.
For example, nothing will prevent you to jump right into a LOOP or IF structure, but it can have unpredictable results.
Usage:
DIM RandomValue
RandomValue = RND
Generates a pseudo-random 16-bit integer (from 0 to 65535).
To get a number from a specific range, use modulo division: RandomValue = RND % 100 + 1
gives you a number from 1 to 100.
Note: Every program always starts with the same seed, meaning that it will have a same set of random numbers generated.
To update the seed randomly, use user's keyboard input - ask for something using INPUT statement, or wait for a keypress using KEY statement.
These statements update the generator seed by multiplying it with amount of time that has passed before a keystroke, which should be random enough.
Credit: Linear congruentrial generator used by this statement is coded by Entroper.
Usage:
DIM Result
Result = SQR(expression)
Computes square root of expression.
Usage:
FONT "file.txt"
Loads custom font from text file and replaces current terminal font with it. Font file can specify only selected characters.
For a sample font file please refer to fonts/box.txt file. Every letter in font file starts with letter ascii code, and then 8 lines 4 character each follow,
where dot (.) is an empty space and capital o (O) is a solid "pixel".
Program MUST end with an END
statement.
You can browse sample/ directory for sample .bas source files, as well as output/ directory for compiled .s (assembly) files of these samples. There are couple of web-based emulators that you can try these with, most notable: deNULL's.
This code is licenced on the terms of the MIT Licence.