MASICA

An educational interactive TinyBASIC interpreter. MASICA is designed to provide an absolute minimum of features and complexity while including what is necessary to illustrate core imperative programming concepts such as variables, iteration and conditional branches. Variable names are single letters.

The MASICA interface is similar to that of early 8-bit microcomputers- a prompt which can evaluate commands immediately or store a sequence of commands prefixed with line numbers. Several commands for removing or editing existing lines are also provided.

Statements

Expressions

MASICA uses a simple dynamic type system- expressions can return strings, numbers or boolean values, and variables can hold any of these types. Predicates are provided for querying the type of an expression in the cases where this matters. Strings are enclosed in double-quotes ("), numbers are signed 32-bit integers and the words true and false are recognized as boolean literals.

The operators + - * / and % are implemented with normal arithmetic precedence, and parentheses can be used to further control evaluation order. The comparison operators == != > < >= and <= are chosen to correspond to the syntax of C and Java. The boolean operators and or and not may only be applied to boolean arguments. Comparison operators are overloaded to compare strings (lexicographically) in addition to numbers. The + operator is also overloaded to perform string concatenation.

In addition to these simple operators, MASICA provides a few intrinsic functions:

Editor Commands

The following editor commands are provided to ease programming:

Examples

Hello, World:

10 print "Hello, World!";
20 goto 10

Die Roller:

10 c = 10
20 print rnd(6)+1;
30 c = c - 1
40 if c > 0 then goto 20

Lunar Lander:

10 a = 100 + rnd(20)
11 v = rnd(5)
12 f = 80 + rnd(10)
20 print "a: ",a,"v: ",v,"f: ",f
30 print "thrust? ";
40 input t
50 if t > f  then t = 0
60 if t > 30 then t = 30
70 f = f - t
80 v = v + 4 - t
90 a = a - v
100 if a >  0 then goto 20
110 if v >  5 then print "CRASH!"
120 if v <= 5 then print "success!"

Fibonacci Sequence:

0 x = 0
1 y = 1
2 z = x + y
3 x = y
4 y = z
5 print y
6 goto 2

NIM (two player):

10 g = 20
11 p = 0
20 print "player: ", p, "gold: ",g
21 print "take how much? ";
30 input t
31 if (t<1) or (t>3) then goto 21
40 g = g - t
50 if g <= 0 then goto 80
60 p = (p + 1) % 2
70 goto 20
80 print "Player ",p,"Loses!"

NIM (versus computer):

10 g = 20
20 print "gold: ",g,"take how much? ";
30 input t
31 if (t<1) or (t>3) then goto 20
40 g = g - t
41 if g <= 0 then goto 110
50 t = (((g % 3)+1)%3)+1
60 print "I will take ",t
70 g = g - t
80 if g >= 0 then goto 20
90 print "Argh, foiled!"
100 end
110 print "Pitiful Human!"