robolson-simplesem 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Manifest +18 -0
- data/README.textile +26 -24
- data/Rakefile +6 -8
- data/bin/simplesem +26 -45
- data/lib/simplesem/arithmetic.treetop +98 -96
- data/lib/simplesem/arithmetic_node_classes.rb +9 -5
- data/lib/simplesem/simple_sem.treetop +112 -111
- data/lib/simplesem/simplesem_program.rb +50 -48
- data/lib/simplesem/version.rb +5 -0
- data/lib/simplesem.rb +2 -2
- data/lib/trollop/trollop.rb +695 -0
- data/sample_programs/gcd.txt +1 -1
- data/sample_programs/hello-world.txt +1 -1
- data/sample_programs/while-loop.txt +1 -1
- data/simplesem.gemspec +9 -10
- data/test/simplesem_test.rb +10 -8
- data/test/test_helper.rb +2 -2
- metadata +15 -11
data/Manifest
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
LICENSE
|
2
|
+
Manifest
|
3
|
+
README.textile
|
4
|
+
Rakefile
|
5
|
+
bin/simplesem
|
6
|
+
lib/simplesem.rb
|
7
|
+
lib/simplesem/arithmetic.treetop
|
8
|
+
lib/simplesem/arithmetic_node_classes.rb
|
9
|
+
lib/simplesem/simple_sem.treetop
|
10
|
+
lib/simplesem/simplesem_program.rb
|
11
|
+
lib/simplesem/version.rb
|
12
|
+
lib/trollop/trollop.rb
|
13
|
+
sample_programs/case-statement.txt
|
14
|
+
sample_programs/gcd.txt
|
15
|
+
sample_programs/hello-world.txt
|
16
|
+
sample_programs/while-loop.txt
|
17
|
+
test/simplesem_test.rb
|
18
|
+
test/test_helper.rb
|
data/README.textile
CHANGED
@@ -8,27 +8,27 @@ Interpreter for the SIMPLESEM language.
|
|
8
8
|
|
9
9
|
SIMPLESEM is used in the CS141 Programming Languages course taught by Professor "Shannon Tauro":http://www.ics.uci.edu/~stauro/ at UC Irvine. This Rubygem was created out of my desire to execute SIMPLESEM programs.
|
10
10
|
|
11
|
-
This interpreter utilizes Nathan Sobo's "Treetop":http://github.com/nathansobo/treetop gem to create a parsing expression grammar for parsing SIMPLESEM commands.
|
11
|
+
This interpreter utilizes Nathan Sobo's "Treetop":http://github.com/nathansobo/treetop gem to create a parsing expression grammar for parsing SIMPLESEM commands.
|
12
12
|
|
13
13
|
h2. Installation and Usage
|
14
14
|
|
15
15
|
Install the simplesem gem with:
|
16
16
|
|
17
|
-
|
17
|
+
bc. $ sudo gem install robolson-simplesem
|
18
18
|
|
19
19
|
Execute a SIMPLESEM program using the simplesem command. Pass the filename of the SIMLESEM source file as an argument.
|
20
20
|
|
21
|
-
|
21
|
+
bc. $ simplesem simplesem_file.txt
|
22
22
|
|
23
23
|
h3. Command Line Options
|
24
24
|
|
25
|
-
The simplesem executable accepts a couple command line options which will display the values in the Data array at the time the program exits.
|
25
|
+
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.
|
26
26
|
|
27
|
-
|
28
|
-
|
29
|
-
|
27
|
+
pre. --help Print help message
|
28
|
+
--inspect Print values in the data array on exit
|
29
|
+
--inspect-history Print values in the data array with change history on exit
|
30
30
|
|
31
|
-
Use
|
31
|
+
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.
|
32
32
|
|
33
33
|
h2. Introduction to SIMPLESEM
|
34
34
|
|
@@ -40,11 +40,11 @@ The @set@ command is used to modify the value stored in a cell. It takes two par
|
|
40
40
|
|
41
41
|
Evaluate the expression (4 * 2) and places the result into location 0:
|
42
42
|
|
43
|
-
|
43
|
+
bc. set 0, 4 * 2
|
44
44
|
|
45
45
|
Assign the value stored at location 0 into location 2:
|
46
46
|
|
47
|
-
|
47
|
+
bc. set 2, D[0]
|
48
48
|
|
49
49
|
h3. Input/Output with SET
|
50
50
|
|
@@ -52,11 +52,11 @@ The @set@ command is also used to print to the screen and to get input from the
|
|
52
52
|
|
53
53
|
Print the value stored at location 0:
|
54
54
|
|
55
|
-
|
55
|
+
bc. set write, D[0]
|
56
56
|
|
57
57
|
Get input from the user and store it at location 1:
|
58
58
|
|
59
|
-
|
59
|
+
bc. set 0, read
|
60
60
|
|
61
61
|
h3. JUMP
|
62
62
|
|
@@ -64,7 +64,7 @@ The @jump@ command performs and unconditional jump to the line number specified.
|
|
64
64
|
|
65
65
|
Jump program execution to the address at location 0:
|
66
66
|
|
67
|
-
|
67
|
+
bc. jump D[0]
|
68
68
|
|
69
69
|
h3. JUMPT
|
70
70
|
|
@@ -72,7 +72,7 @@ The @jumpt@ command (pronounced jump-true), is a conditional jump. It only jumps
|
|
72
72
|
|
73
73
|
Jump to line 7 if the value at <notextile>D[1]</notextile> is equal to the value at <notextile>D[0]</notextile>:
|
74
74
|
|
75
|
-
|
75
|
+
bc. jumpt 7, D[1] = D[0]
|
76
76
|
|
77
77
|
SIMPLESEM supports all the common comparison operators: >, <, >=, <=, !=, and =. Take note that the equality operator is a single '=' sign, not the usual '=='.
|
78
78
|
|
@@ -80,8 +80,8 @@ h3. Comments
|
|
80
80
|
|
81
81
|
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.
|
82
82
|
|
83
|
-
|
84
|
-
|
83
|
+
bc. // This is line number 0.
|
84
|
+
set write, "foo" // a comment after a statement
|
85
85
|
|
86
86
|
h2. Slightly More Advanced Features of SIMPLESEM
|
87
87
|
|
@@ -89,19 +89,19 @@ h3. Nested Data Lookups
|
|
89
89
|
|
90
90
|
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 <notextile>D[0]</notextile> contains the value 9--this statement will set location 5 with the value at location 10.
|
91
91
|
|
92
|
-
|
92
|
+
bc. set 5, D[D[0]+1]
|
93
93
|
|
94
94
|
h3. Indirect Addressing
|
95
95
|
|
96
96
|
Assigns the value stored at location 15 into the cell whose address is the value stored at location 10:
|
97
97
|
|
98
|
-
|
98
|
+
bc. set D[10], D[15]
|
99
99
|
|
100
100
|
h3. Complex Math Expressions
|
101
101
|
|
102
102
|
SIMPLESEM supports standard mathematical order of operations. The following statement sets location 1 to 14 as expected:
|
103
103
|
|
104
|
-
|
104
|
+
bc. set 1, 2+3*4
|
105
105
|
|
106
106
|
h3. Using The Instruction Pointer Variable
|
107
107
|
|
@@ -109,13 +109,15 @@ At any point you can use the @ip@ placeholder in your code and it will evaluate
|
|
109
109
|
|
110
110
|
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.
|
111
111
|
|
112
|
-
<pre
|
112
|
+
<pre>
|
113
|
+
<code>
|
113
114
|
set 0, 0
|
114
115
|
set write, "hello world!"
|
115
116
|
set 0, D[0]+1
|
116
117
|
jumpt ip-3, D[0] < 5
|
117
118
|
halt
|
118
|
-
</code
|
119
|
+
</code>
|
120
|
+
</pre>
|
119
121
|
|
120
122
|
h3. A Sample SIMPLESEM Program
|
121
123
|
|
@@ -136,15 +138,15 @@ set 0, D[0]-D[1]
|
|
136
138
|
jump 8
|
137
139
|
</code></pre>
|
138
140
|
|
141
|
+
Running the above program on the command line will give the following output:
|
139
142
|
<pre>
|
140
|
-
$ simplesem -
|
143
|
+
$ simplesem --inspect-history sample_programs/gcd.txt
|
141
144
|
input: 15
|
142
145
|
input: 35
|
143
146
|
5
|
144
147
|
|
145
|
-
DATA:
|
148
|
+
DATA:
|
146
149
|
0: [15, 10, 5]
|
147
150
|
1: [35, 20, 5]
|
148
151
|
2: [4]
|
149
152
|
</pre>
|
150
|
-
|
data/Rakefile
CHANGED
@@ -1,13 +1,11 @@
|
|
1
|
-
require 'rubygems'
|
2
1
|
require 'rake'
|
3
2
|
require 'echoe'
|
4
3
|
|
5
|
-
Echoe.new('simplesem', '0.1.
|
6
|
-
p.
|
7
|
-
p.
|
8
|
-
p.
|
9
|
-
p.
|
4
|
+
Echoe.new('simplesem', '0.1.4') do |p|
|
5
|
+
p.summary = "SIMPLESEM Interpreter"
|
6
|
+
p.description = "Interpreter for parsing and executing SIMPLESEM programs"
|
7
|
+
p.url = "http://github.com/robolson/simplesem"
|
8
|
+
p.author = "Rob Olson"
|
9
|
+
p.email = "rob@thinkingdigitally.com"
|
10
10
|
p.runtime_dependencies = ["treetop >=1.2.4"]
|
11
11
|
end
|
12
|
-
|
13
|
-
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
|
data/bin/simplesem
CHANGED
@@ -1,55 +1,36 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
#= Overview
|
3
|
-
#
|
4
|
-
# Interpreter for the SIMPLESEM language
|
5
|
-
#
|
6
|
-
#= Usage
|
7
|
-
#
|
8
|
-
# simplesem [-t|-v] filename
|
9
|
-
#
|
10
|
-
# Options:
|
11
|
-
# -h Print this message
|
12
|
-
# -t Print Data array on exit
|
13
|
-
# -v Print Data array with change history on exit. Supersedes -t
|
14
|
-
# if it is also specified.
|
15
|
-
#
|
16
|
-
|
17
|
-
require 'rubygems'
|
18
|
-
require 'rdoc/usage'
|
19
|
-
require 'getoptlong'
|
20
|
-
|
21
|
-
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../lib"))
|
22
|
-
require 'simplesem'
|
23
2
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
)
|
29
|
-
|
30
|
-
verbosity = 0 # by default do not print what is in Data
|
31
|
-
|
32
|
-
opts.each do |opt, arg|
|
33
|
-
case opt
|
34
|
-
when '-h'
|
35
|
-
RDoc::usage
|
36
|
-
when '-t'
|
37
|
-
verbosity = 1
|
38
|
-
when '-v'
|
39
|
-
verbosity = 2
|
40
|
-
end
|
3
|
+
# The gem packager will properly add the lib dir to LOAD_PATH when
|
4
|
+
# executing the gem but load path manipulation is needed during development
|
5
|
+
unless $LOAD_PATH.include?(File.expand_path('../../lib', __FILE__))
|
6
|
+
$LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
|
41
7
|
end
|
42
8
|
|
43
|
-
|
44
|
-
|
45
|
-
|
9
|
+
require 'simplesem'
|
10
|
+
require 'trollop/trollop'
|
11
|
+
|
12
|
+
opts = Trollop::options do
|
13
|
+
version SimpleSem::VERSION
|
14
|
+
banner <<-EOS
|
15
|
+
Interpreter for the SIMPLESEM language by Rob Olson
|
16
|
+
|
17
|
+
Usage: simplesem [options] filename
|
18
|
+
|
19
|
+
Options:
|
20
|
+
EOS
|
21
|
+
|
22
|
+
opt :inspect, "Print values in the data array on exit"
|
23
|
+
opt :inspect_history, "Print values in the data array with change history on exit"
|
24
|
+
conflicts :inspect, :inspect_history
|
46
25
|
end
|
26
|
+
Trollop::die "must specify a single filename" if ARGV.length != 1
|
47
27
|
|
48
|
-
|
28
|
+
|
29
|
+
ssp = SimpleSem::Program.new(ARGV.shift)
|
49
30
|
ssp.run
|
50
31
|
|
51
|
-
if
|
32
|
+
if opts[:inspect_history]
|
52
33
|
puts "\nDATA: \n" + ssp.inspect_data
|
53
|
-
elsif
|
34
|
+
elsif opts[:inspect]
|
54
35
|
puts "\nDATA: \n" + ssp.inspect_data_with_history
|
55
|
-
end
|
36
|
+
end
|
@@ -1,99 +1,101 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
rule comparative
|
7
|
-
operand_1:additive space operator:comparison_op space operand_2:additive <BinaryOperation>
|
8
|
-
end
|
9
|
-
|
10
|
-
rule comparison_op
|
11
|
-
'>=' {
|
12
|
-
def apply(a, b)
|
13
|
-
a >= b
|
14
|
-
end
|
15
|
-
}
|
16
|
-
/
|
17
|
-
'<=' {
|
18
|
-
def apply(a, b)
|
19
|
-
a <= b
|
20
|
-
end
|
21
|
-
}
|
22
|
-
/
|
23
|
-
'>' {
|
24
|
-
def apply(a, b)
|
25
|
-
a > b
|
26
|
-
end
|
27
|
-
}
|
28
|
-
/
|
29
|
-
'<' {
|
30
|
-
def apply(a, b)
|
31
|
-
a < b
|
32
|
-
end
|
33
|
-
}
|
34
|
-
/
|
35
|
-
'!=' {
|
36
|
-
def apply(a, b)
|
37
|
-
a != b
|
38
|
-
end
|
39
|
-
}
|
40
|
-
/
|
41
|
-
'=' {
|
42
|
-
def apply(a, b)
|
43
|
-
a == b
|
44
|
-
end
|
45
|
-
}
|
46
|
-
end
|
47
|
-
|
48
|
-
rule additive
|
49
|
-
operand_1:multitive space operator:additive_op space operand_2:additive <BinaryOperation>
|
50
|
-
/
|
51
|
-
multitive
|
52
|
-
end
|
53
|
-
|
54
|
-
rule additive_op
|
55
|
-
'+' {
|
56
|
-
def apply(a, b)
|
57
|
-
a + b
|
58
|
-
end
|
59
|
-
}
|
60
|
-
/
|
61
|
-
'-' {
|
62
|
-
def apply(a, b)
|
63
|
-
a - b
|
64
|
-
end
|
65
|
-
}
|
66
|
-
end
|
1
|
+
module SimpleSem
|
2
|
+
grammar Arithmetic
|
3
|
+
rule expression
|
4
|
+
comparative / additive
|
5
|
+
end
|
67
6
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
primary
|
72
|
-
end
|
73
|
-
|
74
|
-
rule multitive_op
|
75
|
-
'*' {
|
76
|
-
def apply(a, b)
|
77
|
-
a * b
|
78
|
-
end
|
79
|
-
}
|
80
|
-
/
|
81
|
-
'/' {
|
82
|
-
def apply(a, b)
|
83
|
-
a / b
|
84
|
-
end
|
85
|
-
}
|
86
|
-
end
|
7
|
+
rule comparative
|
8
|
+
operand_1:additive space operator:comparison_op space operand_2:additive <BinaryOperation>
|
9
|
+
end
|
87
10
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
11
|
+
rule comparison_op
|
12
|
+
'>=' {
|
13
|
+
def apply(a, b)
|
14
|
+
a >= b
|
15
|
+
end
|
16
|
+
}
|
17
|
+
/
|
18
|
+
'<=' {
|
19
|
+
def apply(a, b)
|
20
|
+
a <= b
|
21
|
+
end
|
22
|
+
}
|
23
|
+
/
|
24
|
+
'>' {
|
25
|
+
def apply(a, b)
|
26
|
+
a > b
|
27
|
+
end
|
28
|
+
}
|
29
|
+
/
|
30
|
+
'<' {
|
31
|
+
def apply(a, b)
|
32
|
+
a < b
|
33
|
+
end
|
34
|
+
}
|
35
|
+
/
|
36
|
+
'!=' {
|
37
|
+
def apply(a, b)
|
38
|
+
a != b
|
39
|
+
end
|
40
|
+
}
|
41
|
+
/
|
42
|
+
'=' {
|
43
|
+
def apply(a, b)
|
44
|
+
a == b
|
45
|
+
end
|
46
|
+
}
|
47
|
+
end
|
48
|
+
|
49
|
+
rule additive
|
50
|
+
operand_1:multitive space operator:additive_op space operand_2:additive <BinaryOperation>
|
51
|
+
/
|
52
|
+
multitive
|
53
|
+
end
|
54
|
+
|
55
|
+
rule additive_op
|
56
|
+
'+' {
|
57
|
+
def apply(a, b)
|
58
|
+
a + b
|
59
|
+
end
|
60
|
+
}
|
61
|
+
/
|
62
|
+
'-' {
|
63
|
+
def apply(a, b)
|
64
|
+
a - b
|
65
|
+
end
|
66
|
+
}
|
67
|
+
end
|
68
|
+
|
69
|
+
rule multitive
|
70
|
+
operand_1:primary space operator:multitive_op space operand_2:multitive <BinaryOperation>
|
71
|
+
/
|
72
|
+
primary
|
73
|
+
end
|
74
|
+
|
75
|
+
rule multitive_op
|
76
|
+
'*' {
|
77
|
+
def apply(a, b)
|
78
|
+
a * b
|
79
|
+
end
|
80
|
+
}
|
81
|
+
/
|
82
|
+
'/' {
|
83
|
+
def apply(a, b)
|
84
|
+
a / b
|
85
|
+
end
|
86
|
+
}
|
87
|
+
end
|
88
|
+
|
89
|
+
rule number
|
90
|
+
('-'? [1-9] [0-9]* / '0') {
|
91
|
+
def eval(env={})
|
92
|
+
text_value.to_i
|
93
|
+
end
|
94
|
+
}
|
95
|
+
end
|
96
|
+
|
97
|
+
rule space
|
98
|
+
' '*
|
99
|
+
end
|
98
100
|
end
|
99
|
-
end
|
101
|
+
end
|
@@ -1,7 +1,11 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
module SimpleSem
|
2
|
+
module Arithmetic
|
3
|
+
|
4
|
+
class BinaryOperation < Treetop::Runtime::SyntaxNode
|
5
|
+
def eval(env={})
|
6
|
+
operator.apply(operand_1.eval(env), operand_2.eval(env))
|
7
|
+
end
|
5
8
|
end
|
9
|
+
|
6
10
|
end
|
7
|
-
end
|
11
|
+
end
|