robolson-simplesem 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +183 -0
- data/README.textile +122 -0
- data/Rakefile +13 -0
- data/bin/simplesem +15 -0
- data/lib/simplesem/arithmetic.treetop +99 -0
- data/lib/simplesem/arithmetic_node_classes.rb +7 -0
- data/lib/simplesem/simple_sem.treetop +103 -0
- data/lib/simplesem/simplesem_program.rb +45 -0
- data/lib/simplesem.rb +2 -0
- data/sample_programs/case-statement.txt +11 -0
- data/sample_programs/gcd.txt +12 -0
- data/sample_programs/hello-world.txt +5 -0
- data/sample_programs/while-loop.txt +14 -0
- data/simplesem.gemspec +37 -0
- data/test/simplesem_test.rb +119 -0
- data/test/test_helper.rb +18 -0
- metadata +92 -0
data/LICENSE
ADDED
@@ -0,0 +1,183 @@
|
|
1
|
+
Academic Free License (AFL) v. 3.0
|
2
|
+
|
3
|
+
This Academic Free License (the "License") applies to any original work
|
4
|
+
of authorship (the "Original Work") whose owner (the "Licensor") has
|
5
|
+
placed the following licensing notice adjacent to the copyright notice
|
6
|
+
for the Original Work:
|
7
|
+
|
8
|
+
Licensed under the Academic Free License version 3.0
|
9
|
+
|
10
|
+
1) Grant of Copyright License. Licensor grants You a worldwide,
|
11
|
+
royalty-free, non-exclusive, sublicensable license, for the duration of
|
12
|
+
the copyright, to do the following:
|
13
|
+
|
14
|
+
a) to reproduce the Original Work in copies, either alone or as part of
|
15
|
+
a collective work;
|
16
|
+
|
17
|
+
b) to translate, adapt, alter, transform, modify, or arrange the
|
18
|
+
Original Work, thereby creating derivative works ("Derivative Works")
|
19
|
+
based upon the Original Work;
|
20
|
+
|
21
|
+
c) to distribute or communicate copies of the Original Work and
|
22
|
+
Derivative Works to the public, under any license of your choice that
|
23
|
+
does not contradict the terms and conditions, including Licensor's
|
24
|
+
reserved rights and remedies, in this Academic Free License;
|
25
|
+
|
26
|
+
d) to perform the Original Work publicly; and
|
27
|
+
|
28
|
+
e) to display the Original Work publicly.
|
29
|
+
|
30
|
+
2) Grant of Patent License. Licensor grants You a worldwide,
|
31
|
+
royalty-free, non-exclusive, sublicensable license, under patent claims
|
32
|
+
owned or controlled by the Licensor that are embodied in the Original
|
33
|
+
Work as furnished by the Licensor, for the duration of the patents, to
|
34
|
+
make, use, sell, offer for sale, have made, and import the Original Work
|
35
|
+
and Derivative Works.
|
36
|
+
|
37
|
+
3) Grant of Source Code License. The term "Source Code" means the
|
38
|
+
preferred form of the Original Work for making modifications to it and
|
39
|
+
all available documentation describing how to modify the Original Work.
|
40
|
+
Licensor agrees to provide a machine-readable copy of the Source Code of
|
41
|
+
the Original Work along with each copy of the Original Work that
|
42
|
+
Licensor distributes. Licensor reserves the right to satisfy this
|
43
|
+
obligation by placing a machine-readable copy of the Source Code in an
|
44
|
+
information repository reasonably calculated to permit inexpensive and
|
45
|
+
convenient access by You for as long as Licensor continues to distribute
|
46
|
+
the Original Work.
|
47
|
+
|
48
|
+
4) Exclusions From License Grant. Neither the names of Licensor, nor the
|
49
|
+
names of any contributors to the Original Work, nor any of their
|
50
|
+
trademarks or service marks, may be used to endorse or promote products
|
51
|
+
derived from this Original Work without express prior permission of the
|
52
|
+
Licensor. Except as expressly stated herein, nothing in this License
|
53
|
+
grants any license to Licensor's trademarks, copyrights, patents, trade
|
54
|
+
secrets or any other intellectual property. No patent license is granted
|
55
|
+
to make, use, sell, offer for sale, have made, or import embodiments of
|
56
|
+
any patent claims other than the licensed claims defined in Section 2.
|
57
|
+
No license is granted to the trademarks of Licensor even if such marks
|
58
|
+
are included in the Original Work. Nothing in this License shall be
|
59
|
+
interpreted to prohibit Licensor from licensing under terms different
|
60
|
+
from this License any Original Work that Licensor otherwise would have a
|
61
|
+
right to license.
|
62
|
+
|
63
|
+
5) External Deployment. The term "External Deployment" means the use,
|
64
|
+
distribution, or communication of the Original Work or Derivative Works
|
65
|
+
in any way such that the Original Work or Derivative Works may be used
|
66
|
+
by anyone other than You, whether those works are distributed or
|
67
|
+
communicated to those persons or made available as an application
|
68
|
+
intended for use over a network. As an express condition for the grants
|
69
|
+
of license hereunder, You must treat any External Deployment by You of
|
70
|
+
the Original Work or a Derivative Work as a distribution under section
|
71
|
+
1(c).
|
72
|
+
|
73
|
+
6) Attribution Rights. You must retain, in the Source Code of any
|
74
|
+
Derivative Works that You create, all copyright, patent, or trademark
|
75
|
+
notices from the Source Code of the Original Work, as well as any
|
76
|
+
notices of licensing and any descriptive text identified therein as an
|
77
|
+
"Attribution Notice." You must cause the Source Code for any Derivative
|
78
|
+
Works that You create to carry a prominent Attribution Notice reasonably
|
79
|
+
calculated to inform recipients that You have modified the Original
|
80
|
+
Work.
|
81
|
+
|
82
|
+
7) Warranty of Provenance and Disclaimer of Warranty. Licensor warrants
|
83
|
+
that the copyright in and to the Original Work and the patent rights
|
84
|
+
granted herein by Licensor are owned by the Licensor or are sublicensed
|
85
|
+
to You under the terms of this License with the permission of the
|
86
|
+
contributor(s) of those copyrights and patent rights. Except as
|
87
|
+
expressly stated in the immediately preceding sentence, the Original
|
88
|
+
Work is provided under this License on an "AS IS" BASIS and WITHOUT
|
89
|
+
WARRANTY, either express or implied, including, without limitation, the
|
90
|
+
warranties of non-infringement, merchantability or fitness for a
|
91
|
+
particular purpose. THE ENTIRE RISK AS TO THE QUALITY OF THE ORIGINAL
|
92
|
+
WORK IS WITH YOU. This DISCLAIMER OF WARRANTY constitutes an essential
|
93
|
+
part of this License. No license to the Original Work is granted by this
|
94
|
+
License except under this disclaimer.
|
95
|
+
|
96
|
+
8) Limitation of Liability. Under no circumstances and under no legal
|
97
|
+
theory, whether in tort (including negligence), contract, or otherwise,
|
98
|
+
shall the Licensor be liable to anyone for any indirect, special,
|
99
|
+
incidental, or consequential damages of any character arising as a
|
100
|
+
result of this License or the use of the Original Work including,
|
101
|
+
without limitation, damages for loss of goodwill, work stoppage,
|
102
|
+
computer failure or malfunction, or any and all other commercial damages
|
103
|
+
or losses. This limitation of liability shall not apply to the extent
|
104
|
+
applicable law prohibits such limitation.
|
105
|
+
|
106
|
+
9) Acceptance and Termination. If, at any time, You expressly assented
|
107
|
+
to this License, that assent indicates your clear and irrevocable
|
108
|
+
acceptance of this License and all of its terms and conditions. If You
|
109
|
+
distribute or communicate copies of the Original Work or a Derivative
|
110
|
+
Work, You must make a reasonable effort under the circumstances to
|
111
|
+
obtain the express assent of recipients to the terms of this License.
|
112
|
+
This License conditions your rights to undertake the activities listed
|
113
|
+
in Section 1, including your right to create Derivative Works based upon
|
114
|
+
the Original Work, and doing so without honoring these terms and
|
115
|
+
conditions is prohibited by copyright law and international treaty.
|
116
|
+
Nothing in this License is intended to affect copyright exceptions and
|
117
|
+
limitations (including "fair use" or "fair dealing"). This License shall
|
118
|
+
terminate immediately and You may no longer exercise any of the rights
|
119
|
+
granted to You by this License upon your failure to honor the conditions
|
120
|
+
in Section 1(c).
|
121
|
+
|
122
|
+
10) Termination for Patent Action. This License shall terminate
|
123
|
+
automatically and You may no longer exercise any of the rights granted
|
124
|
+
to You by this License as of the date You commence an action, including
|
125
|
+
a cross-claim or counterclaim, against Licensor or any licensee alleging
|
126
|
+
that the Original Work infringes a patent. This termination provision
|
127
|
+
shall not apply for an action alleging patent infringement by
|
128
|
+
combinations of the Original Work with other software or hardware.
|
129
|
+
|
130
|
+
11) Jurisdiction, Venue and Governing Law. Any action or suit relating
|
131
|
+
to this License may be brought only in the courts of a jurisdiction
|
132
|
+
wherein the Licensor resides or in which Licensor conducts its primary
|
133
|
+
business, and under the laws of that jurisdiction excluding its
|
134
|
+
conflict-of-law provisions. The application of the United Nations
|
135
|
+
Convention on Contracts for the International Sale of Goods is expressly
|
136
|
+
excluded. Any use of the Original Work outside the scope of this License
|
137
|
+
or after its termination shall be subject to the requirements and
|
138
|
+
penalties of copyright or patent law in the appropriate jurisdiction.
|
139
|
+
This section shall survive the termination of this License.
|
140
|
+
|
141
|
+
12) Attorneys' Fees. In any action to enforce the terms of this License
|
142
|
+
or seeking damages relating thereto, the prevailing party shall be
|
143
|
+
entitled to recover its costs and expenses, including, without
|
144
|
+
limitation, reasonable attorneys' fees and costs incurred in connection
|
145
|
+
with such action, including any appeal of such action. This section
|
146
|
+
shall survive the termination of this License.
|
147
|
+
|
148
|
+
13) Miscellaneous. If any provision of this License is held to be
|
149
|
+
unenforceable, such provision shall be reformed only to the extent
|
150
|
+
necessary to make it enforceable.
|
151
|
+
|
152
|
+
14) Definition of "You" in This License. "You" throughout this License,
|
153
|
+
whether in upper or lower case, means an individual or a legal entity
|
154
|
+
exercising rights under, and complying with all of the terms of, this
|
155
|
+
License. For legal entities, "You" includes any entity that controls, is
|
156
|
+
controlled by, or is under common control with you. For purposes of this
|
157
|
+
definition, "control" means (i) the power, direct or indirect, to cause
|
158
|
+
the direction or management of such entity, whether by contract or
|
159
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
160
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
161
|
+
|
162
|
+
15) Right to Use. You may use the Original Work in all ways not
|
163
|
+
otherwise restricted or conditioned by this License or by law, and
|
164
|
+
Licensor promises not to interfere with or be responsible for such uses
|
165
|
+
by You.
|
166
|
+
|
167
|
+
16) Modification of This License. This License is Copyright (c) 2005
|
168
|
+
Lawrence Rosen. Permission is granted to copy, distribute, or
|
169
|
+
communicate this License without modification. Nothing in this License
|
170
|
+
permits You to modify this License as applied to the Original Work or to
|
171
|
+
Derivative Works. However, You may modify the text of this License and
|
172
|
+
copy, distribute or communicate your modified version (the "Modified
|
173
|
+
License") and apply it to other original works of authorship subject to
|
174
|
+
the following conditions: (i) You may not indicate in any way that your
|
175
|
+
Modified License is the "Academic Free License" or "AFL" and you may not
|
176
|
+
use those names in the name of your Modified License; (ii) You must
|
177
|
+
replace the notice specified in the first paragraph above with the
|
178
|
+
notice "Licensed under <insert your license name here>" or with a notice
|
179
|
+
of your own that is not confusingly similar to the notice in this
|
180
|
+
License; and (iii) You may not claim that your original works are open
|
181
|
+
source software unless your Modified License has been approved by Open
|
182
|
+
Source Initiative (OSI) and You comply with its license review and
|
183
|
+
certification process.
|
data/README.textile
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
h1. SIMPLESEM Interpreter
|
2
|
+
|
3
|
+
h2. Description
|
4
|
+
|
5
|
+
Interpreter for the SIMPLESEM language.
|
6
|
+
|
7
|
+
Author: "Rob Olson":http://thinkingdigitally.com
|
8
|
+
|
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. To my knowledge, there are none publicly available.
|
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.
|
12
|
+
|
13
|
+
h2. Installation and Usage
|
14
|
+
|
15
|
+
Install the simplesem gem with:
|
16
|
+
|
17
|
+
$ sudo gem install robolson-simplesem
|
18
|
+
|
19
|
+
Execute a SIMPLESEM program using the simplesem command. Pass the filename of the SIMLESEM source file as an argument.
|
20
|
+
|
21
|
+
$ simplesem simplesem_file.txt
|
22
|
+
|
23
|
+
h2. Introduction to SIMPLESEM
|
24
|
+
|
25
|
+
SIMPLESEM is an abstract semantic processor that is based on the Von Neumann model of the fetch-execute cycle.
|
26
|
+
|
27
|
+
h3. SET
|
28
|
+
|
29
|
+
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.
|
30
|
+
|
31
|
+
Evaluate the expression (4 * 2) and places the result into location 0:
|
32
|
+
|
33
|
+
set 0, 4 * 2
|
34
|
+
|
35
|
+
Assign the value stored at location 0 into location 2:
|
36
|
+
|
37
|
+
set 2, D[0]
|
38
|
+
|
39
|
+
h3. Input/Output with SET
|
40
|
+
|
41
|
+
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@.
|
42
|
+
|
43
|
+
Print the value stored at location 0:
|
44
|
+
|
45
|
+
set write, D[0]
|
46
|
+
|
47
|
+
Get input from the user and store it at location 1:
|
48
|
+
|
49
|
+
set 0, read
|
50
|
+
|
51
|
+
h3. JUMP
|
52
|
+
|
53
|
+
The @jump@ command performs and unconditional jump to the line number specified.
|
54
|
+
|
55
|
+
Jump program execution to the address at location 0:
|
56
|
+
|
57
|
+
jump D[0]
|
58
|
+
|
59
|
+
h3. JUMPT
|
60
|
+
|
61
|
+
The @jumpt@ command (pronounced jump-true), is a conditional jump. It only jumps if the expression given is true.
|
62
|
+
|
63
|
+
Jump to line 7 if the value at <notextile>D[1]</notextile> is equal to the value at <notextile>D[0]</notextile>:
|
64
|
+
|
65
|
+
jumpt 7, D[1] = D[0]
|
66
|
+
|
67
|
+
SIMPLESEM supports all the common comparison operators: >, <, >=, <=, !=, and =. Take note that the equality operator is a single '=' sign, not the usual '=='.
|
68
|
+
|
69
|
+
h2. Slightly More Advanced Features of SIMPLESEM
|
70
|
+
|
71
|
+
h3. Nested Data Lookups
|
72
|
+
|
73
|
+
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.
|
74
|
+
|
75
|
+
set 5, D[D[0]+1]
|
76
|
+
|
77
|
+
h3. Indirect Addressing
|
78
|
+
|
79
|
+
Assigns the value stored at location 15 into the cell whose address is the value stored at location 10:
|
80
|
+
|
81
|
+
set D[10], D[15]
|
82
|
+
|
83
|
+
h3. Complex Math Expressions
|
84
|
+
|
85
|
+
SIMPLESEM supports standard mathematical order of operations. The following statement sets location 1 to 14 as expected:
|
86
|
+
|
87
|
+
set 1, 2+3*4
|
88
|
+
|
89
|
+
h3. Using The Instruction Pointer Variable
|
90
|
+
|
91
|
+
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.
|
92
|
+
|
93
|
+
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.
|
94
|
+
|
95
|
+
<pre><code>
|
96
|
+
set 0, 0
|
97
|
+
set write, "hello world!"
|
98
|
+
set 0, D[0]+1
|
99
|
+
jumpt ip-3, D[0] < 5
|
100
|
+
halt
|
101
|
+
</code></pre>
|
102
|
+
|
103
|
+
h3. A Sample SIMPLESEM Program
|
104
|
+
|
105
|
+
The following SIMPLESEM program calculates the "GCD":http://en.wikipedia.org/wiki/Greatest_common_divisor 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.
|
106
|
+
|
107
|
+
<pre><code>
|
108
|
+
set 0, read // first number 'n'
|
109
|
+
set 1, read // second number 'm'
|
110
|
+
set 2, ip + 1 // return pointer for call to GCD
|
111
|
+
jump 8
|
112
|
+
set write, D[0] // print the GCD
|
113
|
+
halt
|
114
|
+
jumpt D[2], D[0] = D[1] // while(m != n)
|
115
|
+
jumpt 12, D[1] < D[0] // if(m < n)
|
116
|
+
set 1, D[1]-D[0]
|
117
|
+
jump 13
|
118
|
+
set 0, D[0]-D[1]
|
119
|
+
jump 8
|
120
|
+
</code></pre>
|
121
|
+
|
122
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
Echoe.new('simplesem', '0.1.0') do |p|
|
6
|
+
p.description = "SIMPLESEM Interpreter"
|
7
|
+
p.url = "http://github.com/robolson/simplesem"
|
8
|
+
p.author = "Rob Olson"
|
9
|
+
p.email = "rko618@gmail.com"
|
10
|
+
p.development_dependencies = ['treetop']
|
11
|
+
end
|
12
|
+
|
13
|
+
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
|
data/bin/simplesem
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'rubygems'
|
3
|
+
|
4
|
+
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../lib"))
|
5
|
+
require 'simplesem'
|
6
|
+
|
7
|
+
if ARGV.empty?
|
8
|
+
puts "Usage:\n\nsimplesem simplesem_file.txt\n\n"
|
9
|
+
exit
|
10
|
+
end
|
11
|
+
|
12
|
+
ssp = SimpleSemProgram.new(ARGV.shift)
|
13
|
+
ssp.run
|
14
|
+
|
15
|
+
puts "\nDATA: " + ssp.data.inspect
|
@@ -0,0 +1,99 @@
|
|
1
|
+
grammar Arithmetic
|
2
|
+
rule expression
|
3
|
+
comparative / additive
|
4
|
+
end
|
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
|
67
|
+
|
68
|
+
rule multitive
|
69
|
+
operand_1:primary space operator:multitive_op space operand_2:multitive <BinaryOperation>
|
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
|
87
|
+
|
88
|
+
rule number
|
89
|
+
('-'? [1-9] [0-9]* / '0') {
|
90
|
+
def eval(env={})
|
91
|
+
text_value.to_i
|
92
|
+
end
|
93
|
+
}
|
94
|
+
end
|
95
|
+
|
96
|
+
rule space
|
97
|
+
' '*
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
grammar SimpleSem
|
2
|
+
include Arithmetic
|
3
|
+
|
4
|
+
rule statement
|
5
|
+
set_stmt / jump_stmt / jumpt_stmt / halt
|
6
|
+
end
|
7
|
+
|
8
|
+
rule halt
|
9
|
+
'halt' {
|
10
|
+
def execute(env={})
|
11
|
+
raise ProgramHalt
|
12
|
+
end
|
13
|
+
}
|
14
|
+
end
|
15
|
+
|
16
|
+
rule set_stmt
|
17
|
+
set_stmt_assign / set_stmt_write / set_stmt_read
|
18
|
+
end
|
19
|
+
|
20
|
+
rule set_stmt_assign
|
21
|
+
'set' space loc:additive comma value:additive {
|
22
|
+
def execute(env)
|
23
|
+
env.data[loc.eval(env)] = value.eval(env)
|
24
|
+
end
|
25
|
+
}
|
26
|
+
end
|
27
|
+
|
28
|
+
rule set_stmt_write
|
29
|
+
'set' space 'write' comma expression {
|
30
|
+
def execute(env)
|
31
|
+
puts expression.eval(env)
|
32
|
+
end
|
33
|
+
}
|
34
|
+
/
|
35
|
+
'set' space 'write' comma '"' string:(!'"' . )* '"' {
|
36
|
+
def execute(env)
|
37
|
+
puts string.text_value
|
38
|
+
end
|
39
|
+
}
|
40
|
+
end
|
41
|
+
|
42
|
+
rule set_stmt_read
|
43
|
+
'set' space loc:additive comma 'read' {
|
44
|
+
def execute(env)
|
45
|
+
print "input: "
|
46
|
+
env.data[loc.eval(env)] = $stdin.gets.strip.to_i
|
47
|
+
end
|
48
|
+
}
|
49
|
+
end
|
50
|
+
|
51
|
+
rule jump_stmt
|
52
|
+
'jump' space loc:additive {
|
53
|
+
def execute(env)
|
54
|
+
env.pc = loc.eval(env)
|
55
|
+
end
|
56
|
+
}
|
57
|
+
end
|
58
|
+
|
59
|
+
rule jumpt_stmt
|
60
|
+
'jumpt' space loc:additive comma expression {
|
61
|
+
def execute(env)
|
62
|
+
if expression.eval(env)
|
63
|
+
env.pc = loc.eval(env)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
}
|
67
|
+
end
|
68
|
+
|
69
|
+
rule primary
|
70
|
+
ip
|
71
|
+
/
|
72
|
+
data_lookup
|
73
|
+
/
|
74
|
+
number
|
75
|
+
/
|
76
|
+
'(' space expression space ')' {
|
77
|
+
def eval(env={})
|
78
|
+
expression.eval(env)
|
79
|
+
end
|
80
|
+
}
|
81
|
+
end
|
82
|
+
|
83
|
+
rule data_lookup
|
84
|
+
'D[' expr:additive ']' {
|
85
|
+
def eval(env)
|
86
|
+
env.data[expr.eval(env)]
|
87
|
+
end
|
88
|
+
}
|
89
|
+
end
|
90
|
+
|
91
|
+
rule ip
|
92
|
+
'ip' {
|
93
|
+
def eval(env)
|
94
|
+
env.pc
|
95
|
+
end
|
96
|
+
}
|
97
|
+
end
|
98
|
+
|
99
|
+
rule comma
|
100
|
+
space ',' space
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'treetop'
|
3
|
+
dir = File.dirname(__FILE__)
|
4
|
+
require File.expand_path("#{dir}/arithmetic_node_classes")
|
5
|
+
Treetop.load File.expand_path("#{dir}/arithmetic")
|
6
|
+
Treetop.load File.expand_path("#{dir}/simple_sem")
|
7
|
+
|
8
|
+
class ProgramHalt < Exception
|
9
|
+
end
|
10
|
+
|
11
|
+
class SimpleSemProgram
|
12
|
+
attr_reader :code
|
13
|
+
attr_accessor :data, :pc
|
14
|
+
|
15
|
+
# Create a SimpleSemProgram instance
|
16
|
+
# Params:
|
17
|
+
# (String)filepath: path to SimpleSem source file.
|
18
|
+
# Optional because it's useful to use in tests without needing to load a file
|
19
|
+
def initialize filepath=nil
|
20
|
+
@code = Array.new
|
21
|
+
if filepath
|
22
|
+
IO.foreach(filepath) do |line|
|
23
|
+
@code << line.split("//", 2)[0].strip # seperate the comment from the code
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
@data = Array.new
|
28
|
+
@pc = 0
|
29
|
+
end
|
30
|
+
|
31
|
+
def run
|
32
|
+
@parser = SimpleSemParser.new
|
33
|
+
|
34
|
+
@pc = 0
|
35
|
+
loop do
|
36
|
+
instruction = @code[@pc] # fetch
|
37
|
+
@pc += 1 # increment
|
38
|
+
begin
|
39
|
+
@parser.parse(instruction).execute(self) # decode and execute
|
40
|
+
rescue ProgramHalt
|
41
|
+
break
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/simplesem.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
set 0, read // first number 'n'
|
2
|
+
set 1, read // second number 'm'
|
3
|
+
set 2, ip + 1 // return pointer for call to GCD
|
4
|
+
jump 6
|
5
|
+
set write, D[0] // print the GCD
|
6
|
+
halt
|
7
|
+
jumpt D[2], D[0] = D[1] // while(m != n)
|
8
|
+
jumpt 10, D[1] < D[0] // if(m < n)
|
9
|
+
set 1, D[1]-D[0]
|
10
|
+
jump 11
|
11
|
+
set 0, D[0]-D[1]
|
12
|
+
jump 6
|
data/simplesem.gemspec
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{simplesem}
|
5
|
+
s.version = "0.1.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Rob Olson"]
|
9
|
+
s.date = %q{2009-02-26}
|
10
|
+
s.default_executable = %q{simplesem}
|
11
|
+
s.description = %q{SIMPLESEM Interpreter}
|
12
|
+
s.email = %q{rko618@gmail.com}
|
13
|
+
s.executables = ["simplesem"]
|
14
|
+
s.extra_rdoc_files = ["LICENSE", "README.textile", "bin/simplesem", "lib/simplesem/arithmetic.treetop", "lib/simplesem/arithmetic_node_classes.rb", "lib/simplesem/simple_sem.treetop", "lib/simplesem/simplesem_program.rb", "lib/simplesem.rb"]
|
15
|
+
s.files = ["LICENSE", "Manifest", "README.textile", "Rakefile", "bin/simplesem", "lib/simplesem/arithmetic.treetop", "lib/simplesem/arithmetic_node_classes.rb", "lib/simplesem/simple_sem.treetop", "lib/simplesem/simplesem_program.rb", "lib/simplesem.rb", "sample_programs/case-statement.txt", "sample_programs/gcd.txt", "sample_programs/hello-world.txt", "sample_programs/while-loop.txt", "simplesem.gemspec", "simplesem.tmproj", "test/simplesem_test.rb", "test/test_helper.rb"]
|
16
|
+
s.has_rdoc = true
|
17
|
+
s.homepage = %q{http://github.com/robolson/simplesem}
|
18
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Simplesem", "--main", "README.textile"]
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
s.rubyforge_project = %q{simplesem}
|
21
|
+
s.rubygems_version = %q{1.3.1}
|
22
|
+
s.summary = %q{SIMPLESEM Interpreter}
|
23
|
+
s.test_files = ["test/simplesem_test.rb", "test/test_helper.rb"]
|
24
|
+
|
25
|
+
if s.respond_to? :specification_version then
|
26
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
27
|
+
s.specification_version = 2
|
28
|
+
|
29
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
30
|
+
s.add_development_dependency(%q<treetop>, [">= 0"])
|
31
|
+
else
|
32
|
+
s.add_dependency(%q<treetop>, [">= 0"])
|
33
|
+
end
|
34
|
+
else
|
35
|
+
s.add_dependency(%q<treetop>, [">= 0"])
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,119 @@
|
|
1
|
+
dir = File.dirname(__FILE__)
|
2
|
+
require File.expand_path("#{dir}/test_helper")
|
3
|
+
libdir = dir + "/../lib"
|
4
|
+
require File.expand_path("#{libdir}/simplesem")
|
5
|
+
|
6
|
+
class SimpleSemParserTest < Test::Unit::TestCase
|
7
|
+
include ParserTestHelper
|
8
|
+
|
9
|
+
def setup
|
10
|
+
@parser = SimpleSemParser.new
|
11
|
+
@ssp = SimpleSemProgram.new
|
12
|
+
@ssp.data[0] = 1
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_set_stmt_assign
|
16
|
+
parse('set 1, D[0]').execute(@ssp)
|
17
|
+
assert_equal [1, 1], @ssp.data
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_set_stmt_write
|
21
|
+
assert_nil parse('set write, "hello world!"').execute(@ssp)
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_jump_stmt
|
25
|
+
parse('jump 5').execute(@ssp)
|
26
|
+
assert_equal 5, @ssp.pc
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_set_to_data_loc
|
30
|
+
parse('set D[0], 2').execute(@ssp)
|
31
|
+
assert_equal 2, @ssp.data[1]
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_complex_expr
|
35
|
+
@ssp.data[1] = 2
|
36
|
+
parse('set 2, D[0]+D[1]*2').execute(@ssp)
|
37
|
+
assert_equal 5, @ssp.data[2]
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_parenthesis
|
41
|
+
@ssp.data[1] = 2
|
42
|
+
parse('set 2, (D[0]+D[1])*2').execute(@ssp)
|
43
|
+
assert_equal 6, @ssp.data[2]
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_set_increment_instr
|
47
|
+
parse('set 0, D[0]+1').execute(@ssp)
|
48
|
+
assert_equal 2, @ssp.data[0]
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_nested_data_lookup
|
52
|
+
@ssp.data[0] = 0
|
53
|
+
@ssp.data[1] = 1
|
54
|
+
parse('set 2, D[D[0]+1]').execute(@ssp)
|
55
|
+
assert_equal 1, @ssp.data[2]
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_instruction_pointer
|
59
|
+
# run two dummy instructions, manually incrementing the program counter
|
60
|
+
@ssp.pc = 1
|
61
|
+
parse('set 0, 0').execute(@ssp)
|
62
|
+
@ssp.pc = 2
|
63
|
+
parse('set 0, ip').execute(@ssp)
|
64
|
+
assert_equal 2, @ssp.data[0] # check that the parser was able to read the ip correctly
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_jump_to_data_loc
|
68
|
+
@ssp.data[0] = 2
|
69
|
+
parse('jump D[0]').execute(@ssp)
|
70
|
+
assert_equal 2, @ssp.pc
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_jumpt_stmt_true
|
74
|
+
@ssp.data[0] = 1
|
75
|
+
parse('jumpt 5, D[0]=D[0]').execute(@ssp)
|
76
|
+
assert_equal 5, @ssp.pc
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_jumpt_stmt_false
|
80
|
+
@ssp.data[0] = 1
|
81
|
+
parse('jumpt 5, D[0]=2').execute(@ssp)
|
82
|
+
assert_equal 0, @ssp.pc # pc should not have changed
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_less_than_comparison
|
86
|
+
# test a jumpt that returns false
|
87
|
+
parse('jumpt 5, D[0] < 0').execute(@ssp)
|
88
|
+
assert_not_equal 5, @ssp.pc
|
89
|
+
|
90
|
+
parse('jumpt 5, D[0] < 2').execute(@ssp)
|
91
|
+
assert_equal 5, @ssp.pc
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_greater_than_comparison
|
95
|
+
parse('jumpt 5, D[0] > 0').execute(@ssp)
|
96
|
+
assert_equal 5, @ssp.pc
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_greater_than_or_eql_comparison
|
100
|
+
parse('jumpt 5, 1 >= D[0]').execute(@ssp)
|
101
|
+
assert_equal 5, @ssp.pc
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_less_than_or_eql_comparison
|
105
|
+
parse('jumpt 5, 0 <= D[0]').execute(@ssp)
|
106
|
+
assert_equal 5, @ssp.pc
|
107
|
+
end
|
108
|
+
|
109
|
+
def test_negative_number
|
110
|
+
parse('set 0, -1').execute(@ssp)
|
111
|
+
assert_equal -1, @ssp.data[0]
|
112
|
+
end
|
113
|
+
|
114
|
+
def test_halt
|
115
|
+
assert_raise ProgramHalt do
|
116
|
+
parse('halt').execute(@ssp)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'treetop'
|
4
|
+
|
5
|
+
module ParserTestHelper
|
6
|
+
def assert_evals_to_self(input)
|
7
|
+
assert_evals_to(input, input)
|
8
|
+
end
|
9
|
+
|
10
|
+
def parse(input)
|
11
|
+
result = @parser.parse(input)
|
12
|
+
unless result
|
13
|
+
puts @parser.terminal_failures.join("\n")
|
14
|
+
end
|
15
|
+
assert !result.nil?
|
16
|
+
result
|
17
|
+
end
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: robolson-simplesem
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rob Olson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-02-26 00:00:00 -08:00
|
13
|
+
default_executable: simplesem
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: treetop
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description: SIMPLESEM Interpreter
|
26
|
+
email: rko618@gmail.com
|
27
|
+
executables:
|
28
|
+
- simplesem
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.textile
|
34
|
+
- bin/simplesem
|
35
|
+
- lib/simplesem/arithmetic.treetop
|
36
|
+
- lib/simplesem/arithmetic_node_classes.rb
|
37
|
+
- lib/simplesem/simple_sem.treetop
|
38
|
+
- lib/simplesem/simplesem_program.rb
|
39
|
+
- lib/simplesem.rb
|
40
|
+
files:
|
41
|
+
- LICENSE
|
42
|
+
- Manifest
|
43
|
+
- README.textile
|
44
|
+
- Rakefile
|
45
|
+
- bin/simplesem
|
46
|
+
- lib/simplesem/arithmetic.treetop
|
47
|
+
- lib/simplesem/arithmetic_node_classes.rb
|
48
|
+
- lib/simplesem/simple_sem.treetop
|
49
|
+
- lib/simplesem/simplesem_program.rb
|
50
|
+
- lib/simplesem.rb
|
51
|
+
- sample_programs/case-statement.txt
|
52
|
+
- sample_programs/gcd.txt
|
53
|
+
- sample_programs/hello-world.txt
|
54
|
+
- sample_programs/while-loop.txt
|
55
|
+
- simplesem.gemspec
|
56
|
+
- simplesem.tmproj
|
57
|
+
- test/simplesem_test.rb
|
58
|
+
- test/test_helper.rb
|
59
|
+
has_rdoc: true
|
60
|
+
homepage: http://github.com/robolson/simplesem
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options:
|
63
|
+
- --line-numbers
|
64
|
+
- --inline-source
|
65
|
+
- --title
|
66
|
+
- Simplesem
|
67
|
+
- --main
|
68
|
+
- README.textile
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: "0"
|
76
|
+
version:
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: "1.2"
|
82
|
+
version:
|
83
|
+
requirements: []
|
84
|
+
|
85
|
+
rubyforge_project: simplesem
|
86
|
+
rubygems_version: 1.2.0
|
87
|
+
signing_key:
|
88
|
+
specification_version: 2
|
89
|
+
summary: SIMPLESEM Interpreter
|
90
|
+
test_files:
|
91
|
+
- test/simplesem_test.rb
|
92
|
+
- test/test_helper.rb
|