Skip to content

EPSILON0-dev/TinyBasic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TinyBasic

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.)


Compiling and Running

Compiling

For PC just run make, for other platforms compiling one .c file + one .h file shouldn't be too difficult.

Running examples

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.

List of the supported commands

Keywords and variable names are case-insensitive.

BASIC commands

  • LET <variable> = <expression>
    Assigns the result of an expression to the given variable, LET keyword 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's putchar())
  • GOTO <line number>
    This command jumps to the given line number.
  • IF <expression> <comparison> <expression> THEN <command>
    Executes the command after the THEN keyword 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 comments
  • CLEAR
    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_PEEK commands

  • POKE <address expression>, <value expression>
    Sets the memory at the given address to the given value
  • PEEK <address expression>, <variable>
    Gets the memory from the given address and stores it in the given variable.
  • POKEB <address expression>, <value expression>
    Same as POKE but only accesses uint8_t instead of peek_t
  • PEEKB <address expression>, <variable>
    Same as POKE but only accesses uint8_t instead of peek_t

FILE_IO commands

  • SAVE <filename>
    Saves memory contents.
  • LOAD <filename>
    Loads memory contents.

Expression solving

Supported operations

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 (!).

Operation precedence

Precedence Operation Operator(s)
1 Subexpressions in brackets ()
2 Unary operators +, -, !
3 Important arythmetics *, /, %
4 Low importance arythmetics +, -
5 Logic operations &, |, ^

Literal formats

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

Configuration

Defines

  • 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 - Enable POKE and PEEK commands.
  • FILE_IO - Enable SAVE and LOAD commands.
  • IO_KILL - Enable breaking the execution if new characters were received during execution.
  • LOOPBACK - Enable cosole loopback (input characters will be sent back).

Data types

  • 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 which POKE and PEEK accesses memory.

IO Defines

In the definitions x is the pointer to the data that has to be sent, received or checked.

  • IO_INIT - Called at the start of main() starts up the IO device.
  • PUTCHAR(x) - Prints the x character 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).

About

Flexible TinyBASIC interpreter designed for use on microcontrollers.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors