This is a simple TinyBASIC interpreter designed for use on microcontrollers
Language supports the most basic commands like LET, PRINT, GOTO, IF and INPUT, 26 single-letter variables are available to the programmer, despite these limitations the language is Turing complete allowing to create (almost) any simple program.
Code memory space, expression tokens limit, maximum line number and shell IO communication way can be configured. No printfs are used (except for the debug stuff), that allows to change the communication method to for example microcontroller's UART module.
POKE and PEEK command can be disabled when used on PC, as they always cause segmentation fault, and SAVE and LOAD can be disabled when used on a MCU that doesn't have access to file system.
In case infinite loop occures there is a way to kill the execution by sending any key to the console. Execution is stopped and there is no problem with loosing progress having to reset the MCU. (KILL_IO must be enabled.)
For PC just run make, for other platforms compiling one .c file + one .h file shouldn't be too difficult.
Examples can be run by copy pasting them into the interactive shell. When using slower microcontrollers, setting transmission delays might be necessary to help their smol brains keep up.
Keywords and variable names are case-insensitive.
LET <variable> = <expression>
Assigns the result of an expression to the given variable,LETkeyword isn't necessary and format<var> = <expr>will also be understood.PRINT <"string"> : <expression>
Prints out strings and expression results to the console,:token can be used as a separator to for example put a value after a string, leaving the separator token at the end of the line disables the linefeed that would have been sent at the end of the line.CHAR <variable>
Print a character in the given variable (equivalent to C'sputchar())GOTO <line number>
This command jumps to the given line number.IF <expression> <comparison> <expression> THEN <command>
Executes the command after theTHENkeyword if the comparison result is true, available comparisons are the following: equal (=), not equal (<>), lower than (<) and greater than (>)INPUT <variable>
Gets the expression from the console and put it into the specified variable.REM <comment>
Do nothing, only purpose of this command is to store commentsCLEAR
Clears the console and homes the cursor.LIST
Lists the program.MEMORY
Shows how much code memory is left.RUN
Starts the program from the first line.NEW
Clears the code memory after confirmation.
POKE <address expression>, <value expression>
Sets the memory at the given address to the given valuePEEK <address expression>, <variable>
Gets the memory from the given address and stores it in the given variable.POKEB <address expression>, <value expression>
Same asPOKEbut only accessesuint8_tinstead ofpeek_tPEEKB <address expression>, <variable>
Same asPOKEbut only accessesuint8_tinstead ofpeek_t
SAVE <filename>
Saves memory contents.LOAD <filename>
Loads memory contents.
Language interpreter contains a simple expression solver supporting basic integer arythmetic operations like: add (+), subtract (-), multiply (*), divide (/) and remainder (%). Basic logic functions are also supported: AND (&), OR (|), XOR (^) and NOT (!).
| Precedence | Operation | Operator(s) |
|---|---|---|
| 1 | Subexpressions in brackets | () |
| 2 | Unary operators | +, -, ! |
| 3 | Important arythmetics | *, /, % |
| 4 | Low importance arythmetics | +, - |
| 5 | Logic operations | &, |, ^ |
| Name | Format | Examples |
|---|---|---|
| Variable | Single letter variable name | A, B, c, z, Z |
| Decimal | Normal value starting with non-zero digit | 0, 1, 2, 420, 1024, 911 |
| Hexadecimal | Hex value starting with 0x prefix |
0x45, 0xFF, 0xDEADBEEF |
| Octal | Value starting with '0' prefix | 033, 0377, 0105, 00 |
| Binary | Value starting with '0b' prefix | 0b0110, 0b1001, 0b01010101 |
NEWLINE- Character that will be interpreted as a new line.BACKSPACE- Character that will be interpreted as a backspace.CODE_MEMORY_SIZE- Size of the program memory.EXPR_MAX_TOKENS- Size of expression solver buffer.MAX_LINUENUM- Maximum valid line number (not including MAX_LINENUM).POKE_PEEK- EnablePOKEandPEEKcommands.FILE_IO- EnableSAVEandLOADcommands.IO_KILL- Enable breaking the execution if new characters were received during execution.LOOPBACK- Enable cosole loopback (input characters will be sent back).
line_t- Format in which line number is stored.var_t- Format in which variables are stored.uvar_t- Unisgned version of the format in which variables are stored.peek_t- Format in whichPOKEandPEEKaccesses memory.
In the definitions x is the pointer to the data that has to be sent, received or checked.
IO_INIT- Called at the start ofmain()starts up the IO device.PUTCHAR(x)- Prints thexcharacter to the IO device.GETCHAR(x)- Return character from the IO device.IO_CHECK(x)- Returns if there are any new characters in IO (used for IO_KILL).