Tiny BASIC Web, version 0.1
©2025 by Mark Damon Hughes
cyberhole.online/basic/
Text licensed as CC BY-NC-SA Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International license
Code licensed as BSD, see LICENSE.txt
Tiny BASIC Web (TBW hereafter) is an interpreted, interactive BASIC which can be embedded in a web page.
Level 1 will support only number variables A-Z, string constants, no labels or optional items. RND will be a pseudo-variable, not a function.
Level 2 is as below.
<html>
<head>
<title>TinyBasicWeb</title>
<link href='tbasicweb.css' rel='stylesheet' type='text/css' />
<script src='js/tbasicweb.js'></script>
</head>
<body onload='tbasicwebinit("terminal", [64,24], [14,24], "myprog.bas")'>
<div id='terminal'></div>
</body>
</html>
To pack the canvas size correctly, pick sizes you like, run it. The browser console will show Actual charsize X,Y
. Then change the ones you give in the function, and reload. My initial guess of [64,24], [14,24]
produced Actual charsize=14.40234375
, so I changed width to 15.
{}
is grouping, []
is optional, …
is 0 or more, |
separates options. Case is ignored, but in this grammar UPPER
is keywords, lower
is syntax term.linenum
:= 1-32767line
:= linenum statement
label
:= linenum|name
name
:= a-z {a-z|0-9}…
: names can be maximum 16 chars.number
:= [+|-]0-9… [. 0-9…] [E[+|-]0-9…]
string
:= " [any char|""]… "
varname
:= name
varname
:= name [$] [( expr {, expr}… )]
: note varname syntax is also used for built-in functions. name cannot otherwise be a reserved word.expr
:= term {op term}…
: note no operator precedence except parens! Strict left-to-right.op
:= {+|-|*|/|%|=|<>|<|<=|>|>=}
term
:= number|string|varname|(expr)
channel
:= # 0-255
statement
:= see belowREM
or '
: starts a comment, runs to end of line.* label
: sets a label. Must be unique across the program.BREAK
: Prematurely ends most recent FOR loop.CLS
: Clear screen.DIM varname {, varname}…
: Dimension array variables and strings. Multiple dimensions are allowed, size is elements x 4 for numeric, elements x 1 for strings (stored as UTF-8).ERROR "why"
: Error with code 255, message "why".FOR varname = expr1 TO expr2 [STEP expr3]
: Starts a loop, sets var to expr1, pushes to stack "varname expr2 expr3 linenum". Continued by NEXT
or terminated by BREAK
.GOSUB label
: temporarily jumps to any line number or label.GOTO label
: jumps to any line number or label.IF expr THEN statement1 [ELSE statement2]
: tests expr, if non-zero executes statement1, otherwise executes statement2. (L2) ELSE.INPUT ["PROMPT",]varname
: Print prompt or "?", request one value for varname.LET varname = expr { , varname = expr}…
: Set variables.NEXT varname
: If top of stack is varname loop, increments by expr3, IF varname <= (if expr3 is positive) or >= expr2 (if expr3 is negative) THEN GOTO linenum ELSE pop from stack.{PRINT|?} [channel ,] {expr {{,|;} expr}… [{,|;}] }
: followed by expressions, separated by semicolon ;
(no spacing) or comma ,
(which inserts a tab). (L2) channel.RETURN
: returns from GOSUB
. Use a variable like rc
to return values.TRAP label
: Any error does a GOTO
label. TRAP
negative number disables it.CLEAR
: Clear memory.EDIT label
: Put program line in the input buffer.END
: End program.LIST [A-B]
: List program, from labels A to B.NEW
: Clear program & memory.RUN
: Clear memory, start program at first line.(L2)
"foo.ext" is a filename. Name can be up to 16 chars long, extension should be "bas" for BASIC, but can be other types for other files.
Use LOCK
and UNLOCK
to protect your programs when you're not working on them.
DIR "filespec"
: List files in tbw/disk
that match filespec
, with *
and ?
wildcards.
ERASE "filename"
: Erase file tbw/disk/filename
. Error if file is locked.
FOPEN channel, "filename", "mode"
: Open file tbw/disk/filename
in mode, which is "r" (read), "w" (write), "a" (append). "w" and "a" error if file is locked.
FPUT channel, expr
: Write value to channel with newline.
FGET channel, varname
: Read one line from channel to varname.
FCLOSE channel
: close & flush channel.
LOAD "filename"
: Clear program & memory, load file tbw/disk/filename
.
LOCK "filename", "password"
: Locks a filename with a secret password, which is also up to 16 chars long.
MERGE "filename"
: Load file tbw/disk/filename
, merging lines.
SAVE "filename"
: Save program to file tbw/disk/filename
. Error if file is locked.
UNLOCK "filename", "password"
: Unlocks.
(L2) all except RND
AT(x,y)
: Moves cursor to coordinate x,y
, returns "", useful in PRINT
.
COLOR(n {, n…})
: Sets color n, returns "", useful in PRINT
. COLOR(0) resets to normal. Example:
PRINT AT(32,0);COLOR(1,31);"EMERGENCY!";COLOR(0);AT(0,1);
COL | NAME | HEX |
---|---|---|
0 | black | #000000 |
1 | maroon | #800000 |
2 | green | #008000 |
3 | olive | #808000 |
4 | navy | #000080 |
5 | purple | #800080 |
6 | teal | #008080 |
7 | gray | #808080 |
8 | silver | #C0C0C0 |
9 | red | #FF0000 |
10 | lime | #00FF00 |
11 | yellow | #FFFF00 |
12 | blue | #0000FF |
13 | magenta | #FF00FF |
14 | cyan | #00FFFF |
15 | white | #FFFFFF |
ERL(), ERN(), ERM$()
: Error line, number, message.
INT(n)
: Truncates n to integer portion.
LOC(n)
: n=0 returns x-coord, n=1 returns y-coord, n=2 returns char under cursor.
(L1) RND
: pseudo-variable as RND(0), use RND*N%N
to get integer 0..N-1.
RND(n)
: n=0 or 1 returns random number from 0.0 to 1.0 (not inclusive). n>1 returns random integer from 0 to n (not inclusive).