simplesem 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,11 @@
1
+ set 0, read
2
+ jumpt 5, D[0] = 1
3
+ jumpt 7, D[0] = 2
4
+ jumpt 9, D[0] = 3
5
+ halt
6
+ set 1, 1
7
+ jump 4
8
+ set 1, 2
9
+ jump 4
10
+ set 1, 3
11
+ jump 4
@@ -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
@@ -0,0 +1,5 @@
1
+ set 0, 0
2
+ set write, "hello world!"
3
+ set 0, D[0]+1
4
+ jumpt ip-3, D[0] < 5
5
+ halt
@@ -0,0 +1,14 @@
1
+ set 0, 4
2
+ set 1, 1
3
+ set 2, -1
4
+ jumpt 10, D[0] <= D[2]
5
+ jumpt 7, D[0] != 0
6
+ set write, D[1]
7
+ jump 8
8
+ set 1, D[1]+D[0]
9
+ set 0, D[0] - 1
10
+ jump 3
11
+ set write, D[0]
12
+ set write, D[1]
13
+ set write, D[2]
14
+ halt
data/simplesem.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{simplesem}
5
+ s.version = "0.1.4"
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-09-27}
10
+ s.default_executable = %q{simplesem}
11
+ s.description = %q{Interpreter for parsing and executing SIMPLESEM programs}
12
+ s.email = %q{rob@thinkingdigitally.com}
13
+ s.executables = ["simplesem"]
14
+ s.extra_rdoc_files = ["LICENSE", "README.textile", "bin/simplesem", "lib/simplesem.rb", "lib/simplesem/arithmetic.treetop", "lib/simplesem/arithmetic_node_classes.rb", "lib/simplesem/simple_sem.treetop", "lib/simplesem/simplesem_program.rb", "lib/simplesem/version.rb", "lib/trollop/trollop.rb"]
15
+ s.files = ["LICENSE", "Manifest", "README.textile", "Rakefile", "bin/simplesem", "lib/simplesem.rb", "lib/simplesem/arithmetic.treetop", "lib/simplesem/arithmetic_node_classes.rb", "lib/simplesem/simple_sem.treetop", "lib/simplesem/simplesem_program.rb", "lib/simplesem/version.rb", "lib/trollop/trollop.rb", "sample_programs/case-statement.txt", "sample_programs/gcd.txt", "sample_programs/hello-world.txt", "sample_programs/while-loop.txt", "test/simplesem_test.rb", "test/test_helper.rb", "simplesem.gemspec"]
16
+ s.homepage = %q{http://github.com/robolson/simplesem}
17
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Simplesem", "--main", "README.textile"]
18
+ s.require_paths = ["lib"]
19
+ s.rubyforge_project = %q{simplesem}
20
+ s.rubygems_version = %q{1.3.5}
21
+ s.summary = %q{SIMPLESEM Interpreter}
22
+ s.test_files = ["test/test_helper.rb", "test/simplesem_test.rb"]
23
+
24
+ if s.respond_to? :specification_version then
25
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
26
+ s.specification_version = 3
27
+
28
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
29
+ s.add_runtime_dependency(%q<treetop>, [">= 1.2.4"])
30
+ else
31
+ s.add_dependency(%q<treetop>, [">= 1.2.4"])
32
+ end
33
+ else
34
+ s.add_dependency(%q<treetop>, [">= 1.2.4"])
35
+ end
36
+ end
@@ -0,0 +1,140 @@
1
+ require 'test_helper'
2
+ require 'simplesem'
3
+
4
+ class SimpleSemParserTest < Test::Unit::TestCase
5
+ include ParserTestHelper
6
+
7
+ def setup
8
+ @parser = SimpleSem::SimpleSemParser.new
9
+ @ssp = SimpleSem::Program.new
10
+ @ssp.data[0] = [1]
11
+ end
12
+
13
+ def test_version_constant_is_set
14
+ assert_not_nil SimpleSem::VERSION
15
+ end
16
+
17
+ def test_set_stmt_assign
18
+ parse('set 1, D[0]').execute(@ssp)
19
+ assert_equal [[1], [1]], @ssp.data
20
+ end
21
+
22
+ def test_set_stmt_write_string
23
+ out = capture_stdout do
24
+ parse('set write, "Hello World!"').execute(@ssp)
25
+ end
26
+ assert_equal "Hello World!\n", out.string
27
+ end
28
+
29
+ def test_set_stmt_write_expr
30
+ out = capture_stdout do
31
+ parse('set write, 2 > 1').execute(@ssp)
32
+ end
33
+ assert_equal "true\n", out.string
34
+ end
35
+
36
+ def test_set_stmt_read
37
+ fake_in = StringIO.new("2\n3\n")
38
+ $stdin = fake_in
39
+
40
+ capture_stdout do # capture_stdout because we do not want "input:"'s in the test output
41
+ parse('set 1, read').execute(@ssp)
42
+ parse('set 1, read').execute(@ssp)
43
+ end
44
+ assert_equal [2, 3], @ssp.data[1]
45
+ end
46
+
47
+ def test_jump_stmt
48
+ parse('jump 5').execute(@ssp)
49
+ assert_equal 5, @ssp.pc
50
+ end
51
+
52
+ def test_set_to_data_loc
53
+ parse('set D[0], 2').execute(@ssp)
54
+ assert_equal 2, @ssp.data[1].last
55
+ end
56
+
57
+ def test_complex_expr
58
+ @ssp.data[1] = [2]
59
+ parse('set 2, D[0]+D[1]*2').execute(@ssp)
60
+ assert_equal 5, @ssp.data[2].last
61
+ end
62
+
63
+ def test_parenthesis
64
+ @ssp.data[1] = [2]
65
+ parse('set 2, (D[0]+D[1])*2').execute(@ssp)
66
+ assert_equal 6, @ssp.data[2].last
67
+ end
68
+
69
+ def test_set_increment_instr
70
+ parse('set 0, D[0]+1').execute(@ssp)
71
+ assert_equal 2, @ssp.data[0].last
72
+ end
73
+
74
+ def test_nested_data_lookup
75
+ @ssp.data[0] = [0]
76
+ @ssp.data[1] = [1]
77
+ parse('set 2, D[D[0]+1]').execute(@ssp)
78
+ assert_equal 1, @ssp.data[2].last
79
+ end
80
+
81
+ def test_instruction_pointer
82
+ # manually incrementing the program counter is required here
83
+ @ssp.pc = 1
84
+ parse('set 0, ip').execute(@ssp)
85
+ assert_equal 1, @ssp.data[0].last # checking that the parser was able to evaluate ip correctly
86
+ end
87
+
88
+ def test_jump_to_data_loc
89
+ @ssp.data[0] = [2]
90
+ parse('jump D[0]').execute(@ssp)
91
+ assert_equal 2, @ssp.pc
92
+ end
93
+
94
+ def test_jumpt_stmt_true
95
+ @ssp.data[0] = [1]
96
+ parse('jumpt 5, D[0]=D[0]').execute(@ssp)
97
+ assert_equal 5, @ssp.pc
98
+ end
99
+
100
+ def test_jumpt_stmt_false
101
+ @ssp.data[0] = [1]
102
+ parse('jumpt 5, D[0]=2').execute(@ssp)
103
+ assert_equal 0, @ssp.pc # pc should not have changed
104
+ end
105
+
106
+ def test_less_than_comparison
107
+ # test a jumpt that returns false
108
+ parse('jumpt 5, D[0] < 0').execute(@ssp)
109
+ assert_not_equal 5, @ssp.pc
110
+
111
+ parse('jumpt 5, D[0] < 2').execute(@ssp)
112
+ assert_equal 5, @ssp.pc
113
+ end
114
+
115
+ def test_greater_than_comparison
116
+ parse('jumpt 5, D[0] > 0').execute(@ssp)
117
+ assert_equal 5, @ssp.pc
118
+ end
119
+
120
+ def test_greater_than_or_eql_comparison
121
+ parse('jumpt 5, 1 >= D[0]').execute(@ssp)
122
+ assert_equal 5, @ssp.pc
123
+ end
124
+
125
+ def test_less_than_or_eql_comparison
126
+ parse('jumpt 5, 0 <= D[0]').execute(@ssp)
127
+ assert_equal 5, @ssp.pc
128
+ end
129
+
130
+ def test_negative_number
131
+ parse('set 0, -1').execute(@ssp)
132
+ assert_equal -1, @ssp.data[0].last
133
+ end
134
+
135
+ def test_halt
136
+ assert_raise SimpleSem::ProgramHalt do
137
+ parse('halt').execute(@ssp)
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,28 @@
1
+ require 'test/unit'
2
+ require 'stringio'
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
19
+
20
+ module Kernel
21
+ def capture_stdout
22
+ out = StringIO.new
23
+ $stdout = out
24
+ yield
25
+ $stdout = STDOUT
26
+ return out
27
+ end
28
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simplesem
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.4
5
+ platform: ruby
6
+ authors:
7
+ - Rob Olson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-27 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: treetop
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.4
24
+ version:
25
+ description: Interpreter for parsing and executing SIMPLESEM programs
26
+ email: rob@thinkingdigitally.com
27
+ executables:
28
+ - simplesem
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.textile
34
+ - bin/simplesem
35
+ - lib/simplesem.rb
36
+ - lib/simplesem/arithmetic.treetop
37
+ - lib/simplesem/arithmetic_node_classes.rb
38
+ - lib/simplesem/simple_sem.treetop
39
+ - lib/simplesem/simplesem_program.rb
40
+ - lib/simplesem/version.rb
41
+ - lib/trollop/trollop.rb
42
+ files:
43
+ - LICENSE
44
+ - Manifest
45
+ - README.textile
46
+ - Rakefile
47
+ - bin/simplesem
48
+ - lib/simplesem.rb
49
+ - lib/simplesem/arithmetic.treetop
50
+ - lib/simplesem/arithmetic_node_classes.rb
51
+ - lib/simplesem/simple_sem.treetop
52
+ - lib/simplesem/simplesem_program.rb
53
+ - lib/simplesem/version.rb
54
+ - lib/trollop/trollop.rb
55
+ - sample_programs/case-statement.txt
56
+ - sample_programs/gcd.txt
57
+ - sample_programs/hello-world.txt
58
+ - sample_programs/while-loop.txt
59
+ - test/simplesem_test.rb
60
+ - test/test_helper.rb
61
+ - simplesem.gemspec
62
+ has_rdoc: true
63
+ homepage: http://github.com/robolson/simplesem
64
+ licenses: []
65
+
66
+ post_install_message:
67
+ rdoc_options:
68
+ - --line-numbers
69
+ - --inline-source
70
+ - --title
71
+ - Simplesem
72
+ - --main
73
+ - README.textile
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: "0"
81
+ version:
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: "1.2"
87
+ version:
88
+ requirements: []
89
+
90
+ rubyforge_project: simplesem
91
+ rubygems_version: 1.3.5
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: SIMPLESEM Interpreter
95
+ test_files:
96
+ - test/test_helper.rb
97
+ - test/simplesem_test.rb