Skip to content

Latest commit

History

48 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Shortcomings (that I know about)

  • Extremely fragile interpreter. I didn't even try to make this robust, so malformed BASIC will most likely mimic g++ error messages.
  • Order of operations simply does not exist. To get correct math evaluation, use a copious amount of parentheses: 2 + 3 * 2 incorrectly evaluates to 8, but 2 * (3 + 2) correctly produces 10.
  • Very limited feature set. The goal was to produce a minimal functional BASIC interpreter for use on the Pi Pico.
  • Unary negation is beyond my comprehension for some reason, so if you want negative literals, do (0 - number): -5 == (0 - 5)

Keyword reference

KeywordSyntaxDescription
PRINTPRINT (expr)Evaluate the expression and print it
PRINTNLPRINTNL (expr)Same as print but without adding a newline
INPUTINPUT (ident)Read a single character from input and store into ident
LETN/ATokenized but not used (ignored).
IFIF (expr) THEN (expr)If the expression evaluates to 1 (True), jump to the line evaluated by the second expr.
THENN/APart of IF
FORFOR (ident) = (expr) TO (expr) [STEP (expr)]Set the ident to first expr, loop until end expr, optionally step by final expr per loop.
TON/APart of FOR, PEEK, and POKE
STEPN/APart of FOR
GOTOGOTO (expr)Jump to line evaluated by expr
PEEKPEEK (expr) TO (ident)Store the value in memory at expr into ident
POKEPOKE (ident) TO (expr)Store the value of ident in memory at expr
GOSUBN/ATokenized but not used (ignored).
RETURNN/ATokenized but not used (ignored).
ENDENDTerminates interpretation immediately
REMREM ...Remarks are completely ignored

Variables

All variables are either a number, string, or array. Variables are case-sensitive, i.e. "X" is not the same as "x". Variables are not strongly typed, so their type can be re-assigned:

10REM "X will be the string Hello"20 X ="Hello"30REM "X will be re-assigned to a number (the length of the string it previously held, 5)"40 X =LEN(X)

Arrays

Arrays can be defined with the ARRAY(number) function. The length of the array is the number provided to the function. Arrays cannot grow/shrink, but adding two arrays will concatenate them into a single larger array:

10REM "X is an array with 5 elements (all initialized to 0)"20 X = ARRAY(5)
30REM "Arrays cannot be resized once created, but adding two together creates a new larger array:"40 Y = X + ARRAY(15)
50REM "Y is an array with space for 20 elements (5 from X, 15 from the anonymous array)"60PRINT"Y has room for ", LEN(Y), " elements."70REM "Arrays can hold any type in each element, including themselves:"80 Y[0] = Y
90PRINT Y[0][0][0]

Functions

FunctionArgument typeReturn typeDescription
ABSnumbernumberAbsolute value of number
LENstring or arraynumberLength of argument
CHRnumberstringNumber to ASCII
INTstringnumberConvert first character of string to number
PEEKnumberanySame as PEEK keyword
LCASEstringstringConvert string to lowercase
UCASEstringstringConvert string to uppercase
ARRAYnumberarrayReturn an empty array of specified length
TYPEanystringReturn type name of argument ("number", "string", "array")
SINnumbernumbersine(number)
COSnumbernumbercosine(number)
TANnumbernumbertangent(number)
SQRTnumbernumbersquare root(number)
LOGnumbernumberlog base 10
FLOORnumbernumberRound down number to nearest integer
CEILnumbernumberRound up number to nearest integer
DEGnumbernumberConvert radians to degrees
RADnumbernumberConvert degrees to radians
ISNANnumbernumber (0 or 1)Check if number is NaN
ISINFnumbernumber (0 or 1)Check if number is ±Inf

Operators

OperatorArgument typesReturn typeDescription
+number or arraynumber or arraynumbers: addition; arrays: concatenation
-numbernumbersubtraction
/numbernumberdivision
^numbernumberexponentiation
%numbernumbermodulo
=anynoneassignment
!N/AN/Anot used
<numbernumber (0 or 1)less than
<=numbernumber (0 or 1)less than or equal
>numbernumber (0 or 1)greater than
>=numbernumber (0 or 1)greater than or equal
==number or stringnumber (0 or 1)equals
!=number or stringnumber (0 or 1)not equals
,anystringstring coercion and concatenation
( and )anyanyevaluation grouping, function calls
[ and ]arrayanyarray indexing

About

"Basic" BASIC implementation in Zig

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages