poison 0.1.0.dev

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,11 @@
1
+ module Poison
2
+ class Treetop::Runtime::SyntaxNode
3
+ def node
4
+ return unless elements
5
+ all = elements.map { |e| e.node if e }.compact
6
+ return if all.empty?
7
+ return all.first if all.size == 1
8
+ all
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ module Poison
2
+ class Parser
3
+ def initialize(machine, source)
4
+ @parser = machine.new
5
+ @source = source
6
+ end
7
+
8
+ def parse
9
+ @syntax = Syntax.new @parser.parse(@source)
10
+
11
+ unless @syntax
12
+ raise SyntaxError.new(@parser.failure_reason)
13
+ end
14
+ @syntax
15
+ end
16
+ end
17
+ end
@@ -0,0 +1 @@
1
+ require 'poison/bootstrap/library/code_loader'
@@ -0,0 +1,16 @@
1
+ module Poison
2
+ class CodeLoader
3
+ def self.execute_file(name)
4
+ source = File.read name
5
+ execute_script source
6
+ end
7
+
8
+ def self.execute_script(source)
9
+ compiler = Compiler.new source
10
+ ast = compiler.parse
11
+
12
+ # just give some output for now
13
+ ast.graph
14
+ end
15
+ end
16
+ end
File without changes
@@ -0,0 +1,221 @@
1
+ module Poison
2
+ class SyntaxError < Exception; end
3
+
4
+ class Syntax
5
+ def initialize(parse_tree)
6
+ @syntax = parse_tree.node
7
+ end
8
+
9
+ def to_sexp
10
+ @syntax.to_sexp
11
+ end
12
+
13
+ def graph
14
+ @syntax.graph
15
+ end
16
+
17
+ class Node
18
+ def to_sexp
19
+ end
20
+
21
+ def graph
22
+ Rubinius::AST::AsciiGrapher.new(self, Node).print
23
+ end
24
+ end
25
+
26
+ class Script < Node
27
+ attr_accessor :statements
28
+
29
+ def initialize(statements)
30
+ @statements = statements
31
+ end
32
+
33
+ def to_sexp
34
+ [:script, @statements.to_sexp]
35
+ end
36
+ end
37
+
38
+ class Statements < Node
39
+ def initialize(statements)
40
+ @statements = statements
41
+ end
42
+
43
+ def to_sexp
44
+ if @statements.size == 1
45
+ @statements.first.to_sexp
46
+ else
47
+ @statements.map { |s| s.to_sexp }
48
+ end
49
+ end
50
+ end
51
+
52
+ class Assign < Node
53
+ def initialize(left, right)
54
+ @left = left
55
+ @right = right
56
+ end
57
+
58
+ def to_sexp
59
+ [:assign, @left.to_sexp, @right.to_sexp]
60
+ end
61
+ end
62
+
63
+ class BinaryOperator
64
+ def initialize(left, right)
65
+ @left = left
66
+ @right = right
67
+ end
68
+
69
+ def to_sexp
70
+ [sexp_name, @left.to_sexp, @right.to_sexp]
71
+ end
72
+ end
73
+
74
+ class Or < BinaryOperator
75
+ def sexp_name
76
+ :or
77
+ end
78
+ end
79
+
80
+ class And < BinaryOperator
81
+ def sexp_name
82
+ :and
83
+ end
84
+ end
85
+
86
+ class Pipe < BinaryOperator
87
+ def sexp_name
88
+ :pipe
89
+ end
90
+ end
91
+
92
+ class Caret < BinaryOperator
93
+ def sexp_name
94
+ :caret
95
+ end
96
+ end
97
+
98
+ class Amp < BinaryOperator
99
+ def sexp_name
100
+ :amp
101
+ end
102
+ end
103
+
104
+ class Bitl < BinaryOperator
105
+ def sexp_name
106
+ :bitl
107
+ end
108
+ end
109
+
110
+ class Bitr < BinaryOperator
111
+ def sexp_name
112
+ :bitr
113
+ end
114
+ end
115
+
116
+ class Plus < BinaryOperator
117
+ def sexp_name
118
+ :plus
119
+ end
120
+ end
121
+
122
+ class Minus < BinaryOperator
123
+ def sexp_name
124
+ :minus
125
+ end
126
+ end
127
+
128
+ class Times < BinaryOperator
129
+ def sexp_name
130
+ :times
131
+ end
132
+ end
133
+
134
+ class Div < BinaryOperator
135
+ def sexp_name
136
+ :div
137
+ end
138
+ end
139
+
140
+ class Rem < BinaryOperator
141
+ def sexp_name
142
+ :rem
143
+ end
144
+ end
145
+
146
+ class Pow < BinaryOperator
147
+ def sexp_name
148
+ :pow
149
+ end
150
+ end
151
+
152
+ class Expression < Node
153
+ attr_accessor :expression
154
+
155
+ def initialize(expression)
156
+ @expression = expression
157
+ end
158
+
159
+ def to_sexp
160
+ [:expr].concat @expression.map { |e| e.to_sexp }
161
+ end
162
+ end
163
+
164
+ class Message < Node
165
+ attr_accessor :name
166
+
167
+ def initialize(name)
168
+ @name = name
169
+ end
170
+
171
+ def to_sexp
172
+ [:message, [@name, nil, nil]]
173
+ end
174
+ end
175
+
176
+ class Value < Node
177
+ attr_accessor :value
178
+
179
+ def initialize(value)
180
+ @value = value
181
+ end
182
+
183
+ def to_sexp
184
+ [:value, @value.to_sexp]
185
+ end
186
+ end
187
+
188
+ class Literal < Node
189
+ attr_accessor :value
190
+
191
+ def initialize(value)
192
+ @value = value
193
+ end
194
+
195
+ def to_sexp
196
+ [@value, nil, nil]
197
+ end
198
+ end
199
+
200
+ class NilKind < Literal
201
+ def initialize
202
+ @value = nil
203
+ end
204
+ end
205
+
206
+ class Boolean < Literal
207
+ end
208
+
209
+ class Integer < Literal
210
+ end
211
+
212
+ class Real < Literal
213
+ end
214
+
215
+ class Imaginary < Literal
216
+ end
217
+
218
+ class String < Literal
219
+ end
220
+ end
221
+ end
@@ -0,0 +1,14 @@
1
+ module Poison
2
+ module VERSION
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ TINY = 0
6
+ BUILD = 'dev'
7
+
8
+ STRING = [MAJOR, MINOR, TINY, BUILD].join('.')
9
+ end
10
+
11
+ def VERSION.to_s
12
+ self::STRING
13
+ end
14
+ end
@@ -0,0 +1,114 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe "The Assign node" do
4
+ relates "a = 1" do
5
+ parse do
6
+ [:assign, [:expr, [:message, ["a", nil, nil]]],
7
+ [:expr, [:value, [1, nil, nil]]]]
8
+ end
9
+ end
10
+
11
+ relates "a ||= 1" do
12
+ parse do
13
+ [:assign, [:expr, [:message, ["a", nil, nil]]],
14
+ [:or, [:expr, [:message, ["a", nil, nil]]],
15
+ [:expr, [:value, [1, nil, nil]]]]]
16
+ end
17
+ end
18
+
19
+ relates "a &&= 1" do
20
+ parse do
21
+ [:assign, [:expr, [:message, ["a", nil, nil]]],
22
+ [:and, [:expr, [:message, ["a", nil, nil]]],
23
+ [:expr, [:value, [1, nil, nil]]]]]
24
+ end
25
+ end
26
+
27
+ relates "a |= 1" do
28
+ parse do
29
+ [:assign, [:expr, [:message, ["a", nil, nil]]],
30
+ [:pipe, [:expr, [:message, ["a", nil, nil]]],
31
+ [:expr, [:value, [1, nil, nil]]]]]
32
+ end
33
+ end
34
+
35
+ relates "a ^= 1" do
36
+ parse do
37
+ [:assign, [:expr, [:message, ["a", nil, nil]]],
38
+ [:caret, [:expr, [:message, ["a", nil, nil]]],
39
+ [:expr, [:value, [1, nil, nil]]]]]
40
+ end
41
+ end
42
+
43
+ relates "a &= 1" do
44
+ parse do
45
+ [:assign, [:expr, [:message, ["a", nil, nil]]],
46
+ [:amp, [:expr, [:message, ["a", nil, nil]]],
47
+ [:expr, [:value, [1, nil, nil]]]]]
48
+ end
49
+ end
50
+
51
+ relates "a <<= 1" do
52
+ parse do
53
+ [:assign, [:expr, [:message, ["a", nil, nil]]],
54
+ [:bitl, [:expr, [:message, ["a", nil, nil]]],
55
+ [:expr, [:value, [1, nil, nil]]]]]
56
+ end
57
+ end
58
+
59
+ relates "a >>= 1" do
60
+ parse do
61
+ [:assign, [:expr, [:message, ["a", nil, nil]]],
62
+ [:bitr, [:expr, [:message, ["a", nil, nil]]],
63
+ [:expr, [:value, [1, nil, nil]]]]]
64
+ end
65
+ end
66
+
67
+ relates "a += 1" do
68
+ parse do
69
+ [:assign, [:expr, [:message, ["a", nil, nil]]],
70
+ [:plus, [:expr, [:message, ["a", nil, nil]]],
71
+ [:expr, [:value, [1, nil, nil]]]]]
72
+ end
73
+ end
74
+
75
+ relates "a -= 1" do
76
+ parse do
77
+ [:assign, [:expr, [:message, ["a", nil, nil]]],
78
+ [:minus, [:expr, [:message, ["a", nil, nil]]],
79
+ [:expr, [:value, [1, nil, nil]]]]]
80
+ end
81
+ end
82
+
83
+ relates "a *= 1" do
84
+ parse do
85
+ [:assign, [:expr, [:message, ["a", nil, nil]]],
86
+ [:times, [:expr, [:message, ["a", nil, nil]]],
87
+ [:expr, [:value, [1, nil, nil]]]]]
88
+ end
89
+ end
90
+
91
+ relates "a /= 1" do
92
+ parse do
93
+ [:assign, [:expr, [:message, ["a", nil, nil]]],
94
+ [:div, [:expr, [:message, ["a", nil, nil]]],
95
+ [:expr, [:value, [1, nil, nil]]]]]
96
+ end
97
+ end
98
+
99
+ relates "a %= 1" do
100
+ parse do
101
+ [:assign, [:expr, [:message, ["a", nil, nil]]],
102
+ [:rem, [:expr, [:message, ["a", nil, nil]]],
103
+ [:expr, [:value, [1, nil, nil]]]]]
104
+ end
105
+ end
106
+
107
+ relates "a **= 1" do
108
+ parse do
109
+ [:assign, [:expr, [:message, ["a", nil, nil]]],
110
+ [:pow, [:expr, [:message, ["a", nil, nil]]],
111
+ [:expr, [:value, [1, nil, nil]]]]]
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe "The Message node" do
4
+ relates "print" do
5
+ parse do
6
+ [:expr, [:message, ["print", nil, nil]]]
7
+ end
8
+ end
9
+
10
+ relates <<-ruby do
11
+ "hello" print
12
+ ruby
13
+
14
+ parse do
15
+ [:expr, [:value, ["hello", nil, nil]],
16
+ [:message, ["print", nil, nil]]]
17
+ end
18
+ end
19
+
20
+ relates <<-ruby do
21
+ 1 string print
22
+ ruby
23
+
24
+ parse do
25
+ [:expr, [:value, [1, nil, nil]],
26
+ [:message, ["string", nil, nil]],
27
+ [:message, ["print", nil, nil]]]
28
+ end
29
+ end
30
+
31
+ relates <<-ruby do
32
+ name string print
33
+ ruby
34
+
35
+ parse do
36
+ [:expr, [:message, ["name", nil, nil]],
37
+ [:message, ["string", nil, nil]],
38
+ [:message, ["print", nil, nil]]]
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe "The Value node" do
4
+ relates "1" do
5
+ parse do
6
+ [:expr, [:value, [1, nil, nil]]]
7
+ end
8
+ end
9
+
10
+ relates "2.4" do
11
+ parse do
12
+ [:expr, [:value, [2.4, nil, nil]]]
13
+ end
14
+ end
15
+
16
+ relates "3.2i" do
17
+ parse do
18
+ [:expr, [:value, ["3.2i", nil, nil]]]
19
+ end
20
+ end
21
+
22
+ relates "0x42" do
23
+ parse do
24
+ [:expr, [:value, [66, nil, nil]]]
25
+ end
26
+ end
27
+
28
+ relates "nil" do
29
+ parse do
30
+ [:expr, [:value, [nil, nil, nil]]]
31
+ end
32
+ end
33
+
34
+ relates "true" do
35
+ parse do
36
+ [:expr, [:value, [true, nil, nil]]]
37
+ end
38
+ end
39
+
40
+ relates "false" do
41
+ parse do
42
+ [:expr, [:value, [false, nil, nil]]]
43
+ end
44
+ end
45
+
46
+ relates '"hello"' do
47
+ parse do
48
+ [:expr, [:value, ["hello", nil, nil]]]
49
+ end
50
+ end
51
+
52
+ relates "'world'" do
53
+ parse do
54
+ [:expr, [:value, ["world", nil, nil]]]
55
+ end
56
+ end
57
+ end