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:
- 26 string variables, named a$-z$
- string literals, variables and expressions can be used in assignments and print statements
- string expressions can be compared for equality in if statements
- string expressions can be concatenated using '+'
- the following 'GWBASIC' string functions:
- left$(s$, i)
- returns the first i characters of s$
- mid$(s$, i, j)
- returns the substring of length j starting at position i in s$
- right(s$, i, j)
- returns the last i characters of s$
- str$(i)
- returns the integer value i as a string - opposite of val()
- chr$(i)
- returns the i'th ASCII character - opposite of asc()
- val(s$)
- returns the numeric value of the string of digits s$ - opposite of str()
- len$(s$)
- returns the number of characters in s$
- instr$(t$, s$)
- returns the position of the string t$ in s$ (or 0)
- asc(s$)
- returns the ASCII code for the first character in s$ - opposite of val()
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
- strings are limited to 255 characters
- 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:- the 5 source files
- a small test program
- a windows executable, ubasic.exe
Comments, suggestions and bug reports welcome. Dave@zenoshrdlu.com
See here for other KAP and CHDK stuff of mine.