/ToyLanguageInterpreter

A simple toy programming language implemented in Java

Primary LanguageJava

Toy Language Interpreter

A simple language interpreter written in Java. A program in this toy language is represented by a series of Statements which may operate on other Statements and/or Expressions.
A Statement is an instruction which can be executed, such as AssignmentStatement (a=13).
An Expression is an entity such as ArithmeticExpression (2*(a+3)) or ReadHeapExpression (readHeap(addr)) which can be evaluated to an integer value. The interpreter expects statements as inputs.

-if statements

Syntax: if condition then statement else otherstatement
Example (assuming a is defined):
if a>0 then print(a*10) else a=a+1;print(a)

-while statement

Syntax: while(conditionExpression): statement
Example (assuming n and i is defined):
while(i<n): print(i);i=i+1

-print statement

Syntax: print(expression)
Example:
print(1+3*4)
print(readHeap(ptr)*3)

-assignment statements

Syntax: a=expression
Example:
a=(1>=2)+1*3

-open file statements

Syntax: openFile(descriptorVariableName, filename)
Note: filename does not contain spaces or ""
Example:
openFile(desc, file.txt)

-read file statements

Syntax: readFile(descr, varName) where descr is the descriptor of the opened file and varName is the variable to read into
Example (after having opened the file):
readFile(descriptor, a)

-close file statements

Syntax: closeFile(descriptor)

-allocate space into the heap

Syntax: new(varHeapPtr, initValueExpr)
Example:
new(ptr, 11). Now at address ptr value 11 is stored

-read from the heap

Syntax: readHeap(addrExpr)
Example:
a=readHeap(ptr). a now hold the value from address ptr

-compound statement

Syntax: statement1;statement2;statement3
Example:
a=10;new(addr,10);b=readHeap(addr);print(a==b). All statements separated by ; are loaded onto the execution stack in the order that they appear in the line

-decrement/increment statement

Syntax: var++ or var--
Example:
a=11
a--
Now a holds the value 10.

-dereference expression

Syntax: *addr where addr is a heap address
Example:
new(ptr, 12)
a=*ptr
Now a holds the value 12.
This operator can only be used in print and assignment statements at the moment

-fork statement

Syntax: fork(statement)
Example: a=1;new(ptr, a)
fork(a=a+1;print(a);writeHeap(ptr, 10);print(readHeap(ptr)))
print(a);a=a;a=a;a=a;print(readHeap(ptr)) The fork statement creates a new thread to execute the statements given as parameter. The variable table is copied while the files and heap are passed by reference. This way any modification performed by one thread will be visible in all others. At the start, a=1 and 1 is stored at address ptr in the heap. When the fork statement executes, its thread will have a=2, output 2, modifiy the global heap writing 10 at address ptr and output 10. Back in the main function, the statement right after fork will be executing in parallel with the fork statement, in the main thread. It will print 1, the value of a in this thread, and print 10, the value written to the heap by the other thread.
In between the two print statements there are 3 useless statements which are used to fill execution time to let the other thread execute the heap writing before we read from it.
A more elegant method of achieving this would be a wait() statement which is on the todo list.