1
Programs / QBASIC/QB64 subset interpreter
« on: July 03, 2021, 03:03:26 pm »
QBASIC/QB64 subset interpreter by Ed Davis.
----------------------------------------------------------------------------------
Currently supports:
Double and string variables only. No dim for these.
One or two dimensional arrays.
All standard operators, with hopefully the correct precedence.
String operators:
+
=, <>, <, >, <=, >=
Numeric binary operators
^,
*, /
\
mod,
+, -,
shl,<<, shr,>>
=, <>, <, >, <=, >=,
and,
or,
xor,
eqv,
imp,
Numeric unary operators
not, -, +
Commands supported:
bye, quit - exits the interpreter
clear - clears variables
dump - shows info about arrays
edit - edits the current or default program
files - shows a list of files
help - simple help screen
list - lists current program
list vars - iists variables
load, old - loads a program into the interpreter
new - discards the current program
reload - reloads the current program from disk
run - runs the current program
save - saves the current program
ston - turns stepping on
stoff - turns stepping off
tron - turns tracing on
troff - turns tracing off
Statements:
Iteration:
do [while|until]
stmts
loop
or
do
stmts
loop [while|until]
exit do
while stmts wend - exit while
for index = n to expr [step n]
next
exit for
Control transfer:
gosub, return
goto - buggy if you jump outside of loops or multiline if
Selection:
singleline or multiline if
if elseif else end if
const - declare numeric or string constants
dim - declares double or string arrays
end, stop, system - exits program to interpreter
Other commands:
chdir circle cls color draw environ input line locate mid$ paint
palette play preset print, ? pset randomize rem screen shell
sleep sound swap view width window
QB64 commands:
_delay _display _freeimage _fullscreen _limit _printstring
_screenmove _title
Numeric functions:
abs acos acosh acot acoth acsc acsch asc asec asech asin asinh
atanh atn, atan cdbl cint clng cos cosh cot coth csc csch csng
csrlin cvd cvi exp false frac fix instr int len ln log log10 peek
point pos rnd screen sec sech sgn sin sinh sqr, sqrt tan tanh
timer true ubound val
QB64 Numeric functions:
_atan2 _ceil _d2g _d2r _fontwidth _fontheight _g2d _g2r _height
_instrrev _keydown _keyhit _mousebutton _mouseinput _mousex
_mousey _newimage _pi _r2d _r2g _rgb _rgba _rgba32 _rgb32 _round
_width
String Functions:
chr$ command$ date$ environ$ hex$ inkey$ lcase$ left$" lpad$
ltrim$ mid$ mki$ oct$ replace$ right$ rpad$ rtrim$ space$ str$
string$ time$ trim$ ucase$
QB64 string functions:
_clipboard$ _cwd$ _os$ _startdir$ _title$ _trim$
No other numeric data types besides double. Only suffix accepted is $ for strings.
Only one or two dimensional arrays
No subs or functions.
No dim.
Lots of other stuff missing.
You can run a single line of code at the ">" prompt:
>n=1:lim=25:while n<=lim:k=3:p=1:n=n+2:while k*k<=n and p:p=(n\k)*k<>n:k=k+2:wend:while p:print n; " is prime":exit while:wend:wend
You'll get:
3 is prime
5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
19 is prime
23 is prime
Or you can just use it as a fancy calculator:
>3 + 4/(2*3*4) - 4/(4*5*6) + 4/(6*7*8) - 4/(8*9*10) + 4/(10*11*12) - 4/(12*13*14)
3.1408
To run an example:
Start the interpreter
You'll get a prompt. type: run filename
For example:
>run matrix.bas
Or:
>load matrix.bas
>run
To edit a program, type "edit". If no program is loaded, it will edit "default.bas".
It uses notepad.exe by default, unless the EDITOR environment variable is defined, in which case it will use whatever that points to.
You can single step:
>load matrix.bas
>tron
>ston
>run
Press enter to keep on stepping
After a run, you can show variables:
>list vars
I'm sure there are lots of bugs^H^H^H^Hfeatures, so beware!
The interpreter:
Sample programs:
Fireworks by Ken G.
Snake by Johnno and BPlus
Laser Show by Sierra Ken:
Matrix by unknown:
A Tiny Basic interpreter - very limited
----------------------------------------------------------------------------------
Currently supports:
Double and string variables only. No dim for these.
One or two dimensional arrays.
All standard operators, with hopefully the correct precedence.
String operators:
+
=, <>, <, >, <=, >=
Numeric binary operators
^,
*, /
\
mod,
+, -,
shl,<<, shr,>>
=, <>, <, >, <=, >=,
and,
or,
xor,
eqv,
imp,
Numeric unary operators
not, -, +
Commands supported:
bye, quit - exits the interpreter
clear - clears variables
dump - shows info about arrays
edit - edits the current or default program
files - shows a list of files
help - simple help screen
list - lists current program
list vars - iists variables
load, old - loads a program into the interpreter
new - discards the current program
reload - reloads the current program from disk
run - runs the current program
save - saves the current program
ston - turns stepping on
stoff - turns stepping off
tron - turns tracing on
troff - turns tracing off
Statements:
Iteration:
do [while|until]
stmts
loop
or
do
stmts
loop [while|until]
exit do
while stmts wend - exit while
for index = n to expr [step n]
next
exit for
Control transfer:
gosub, return
goto - buggy if you jump outside of loops or multiline if
Selection:
singleline or multiline if
if elseif else end if
const - declare numeric or string constants
dim - declares double or string arrays
end, stop, system - exits program to interpreter
Other commands:
chdir circle cls color draw environ input line locate mid$ paint
palette play preset print, ? pset randomize rem screen shell
sleep sound swap view width window
QB64 commands:
_delay _display _freeimage _fullscreen _limit _printstring
_screenmove _title
Numeric functions:
abs acos acosh acot acoth acsc acsch asc asec asech asin asinh
atanh atn, atan cdbl cint clng cos cosh cot coth csc csch csng
csrlin cvd cvi exp false frac fix instr int len ln log log10 peek
point pos rnd screen sec sech sgn sin sinh sqr, sqrt tan tanh
timer true ubound val
QB64 Numeric functions:
_atan2 _ceil _d2g _d2r _fontwidth _fontheight _g2d _g2r _height
_instrrev _keydown _keyhit _mousebutton _mouseinput _mousex
_mousey _newimage _pi _r2d _r2g _rgb _rgba _rgba32 _rgb32 _round
_width
String Functions:
chr$ command$ date$ environ$ hex$ inkey$ lcase$ left$" lpad$
ltrim$ mid$ mki$ oct$ replace$ right$ rpad$ rtrim$ space$ str$
string$ time$ trim$ ucase$
QB64 string functions:
_clipboard$ _cwd$ _os$ _startdir$ _title$ _trim$
No other numeric data types besides double. Only suffix accepted is $ for strings.
Only one or two dimensional arrays
No subs or functions.
No dim.
Lots of other stuff missing.
You can run a single line of code at the ">" prompt:
>n=1:lim=25:while n<=lim:k=3:p=1:n=n+2:while k*k<=n and p:p=(n\k)*k<>n:k=k+2:wend:while p:print n; " is prime":exit while:wend:wend
You'll get:
3 is prime
5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
19 is prime
23 is prime
Or you can just use it as a fancy calculator:
>3 + 4/(2*3*4) - 4/(4*5*6) + 4/(6*7*8) - 4/(8*9*10) + 4/(10*11*12) - 4/(12*13*14)
3.1408
To run an example:
Start the interpreter
You'll get a prompt. type: run filename
For example:
>run matrix.bas
Or:
>load matrix.bas
>run
To edit a program, type "edit". If no program is loaded, it will edit "default.bas".
It uses notepad.exe by default, unless the EDITOR environment variable is defined, in which case it will use whatever that points to.
You can single step:
>load matrix.bas
>tron
>ston
>run
Press enter to keep on stepping
After a run, you can show variables:
>list vars
I'm sure there are lots of bugs^H^H^H^Hfeatures, so beware!
The interpreter:
Code: QB64: [Select]
- ' QBASIC/QB64 subset interpreter by Ed Davis.
- ' ------------------------------------------------------------------------------------------
- '------------------------------------------------------------------------
- ' 03 Jul 2021 todo
- ' [x] store/retrieve variables like eval-ed4
- ' [x] const id[$] = number|string {, const id[$] = number|string}
- ' [ ] consolidate loop handling data structures
- ' [x] arrays
- ' [x] parse
- ' [x] allocate 1 dimensional
- ' [x] allocate 2 dimensional
- ' [x] assign (idstmt, stridstmt)
- ' [x] reference (strfactor, primary)
- ' [ ] Subs
- ' [ ] Functions
- ' [ ] Shared variables
- ' getvarindex& (getstrindex$), used in:
- ' forstmt: to reference the "i" variable
- ' inputstmt: to reference the "input" variable: input "", numeric_store(i)
- ' swapstmt: reference: swap(numeric_store(i1), numeric_store(i2))
- ' assignment: numeric_store(i) = value
- ' primary: primary# = numeric_store(i)
- 'idstmt (stridstmt) - only called by assignment
- '------------------------------------------------------------------------
- ' Currently supports:
- '
- ' Double and string variables only. No dim for these.
- ' One or two dimensional arrays.
- ' All standard operators, with hopefully the correct precedence.
- '
- ' String operators:
- ' +
- ' =, <>, <, >, <=, >=
- '
- '
- ' Numeric binary operators
- ' ^,
- ' *, /
- ' \
- ' mod,
- ' +, -,
- ' shl,<<, shr,>>
- ' =, <>, <, >, <=, >=,
- ' and,
- ' or,
- ' xor,
- ' eqv,
- ' imp,
- '
- ' Numeric unary operators
- ' not, -, +
- '
- ' Commands supported:
- '
- ' bye, quit - exits the interpreter
- ' clear - clears variables
- ' dump - shows info about arrays
- ' edit - edits the current or default program
- ' files - shows a list of files
- ' help - simple help screen
- ' list - lists current program
- ' list vars - iists variables
- ' load, old - loads a program into the interpreter
- ' new - discards the current program
- ' reload - reloads the current program from disk
- ' run - runs the current program
- ' save - saves the current program
- ' ston - turns stepping on
- ' stoff - turns stepping off
- ' tron - turns tracing on
- ' troff - turns tracing off
- '
- ' Statements:
- '
- ' Iteration:
- ' do [while|until]
- ' stmts
- ' loop
- '
- ' or
- '
- ' do
- ' stmts
- ' loop [while|until]
- '
- ' exit do
- '
- ' while stmts wend - exit while
- '
- ' for index = n to expr [step n]
- ' next
- '
- ' exit for
- '
- ' Control transfer:
- ' gosub, return
- ' goto - buggy if you jump outside of loops or multiline if
- '
- ' Selection:
- ' singleline or multiline if
- ' if elseif else end if
- '
- ' const - declare numeric or string constants
- '
- ' dim - declares double or string arrays
- '
- ' end, stop, system - exits program to interpreter
- '
- ' Other commands:
- '
- ' chdir circle cls color draw environ input line locate mid$ paint
- ' palette play preset print, ? pset randomize rem screen shell
- ' sleep sound swap view width window
- '
- ' QB64 commands:
- '
- ' _delay _display _freeimage _fullscreen _limit _printstring
- ' _screenmove _title
- '
- ' Numeric functions:
- '
- ' abs acos acosh acot acoth acsc acsch asc asec asech asin asinh
- ' atanh atn, atan cdbl cint clng cos cosh cot coth csc csch csng
- ' csrlin cvd cvi exp false frac fix instr int len ln log log10 peek
- ' point pos rnd screen sec sech sgn sin sinh sqr, sqrt tan tanh
- ' timer true ubound val
- '
- ' QB64 Numeric functions:
- '
- ' _atan2 _ceil _d2g _d2r _fontwidth _fontheight _g2d _g2r _height
- ' _instrrev _keydown _keyhit _mousebutton _mouseinput _mousex
- ' _mousey _newimage _pi _r2d _r2g _rgb _rgba _rgba32 _rgb32 _round
- ' _width
- '
- ' String Functions:
- '
- ' chr$ command$ date$ environ$ hex$ inkey$ lcase$ left$" lpad$
- ' ltrim$ mid$ mki$ oct$ replace$ right$ rpad$ rtrim$ space$ str$
- ' string$ time$ trim$ ucase$
- '
- ' QB64 string functions:
- '
- ' _clipboard$ _cwd$ _os$ _startdir$ _title$ _trim$
- '
- ' No other numeric data types besides double. Only suffix accepted is $ for strings.
- ' Only up to two dimensional arrays
- ' No subs or functions.
- ' No dim.
- ' Lots of other stuff missing.
- '------------------------------------------------------------------------------------------
- type do_loop_t
- ' do/while/for/if tracking
- ' for arrays: make sure the user specified index is between lo_bnd..hi_bnd inclusive
- ' then, computed index = v.index + user_index - v.lo_bnd
- type names_t
- ' variable names
- ' string and numeric values
- ' string and numeric arrays
- ' used by expression parser
- 'for performance timing
- 'dim shared scantime as double
- 'dim shared starttime as double
- 'dim shared nsyms as long
- '---------------------------------------------------------------------------------------------------
- ' Listed here since I can not remember them:
- ' % = integer (16 bit)
- ' & = long (32 bit)
- ' ! = single (default)
- ' # = double
- ' $ = string
- '---------------------------------------------------------------------------------------------------
- ' Maybe add:
- ' min(x, x1, x2...), max(...), ave(...), sum(...)
- '#define floor(x) ((x*2.0-0.5)shr 1)
- '#define ceil(x) (-((-x*2.0-0.5)shr 1))
- '---------------------------------------------------------------------------------------------------
- call init_scanner
- tracing = false
- stepping = false
- str_st_ndx = 0
- num_st_ndx = 0
- '------------------------------------------------------------------------
- ' main loop
- '------------------------------------------------------------------------
- dim cmd$
- quit = false
- 'starttime = timer
- cmd$ = command$
- _dest 0
- pgm(0) = "run " + cmd$
- call showhelp
- ' show timings
- sub showtime
- 'dim total_time as double
- 'total_time = timer - starttime
- 'print "Total time: "; total_time; " Scan time: "; scantime; " Parse time: "; total_time - scantime; " Symbols: "; nsyms
- 'sleep
- function at_line$
- function rest_of_line$
- sub dump_tables
- print var_names(i).vname, var_names(i).index, var_names(i).lo_bnd, var_names(i).hi_bnd, var_names(i).a_len
- '------------------------------------------------------------------------
- ' command processor
- '------------------------------------------------------------------------
- 'print "docmd"
- errors = false
- restart_loop:
- stackp = 0 ' these were -1 ??? @review
- loopp = 0 ' these were -1 ??? @review
- while_sp = 0
- do_sp = 0
- if_sp = 0
- loop_top:
- ' need to account for:
- ' - assignment
- ' let ...
- ' [str]ident = expression
- ' [str]ident(expression [, expression]) = expression
- ' - labels
- ' ident:
- ' - non-assignment, including labels
- '
- call assignment
- call assignment
- call array_assignment
- call getsym
- call printstmt
- call printstmt
- call getsym
- '------------------------------------------------------------------------
- ' variable storage/retrieval
- '------------------------------------------------------------------------
- ' find position of vname in var_names
- find_vname& = i
- find_vname& = 0
- ' helper function for 2d arrays
- ar_scale = i - (lo - 1)
- ' get the index of "a" in either string_arr_store or numeric_arr_store
- ' pointing to: a(expr [, expr])
- call getsym
- i = find_vname&(ident)
- expect("(")
- index = numeric_expr#
- expect(",")
- index2 = numeric_expr#
- expect(")")
- ' verfiy that the index is within range
- ' compute the actual index
- lo = var_names(i).lo_bnd
- lo2 = var_names(i).lo_bnd2
- x = var_names(i).index + (var_names(i).a_width * (ar_scale(index2, lo2) - 1) + ar_scale(index, lo)) - 1
- x = var_names(i).index + ar_scale(index, lo) - 1
- 'x = var_names(i).index + index - (var_names(i).lo_bnd - 1) - 1
- 'print "index: "; x
- get_array_index& = x
- ' primary: if var does not exist, create it. Return the var store index
- ' sym is the numeric variable name
- ident = sym: ident_type = symtype
- call getsym
- ' see if variable exists
- i = find_vname&(ident)
- getvarindex& = var_names(i).index
- 'if side = right_side then print at_line$; "Reference to unassigned variable: "; ident: errors = true
- ' create a new variable
- num_store_max = num_store_max + 1
- var_names_max = var_names_max + 1
- var_names(var_names_max).vname = ident
- var_names(var_names_max).symtype = ident_type
- var_names(var_names_max).index = num_store_max
- numeric_store(num_store_max) = 0 ' default value
- getvarindex& = num_store_max
- ident = sym: ident_type = symtype
- call getsym
- ' see if variable exists
- i = find_vname&(ident)
- getstrindex& = var_names(i).index
- 'if side = right_side then print at_line$; "Reference to unassigned variable: "; ident: errors = true
- ' create a new variable
- str_store_max = str_store_max + 1
- var_names_max = var_names_max + 1
- var_names(var_names_max).vname = ident
- var_names(var_names_max).symtype = ident_type
- var_names(var_names_max).index = str_store_max
- string_store(str_store_max) = "" ' default value
- getstrindex& = str_store_max
- ' a(expr)
- ' when called, sym pointing at the ident
- function get_numeric_array_value#
- ident = sym: ident_type = symtype
- x = get_array_index&(ident)
- n = numeric_arr_store(x)
- get_numeric_array_value# = n
- ' a(expr)
- ' when called, sym pointing at the ident
- function get_string_array_value$
- ident = sym: ident_type = symtype
- x = get_array_index&(ident)
- s = string_arr_store(x)
- get_string_array_value$ = s
- sub stridstmt
- vname = sym
- 'print "stridstmt"
- i = getstrindex&(left_side)
- expect("=")
- string_store(i) = strexpression$
- sub idstmt
- vname = sym
- i = getvarindex&(left_side)
- expect("=")
- numeric_store(i) = numeric_expr#
- ' ident = expression
- sub assignment
- call idstmt
- call stridstmt
- ' ident(expression [, expression]) = expression
- sub array_assignment
- ident = sym: ident_type = symtype
- x = get_array_index&(ident)
- expect("=")
- s = strexpression$
- 'assign string
- string_arr_store(x) = s
- n = numeric_expr#
- 'assign number
- numeric_arr_store(x) = n
- '------------------------------------------------------------------------
- ' statement parsing
- '------------------------------------------------------------------------
- sub showhelp
- print "bye or quit -- exit"
- print "help -- show this screen"
- print "clear -- clear variables"
- print "edit -- edit current program"
- print "list -- show source"
- print "list vars -- show variables"
- print "load -- load program from disk"
- print "new -- clear program in memory"
- print "reload -- reload program from disk"
- print "run -- run program in memory"
- print "save -- save program to disk"
- print ""
- print "cls -- clear screen"
- print "tron -- tracing on"
- print "troff -- tracing off"
- print "ston -- stepping on"
- print "stoff -- stepping off"
- filespec = strexpression$
- filespec = sym ' gettoeol destroys sym
- filespec = filespec + gettoeol$
- print prompt; ": ";
- getfn$ = filespec
- sub clearvars
- string_store(i) = ""
- numeric_store(i) = 0
- sub initvars
- clearvars
- var_names(i).vname = ""
- var_names(i).index = 0
- str_store_max = 0: num_store_max = 0: var_names_max = 0
- initvars
- clearprog
- n = 0
- 'if pgm(0) <> "" then
- n = the_num + 1
- n = n + 1
- pgm(n) = pgm(0)
- 'end if
- close #1
- curline = 0
- sub editstmt
- sub runprog
- sub saveprog
- filespec = getfn$("Save as")
- print at_line$; "*** error: you don't have permission to write to that file."
- close #1
- sub liststmt
- sub chdircmd
- chdir strexpression$
- ' CIRCLE [STEP] (x!,y!),radius![,[color%] [,[start!] [,[end!] [,aspect!]]]]
- sub circlestmt
- expect("(")
- x = numeric_expr#
- expect(",")
- y = numeric_expr#
- expect(")")
- expect(",")
- radius = numeric_expr#
- '[,[color%] [,[start!] [,[end!] [,aspect!]]]]
- elipse = numeric_expr#
- arcend = numeric_expr#
- elipse = numeric_expr#
- arcbeg = numeric_expr#
- elipse = numeric_expr#
- arcend = numeric_expr#
- elipse = numeric_expr#
- ' [,[start!] [,[end!] [,aspect!]]]]
- clr = numeric_expr#
- elipse = numeric_expr#
- arcend = numeric_expr#
- elipse = numeric_expr#
- arcbeg = numeric_expr#
- elipse = numeric_expr#
- arcend = numeric_expr#
- elipse = numeric_expr#
- ' color [fore] [,back]
- sub colorstmt
- back = numeric_expr#
- color , back
- fore = numeric_expr#
- back = numeric_expr#
- color fore, back
- color fore
- lo = numeric_expr#
- hi = numeric_expr#
- hi = lo
- lo = 0
- ' dim ident(numeric expression [to numeric expression]) {, ident(numeric expression [to numeric expression])}
- sub dimstmt
- ident = sym
- ident_type = symtype
- call getsym ' skip array name
- expect("(")
- lo2 = 0: hi2 = 0: multi = false
- expect(")")
- ' see if it already exists
- i = find_vname&(ident)
- ' add it
- a_len = hi - lo + 1
- a_width = a_len
- a_len = a_len * (hi2 - lo2 + 1)
- var_names_max = var_names_max + 1
- var_names(var_names_max).vname = ident
- var_names(var_names_max).symtype = ident_type
- var_names(var_names_max).lo_bnd = lo
- var_names(var_names_max).hi_bnd = hi
- var_names(var_names_max).lo_bnd2 = lo2
- var_names(var_names_max).hi_bnd2 = hi2
- var_names(var_names_max).multi = multi
- var_names(var_names_max).a_len = a_len
- var_names(var_names_max).a_width = a_width
- index = str_arr_stor_max + 1
- str_arr_stor_max = str_arr_stor_max + a_len
- index = num_arr_stor_max + 1
- num_arr_stor_max = num_arr_stor_max + a_len
- var_names(var_names_max).index = index
- call getsym
- ' const id[$] = number|string {, const id[$] = number|string}
- sub conststmt
- i = find_vname&(sym)
- call assignment
- var_names(var_names_max).is_const = true
- sub drawstmt
- s = strexpression$
- draw s
- sub environstmt
- environ strexpression$
- ' need to account for loop [until|while expr] and next [i]
- sub exitstmt
- call getsym
- while_sp = while_sp - 1
- call getsym
- call getsym
- do_sp = do_sp - 1
- call getsym
- call getsym ' skip until\while
- ' somehow skip over the until\while expression
- call getsym
- call getsym
- loopp = loopp - 1
- call getsym
- ' for xvar = -1.5 to 1.5 step .01
- sub forstmt
- xvar = getvarindex&(left_side) ' get position of "i"
- expect("=")
- numeric_store(xvar) = numeric_expr#
- loopp = loopp + 1
- loopvars(loopp) = xvar
- looplines(loopp) = curline
- expect("to")
- loopmax(loopp) = numeric_expr#
- loopoff(loopp) = textp
- ' finds target, using current sym
- function get_target&
- get_target = numeric_expr#
- lbl = sym
- get_target& = i
- get_target& = 0
- sub gosubstmt
- target = get_target&
- validlinenum(target)
- stackp = stackp + 1
- gosubstack(stackp) = curline
- ' 26 May 2021 was just textp
- gosuboffstack(stackp) = textp - 1
- 'print "textp:"; textp; "=>"; pgm$(curline)
- 'if sym = ":" then gosuboffstack(stackp) = textp
- sub gotostmt
- target = get_target&
- gotoline(target)
- ' single line if: if expr then if expr then if expr then s else s else s else s
- sub ifstmt
- level = 0
- begin:
- level = level + 1
- cond = numeric_expr#
- b = accept&("then")
- '*** multiline if processing ***
- '*** singleline if processing ***
- goto begin
- call find_matching_sline_if
- ' if else found, pick up there, otherwise skip rest of stmt
- if_sp = if_sp + 1
- if_stack(if_sp) = curline
- 'print at_line$; "if after inc: if_sp: "; if_sp, pgm(curline)
- 'need to find the next corresponding 'elseif' or 'else' or 'endif'
- restart:
- ' on the "if" or "elseif" line, so skip it
- s = find_matching_else$ 'either elseif, else or endif
- 'print at_line$; "found: "; sym
- 'print sym; ": "; mid$(thelin, textp)
- call getsym 'skip "elseif"
- cond = numeric_expr#
- b = accept&("then")
- 'print at_line$; "elseif evaluated to: "; cond
- goto restart
- call getsym ' skip the else, so docmd goes to next line
- call endifstmt
- ' called from docmd()
- sub elseifstmt
- 'scan until matching endif
- ' but first, allow more "elseif"'s
- s = find_matching_else$
- ' allow an "else"
- s = find_matching_else$
- ' finally, need an "endif"
- ' pop the if stack
- if_sp = if_sp - 1
- call getsym ' skip "endif"
- ' done
- ' called from docmd()
- sub elsestmt
- 'print at_line$; "else begin: if_sp: "; if_sp, pgm(curline)
- 'part of a single-line if?
- 'if not "else", then single-line if
- ' looks like multiline if - but have we seen the start of it?
- 'scan until matching endif
- call getsym 'skip the "endif"
- 'pop the if stack
- if_sp = if_sp - 1
- 'print at_line$; "else end: if_sp: "; if_sp, pgm(curline)
- ' called from docmd()
- sub endifstmt
- if_sp = if_sp - 1
- 'print at_line$; "endif: if_sp: "; if_sp, pgm(curline)
- call getsym 'skip "endif"
- ' input [;] ["prompt" ;|,] variablelist
- sub inputsetup
- call getsym
- print "? ";
- expect(",")
- ' input [;] ["prompt" ;|,] variablelist
- sub inputstmt
- inputsetup
- ident = sym: ident_type = symtype
- i = find_vname&(ident)
- x = get_array_index&(ident)
- 'assign string
- string_arr_store(x) = st
- 'assign number
- numeric_arr_store(x) = n
- i = getstrindex&(left_side)
- string_store(i) = st
- i = getvarindex&(left_side)
- numeric_store(i) = n
- ' line input [;] ["prompt";] variable$
- sub lineinputstmt
- inputsetup
- ident = sym: ident_type = symtype
- i = find_vname&(ident)
- x = get_array_index&(ident)
- 'assign string
- string_arr_store(x) = st
- i = getstrindex&(left_side)
- string_store(i) = st
- ' line [[step](x1!,y1!)]-[step](x2!,y2!) [,[color%] [,[b | bf] [,style%]]]
- ' ??? step is not currently supported
- sub linestmt
- step1 = false: step2 = false
- expect("(")
- x1 = numeric_expr#
- expect(",")
- y1 = numeric_expr#
- expect(")")
- expect("-")
- expect("(")
- x2 = numeric_expr#
- expect(",")
- y2 = numeric_expr#
- expect(")")
- ' so far we have: line(x, y)-(x2, y2)
- '[,[color%] [,[b | bf] [,style%]]]
- ' only acceptable value is a ","
- '1) ,c
- '2) ,c,b
- '3) ,c,b,s
- '4) ,c,,s
- '5) ,,b
- '6) ,,b,s
- '7) ,,,s
- 'must have s (7)
- 'must have b
- 'must have s (6)
- '(5)
- call getsym ' skip "B"
- 'must have c
- clr = numeric_expr#
- 'must have s (4)
- 'must have b
- 'must have s (3)
- '(2)
- call getsym ' skip "B"
- '(1)
- sub locatestmt
- col = numeric_expr#
- locate , col
- row = numeric_expr#
- col = numeric_expr#
- locate row, col
- locate row
- ' mid$(s, i, n)
- sub midstmt
- expect("(")
- xvar = getstrindex&(left_side)
- expect(",")
- start = numeric_expr#
- expect(")")
- expect("=")
- sub nextstmt
- ' increment the current "i"
- numeric_store(loopvars(loopp)) = numeric_store(loopvars(loopp)) + loopstep(loopp)
- ' see if "for" should continue
- cont = false
- cont = true
- cont = true
- loopp = loopp - 1
- ' PAINT [STEP] (column%, row%), fillColor[, borderColor%]
- sub paintstmt
- expect("(")
- x = numeric_expr#
- expect(",")
- y = numeric_expr#
- expect(")")
- f = numeric_expr#
- ' palette [attribute%,color&]
- sub palettestmt
- a = numeric_expr#
- expect(",")
- c = numeric_expr#
- palette a, c
- sub printstmt
- printed = false
- printed = true
- val_type = any_expr&(0)
- print pop_str$,
- print pop_num#,
- print pop_str$;
- print pop_num#;
- print pop_str$
- print pop_num#
- ' preset (column, row)
- ' preset [step] (x!,y!) [,color%]
- sub presetstmt
- expect("(")
- x = numeric_expr#
- expect(",")
- y = numeric_expr#
- expect(")")
- ' pset (column, row)
- ' pset [step] (x!,y!) [,color%]
- ' PSET [STEP] (x!,y!) [,color%]
- sub psetstmt
- expect("(")
- x = numeric_expr#
- expect(",")
- y = numeric_expr#
- expect(")")
- clr = numeric_expr#
- sub randomizer
- sub returnstmt
- lin = gosubstack(stackp)
- offs = gosuboffstack(stackp)
- 'print "["; curline; "] "; "returning to: "; lin; ": "; offs; " while_sp: "; while_sp
- stackp = stackp - 1
- ' SCREEN mode% [,[colorswitch%] [,[activepage%] [,visualpage%]]]
- sub screenstmt
- screen numeric_expr#
- ' shell [string]
- sub shellstmt
- s = ""
- s = s + the_ch
- call getch
- shell s
- 'print "shell: "; s
- call skiptoeol
- ' sleep [seconds]
- sub sleepstmt
- ' swap v1, v2
- sub swapstmt
- sym1 = sym
- symtype1 = symtype
- i1 = getvarindex&(left_side)
- i1 = getstrindex&(left_side)
- expect(",")
- sym2 = sym
- symtype2 = symtype
- i2 = getvarindex&(left_side)
- i2 = getstrindex&(left_side)
- ' VIEW [[SCREEN] (x1!,y1!)-(x2!,y2!) [,[color%] [,border%]]]
- sub viewstmt
- expect("(")
- x1 = numeric_expr#
- expect(",")
- y1 = numeric_expr#
- expect(")")
- expect("-")
- expect("(")
- x2 = numeric_expr#
- expect(",")
- y2 = numeric_expr#
- expect(")")
- border = numeric_expr#
- clr = numeric_expr#
- border = numeric_expr#
- while_sp = while_sp + 1
- while_line(while_sp) = curline
- while_off(while_sp) = textp
- 'print "["; curline; "] "; "*while:sym:";sym; " textp:";textp; " =>";mid$(pgm(curline), textp); " while_sp: "; while_sp
- while_sp = while_sp - 1
- 'print "["; curline; "] "; "*wend bool_expr is 0!"; " while_sp: "; while_sp
- call getsym
- sub wendstmt
- whilestmt(false)
- ' do [(while|until) expr][:]
- do_sp = do_sp + 1
- do_loop(do_sp).lline = curline
- do_loop(do_sp).loff = textp
- 'print "*do:"; "sym:"; sym; " textp:";textp; "=>";mid$(pgm(curline), textp - len(sym))
- call getsym
- do_sp = do_sp - 1
- call getsym
- call getsym
- do_sp = do_sp - 1
- call getsym
- ' loop [(while|until) expr]
- sub loopstmt
- call getsym
- do_sp = do_sp - 1
- call getsym
- do_sp = do_sp - 1
- 'print "loop line:"; curline; "off:"; do_loop(do_sp).loff; "==>"; pgm(curline)
- dostmt(false)
- ' width , height
- ' width width
- ' width width, height
- sub widthstmt
- width , numeric_expr#
- w = numeric_expr#
- width w , numeric_expr#
- width w
- ' window [ [ screen] (x1!, y1!) - (x2!, y2!)]
- sub windowstmt
- expect("(")
- x1 = numeric_expr#
- expect(",")
- y1 = numeric_expr#
- expect(")")
- expect("-")
- expect("(")
- x2 = numeric_expr#
- expect(",")
- y2 = numeric_expr#
- expect(")")
- '------------------------------------------------------------------------
- ' Various helper routines
- '------------------------------------------------------------------------
- sub skip_exit
- call getsym
- call getsym
- level = 1
- more = true
- have_sym = false
- endif_count = 0: wend_count = 0: next_count = 0: loop_count = 0
- call skip_exit
- call getsym
- have_sym = false
- 'print at_line$; "matching, level"; level; "sym=>"; sym
- 'if isalpha&(mid$(sym, 1, 1)) then print "fm: level: sym: "; level; ": '"; sym; "' "; mid$(thelin, textp, 40)
- call getsym
- call getsym ' skip the "then"
- ' if nothing past "then", it is a multiline if
- case "do"
- loop_count = loop_count - 1
- case "loop"
- loop_count = loop_count + 1
- have_sym = true
- ' find matching elseif/else/endif
- function find_matching_else$
- find_matching_else$ = ""
- level = 0
- 'print "find_matching_else: "; curline; " sym: "; sym; " level: "; level; "textp: "; textp; " line:"; thelin
- level = level + 1
- level = level - 1
- sub find_matching_sline_if
- level = 1
- 'print "find_matching_sline_if level: "; level; " sym: "; sym
- level = level + 1
- level = level - 1
- call getsym
- function is_multi_line_if&
- is_multi_line_if& = false
- ' is it single or multi line "if" - ignore single line if's
- call getsym
- call getsym
- ' multi line "if"
- is_multi_line_if& = true
- accept& = false
- function is_stmt_end&
- function storeline&
- 'print "storeline"
- storeline& = false
- storeline& = true
- sub clearprog
- pgm(i) = ""
- validlinenum(target)
- '------------------------------------------------------------------------
- '------[QB64 specific functions]-----------------------------------------
- '------------------------------------------------------------------------
- ' _atan2(y, x)
- function atan2fun#
- expect("(")
- y = numeric_expr#
- expect(",")
- x = numeric_expr#
- expect(")")
- sub filesstmt
- s = ""
- files s
- ' freeimage [image]&
- sub freeimage
- _freeimage numeric_expr#
- sub limitstmt
- _limit numeric_expr#
- ' ([start], haystack, needle)
- function instrrevfun&
- expect("(")
- i = 0
- i = numeric_expr#
- expect(",")
- haystack = strexpression$
- expect(",")
- needle = strexpression$
- expect(")")
- sub titlestmt
- _title strexpression$
- ' _newimage(width&, height&[, {0|1|2|7|8|9|10|11|12|13|256|32}])
- function newimagefun&
- expect("(")
- w = numeric_expr#
- expect(",")
- h = numeric_expr#
- mode = numeric_expr#
- expect(")")
- 'colorIndex~& = _RGB(red&, green&, blue&[, imageHandle&])
- function rgbfun~&
- expect("(")
- r = numeric_expr#
- expect(",")
- g = numeric_expr#
- expect(",")
- b = numeric_expr#
- h = numeric_expr#
- expect(")")
- 'color32value~& = _RGB32(red&, green&, blue&, alpha&)
- 'color32value~& = _RGB32(red&, green&, blue&)
- 'color32value~& = _RGB32(intensity&, alpha&)
- 'color32value~& = _RGB32(intensity&)
- function rgb32fun~&
- expect("(")
- r = numeric_expr#
- expect(",")
- g = numeric_expr#
- expect(",")
- b = numeric_expr#
- expect(",")
- a = numeric_expr#
- expect(")")
- '_RGBA(red&, green&, blue&, alpha&[, imageHandle&])
- function rgbafun~&
- expect("(")
- r = numeric_expr#
- expect(",")
- g = numeric_expr#
- expect(",")
- b = numeric_expr#
- expect(",")
- a = numeric_expr#
- h = numeric_expr#
- expect(")")
- 'color32value~& = _RGBA32(red&, green&, blue&, alpha&)
- function rgba32fun~&
- expect("(")
- r = numeric_expr#
- expect(",")
- g = numeric_expr#
- expect(",")
- b = numeric_expr#
- expect(",")
- a = numeric_expr#
- expect(")")
- '_PRINTSTRING(column, row), textExpression$[, imageHandle&]
- sub printstringstmt
- expect("(")
- c = numeric_expr#
- expect(",")
- r = numeric_expr#
- expect(")")
- expect(",")
- ex = strexpression$
- ' _SCREENMOVE {column&, row&|_MIDDLE}
- sub screenmovestmt
- 'print "screenmovestmt:"; sym
- call getsym 'and skip over _middle
- c = numeric_expr#
- expect(",")
- r = numeric_expr#
- _screenmove c, r
- ' sound frequence, duration
- sub soundstmt
- f = numeric_expr#
- expect(",")
- d = numeric_expr#
- sound f, d
- '------------------------------------------------------------------------
- ' various functions called from primary
- '------------------------------------------------------------------------
- sinh = (e ^ z - e ^ (-z)) / 2
- tanh = (e ^ (2 * z) - 1) / (e ^ (2 * z) + 1)
- asin2 = -halfpi
- asin2 = halfpi
- cosh = (e ^ z + e ^ (-z)) / 2
- shlf# = x
- shrf# = x
- ' ([start,] haystack, needle)
- function instrfun&
- expect("(")
- i = 1
- i = numeric_expr#
- expect(",")
- haystack = strexpression$
- expect(",")
- needle = strexpression$
- expect(")")
- ' mid$(s$, start [, end])
- function midfun$
- expect("(")
- i = strexpression$
- expect(",")
- x = numeric_expr#
- y = numeric_expr#
- expect(")")
- ' lpad$(s$, padded_len [, pad_string$])
- function lpadfun$
- expect("(")
- s = strexpression$
- expect(",")
- padded_len = numeric_expr#
- pad_string = " "
- pad_string = strexpression$
- expect(")")
- lpadfun$ = s
- ' result = peek(string)
- 'result = Point( coord_x, coord_y [,buffer] )
- 'result = Point( function_index )
- function pointfun#
- expect("(")
- x = numeric_expr#
- expect(")")
- function posfun#
- expect("(")
- expect(")")
- ' rpad$(s$, padded_len [, pad_string$])
- function rpadfun$
- expect("(")
- s = strexpression$
- expect(",")
- padded_len = numeric_expr#
- pad_string = " "
- pad_string = strexpression$
- expect(")")
- rpadfun$ = s
- ' replace$(haystack$, needle$ [, newst$])
- function replacefun$
- expect("(")
- haystack = strexpression$
- expect(",")
- needle = strexpression$
- newst = ""
- newst = strexpression$
- expect(")")
- start = 1
- replacefun$ = haystack
- ' ubound(array-name)
- function uboundfun&
- expect("(")
- i = find_vname(sym)
- uboundfun& = var_names(i).hi_bnd
- call getsym
- expect(")")
- ' screen(row, col)
- function screenfun&
- expect("(")
- row = numeric_expr#
- expect(",")
- col = numeric_expr#
- expect(")")
- '------------------------------------------------------------------------
- ' expression parser
- '------------------------------------------------------------------------
- function strfactor$
- case "left$"
- call getsym:
- expect("(")
- s = strexpression$
- expect(",")
- x = numeric_expr#
- expect(")")
- case "right$"
- call getsym
- expect("(")
- s = strexpression$
- expect(",")
- x = numeric_expr#
- expect(")")
- case "space$"
- call getsym
- expect("(")
- expect(")")
- case "string$"
- call getsym ' string$(n [, strexpr])
- expect("(")
- x = numeric_expr#
- expect(",")
- expect(")")
- call getsym
- strfactor$ = get_string_array_value$
- strfactor$ = string_store(getstrindex&(right_side))
- print at_line$; "In strfactor, expecting an operand, found: "; sym; " symtype is: "; symtype: errors = true
- call getsym
- function primary#
- case "log"
- call getsym:
- expect("(")
- primary# = i
- expect(")")
- case "rnd"
- call getsym:
- expect(")")
- primary# = rnd
- primary# = the_num
- call getsym
- primary# = get_numeric_array_value#
- primary# = numeric_store(getvarindex&(right_side))
- print at_line$; "In primary, expecting an operand, found: "; sym; " symtype is: "; symtype: errors = true
- call getsym
- function strexpression$
- s = strfactor$
- s = s + strfactor$
- strexpression$ = s
- '-------------------------------------------------------------------------------------------------
- str_st_ndx = str_st_ndx + 1
- str_stack(str_st_ndx) = s
- num_st_ndx = num_st_ndx + 1
- num_stack(num_st_ndx) = n
- function pop_str$
- pop_str = str_stack(str_st_ndx)
- str_st_ndx = str_st_ndx - 1
- function pop_num#
- pop_num = num_stack(num_st_ndx)
- num_st_ndx = num_st_ndx - 1
- s2 = pop_str$
- s = pop_str$
- print at_line$; "In expr, expecting a string operator, found: "; op; " symtype is: "; symtype: errors = true
- call getsym
- push_num(n)
- evalstrexpr& = tynum
- n2 = pop_num#
- n = pop_num#
- print at_line$; "In expr, expecting a numeric operator, found: "; op; " symtype is: "; symtype: errors = true
- call getsym
- push_num(n)
- evalnumericexpr& = tynum
- ' return the type of expression, either string or numeric; result is on the stack
- ' we need to decide which primary to call - numeric or string
- ' leading parens don't tell us which primary, so just do recursive call
- left_type = any_expr&(0)
- expect(")")
- push_str(strexpression$)
- left_type = tystring
- push_num(primary#)
- left_type = tynum
- print at_line$; "In expr, expecting an expr, found: "; sym; " symtype is: "; symtype; " - near column: "; textp
- errors = true
- call getsym
- do ' while binary operator and precedence(sym) >= p
- prec = binary_prec&(sym)
- op = sym
- call getsym
- ' all operators are left associative in qbasic
- prec = prec + 1
- right_type = any_expr&(prec)
- left_type = evalstrexpr&(op)
- left_type = evalnumericexpr&(op)
- print at_line$; "type missmatch in expr - left_type:"; left_type; " right_type:"; right_type: errors = true
- call getsym
- any_expr& = left_type
- numeric_expr2# = pop_num#
- ' process and return a numeric expression
- function numeric_expr#
- numeric_expr# = numeric_expr2#(0)
- function bool_expr&
- bool_expr& = (numeric_expr# <> 0)
- '------------------------------------------------------------------------
- ' scanner
- '------------------------------------------------------------------------
- sub init_scanner
- ctype_arr(i) = ct_unknown
- ' alpha
- ctype_arr(i) = ct_alpha
- ctype_arr(i) = ct_alpha
- ' num
- ctype_arr(i) = ct_digit
- function peek_ch$
- ' other code relies on textp always being incremented; so do it even on EOL
- sub getch
- the_ch = ""
- textp = textp + 1
- 'print "getch: textp: "; textp; " the_ch: "; the_ch; " thelin: "; thelin
- sub readident
- sym = ""
- getch
- symtype = tyident
- ' see if we have "end if", if so, convert to "endif"
- case " "
- sym = "endif"
- getch ' skip " "
- getch ' skip "i"
- getch ' skip "f"
- sub readnumber
- sym = ""
- sym = sym + the_ch
- getch
- sym = sym + the_ch
- getch
- sym = sym + the_ch
- getch
- sym = sym + "e"
- getch
- sym = sym + the_ch
- getch
- symtype = tynum
- ' on entry pointing to 'h'
- sub readhex
- sym = "&h"
- getch ' skip the 'h'
- sym = sym + the_ch
- getch
- symtype = tynum
- sub readstr
- getch
- sym = sym + the_ch
- getch
- getch
- symtype = tystring
- sub skiptoeol
- the_ch = ""
- sym = ""
- symtype = tyunknown
- function gettoeol$
- s = ""
- s = s + the_ch
- getch
- call getsym
- gettoeol$ = s
- ' symtype: unknown, tystring, tynum, tyident, tystrident
- ' sym: the symbol just read, above, and punctuation
- sub getsym
- 'print "in getsym"
- 'dim ttt as double
- 'ttt = timer
- 'nsyms = nsyms + 1
- sym = ""
- symtype = tyunknown
- ' skip white space
- sym = the_ch
- case ct_punc1: getch 'punctuation
- case ct_alpha: readident 'identifier
- case ct_dquote: readstr 'double quote
- case ct_squote: skiptoeol 'comment
- case ct_lt
- getch
- case ct_gt
- getch
- case ct_amp:
- getch
- readhex
- getch
- 'scantime = scantime + (timer - ttt)
- 'print "initgetsym"
- curline = n
- textp = col
- thelin = pgm(curline)
- the_ch = " "
- call getsym
Sample programs:
Fireworks by Ken G.
Code: QB64: [Select]
- '_FULLSCREEN
- PRINT " FIREWORKS"
- PRINT " By Ken G."
- PRINT " The show lasts around 3 minutes total."
- PRINT " Or press Esc to end anytime."
- PRINT " There's some surprises at the end."
- 'Start Loop Here
- go:
- a$ = INKEY$
- _DELAY .1
- _DELAY .1
- _DELAY .1
- explosion:
- dd = dd + 1
- dxx = dxx + dxx / 4
- dxx2 = dxx2 + dxx2 / 4
- dxx3 = dxx3 + dxx3 / 4
- dxx4 = dxx4 + dxx4 / 4
- dxx5 = dxx5 + dxx5 / 4
- dxx6 = dxx6 + dxx6 / 4
- dyy = dyy + dyy / 4
- dyy2 = dyy2 + dyy2 / 4
- dyy3 = dyy3 + dyy3 / 4
- dyy4 = dyy4 + dyy4 / 4
- dyy5 = dyy5 + dyy5 / 4
- dyy6 = dyy6 + dyy6 / 4
- dxx7 = dxx7 + dxx7 / 4
- dxx8 = dxx8 + dxx8 / 4
- dxx9 = dxx9 + dxx9 / 4
- dxx10 = dxx10 + dxx10 / 4
- dxx11 = dxx11 + dxx11 / 4
- dxx12 = dxx12 + dxx12 / 4
- dyy7 = dyy7 + dyy7 / 4
- dyy8 = dyy8 + dyy8 / 4
- dyy9 = dyy9 + dyy9 / 4
- dyy10 = dyy10 + dyy10 / 4
- dyy11 = dyy11 + dyy11 / 4
- dyy12 = dyy12 + dyy12 / 4
- dxx13 = dxx13 + dxx13 / 4
- dxx14 = dxx14 + dxx14 / 4
- dxx15 = dxx15 + dxx15 / 4
- dxx16 = dxx16 + dxx16 / 4
- dxx17 = dxx17 + dxx17 / 4
- dxx18 = dxx18 + dxx18 / 4
- dyy13 = dyy13 + dyy13 / 4
- dyy14 = dyy14 + dyy14 / 4
- dyy15 = dyy15 + dyy15 / 4
- dyy16 = dyy16 + dyy16 / 4
- dyy17 = dyy17 + dyy17 / 4
- dyy18 = dyy18 + dyy18 / 4
- dxx19 = dxx19 + dxx19 / 4
- dxx20 = dxx20 + dxx20 / 4
- dxx21 = dxx21 + dxx21 / 4
- dxx22 = dxx22 + dxx22 / 4
- dxx23 = dxx23 + dxx23 / 4
- dxx24 = dxx24 + dxx24 / 4
- dyy19 = dyy19 + dyy19 / 4
- dyy20 = dyy20 + dyy20 / 4
- dyy21 = dyy21 + dyy21 / 4
- dyy22 = dyy22 + dyy22 / 4
- dyy23 = dyy23 + dyy23 / 4
- dyy24 = dyy24 + dyy24 / 4
- NEXT c
- _DELAY .05
- NEXT cc
- GOTO explosion:
- goingback:
- dd = 0
- ee = ee + 1
- tt = tt + 1
- delay:
- _DELAY tm
- GOTO go:
- 'FINAL BOOM!
- finalboom:
- boom:
- _DELAY .1
- _DELAY .1
- _DELAY .1
- aa$ = INKEY$
- _DELAY .02
- NEXT sz
- _DELAY .02
- NEXT sz
- f = f + 1
- GOTO finalboom:
- face:
- 'Make the face.
- 'Explode the face!
- a2$ = INKEY$
- _LIMIT 5000
- _DELAY .002
- NEXT b
- _DELAY .002
- NEXT b
- NEXT yt
- NEXT xt
Snake by Johnno and BPlus
Code: QB64: [Select]
- 'from Johnno's mod:
- ' 2019-02-27 B+ mod more snake colors, green garden, fruit white
- ' put back in snake death when reverses itself, ie the head enters the body
- ' put back in snake dies when head hit another segment of body
- ' put back in outer game restart loop after delay
- ' added worm holes at corners!!!!!!!!!!!!!!!!
- ' ADDED escape clause or q to quit game!!!!!!!!!!!!
- SCREEN 12
- WHILE 1
- ' initialize game
- snake(i, j) = 0
- x = 15: y = 3: died = 0: score = 0
- '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> dont put fruit at snake position
- 'fx = 3: fy = 2 '>>>>>>>>>>> test corner boundry, OK
- 'fx = 77: fy = 28 '>>>>>>>>>>> test corner boundry, OK
- yd = 1: xd = 0: snakelength = 1
- '>>>>>>>>>>>>>>>>>>>>> The snake really does have to die if it's headed back into it's body
- keypress$ = INKEY$
- END '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> here is quit or escape clause!!!!
- ' update snake position
- x = x + xd
- y = y + yd
- ' did snake just die by hitting wall /boundry?
- ' did snake run into itself >>> DIES!
- ' did snake eat fruit
- score = score + 1 ' <------- Increase score
- snakelength = snakelength + 1
- 'did snake disappear into worm holes at corners >>>>>>>>>>>>>>>>>>>> B+ mod
- x = 37: y = 13 'move snake to screen center, same direction as in corner
- '>>>>>>>>>> Now for the drawing!!!!!!!!!!!!!!!!!!!
- 'show garden >>>> use garden color as back color for small boxes and snake head
- 'show fruit
- 'show snake
- snake(1, 0) = y: snake(1, 1) = x
- snake(sn + 1, 0) = snake(sn, 0): snake(sn + 1, 1) = snake(sn, 1)
- _DELAY 3
Laser Show by Sierra Ken:
Code: QB64: [Select]
- _TITLE "Laser Show - by SierraKen - Press Esc to end."
- c1 = 255
- c2 = 0
- c3 = 0
- c4 = 255
- c5 = 0
- c6 = 0
- c7 = 255
- c8 = 0
- c9 = 0
- c10 = 255
- c11 = 0
- c12 = 0
- _LIMIT 10
- a$ = INKEY$
- c1 = 255: c2 = 0: c3 = 0
- c1 = 0: c2 = 255: c3 = 0
- c1 = 0: c2 = 0: c3 = 255
- c1 = 0: c2 = 255: c3 = 255
- c1 = 255: c2 = 255: c3 = 0
- c1 = 255: c2 = 0: c3 = 255
- c1 = 255: c2 = 255: c3 = 255
- oldx = x
- oldy = y
- c4 = 255: c5 = 0: c6 = 0
- c4 = 0: c5 = 255: c6 = 0
- c4 = 0: c5 = 0: c6 = 255
- c4 = 0: c5 = 255: c6 = 255
- c4 = 255: c5 = 255: c6 = 0
- c4 = 255: c5 = 0: c6 = 255
- c4 = 255: c5 = 255: c6 = 255
- oldx2 = x2
- oldy2 = y2
- c7 = 255: c8 = 0: c9 = 0
- c7 = 0: c8 = 255: c9 = 0
- c7 = 0: c8 = 0: c9 = 255
- c7 = 0: c8 = 255: c9 = 255
- c7 = 255: c8 = 255: c9 = 0
- c7 = 255: c8 = 0: c9 = 255
- c7 = 255: c8 = 255: c9 = 255
- oldx3 = x3
- oldy3 = y3
- c10 = 255: c11 = 0: c12 = 0
- c10 = 0: c11 = 255: c12 = 0
- c10 = 0: c11 = 0: c12 = 255
- c10 = 0: c11 = 255: c12 = 255
- c10 = 255: c11 = 255: c12 = 0
- c10 = 255: c11 = 0: c12 = 255
- c10 = 255: c11 = 255: c12 = 255
- oldx4 = x4
- oldy4 = y4
Matrix by unknown:
Code: QB64: [Select]
- '_LIMIT 15
A Tiny Basic interpreter - very limited
Code: QB64: [Select]
- 'Circa Feb 2007. Tiny Basic interpreter. No subs or functions.
- 'By Ed Davis
- 'ch$
- 'thelin$
- 'tok$
- ' curline
- ' i
- ' n1
- ' n
- ' num
- ' printnl
- ' sp
- ' textp
- ' var
- mainloop:
- num = 0
- gosub initgetsym
- gosub validlinenum
- gosub docmd
- docmd:
- num = curline + 1
- gosub initgetsym
- gosub clearvars
- pgm$(i) = ""
- next i
- gosub clearvars
- num = 1
- gosub initgetsym
- goto docmd
- gosub idstmt
- goto docmd
- printstmt:
- printnl = 1
- gosub getsym
- printnl = 1
- gosub getsym
- gosub expression
- print n;
- gosub getsym
- printnl = 0
- inputstmt:
- gosub getsym
- gosub getsym
- gosub getsym
- print "expecting ','"
- print "? ";
- gosub getvarindex
- var = n
- gosub getsym
- idstmt:
- gosub getvarindex
- var = n
- gosub getsym
- gosub getsym
- gosub expression
- vars(var) = n
- liststmt:
- gosub getsym
- next i
- gotostmt:
- gosub getsym
- gosub gotoline
- print "Line number must follow goto"
- ifstmt:
- gosub getsym
- gosub expression
- gosub gotoline
- gotoline:
- gosub validlinenum
- gosub initgetsym
- validlinenum:
- print "Line number out of range"
- clearvars:
- vars(i) = 0
- next i
- expression:
- sp = 0
- gosub relexpr
- gosub pop
- relexpr:
- gosub addexpr
- rel_oper$ = tok$
- gosub relexprhlp
- gosub push
- relexprhlp:
- gosub getsym
- gosub addexpr
- gosub pop
- n1 = n
- gosub pop
- addexpr:
- gosub term
- add_oper$ = tok$
- gosub addexprhlp
- gosub push
- addexprhlp:
- gosub getsym
- gosub term
- gosub pop
- n1 = n
- gosub pop
- term:
- gosub factor
- mul_oper$ = tok$
- gosub termhlp
- gosub push
- termhlp:
- gosub getsym
- gosub factor
- gosub pop
- n1 = n
- gosub pop
- factor:
- gosub getsym
- gosub factor
- gosub pop
- n = -n
- gosub push
- gosub getsym
- gosub expression
- n = num
- gosub push
- gosub getsym
- gosub getvarindex
- n = vars(n)
- gosub push
- gosub getsym
- getvarindex:
- print "Not a variable"
- push:
- sp = sp + 1
- stack(sp) = n
- pop:
- n = stack(sp)
- sp = sp - 1
- initgetsym:
- curline = num
- textp = 1
- thelin$ = pgm$(curline)
- ch$ = " "
- gosub getsym
- skiptoeol:
- gosub getch
- gosub getsym
- getsym:
- tok$ = ""
- gosub getch
- tok$ = ch$
- gosub getch
- tok$ = tok$ + ch$
- gosub getch
- gosub getch
- print "What->"; ch$
- readstr:
- gosub getch
- tok$ = tok$ + ch$
- gosub getch
- gosub getch
- readint:
- tok$ = ""
- tok$ = tok$ + ch$
- gosub getch
- readident:
- tok$ = ""
- tok$ = tok$ + ch$
- gosub getch
- getch:
- textp = textp + 1