SIMPLESEM Interpreter

Author: Rob Olson

Description

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

Use --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

The 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[0]

Input/Output with SET

The set command is also used to print to the screen and to get input from the user, through the special registers read and write.

Print the value stored at location 0:

set write, D[0]

Get input from the user and store it at location 1:

set 0, read

JUMP

The jump command performs and unconditional jump to the line number specified.

Jump program execution to the address at location 0:

jump D[0]

JUMPT

The 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[1] is equal to the value at D[0]:

jumpt 7, D[1] = D[0]

SIMPLESEM supports all the common comparison operators: >, <, >=, <=, !=, and =. Take note that the equality operator is a single ‘=’ sign, not the usual ‘==’.

Comments

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[0] contains the value 9—this statement will set location 5 with the value at location 10.

set 5, D[D[0]+1]

Indirect Addressing

Assigns the value stored at location 15 into the cell whose address is the value stored at location 10:

set D[10], D[15]

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[0]+1
jumpt ip-3, D[0] < 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 sample_programs folder.


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[0]           // print the GCD
halt
jumpt D[2], D[0] = D[1]   // while(m != n)
jumpt 12, D[1] < D[0]     // if(m < n)
set 1, D[1]-D[0]
jump 13
set 0, D[0]-D[1]
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</p>
<p><span class="caps">DATA</span>:
0: [15, 10, 5]
1: [35, 20, 5]
2: <sup class="footnote" id="fnr4"><a href="#fn4">4</a></sup>