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 SILLY v. 17.0 are as follows:
There are no spaces between the characters in an identifier, number, or string. Otherwise, tokens in the other rules are all separated by whitespace.
The SILLY language is case-sensitive, so variables
a
and A
are
considered unique.
There are three data types in SILLY: number, string, and Boolean.
The interpreter relies heavily on run-time type checking when evaluating expressions. Every variable must be declared and initialized before it can be used in expressions or assigned new values. Referencing an undeclared variable (e.g., in an expression or assignment statement) will result in a run-time error. All operators must be applied to values of the same type. The '+' operator can be applied to two numbers (where it represents addition) or two strings (where it represents concatenation). The math operators ('+', '-', '*', '/', and '%') can only be applied to numbers, 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. Numbers 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 a number (which is rounded down to an integer). Any type mismatch will result in a runtime error.
For example:
SAMPLE CODE (output in red) >>> var x = 5 >>> output x 5 >>> var y = ( x + 1 ) >>> output y 6 >>> output ( ( x * y ) + 1 ) 31 >>> x = ( ( y / 2 ) + ( x * 2 ) ) >>> output x 13 >>> var word = "foo" >>> var longer = ( word + "bar" ) >>> output ( "longer=" + longer ) longer=foobar >>> repeat 4 times output "howdy" end howdy howdy howdy howdy >>> repeat 3.9 times output "doodly" end doodly doodly doodly >>> var num = 1 >>> var N = 5 repeat N times output num num = ( num * 2 ) end 1 2 4 8 16 >>> var flag = true >>> output flag true >>> output ( flag == true ) true >>> output ( "foo" != "foo" ) false >>> output ( 3 > 2 ) true var test = ( "smith" <= "smooth" ) >>> output test true >>> output ( ( 3.5 + 1 ) ) 4.5 >>> output ( ( ( "foo" ) ) ) foo >>> output { "foo" ( 3 - 1 ) ( "a" + ( "b" + "c" ) ) true } foo 2 abc true >>> var count = 5 >>> while ( count > -1 ) do output count count = ( count - 1 ) end 5 4 3 2 1 0 >>> var a = 1 >>> var b = 5 >>> while ( a < b ) do a = ( a + 1 ) b = ( b - 2 ) end >>> output { "a=" a "b=" b } a= 3 b= 1
An incomplete version of the SILLY interpreter is provided for you via the following classes/files:
Token.java
,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, which runs an infinite loop to read and execute statements entered at the keyboard.MemorySpace.java
,StackSegment.java
,HeapSegment.java
: These classes define the memory segments for storing variables and their values, with number 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.VarDeclaration.java
,Assignment.java
,Output.java
,Repeat.java
,Import.java
: These classes, derived fromStatement
, define specific statements of the SILLY language.DataValue.java
,NumberValue.java
,StringValue.java
,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 (number, string, Boolean, or identifier) or a complex expression involving operators. It contains an inner class,Term
, as a helper.
For this assignment, you will need to make the following modifications/additions:
BooleanValue
data type is implemented, but the only Boolean operators defined are '=='and '!='. Update the Token
and Expression
classes so that the other comparison operators ('>', '>=', '<', and '<=') are recognized and can be used to build Boolean expressions (evaluating to true/false). Keep in mind that if any comparison operator is applied to values of different types (e.g., a string and a number), an exception with appropriate runtime error message should be thrown.
( 3 )
would not be recognized as legal by the interpreter. Modify the Expression
class so that this option is implemented and correctly handled. Output
class so that the other option, which allows you to print multiple expressions contained in curly-braces, is implemented and correctly handled. Note: this requires storing the expressions when an output statement is created, since the expressions cannot be evaluated until the statement is executed.While
class that defines while loops. This class should be similar
to the Repeat
class 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 Token
and Statement
classes. Note that in making these additions, the main program file
(Interpreter.java
) need not be
modified at all. Instead, you will primarily be adding new derived classes, with some minor modifications to Token
, Statement
, and
Expression
. As you add new
features, be sure that syntax and runtime errors result in exceptions being thrown with informative messages.
Also note that an import statement is especially useful as you debug your interpreter. You may enter SILLY statements in a text file, then use an import statement (e.g., import prog.txt
) to read in and execute those statements in sequence.