halunke 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +3 -1
- data/examples/hello.hal +19 -0
- data/examples/web.hal +20 -0
- data/exe/halunke +11 -7
- data/halunke.gemspec +2 -0
- data/lib/halunke/grammar.y +16 -32
- data/lib/halunke/interpreter.rb +56 -2
- data/lib/halunke/lexer.rb +95 -80
- data/lib/halunke/lexer.rl +7 -3
- data/lib/halunke/nodes.rb +24 -25
- data/lib/halunke/parser.rb +97 -112
- data/lib/halunke/runtime.rb +11 -222
- data/lib/halunke/runtime/false.hal +18 -0
- data/lib/halunke/runtime/harray.rb +30 -0
- data/lib/halunke/runtime/hclass.rb +96 -0
- data/lib/halunke/runtime/hdictionary.rb +26 -0
- data/lib/halunke/runtime/hfunction.rb +27 -0
- data/lib/halunke/runtime/hnative_object.rb +21 -0
- data/lib/halunke/runtime/hnumber.rb +41 -0
- data/lib/halunke/runtime/hobject.rb +30 -0
- data/lib/halunke/runtime/hstdout.rb +16 -0
- data/lib/halunke/runtime/hstring.rb +35 -0
- data/lib/halunke/runtime/hunassigned_bareword.rb +19 -0
- data/lib/halunke/runtime/hweb.rb +40 -0
- data/lib/halunke/runtime/true.hal +18 -0
- data/lib/halunke/version.rb +1 -1
- metadata +31 -2
data/lib/halunke/lexer.rl
CHANGED
@@ -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
|
|
data/lib/halunke/nodes.rb
CHANGED
@@ -1,41 +1,35 @@
|
|
1
|
-
require "halunke/runtime"
|
2
|
-
|
3
1
|
module Halunke
|
4
2
|
Nodes = Struct.new(:nodes) do
|
5
|
-
def
|
6
|
-
|
3
|
+
def eval(context)
|
4
|
+
nodes.map { |node| node.eval(context) }.last
|
7
5
|
end
|
8
6
|
|
9
|
-
def
|
10
|
-
|
7
|
+
def to_message
|
8
|
+
MessageNode.new(nodes)
|
11
9
|
end
|
12
10
|
|
13
|
-
def
|
14
|
-
|
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
|
-
|
15
|
+
def to_dictionary
|
16
|
+
DictionaryNode.new(nodes)
|
21
17
|
end
|
22
18
|
end
|
23
19
|
|
24
|
-
|
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
|
-
|
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
|
-
|
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(
|
45
|
+
def eval(context)
|
53
46
|
signature = params.nodes.map(&:node).map(&:value)
|
54
47
|
|
55
|
-
|
56
|
-
body.eval(
|
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
|
63
|
-
|
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
|
-
|
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
|
|
data/lib/halunke/parser.rb
CHANGED
@@ -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',
|
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
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
12,
|
37
|
-
12,
|
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
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
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
|
-
|
51
|
-
|
52
|
-
nil, nil,
|
53
|
-
|
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, -
|
57
|
-
-
|
58
|
-
32, -
|
59
|
-
-
|
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,
|
63
|
-
nil,
|
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,
|
67
|
-
nil,
|
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,
|
82
|
+
nil, 1, 0, -8 ]
|
71
83
|
|
72
84
|
racc_goto_default = [
|
73
|
-
nil, nil, nil, 3
|
85
|
+
nil, nil, nil, 3 ]
|
74
86
|
|
75
87
|
racc_reduce_table = [
|
76
88
|
0, 0, :racc_error,
|
77
|
-
1,
|
78
|
-
0,
|
79
|
-
2,
|
80
|
-
1,
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
3,
|
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
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
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 =
|
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
|
-
:
|
110
|
-
:
|
111
|
-
:
|
112
|
-
:
|
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 =
|
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',
|
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',
|
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',
|
187
|
+
module_eval(<<'.,.,', 'grammar.y', 24)
|
180
188
|
def _reduce_3(val, _values, result)
|
181
|
-
result = Nodes.new([val[0]]
|
189
|
+
result = Nodes.new([val[0]].concat(val[1].nodes))
|
182
190
|
result
|
183
191
|
end
|
184
192
|
.,.,
|
185
193
|
|
186
|
-
|
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',
|
201
|
+
module_eval(<<'.,.,', 'grammar.y', 29)
|
189
202
|
def _reduce_5(val, _values, result)
|
190
|
-
result =
|
203
|
+
result = StringNode.new(val[0])
|
191
204
|
result
|
192
205
|
end
|
193
206
|
.,.,
|
194
207
|
|
195
|
-
module_eval(<<'.,.,', 'grammar.y',
|
208
|
+
module_eval(<<'.,.,', 'grammar.y', 30)
|
196
209
|
def _reduce_6(val, _values, result)
|
197
|
-
result =
|
210
|
+
result = BarewordNode.new(val[0])
|
198
211
|
result
|
199
212
|
end
|
200
213
|
.,.,
|
201
214
|
|
202
|
-
module_eval(<<'.,.,', 'grammar.y',
|
215
|
+
module_eval(<<'.,.,', 'grammar.y', 31)
|
203
216
|
def _reduce_7(val, _values, result)
|
204
|
-
result =
|
217
|
+
result = UnassignedNode.new(BarewordNode.new(val[0]))
|
205
218
|
result
|
206
219
|
end
|
207
220
|
.,.,
|
208
221
|
|
209
|
-
module_eval(<<'.,.,', 'grammar.y',
|
222
|
+
module_eval(<<'.,.,', 'grammar.y', 32)
|
210
223
|
def _reduce_8(val, _values, result)
|
211
|
-
result =
|
224
|
+
result = Nodes.new([])
|
212
225
|
result
|
213
226
|
end
|
214
227
|
.,.,
|
215
228
|
|
216
|
-
module_eval(<<'.,.,', 'grammar.y',
|
229
|
+
module_eval(<<'.,.,', 'grammar.y', 33)
|
217
230
|
def _reduce_9(val, _values, result)
|
218
|
-
result =
|
231
|
+
result = FunctionNode.new(ArrayNode.new([]), val[1])
|
219
232
|
result
|
220
233
|
end
|
221
234
|
.,.,
|
222
235
|
|
223
|
-
module_eval(<<'.,.,', 'grammar.y',
|
236
|
+
module_eval(<<'.,.,', 'grammar.y', 34)
|
224
237
|
def _reduce_10(val, _values, result)
|
225
|
-
result =
|
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',
|
243
|
+
module_eval(<<'.,.,', 'grammar.y', 35)
|
231
244
|
def _reduce_11(val, _values, result)
|
232
|
-
result =
|
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',
|
250
|
+
module_eval(<<'.,.,', 'grammar.y', 36)
|
238
251
|
def _reduce_12(val, _values, result)
|
239
|
-
result =
|
252
|
+
result = val[1].to_array
|
240
253
|
result
|
241
254
|
end
|
242
255
|
.,.,
|
243
256
|
|
244
|
-
module_eval(<<'.,.,', 'grammar.y',
|
257
|
+
module_eval(<<'.,.,', 'grammar.y', 37)
|
245
258
|
def _reduce_13(val, _values, result)
|
246
|
-
result =
|
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
|
.,.,
|