halunke 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,15 +5,17 @@
5
5
  number = ('+'|'-')?[0-9]+('.'[0-9]+)?;
6
6
  string = '"' [^"]* '"';
7
7
  unassigned_bareword = "'" [a-zA-Z_]+;
8
- bareword = [a-zA-Z_]+;
8
+ bareword = [a-zA-Z_]+ | '+' | '-' | '<' | '>' | '=' | '@';
9
9
  open_paren = '(';
10
10
  close_paren = ')';
11
11
  open_curly = '{';
12
12
  close_curly = '}';
13
13
  open_bracket = '[';
14
14
  close_bracket = ']';
15
+ open_dict_bracket = '@[';
16
+ start_comment = '/*';
17
+ end_comment = '*/';
15
18
  bar = "|";
16
- operator = '+' | '-' | '<' | '>' | '=';
17
19
 
18
20
  main := |*
19
21
 
@@ -27,8 +29,10 @@
27
29
  close_curly => { emit(:CLOSE_CURLY, data[ts...te]) };
28
30
  open_bracket => { emit(:OPEN_BRACKET, data[ts...te]) };
29
31
  close_bracket => { emit(:CLOSE_BRACKET, data[ts...te]) };
32
+ open_dict_bracket => { emit(:OPEN_DICT_BRACKET, data[ts...te]) };
33
+ start_comment => { emit(:START_COMMENT, data[ts...te]) };
34
+ end_comment => { emit(:END_COMMENT, data[ts...te]) };
30
35
  bar => { emit(:BAR, data[ts...te]) };
31
- operator => { emit(:OPERATOR, data[ts ... te]) };
32
36
  space;
33
37
  any => { raise "Could not lex '#{ data[ts...te] }'" };
34
38
 
@@ -1,41 +1,35 @@
1
- require "halunke/runtime"
2
-
3
1
  module Halunke
4
2
  Nodes = Struct.new(:nodes) do
5
- def initialize(nodes = [])
6
- super(nodes)
3
+ def eval(context)
4
+ nodes.map { |node| node.eval(context) }.last
7
5
  end
8
6
 
9
- def concat(other)
10
- Nodes.new(nodes.concat(other.nodes))
7
+ def to_message
8
+ MessageNode.new(nodes)
11
9
  end
12
10
 
13
- def eval(context)
14
- val = nil
15
-
16
- nodes.each do |node|
17
- val = node.eval(context)
18
- end
11
+ def to_array
12
+ ArrayNode.new(nodes)
13
+ end
19
14
 
20
- val
15
+ def to_dictionary
16
+ DictionaryNode.new(nodes)
21
17
  end
22
18
  end
23
19
 
24
- LiteralNode = Struct.new(:value)
25
-
26
- class NumberNode < LiteralNode
20
+ NumberNode = Struct.new(:value) do
27
21
  def eval(context)
28
22
  context["Number"].create_instance(value)
29
23
  end
30
24
  end
31
25
 
32
- class StringNode < LiteralNode
26
+ StringNode = Struct.new(:value) do
33
27
  def eval(context)
34
28
  context["String"].create_instance(value)
35
29
  end
36
30
  end
37
31
 
38
- class BarewordNode < LiteralNode
32
+ BarewordNode = Struct.new(:value) do
39
33
  def eval(context)
40
34
  context[value]
41
35
  end
@@ -43,28 +37,33 @@ module Halunke
43
37
 
44
38
  UnassignedNode = Struct.new(:node) do
45
39
  def eval(context)
46
- raise "Not unassigned: #{node.value} has value #{context[node.value].inspect}" if context.key? node.value
47
40
  context["UnassignedBareword"].create_instance(node.value)
48
41
  end
49
42
  end
50
43
 
51
44
  FunctionNode = Struct.new(:params, :body) do
52
- def eval(_context)
45
+ def eval(context)
53
46
  signature = params.nodes.map(&:node).map(&:value)
54
47
 
55
- HFunction.new(signature, lambda { |context|
56
- body.eval(context)
48
+ context["Function"].new(signature, lambda { |call_context|
49
+ body.eval(call_context)
57
50
  })
58
51
  end
59
52
  end
60
53
 
61
54
  ArrayNode = Struct.new(:nodes) do
62
- def initialize(nodes = [])
63
- super(nodes)
55
+ def eval(context)
56
+ context["Array"].create_instance(nodes.map { |node| node.eval(context) })
64
57
  end
58
+ end
65
59
 
60
+ DictionaryNode = Struct.new(:nodes) do
66
61
  def eval(context)
67
- context["Array"].create_instance(nodes.map { |node| node.eval(context) })
62
+ hash = {}
63
+ nodes.each_slice(2) do |key, value|
64
+ hash[key.eval(context)] = value.eval(context)
65
+ end
66
+ context["Dictionary"].create_instance(hash)
68
67
  end
69
68
  end
70
69
 
@@ -13,7 +13,7 @@ require "halunke/nodes"
13
13
  module Halunke
14
14
  class Parser < Racc::Parser
15
15
 
16
- module_eval(<<'...end grammar.y/module_eval...', 'grammar.y', 64)
16
+ module_eval(<<'...end grammar.y/module_eval...', 'grammar.y', 48)
17
17
 
18
18
  def parse(code)
19
19
  @tokens = Lexer.new.tokenize(code)
@@ -27,72 +27,80 @@ end
27
27
  ##### State transition tables begin ###
28
28
 
29
29
  racc_action_table = [
30
- 8, 9, 10, 6, 13, 5, 20, 11, 12, 7,
31
- 21, 17, 8, 9, 10, 6, 25, 5, 27, 11,
32
- 12, 7, 8, 9, 10, 6, 28, 5, 29, 11,
33
- 12, 7, 8, 9, 10, 6, 25, 5, 31, 11,
34
- 12, 7, 8, 9, 10, 6, nil, 5, nil, 11,
35
- 12, 7, 8, 9, 10, 6, nil, 5, nil, 11,
36
- 12, 7, 8, 9, 10, 6, nil, 5, nil, 11,
37
- 12, 7 ]
30
+ 4, 5, 6, 10, 13, 9, 21, 7, 11, 22,
31
+ 12, 23, 8, 4, 5, 6, 10, 26, 9, 27,
32
+ 7, 11, 28, 12, 29, 8, 4, 5, 6, 10,
33
+ 31, 9, nil, 7, 11, nil, 12, nil, 8, 4,
34
+ 5, 6, 10, nil, 9, nil, 7, 11, nil, 12,
35
+ 17, 8, 4, 5, 6, 10, nil, 9, nil, 7,
36
+ 11, nil, 12, nil, 8, 4, 5, 6, 10, nil,
37
+ 9, nil, 7, 11, nil, 12, nil, 8, 4, 5,
38
+ 6, 10, nil, 9, nil, 7, 11, nil, 12, nil,
39
+ 8, 4, 5, 6, 10, nil, 9, nil, 7, 11,
40
+ nil, 12, nil, 8, 4, 5, 6, 10, nil, 9,
41
+ nil, 7, 11, nil, 12, nil, 8, 4, 5, 6,
42
+ 10, nil, 9, nil, 7, 11, nil, 12, nil, 8 ]
38
43
 
39
44
  racc_action_check = [
40
- 5, 5, 5, 5, 1, 5, 13, 5, 5, 5,
41
- 15, 5, 0, 0, 0, 0, 17, 0, 19, 0,
42
- 0, 0, 3, 3, 3, 3, 22, 3, 23, 3,
43
- 3, 3, 6, 6, 6, 6, 24, 6, 26, 6,
44
- 6, 6, 7, 7, 7, 7, nil, 7, nil, 7,
45
- 7, 7, 16, 16, 16, 16, nil, 16, nil, 16,
46
- 16, 16, 18, 18, 18, 18, nil, 18, nil, 18,
47
- 18, 18 ]
45
+ 0, 0, 0, 0, 1, 0, 13, 0, 0, 15,
46
+ 0, 16, 0, 3, 3, 3, 3, 19, 3, 20,
47
+ 3, 3, 24, 3, 25, 3, 8, 8, 8, 8,
48
+ 30, 8, nil, 8, 8, nil, 8, nil, 8, 9,
49
+ 9, 9, 9, nil, 9, nil, 9, 9, nil, 9,
50
+ 9, 9, 10, 10, 10, 10, nil, 10, nil, 10,
51
+ 10, nil, 10, nil, 10, 11, 11, 11, 11, nil,
52
+ 11, nil, 11, 11, nil, 11, nil, 11, 12, 12,
53
+ 12, 12, nil, 12, nil, 12, 12, nil, 12, nil,
54
+ 12, 17, 17, 17, 17, nil, 17, nil, 17, 17,
55
+ nil, 17, nil, 17, 18, 18, 18, 18, nil, 18,
56
+ nil, 18, 18, nil, 18, nil, 18, 28, 28, 28,
57
+ 28, nil, 28, nil, 28, 28, nil, 28, nil, 28 ]
48
58
 
49
59
  racc_action_pointer = [
50
- 10, 4, nil, 20, nil, -2, 30, 40, nil, nil,
51
- nil, nil, nil, 6, nil, 2, 50, 6, 60, 6,
52
- nil, nil, 18, 15, 26, nil, 32, nil, nil, nil,
53
- nil, nil ]
60
+ -2, 4, nil, 11, nil, nil, nil, nil, 24, 37,
61
+ 50, 63, 76, 6, nil, -6, 3, 89, 102, 6,
62
+ 8, nil, nil, nil, 9, 18, nil, nil, 115, nil,
63
+ 22, nil ]
54
64
 
55
65
  racc_action_default = [
56
- -2, -18, -1, -2, -4, -2, -18, -2, -13, -14,
57
- -15, -16, -17, -18, -3, -18, -2, -10, -2, -18,
58
- 32, -5, -18, -18, -10, -12, -18, -8, -6, -9,
59
- -11, -7 ]
66
+ -2, -14, -1, -2, -4, -5, -6, -7, -2, -2,
67
+ -14, -2, -2, -14, -3, -14, -14, -2, -2, -14,
68
+ -14, 32, -8, -9, -14, -14, -12, -13, -2, -11,
69
+ -14, -10 ]
60
70
 
61
71
  racc_goto_table = [
62
- 2, 23, 1, 14, 18, 15, 16, 19, 30, nil,
63
- nil, nil, nil, nil, nil, nil, 22, nil, 26 ]
72
+ 2, 1, 18, 14, nil, nil, nil, nil, 15, 16,
73
+ nil, 19, 20, nil, nil, nil, nil, 24, 25, nil,
74
+ nil, nil, nil, nil, nil, nil, nil, nil, 30 ]
64
75
 
65
76
  racc_goto_check = [
66
- 2, 6, 1, 2, 3, 2, 5, 2, 6, nil,
67
- nil, nil, nil, nil, nil, nil, 2, nil, 2 ]
77
+ 2, 1, 3, 2, nil, nil, nil, nil, 2, 2,
78
+ nil, 2, 2, nil, nil, nil, nil, 2, 2, nil,
79
+ nil, nil, nil, nil, nil, nil, nil, nil, 2 ]
68
80
 
69
81
  racc_goto_pointer = [
70
- nil, 2, 0, -2, nil, 1, -16, nil ]
82
+ nil, 1, 0, -8 ]
71
83
 
72
84
  racc_goto_default = [
73
- nil, nil, nil, 3, 4, nil, nil, 24 ]
85
+ nil, nil, nil, 3 ]
74
86
 
75
87
  racc_reduce_table = [
76
88
  0, 0, :racc_error,
77
- 1, 15, :_reduce_1,
78
- 0, 16, :_reduce_2,
79
- 2, 16, :_reduce_3,
80
- 1, 17, :_reduce_none,
81
- 3, 17, :_reduce_5,
82
- 4, 17, :_reduce_6,
83
- 4, 17, :_reduce_7,
84
- 3, 17, :_reduce_8,
89
+ 1, 17, :_reduce_1,
90
+ 0, 18, :_reduce_2,
91
+ 2, 18, :_reduce_3,
92
+ 1, 19, :_reduce_4,
93
+ 1, 19, :_reduce_5,
94
+ 1, 19, :_reduce_6,
95
+ 1, 19, :_reduce_7,
96
+ 3, 19, :_reduce_8,
85
97
  3, 19, :_reduce_9,
86
- 0, 20, :_reduce_10,
87
- 2, 20, :_reduce_11,
88
- 1, 21, :_reduce_12,
89
- 1, 18, :_reduce_13,
90
- 1, 18, :_reduce_14,
91
- 1, 18, :_reduce_15,
92
- 1, 18, :_reduce_16,
93
- 1, 18, :_reduce_17 ]
98
+ 6, 19, :_reduce_10,
99
+ 4, 19, :_reduce_11,
100
+ 3, 19, :_reduce_12,
101
+ 3, 19, :_reduce_13 ]
94
102
 
95
- racc_reduce_n = 18
103
+ racc_reduce_n = 14
96
104
 
97
105
  racc_shift_n = 32
98
106
 
@@ -106,13 +114,15 @@ racc_token_table = {
106
114
  :CLOSE_PAREN => 6,
107
115
  :OPEN_CURLY => 7,
108
116
  :CLOSE_CURLY => 8,
109
- :OPERATOR => 9,
110
- :UNASSIGNED_BAREWORD => 10,
111
- :OPEN_BRACKET => 11,
112
- :CLOSE_BRACKET => 12,
113
- :BAR => 13 }
117
+ :UNASSIGNED_BAREWORD => 9,
118
+ :OPEN_BRACKET => 10,
119
+ :CLOSE_BRACKET => 11,
120
+ :OPEN_DICT_BRACKET => 12,
121
+ :BAR => 13,
122
+ :START_COMMENT => 14,
123
+ :END_COMMENT => 15 }
114
124
 
115
- racc_nt_base = 14
125
+ racc_nt_base = 16
116
126
 
117
127
  racc_use_result_var = true
118
128
 
@@ -142,19 +152,17 @@ Racc_token_to_s_table = [
142
152
  "CLOSE_PAREN",
143
153
  "OPEN_CURLY",
144
154
  "CLOSE_CURLY",
145
- "OPERATOR",
146
155
  "UNASSIGNED_BAREWORD",
147
156
  "OPEN_BRACKET",
148
157
  "CLOSE_BRACKET",
158
+ "OPEN_DICT_BRACKET",
149
159
  "BAR",
160
+ "START_COMMENT",
161
+ "END_COMMENT",
150
162
  "$start",
151
163
  "Program",
152
164
  "Expressions",
153
- "Expression",
154
- "Literal",
155
- "Args",
156
- "UnassignedBarewords",
157
- "UnassignedBareword" ]
165
+ "Expression" ]
158
166
 
159
167
  Racc_debug_parser = false
160
168
 
@@ -162,116 +170,93 @@ Racc_debug_parser = false
162
170
 
163
171
  # reduce 0 omitted
164
172
 
165
- module_eval(<<'.,.,', 'grammar.y', 17)
173
+ module_eval(<<'.,.,', 'grammar.y', 19)
166
174
  def _reduce_1(val, _values, result)
167
175
  result = val[0]
168
176
  result
169
177
  end
170
178
  .,.,
171
179
 
172
- module_eval(<<'.,.,', 'grammar.y', 21)
180
+ module_eval(<<'.,.,', 'grammar.y', 23)
173
181
  def _reduce_2(val, _values, result)
174
- result = Nodes.new
182
+ result = Nodes.new([])
175
183
  result
176
184
  end
177
185
  .,.,
178
186
 
179
- module_eval(<<'.,.,', 'grammar.y', 22)
187
+ module_eval(<<'.,.,', 'grammar.y', 24)
180
188
  def _reduce_3(val, _values, result)
181
- result = Nodes.new([val[0]]).concat(val[1])
189
+ result = Nodes.new([val[0]].concat(val[1].nodes))
182
190
  result
183
191
  end
184
192
  .,.,
185
193
 
186
- # reduce 4 omitted
194
+ module_eval(<<'.,.,', 'grammar.y', 28)
195
+ def _reduce_4(val, _values, result)
196
+ result = NumberNode.new(val[0])
197
+ result
198
+ end
199
+ .,.,
187
200
 
188
- module_eval(<<'.,.,', 'grammar.y', 27)
201
+ module_eval(<<'.,.,', 'grammar.y', 29)
189
202
  def _reduce_5(val, _values, result)
190
- result = Halunke::FunctionNode.new(Halunke::ArrayNode.new([]), val[1])
203
+ result = StringNode.new(val[0])
191
204
  result
192
205
  end
193
206
  .,.,
194
207
 
195
- module_eval(<<'.,.,', 'grammar.y', 28)
208
+ module_eval(<<'.,.,', 'grammar.y', 30)
196
209
  def _reduce_6(val, _values, result)
197
- result = Halunke::FunctionNode.new(val[1], val[2])
210
+ result = BarewordNode.new(val[0])
198
211
  result
199
212
  end
200
213
  .,.,
201
214
 
202
- module_eval(<<'.,.,', 'grammar.y', 29)
215
+ module_eval(<<'.,.,', 'grammar.y', 31)
203
216
  def _reduce_7(val, _values, result)
204
- result = Halunke::MessageSendNode.new(val[1], MessageNode.new(val[2].nodes))
217
+ result = UnassignedNode.new(BarewordNode.new(val[0]))
205
218
  result
206
219
  end
207
220
  .,.,
208
221
 
209
- module_eval(<<'.,.,', 'grammar.y', 30)
222
+ module_eval(<<'.,.,', 'grammar.y', 32)
210
223
  def _reduce_8(val, _values, result)
211
- result = ArrayNode.new(val[1].nodes)
224
+ result = Nodes.new([])
212
225
  result
213
226
  end
214
227
  .,.,
215
228
 
216
- module_eval(<<'.,.,', 'grammar.y', 34)
229
+ module_eval(<<'.,.,', 'grammar.y', 33)
217
230
  def _reduce_9(val, _values, result)
218
- result = Halunke::ArrayNode.new(val[1].nodes)
231
+ result = FunctionNode.new(ArrayNode.new([]), val[1])
219
232
  result
220
233
  end
221
234
  .,.,
222
235
 
223
- module_eval(<<'.,.,', 'grammar.y', 38)
236
+ module_eval(<<'.,.,', 'grammar.y', 34)
224
237
  def _reduce_10(val, _values, result)
225
- result = Nodes.new
238
+ result = FunctionNode.new(val[2].to_array, val[4])
226
239
  result
227
240
  end
228
241
  .,.,
229
242
 
230
- module_eval(<<'.,.,', 'grammar.y', 39)
243
+ module_eval(<<'.,.,', 'grammar.y', 35)
231
244
  def _reduce_11(val, _values, result)
232
- result = Nodes.new([val[0]]).concat(val[1])
245
+ result = MessageSendNode.new(val[1], val[2].to_message)
233
246
  result
234
247
  end
235
248
  .,.,
236
249
 
237
- module_eval(<<'.,.,', 'grammar.y', 43)
250
+ module_eval(<<'.,.,', 'grammar.y', 36)
238
251
  def _reduce_12(val, _values, result)
239
- result = UnassignedNode.new(BarewordNode.new(val[0]))
252
+ result = val[1].to_array
240
253
  result
241
254
  end
242
255
  .,.,
243
256
 
244
- module_eval(<<'.,.,', 'grammar.y', 47)
257
+ module_eval(<<'.,.,', 'grammar.y', 37)
245
258
  def _reduce_13(val, _values, result)
246
- result = NumberNode.new(val[0])
247
- result
248
- end
249
- .,.,
250
-
251
- module_eval(<<'.,.,', 'grammar.y', 48)
252
- def _reduce_14(val, _values, result)
253
- result = StringNode.new(val[0])
254
- result
255
- end
256
- .,.,
257
-
258
- module_eval(<<'.,.,', 'grammar.y', 50)
259
- def _reduce_15(val, _values, result)
260
- result = BarewordNode.new(val[0])
261
- result
262
- end
263
- .,.,
264
-
265
- module_eval(<<'.,.,', 'grammar.y', 51)
266
- def _reduce_16(val, _values, result)
267
- result = BarewordNode.new(val[0])
268
- result
269
- end
270
- .,.,
271
-
272
- module_eval(<<'.,.,', 'grammar.y', 52)
273
- def _reduce_17(val, _values, result)
274
- result = UnassignedNode.new(BarewordNode.new(val[0]))
259
+ result = val[1].to_dictionary
275
260
  result
276
261
  end
277
262
  .,.,