Author: Rob Olson
Interpreter for the SIMPLESEM language.
SIMPLESEM is used in the CS141 Programming Languages course taught by Professor Shannon Tauro at UC Irvine. This Rubygem was created out of my desire to execute SIMPLESEM programs.
This interpreter utilizes Nathan Sobo’s Treetop gem to create a parsing expression grammar for parsing SIMPLESEM commands.
Installation and Usage
Install the simplesem gem with:
$ sudo gem install robolson-simplesem
Execute a SIMPLESEM program using the simplesem command. Pass the filename of the SIMLESEM source file as an argument.
$ simplesem simplesem_file.txt
Command Line Options
The simplesem executable accepts a couple optional command line options which will display the values in the Data array at the time the program exits.
--help Print help message --inspect Print values in the data array on exit --inspect-history Print values in the data array with change history on exit
--inspect if you only want to see the ending value at each position in the Data array, otherwise use
--inspect-history to see each data location’s history.
Introduction to SIMPLESEM
SIMPLESEM is an abstract semantic processor that is based on the Von Neumann model of the fetch-execute cycle.
set command is used to modify the value stored in a cell. It takes two parameters: the address of the cell whose contents is to be set, and the expression evaluating the new value.
Evaluate the expression (4 * 2) and places the result into location 0:
set 0, 4 * 2
Assign the value stored at location 0 into location 2:
set 2, D
Input/Output with SET
set command is also used to print to the screen and to get input from the user, through the special registers
Print the value stored at location 0:
set write, D
Get input from the user and store it at location 1:
set 0, read
jump command performs and unconditional jump to the line number specified.
Jump program execution to the address at location 0:
jumpt command (pronounced jump-true), is a conditional jump. It only jumps if the expression given is true.
Jump to line 7 if the value at D is equal to the value at D:
jumpt 7, D = D
SIMPLESEM supports all the common comparison operators: >, <, >=, <=, !=, and =. Take note that the equality operator is a single ‘=’ sign, not the usual ‘==’.
SIMPLESEM comments begin with two forward slashes. Everything on the line following
// is considered a comment and is ignored by the interpreter. Important: Comments still consume line numbers! Keep this in mind when writing jump statements.
// This is line number 0. set write, "foo" // a comment after a statement
Slightly More Advanced Features of SIMPLESEM
Nested Data Lookups
SIMPLESEM supports nesting expressions inside of the address location for accessing the data array. This statement looks up the value at location 0, adds 1 to it, then uses the result as the address of another data lookup. If D contains the value 9—this statement will set location 5 with the value at location 10.
set 5, D[D+1]
Assigns the value stored at location 15 into the cell whose address is the value stored at location 10:
set D, D
Complex Math Expressions
SIMPLESEM supports standard mathematical order of operations. The following statement sets location 1 to 14 as expected:
set 1, 2+3*4
Using The Instruction Pointer Variable
At any point you can use the
ip placeholder in your code and it will evaluate to the current value of the instruction pointer, also known as the program counter. The ip is always 1 greater than the current line number because in the fetch-execute cycle the processor increments the ip after fetching the next instruction and executing it.
The following program will output the text “hello world!” five times and then exit. It uses the
ip variable to jump execution back to the
set write statement.
set 0, 0 set write, "hello world!" set 0, D+1 jumpt ip-3, D < 5 halt
A Sample SIMPLESEM Program
The following SIMPLESEM program calculates the GCD of two numbers. The program prompts the user to enter the two numbers when the program is executed. This program is also included in the
set 0, read // first number 'n' set 1, read // second number 'm' set 2, ip + 1 // return pointer for call to GCD jump 8 set write, D // print the GCD halt jumpt D, D = D // while(m != n) jumpt 12, D < D // if(m < n) set 1, D-D jump 13 set 0, D-D jump 8
Running the above program on the command line will give the following output:
$ simplesem --inspect-history sample_programs/gcd.txt input: 15 input: 35 5 DATA: 0: [15, 10, 5] 1: [35, 20, 5] 2: