simplesem 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
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/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 ADDED
@@ -0,0 +1,152 @@
1
+ h1. SIMPLESEM Interpreter
2
+
3
+ Author: "Rob Olson":http://thinkingdigitally.com
4
+
5
+ h2. Description
6
+
7
+ Interpreter for the SIMPLESEM language.
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.
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
+ bc. $ 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
+ bc. $ simplesem simplesem_file.txt
22
+
23
+ h3. Command Line Options
24
+
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
+
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
+
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
+
33
+ h2. Introduction to SIMPLESEM
34
+
35
+ SIMPLESEM is an abstract semantic processor that is based on the Von Neumann model of the fetch-execute cycle.
36
+
37
+ h3. SET
38
+
39
+ 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.
40
+
41
+ Evaluate the expression (4 * 2) and places the result into location 0:
42
+
43
+ bc. set 0, 4 * 2
44
+
45
+ Assign the value stored at location 0 into location 2:
46
+
47
+ bc. set 2, D[0]
48
+
49
+ h3. Input/Output with SET
50
+
51
+ 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@.
52
+
53
+ Print the value stored at location 0:
54
+
55
+ bc. set write, D[0]
56
+
57
+ Get input from the user and store it at location 1:
58
+
59
+ bc. set 0, read
60
+
61
+ h3. JUMP
62
+
63
+ The @jump@ command performs and unconditional jump to the line number specified.
64
+
65
+ Jump program execution to the address at location 0:
66
+
67
+ bc. jump D[0]
68
+
69
+ h3. JUMPT
70
+
71
+ The @jumpt@ command (pronounced jump-true), is a conditional jump. It only jumps if the expression given is true.
72
+
73
+ Jump to line 7 if the value at <notextile>D[1]</notextile> is equal to the value at <notextile>D[0]</notextile>:
74
+
75
+ bc. jumpt 7, D[1] = D[0]
76
+
77
+ SIMPLESEM supports all the common comparison operators: >, <, >=, <=, !=, and =. Take note that the equality operator is a single '=' sign, not the usual '=='.
78
+
79
+ h3. Comments
80
+
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
+
83
+ bc. // This is line number 0.
84
+ set write, "foo" // a comment after a statement
85
+
86
+ h2. Slightly More Advanced Features of SIMPLESEM
87
+
88
+ h3. Nested Data Lookups
89
+
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
+
92
+ bc. set 5, D[D[0]+1]
93
+
94
+ h3. Indirect Addressing
95
+
96
+ Assigns the value stored at location 15 into the cell whose address is the value stored at location 10:
97
+
98
+ bc. set D[10], D[15]
99
+
100
+ h3. Complex Math Expressions
101
+
102
+ SIMPLESEM supports standard mathematical order of operations. The following statement sets location 1 to 14 as expected:
103
+
104
+ bc. set 1, 2+3*4
105
+
106
+ h3. Using The Instruction Pointer Variable
107
+
108
+ 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.
109
+
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
+
112
+ <pre>
113
+ <code>
114
+ set 0, 0
115
+ set write, "hello world!"
116
+ set 0, D[0]+1
117
+ jumpt ip-3, D[0] < 5
118
+ halt
119
+ </code>
120
+ </pre>
121
+
122
+ h3. A Sample SIMPLESEM Program
123
+
124
+ 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.
125
+
126
+ <pre><code>
127
+ set 0, read // first number 'n'
128
+ set 1, read // second number 'm'
129
+ set 2, ip + 1 // return pointer for call to GCD
130
+ jump 8
131
+ set write, D[0] // print the GCD
132
+ halt
133
+ jumpt D[2], D[0] = D[1] // while(m != n)
134
+ jumpt 12, D[1] < D[0] // if(m < n)
135
+ set 1, D[1]-D[0]
136
+ jump 13
137
+ set 0, D[0]-D[1]
138
+ jump 8
139
+ </code></pre>
140
+
141
+ Running the above program on the command line will give the following output:
142
+ <pre>
143
+ $ simplesem --inspect-history sample_programs/gcd.txt
144
+ input: 15
145
+ input: 35
146
+ 5
147
+
148
+ DATA:
149
+ 0: [15, 10, 5]
150
+ 1: [35, 20, 5]
151
+ 2: [4]
152
+ </pre>
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'rake'
2
+ require 'echoe'
3
+
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
+ p.runtime_dependencies = ["treetop >=1.2.4"]
11
+ end
data/bin/simplesem ADDED
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/env ruby
2
+
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__))
7
+ end
8
+
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
25
+ end
26
+ Trollop::die "must specify a single filename" if ARGV.length != 1
27
+
28
+
29
+ ssp = SimpleSem::Program.new(ARGV.shift)
30
+ ssp.run
31
+
32
+ if opts[:inspect_history]
33
+ puts "\nDATA: \n" + ssp.inspect_data
34
+ elsif opts[:inspect]
35
+ puts "\nDATA: \n" + ssp.inspect_data_with_history
36
+ end
data/lib/simplesem.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'simplesem/version'
2
+ require 'simplesem/simplesem_program'
@@ -0,0 +1,101 @@
1
+ module SimpleSem
2
+ grammar Arithmetic
3
+ rule expression
4
+ comparative / additive
5
+ end
6
+
7
+ rule comparative
8
+ operand_1:additive space operator:comparison_op space operand_2:additive <BinaryOperation>
9
+ end
10
+
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
100
+ end
101
+ end