uBasic with Strings


Adam Dunkels' uBASIC is a very small Basic Interpreter that (in a modified form) is used as a script engine by CHDK. uBASIC originally offered only the most basic BASIC functionality: if/then/else, for/next, let, goto, gosub, print, and mathematical expressions with support only for integer variables with single character names. The version embedded in CHDK has some enhancements - do-while, select-case and few more operators, but is still integer only.

Largely for fun I decided to add string handling. The version downloadable here is a stand-alone interpreter that does not include the CHDK enhancements (that version is here) so for example, all lines must have line numbers). It offers the following enhancements over Adam's original:

Supplied Test Program Program Output
1 print "start of test"
2 a$= "abcdefghi"
3 b$="123456789"
5 print "Length of a$=", len(a$)
6 print "Length of b$=", len(b$)
7 if len(a$) = len(b$) then print "same length"
8 if (a$ = b$) then print "same string"
9 c$=left$(a$+ b$,12)
10 print c$
11 c$=right$(a$+b$, 12)
12 print c$
13 c$=mid$(a$+b$, 8,8)
14 print c$
15 c$=str$(13+42)
16 print c$
17 print len(c$)
18 print len("this" + "that")
19 c$ = chr$(34)
20 print c$
21 j = asc(c$)
22 print j
23 print val("12345")
24 i=instr(3, "123456789", "67")
24 print "position of '67' in '123456789' is", i
25 print mid$(a$,2,2)+"xyx"
30 print "end of test"
start of test
Length of a$= 9
Length of b$= 9
same length
abcdefghi123
ghi123456789
hi123456
55
2
8
"
34
12345
position of '67' in '123456789' is 6
bcxyx
end of test

Restrictions

  1. strings are limited to 255 characters
  2. the string buffer is 4000 bytes long (so you can't have 26 strings each 255 characters long)

These restrictions are trivially easy to amend. Note that operands are checked for errors (out of range etc) and there is a garbage collector, but my testing has not been exhaustive

Downloading

The zip file contains:

Comments, suggestions and bug reports welcome. Dave@zenoshrdlu.com

See here for other KAP and CHDK stuff of mine.