For this assignment, you are to complete an interpreter for a simple, procedural programming language named SILLY (Simple, Interpreted, Limited Language for You). The grammar rules for the SILLY language are as follows:
There are no spaces between the characters in an identifier, integer, or string. Otherwise, tokens in the other rules are all separated by whitespace.
The SILLY language is case sensitive, so variables
considered unique. There are three data types in SILLY: integer, string and Boolean. Variables are not explicitly declared but must be assigned a value before they can be used in an expression or statement. The interpreter relies heavily on run-time type checking when evaluating expressions. The '+' operator can be applied to integers (where it represents addition) or strings (where it represents concatenation). If applied to a string and another data type, the data value is coerced into a string by placing quotes around it, then concatenated. The math operators ('+', '-', '*', '/', and '%') can only be applied to integers, whereas the Boolean operators ('and', 'or', and 'not') can only be applied to Boolean values. Comparison operators ('==', '!=', '>', '>=', '<', and '<=') can be applied to any two values of the same type. Integers are ordered numerically (e.g., 2 < 3); strings are ordered alphabetically (e.g., "bar" < "foo"); Booleans are ordered truthfully (e.g., false < true). In addition, the expressions in if statements and while loops must evaluate to Boolean values, whereas the expression in a repeat loop must evaluate to an integer. Any type mismatches will result in a runtime error.
SAMPLE CODE (output in red) >>> x = 5 ; >>> output x ; 5 >>> y = x + 1 ; >>> output y ; 6 >>> output ( x * y ) + 1 ; 31 >>> num1 = ( y / 2 ) + ( x * 2 ) ; >>> output num1 ; 13 >>> word = "foo" ; >>> longer = word + "bar" ; >>> output "longer=" + longer ; "longer=foobar" >>> output longer + 123 ; "foobar123" >>> output "foo_" + ( 3 - 1 ) ; "foo_2" >>> repeat 3 output "howdy" ; end "howdy" "howdy" "howdy" >>> num = 1 ; >>> N = 5 ; repeat N output num ; num = num * 2 ; end 1 2 4 8 16 >>> flag = true ; >>> output flag ; true >>> output 3 > 2 ; true >>> output "foo" == "bar" ; false >>> if true output "OKAY" ; end "OKAY" >>> count = 5 ; >>> // identify number type if count < 0 output "neg" ; elif count > 0 output "pos" ; else output "zero" ; end "pos" >>> while count > -1 output count ; // display count = count - 1 ; // increment end 5 4 3 2 1 0 >>> opposite = not flag ; >>> output opposite ; false >>> output flag or not flag ; true >>> n1 = 4 ; >>> n2 = 2 ; >>> while ( n1 > 0 ) and ( n2 > 0 ) n1 = n1 - 1 ; n2 = n2 - 1 ; end output n1 + "--" + n2 ; "2--0"
An incomplete version of the SILLY interpreter is provided for you via the following classes/files:
TokenStream.java: These classes define the different types of tokens that make up the language, and define an input stream for reading in program tokens.
Interpreter.java: This is the main interpreter class for the language. By default, the interpreter reads statements from and displays output to the console. If you specify a file name as a command line argument, however, the interpreter will read statements directly from the file.
HeapSegment.java: These classes define the memory segments for storing variables and their values, with integer and Boolean values stored directly on the stack and strings stored on the heap.
Statement.java: This abstract class provides the framework for all types of statements and includes a general-purpose method for reading a statement.
Repeat.java: These classes, derived from
Statement, define specific statements of the SILLY language.
BooleanValue.java: This interface and implementing classes define the types of values that can be stored in SILLY.
Expression.java: This class defines an expression, which is either a simple term (integer, string, Boolean, or identifier) or a complex expression involving operators. It contains an inner class,
Term, as a helper.
To produce a fully functional interpreter for the SILLY language, you will need to make the following modifications/additions:
Outputclass so that the expression is optional.
BooleanValuedata type is implemented, but no Boolean operators are defined. As a result, the Boolean constants
falsecan be output and assigned to variables, but no comparisons can be made. Update the
Expressionclasses so that the comparison operators ('==', '!=', '>', '>=', '<', and '<=') are recognized and can be used to build Boolean expressions (evaluating to true/false). If any of these comparison operators are applied to values of different types (e.g., a string and an integer), an exception with appropriate runtime error message should be thrown.
Whileclass that defines while loops. This class should be similar to the
Repeatclass due to their structural similarity (both contain statements that are controlled by a test). However, a while statement is controlled by a Boolean expression and repeatedly executes the block of enclosed statements as long as the expression evaluates to true. Note that you will also need to make small additions to the
Ifclass that defines if statements, which may include optional
elsecases. Note that there can be arbitrarily many
elifcases, each with its own test, but at most one
not. If any of these operators are applied to non-Boolean values, an exception with appropriate runtime error message should be thrown.
Note that in making these additions, the main program file
Interpreter.java) need not be
modified at all. Instead, you will primarily adding new classes, with some minor modifications to
Expression. As you add new
features, be sure that syntax and runtime errors result in exceptions being thrown with informative messages.