mirah 0.0.4-java
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +15 -0
- data/README.txt +51 -0
- data/Rakefile +86 -0
- data/bin/duby +10 -0
- data/bin/dubyc +10 -0
- data/bin/dubyp +10 -0
- data/bin/jrubyp +36 -0
- data/bin/mirah +9 -0
- data/bin/mirah.cmd +1 -0
- data/bin/mirahc +9 -0
- data/bin/mirahc.cmd +1 -0
- data/bin/mirahp +9 -0
- data/bin/mirahp.cmd +1 -0
- data/examples/ant/example-build.xml +7 -0
- data/examples/appengine/Rakefile +19 -0
- data/examples/appengine/Readme +29 -0
- data/examples/appengine/src/org/mirah/MirahApp.mirah +57 -0
- data/examples/appengine/src/org/mirah/list.dhtml +15 -0
- data/examples/appengine/war/WEB-INF/lib/dubydatastore.jar +0 -0
- data/examples/bintrees.mirah +66 -0
- data/examples/construction.mirah +8 -0
- data/examples/dynamic.mirah +17 -0
- data/examples/edb.mirah +3 -0
- data/examples/fib.mirah +16 -0
- data/examples/fields.mirah +22 -0
- data/examples/fractal.mirah +55 -0
- data/examples/java_thing.mirah +13 -0
- data/examples/plugins/appengine/Rakefile +55 -0
- data/examples/plugins/appengine/lib/com/google/appengine/ext/duby/db/datastore.rb +375 -0
- data/examples/plugins/appengine/src/com/google/appengine/ext/duby/db/Model.duby +336 -0
- data/examples/plugins/appengine/test/com/google/appengine/ext/duby/db/ModelTest.duby +113 -0
- data/examples/simple_class.mirah +12 -0
- data/examples/sort_closure.mirah +7 -0
- data/examples/swing.mirah +20 -0
- data/examples/tak.mirah +15 -0
- data/examples/test.edb +9 -0
- data/examples/wiki/Rakefile +18 -0
- data/examples/wiki/src/org/mirah/wiki/MirahWiki.duby +324 -0
- data/examples/wiki/src/org/mirah/wiki/edit.eduby.html +42 -0
- data/examples/wiki/src/org/mirah/wiki/error.eduby.html +2 -0
- data/examples/wiki/src/org/mirah/wiki/layout.eduby.html +69 -0
- data/examples/wiki/src/org/mirah/wiki/parser.eduby.html +7 -0
- data/examples/wiki/src/org/mirah/wiki/view.eduby.html +15 -0
- data/examples/wiki/war/WEB-INF/classes/test/HeredocContext.class +0 -0
- data/examples/wiki/war/WEB-INF/classes/test/MirahParser.class +0 -0
- data/examples/wiki/war/WEB-INF/lib/appengine-api.jar +0 -0
- data/examples/wiki/war/WEB-INF/lib/dubydatastore.jar +0 -0
- data/examples/wiki/war/WEB-INF/lib/jmeta-runtime.jar +0 -0
- data/examples/wiki/war/WEB-INF/lib/pegdown-stubs.jar +0 -0
- data/examples/wiki/war/WEB-INF/pegdown.jar +0 -0
- data/examples/wiki/war/app.yaml +21 -0
- data/examples/wiki/war/public/favicon.ico +0 -0
- data/examples/wiki/war/public/images/appengine_duby.png +0 -0
- data/examples/wiki/war/public/images/back.gif +0 -0
- data/examples/wiki/war/public/images/dir.gif +0 -0
- data/examples/wiki/war/public/images/file.gif +0 -0
- data/examples/wiki/war/public/javascripts/prettify.js +61 -0
- data/examples/wiki/war/public/robots.txt +0 -0
- data/examples/wiki/war/public/stylesheets/main.css +156 -0
- data/examples/wiki/war/public/stylesheets/prettify.css +1 -0
- data/examples/wiki/war/public/stylesheets/sh_style.css +66 -0
- data/examples/wiki/war/public/stylesheets/source.css +21 -0
- data/examples/wiki/war/public/wmd/images/bg-fill.png +0 -0
- data/examples/wiki/war/public/wmd/images/bg.png +0 -0
- data/examples/wiki/war/public/wmd/images/blockquote.png +0 -0
- data/examples/wiki/war/public/wmd/images/bold.png +0 -0
- data/examples/wiki/war/public/wmd/images/code.png +0 -0
- data/examples/wiki/war/public/wmd/images/h1.png +0 -0
- data/examples/wiki/war/public/wmd/images/hr.png +0 -0
- data/examples/wiki/war/public/wmd/images/img.png +0 -0
- data/examples/wiki/war/public/wmd/images/italic.png +0 -0
- data/examples/wiki/war/public/wmd/images/link.png +0 -0
- data/examples/wiki/war/public/wmd/images/ol.png +0 -0
- data/examples/wiki/war/public/wmd/images/redo.png +0 -0
- data/examples/wiki/war/public/wmd/images/separator.png +0 -0
- data/examples/wiki/war/public/wmd/images/ul.png +0 -0
- data/examples/wiki/war/public/wmd/images/undo.png +0 -0
- data/examples/wiki/war/public/wmd/images/wmd-on.png +0 -0
- data/examples/wiki/war/public/wmd/images/wmd.png +0 -0
- data/examples/wiki/war/public/wmd/showdown.js +421 -0
- data/examples/wiki/war/public/wmd/wmd-base.js +1799 -0
- data/examples/wiki/war/public/wmd/wmd-plus.js +311 -0
- data/examples/wiki/war/public/wmd/wmd.js +73 -0
- data/javalib/JRubyParser.jar +0 -0
- data/javalib/dynalang-invoke-0.1.jar +0 -0
- data/javalib/mirah-bootstrap.jar +0 -0
- data/javalib/mirah-parser.jar +0 -0
- data/lib/duby.rb +2 -0
- data/lib/mirah.rb +338 -0
- data/lib/mirah/appengine_tasks.rb +146 -0
- data/lib/mirah/ast.rb +615 -0
- data/lib/mirah/ast/call.rb +307 -0
- data/lib/mirah/ast/class.rb +311 -0
- data/lib/mirah/ast/flow.rb +364 -0
- data/lib/mirah/ast/intrinsics.rb +470 -0
- data/lib/mirah/ast/literal.rb +154 -0
- data/lib/mirah/ast/local.rb +89 -0
- data/lib/mirah/ast/method.rb +360 -0
- data/lib/mirah/ast/scope.rb +208 -0
- data/lib/mirah/ast/structure.rb +226 -0
- data/lib/mirah/ast/type.rb +130 -0
- data/lib/mirah/compiler.rb +341 -0
- data/lib/mirah/env.rb +33 -0
- data/lib/mirah/jvm/base.rb +258 -0
- data/lib/mirah/jvm/compiler.rb +885 -0
- data/lib/mirah/jvm/method_lookup.rb +203 -0
- data/lib/mirah/jvm/source_compiler.rb +737 -0
- data/lib/mirah/jvm/source_generator/builder.rb +444 -0
- data/lib/mirah/jvm/source_generator/loops.rb +110 -0
- data/lib/mirah/jvm/source_generator/precompile.rb +188 -0
- data/lib/mirah/jvm/source_generator/typer.rb +11 -0
- data/lib/mirah/jvm/typer.rb +151 -0
- data/lib/mirah/jvm/types.rb +416 -0
- data/lib/mirah/jvm/types/basic_types.rb +33 -0
- data/lib/mirah/jvm/types/boolean.rb +17 -0
- data/lib/mirah/jvm/types/enumerable.rb +65 -0
- data/lib/mirah/jvm/types/extensions.rb +86 -0
- data/lib/mirah/jvm/types/factory.rb +186 -0
- data/lib/mirah/jvm/types/floats.rb +86 -0
- data/lib/mirah/jvm/types/integers.rb +171 -0
- data/lib/mirah/jvm/types/intrinsics.rb +376 -0
- data/lib/mirah/jvm/types/literals.rb +74 -0
- data/lib/mirah/jvm/types/methods.rb +614 -0
- data/lib/mirah/jvm/types/number.rb +143 -0
- data/lib/mirah/nbcompiler.rb +29 -0
- data/lib/mirah/plugin/edb.rb +29 -0
- data/lib/mirah/plugin/gwt.rb +173 -0
- data/lib/mirah/plugin/java.rb +55 -0
- data/lib/mirah/transform.rb +266 -0
- data/lib/mirah/transform2.rb +728 -0
- data/lib/mirah/typer.rb +407 -0
- data/lib/mirah_task.rb +107 -0
- data/test/test_ast.rb +359 -0
- data/test/test_compilation.rb +112 -0
- data/test/test_env.rb +42 -0
- data/test/test_gwt.rb +58 -0
- data/test/test_java_typer.rb +183 -0
- data/test/test_javac_compiler.rb +63 -0
- data/test/test_jvm_compiler.rb +2607 -0
- data/test/test_typer.rb +221 -0
- metadata +235 -0
data/test/test_ast.rb
ADDED
@@ -0,0 +1,359 @@
|
|
1
|
+
require 'mirah'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'jruby'
|
4
|
+
|
5
|
+
class TestAst < Test::Unit::TestCase
|
6
|
+
include Duby
|
7
|
+
|
8
|
+
def test_args
|
9
|
+
new_ast = AST.parse("def foo(a, *c, &d); end").body[0]
|
10
|
+
arguments = new_ast.arguments
|
11
|
+
|
12
|
+
assert_not_nil(arguments)
|
13
|
+
inspected = "Arguments\n RequiredArgument(a)\n RestArgument(c)\n BlockArgument(d)"
|
14
|
+
assert_equal(inspected, arguments.inspect)
|
15
|
+
|
16
|
+
assert(AST::Arguments === arguments)
|
17
|
+
children = arguments.children
|
18
|
+
assert_not_nil(children)
|
19
|
+
assert_equal(5, children.size)
|
20
|
+
assert(Array === children[0])
|
21
|
+
assert(AST::RequiredArgument === children[0][0])
|
22
|
+
assert_equal("a", children[0][0].name)
|
23
|
+
assert_equal(arguments, children[0][0].parent)
|
24
|
+
assert(AST::RestArgument === children[2])
|
25
|
+
assert_equal("c", children[2].name)
|
26
|
+
assert_equal(arguments, children[2].parent)
|
27
|
+
assert(AST::BlockArgument === children[4])
|
28
|
+
assert_equal("d", children[4].name)
|
29
|
+
assert_equal(arguments, children[4].parent)
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_locals
|
33
|
+
new_ast = AST.parse("a = 1; a").body
|
34
|
+
|
35
|
+
assert_not_nil(new_ast)
|
36
|
+
assert(AST::Body === new_ast)
|
37
|
+
inspected = "Body\n LocalAssignment(name = a, scope = Script, captured = false)\n Fixnum(1)\n FunctionalCall(a)"
|
38
|
+
assert_equal(inspected, new_ast.inspect)
|
39
|
+
|
40
|
+
asgn = new_ast[0]
|
41
|
+
var = new_ast[1]
|
42
|
+
|
43
|
+
assert(AST::LocalAssignment === asgn)
|
44
|
+
assert_equal("a", asgn.name)
|
45
|
+
assert(AST::Fixnum === asgn.value)
|
46
|
+
assert(AST::FunctionalCall === var)
|
47
|
+
assert_equal("a", var.name)
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_fields
|
51
|
+
new_ast = AST.parse("@a = 1; @a").body
|
52
|
+
|
53
|
+
assert_not_nil(new_ast)
|
54
|
+
assert(AST::Body === new_ast)
|
55
|
+
inspected = "Body\n FieldAssignment(a)\n Fixnum(1)\n Field(a)"
|
56
|
+
assert_equal(inspected, new_ast.inspect)
|
57
|
+
|
58
|
+
asgn = new_ast[0]
|
59
|
+
var = new_ast[1]
|
60
|
+
|
61
|
+
assert(AST::FieldAssignment === asgn)
|
62
|
+
assert_equal("a", asgn.name)
|
63
|
+
assert(AST::Fixnum === asgn.value)
|
64
|
+
assert(AST::Field === var)
|
65
|
+
assert_equal("a", var.name)
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_array
|
69
|
+
new_ast = AST.parse("[a = 1, 1]").body[0]
|
70
|
+
|
71
|
+
assert_not_nil(new_ast)
|
72
|
+
assert(AST::Array === new_ast)
|
73
|
+
assert_equal("Array\n LocalAssignment(name = a, scope = Script, captured = false)\n Fixnum(1)\n Fixnum(1)", new_ast.inspect)
|
74
|
+
|
75
|
+
assert(AST::LocalAssignment === new_ast[0])
|
76
|
+
assert(AST::Fixnum === new_ast[1])
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_call
|
80
|
+
new_ast = AST.parse("1.foo(1)").body[0]
|
81
|
+
|
82
|
+
assert_not_nil(new_ast)
|
83
|
+
assert(AST::Call === new_ast)
|
84
|
+
assert_equal("Call(foo)\n Fixnum(1)\n Fixnum(1)", new_ast.inspect)
|
85
|
+
|
86
|
+
assert_equal("foo", new_ast.name)
|
87
|
+
assert(AST::Fixnum === new_ast.target)
|
88
|
+
assert_not_nil(new_ast.parameters)
|
89
|
+
assert_equal(1, new_ast.parameters.size)
|
90
|
+
assert(AST::Fixnum === new_ast.parameters[0])
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_fcall
|
94
|
+
new_ast = AST.parse("foo(1)").body[0]
|
95
|
+
|
96
|
+
assert_not_nil(new_ast)
|
97
|
+
assert(AST::FunctionalCall === new_ast)
|
98
|
+
assert_equal("FunctionalCall(foo)\n Fixnum(1)", new_ast.inspect)
|
99
|
+
|
100
|
+
assert_equal("foo", new_ast.name)
|
101
|
+
assert_not_nil(new_ast.parameters)
|
102
|
+
assert_equal(1, new_ast.parameters.size)
|
103
|
+
assert(AST::Fixnum === new_ast.parameters[0])
|
104
|
+
end
|
105
|
+
|
106
|
+
def test_if
|
107
|
+
new_ast = AST.parse("if 1; 2; elsif 3; 4; else; 5; end").body[0]
|
108
|
+
|
109
|
+
assert_not_nil(new_ast)
|
110
|
+
assert(AST::If === new_ast)
|
111
|
+
assert_equal("If\n Condition\n Fixnum(1)\n Fixnum(2)\n If\n Condition\n Fixnum(3)\n Fixnum(4)\n Fixnum(5)", new_ast.inspect)
|
112
|
+
|
113
|
+
assert(AST::Condition === new_ast.condition)
|
114
|
+
assert(AST::Fixnum === new_ast.condition.predicate)
|
115
|
+
assert(AST::Fixnum === new_ast.body)
|
116
|
+
assert(AST::If === new_ast.else)
|
117
|
+
assert(AST::Condition === new_ast.else.condition)
|
118
|
+
assert(AST::Fixnum === new_ast.else.condition.predicate)
|
119
|
+
assert(AST::Fixnum === new_ast.else.body)
|
120
|
+
assert(AST::Fixnum === new_ast.else.else)
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_begin
|
124
|
+
new_ast = AST.parse("begin; 1; 2; end").body
|
125
|
+
|
126
|
+
assert_not_nil(new_ast)
|
127
|
+
assert_equal("Body\n Fixnum(1)\n Fixnum(2)", new_ast.inspect)
|
128
|
+
assert(AST::Body === new_ast)
|
129
|
+
assert(AST::Fixnum === new_ast[0])
|
130
|
+
|
131
|
+
new_ast = AST.parse("begin; 1; end").body[0]
|
132
|
+
assert(AST::Fixnum === new_ast)
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_block
|
136
|
+
new_ast = AST.parse("1; 2").body
|
137
|
+
|
138
|
+
assert_not_nil(new_ast)
|
139
|
+
assert_equal("Body\n Fixnum(1)\n Fixnum(2)", new_ast.inspect)
|
140
|
+
assert(AST::Body === new_ast)
|
141
|
+
assert(AST::Fixnum === new_ast[0])
|
142
|
+
|
143
|
+
new_ast = AST.parse("1").body[0]
|
144
|
+
assert(AST::Fixnum === new_ast)
|
145
|
+
end
|
146
|
+
|
147
|
+
def test_fixnum
|
148
|
+
new_ast = AST.parse("1").body[0]
|
149
|
+
|
150
|
+
assert_not_nil(new_ast)
|
151
|
+
assert_equal("Fixnum(1)", new_ast.inspect)
|
152
|
+
assert(AST::Fixnum === new_ast)
|
153
|
+
assert_equal(1, new_ast.literal)
|
154
|
+
end
|
155
|
+
|
156
|
+
def test_float
|
157
|
+
new_ast = AST.parse("1.0").body[0]
|
158
|
+
|
159
|
+
assert_not_nil(new_ast)
|
160
|
+
assert_equal("Float(1.0)", new_ast.inspect)
|
161
|
+
assert(AST::Float === new_ast)
|
162
|
+
assert_equal(1.0, new_ast.literal)
|
163
|
+
end
|
164
|
+
|
165
|
+
def test_class
|
166
|
+
new_ast = AST.parse("class Foo < Bar; 1; 2; end").body[0]
|
167
|
+
|
168
|
+
assert_not_nil(new_ast)
|
169
|
+
assert_equal("ClassDefinition(Foo)\n Constant(Bar)\n Body\n Body\n Fixnum(1)\n Fixnum(2)", new_ast.inspect)
|
170
|
+
assert(AST::ClassDefinition === new_ast)
|
171
|
+
assert_equal("Foo", new_ast.name)
|
172
|
+
|
173
|
+
assert(AST::Body === new_ast.body)
|
174
|
+
assert(AST::Fixnum === new_ast.body[0][0])
|
175
|
+
|
176
|
+
new_ast = AST.parse("class Foo < Bar; def foo; end; end").body[0]
|
177
|
+
|
178
|
+
assert_not_nil(new_ast)
|
179
|
+
assert_equal("ClassDefinition(Foo)\n Constant(Bar)\n Body\n MethodDefinition(foo)\n {:return=>nil}\n Arguments\n Null(nil)", new_ast.inspect)
|
180
|
+
assert_equal(new_ast, new_ast.body.parent)
|
181
|
+
end
|
182
|
+
|
183
|
+
def test_defn
|
184
|
+
new_ast = AST.parse("def foo(a, b); 1; end").body[0]
|
185
|
+
|
186
|
+
assert_not_nil(new_ast)
|
187
|
+
assert_equal("MethodDefinition(foo)\n {:return=>nil}\n Arguments\n RequiredArgument(a)\n RequiredArgument(b)\n Fixnum(1)", new_ast.inspect)
|
188
|
+
assert(AST::MethodDefinition === new_ast)
|
189
|
+
assert_equal("foo", new_ast.name)
|
190
|
+
assert_not_nil(new_ast.signature)
|
191
|
+
assert_equal(1, new_ast.signature.size)
|
192
|
+
assert(nil === new_ast.signature[:return])
|
193
|
+
assert(AST::Arguments === new_ast.arguments)
|
194
|
+
assert(AST::Fixnum === new_ast.body)
|
195
|
+
|
196
|
+
new_ast = AST.parse("def foo; end").body[0]
|
197
|
+
|
198
|
+
assert_not_nil(new_ast)
|
199
|
+
assert_equal("MethodDefinition(foo)\n {:return=>nil}\n Arguments\n Null(nil)", new_ast.inspect)
|
200
|
+
assert_not_nil(new_ast.arguments)
|
201
|
+
assert_equal([], new_ast.arguments.args)
|
202
|
+
assert_equal(nil, new_ast.arguments.opt_args)
|
203
|
+
assert_equal(nil, new_ast.arguments.rest_arg)
|
204
|
+
assert_equal(nil, new_ast.arguments.block_arg)
|
205
|
+
|
206
|
+
new_ast = AST.parse("def foo(a, b):int; 1; end").body[0]
|
207
|
+
assert_equal("FunctionalCall(int)", new_ast.return_type.to_s)
|
208
|
+
end
|
209
|
+
|
210
|
+
def test_defs
|
211
|
+
new_ast = AST.parse("def self.foo(a, b); 1; end").body[0]
|
212
|
+
|
213
|
+
assert_not_nil(new_ast)
|
214
|
+
inspected = "StaticMethodDefinition(foo)\n {:return=>nil}\n Arguments\n RequiredArgument(a)\n RequiredArgument(b)\n Fixnum(1)"
|
215
|
+
assert_equal(inspected, new_ast.inspect)
|
216
|
+
assert(AST::StaticMethodDefinition === new_ast)
|
217
|
+
assert_equal("foo", new_ast.name)
|
218
|
+
assert_not_nil(new_ast.signature)
|
219
|
+
assert_equal(1, new_ast.signature.size)
|
220
|
+
assert(nil === new_ast.signature[:return])
|
221
|
+
assert(AST::Arguments === new_ast.arguments)
|
222
|
+
assert(AST::Fixnum === new_ast.body)
|
223
|
+
|
224
|
+
new_ast = AST.parse("def self.foo; end").body[0]
|
225
|
+
|
226
|
+
assert_not_nil(new_ast)
|
227
|
+
assert_equal("StaticMethodDefinition(foo)\n {:return=>nil}\n Arguments\n Null(nil)", new_ast.inspect)
|
228
|
+
assert_not_nil(new_ast.arguments)
|
229
|
+
assert_equal([], new_ast.arguments.args)
|
230
|
+
assert_equal(nil, new_ast.arguments.opt_args)
|
231
|
+
assert_equal(nil, new_ast.arguments.rest_arg)
|
232
|
+
assert_equal(nil, new_ast.arguments.block_arg)
|
233
|
+
end
|
234
|
+
|
235
|
+
def test_return
|
236
|
+
new_ast = AST.parse("return 1").body[0]
|
237
|
+
|
238
|
+
assert_not_nil(new_ast)
|
239
|
+
inspected = "Return\n Fixnum(1)"
|
240
|
+
assert_equal(inspected, new_ast.inspect)
|
241
|
+
assert(AST::Return === new_ast)
|
242
|
+
assert(AST::Fixnum === new_ast.value)
|
243
|
+
end
|
244
|
+
|
245
|
+
def test_vcall
|
246
|
+
new_ast = AST.parse("foo").body[0]
|
247
|
+
|
248
|
+
assert_not_nil(new_ast)
|
249
|
+
assert(AST::FunctionalCall === new_ast)
|
250
|
+
assert_equal("FunctionalCall(foo)", new_ast.inspect)
|
251
|
+
|
252
|
+
assert_equal("foo", new_ast.name)
|
253
|
+
assert_not_nil(new_ast.parameters)
|
254
|
+
assert_equal(0, new_ast.parameters.size)
|
255
|
+
end
|
256
|
+
|
257
|
+
def test_while
|
258
|
+
new_ast = AST.parse("while 1; 2; end").body[0]
|
259
|
+
|
260
|
+
assert_not_nil(new_ast)
|
261
|
+
assert(AST::Loop === new_ast)
|
262
|
+
assert_equal("Loop(check_first = true, negative = false)\n Body\n Condition\n Fixnum(1)\n Body\n Fixnum(2)\n Body", new_ast.inspect)
|
263
|
+
assert(new_ast.check_first?)
|
264
|
+
assert(!new_ast.negative?)
|
265
|
+
assert(AST::Condition === new_ast.condition)
|
266
|
+
assert(AST::Fixnum === new_ast.condition.predicate)
|
267
|
+
assert(AST::Fixnum === new_ast.body)
|
268
|
+
|
269
|
+
new_ast = AST.parse("begin; 2; end while 1").body[0]
|
270
|
+
|
271
|
+
assert_not_nil(new_ast)
|
272
|
+
assert(AST::Loop === new_ast)
|
273
|
+
assert_equal("Loop(check_first = false, negative = false)\n Body\n Condition\n Fixnum(1)\n Body\n Fixnum(2)\n Body", new_ast.inspect)
|
274
|
+
assert(!new_ast.check_first?)
|
275
|
+
assert(!new_ast.negative?)
|
276
|
+
assert(AST::Condition === new_ast.condition)
|
277
|
+
assert(AST::Fixnum === new_ast.condition.predicate)
|
278
|
+
assert(AST::Fixnum === new_ast.body)
|
279
|
+
end
|
280
|
+
|
281
|
+
def test_until
|
282
|
+
new_ast = AST.parse("until 1; 2; end").body[0]
|
283
|
+
|
284
|
+
assert_not_nil(new_ast)
|
285
|
+
assert(AST::Loop === new_ast)
|
286
|
+
assert_equal("Loop(check_first = true, negative = true)\n Body\n Condition\n Fixnum(1)\n Body\n Fixnum(2)\n Body", new_ast.inspect)
|
287
|
+
assert(new_ast.check_first?)
|
288
|
+
assert(new_ast.negative?)
|
289
|
+
assert(AST::Condition === new_ast.condition)
|
290
|
+
assert(AST::Fixnum === new_ast.condition.predicate)
|
291
|
+
assert(AST::Fixnum === new_ast.body)
|
292
|
+
|
293
|
+
new_ast = AST.parse("begin; 2; end until 1").body[0]
|
294
|
+
|
295
|
+
assert_not_nil(new_ast)
|
296
|
+
assert(AST::Loop === new_ast)
|
297
|
+
assert_equal("Loop(check_first = false, negative = true)\n Body\n Condition\n Fixnum(1)\n Body\n Fixnum(2)\n Body", new_ast.inspect)
|
298
|
+
assert(!new_ast.check_first?)
|
299
|
+
assert(new_ast.negative?)
|
300
|
+
assert(AST::Condition === new_ast.condition)
|
301
|
+
assert(AST::Fixnum === new_ast.condition.predicate)
|
302
|
+
assert(AST::Fixnum === new_ast.body)
|
303
|
+
end
|
304
|
+
|
305
|
+
def test_string
|
306
|
+
new_ast = AST.parse("'foo'").body[0]
|
307
|
+
|
308
|
+
assert_not_nil(new_ast)
|
309
|
+
assert(AST::String === new_ast)
|
310
|
+
assert_equal("String(\"foo\")", new_ast.inspect)
|
311
|
+
assert_equal("foo", new_ast.literal)
|
312
|
+
end
|
313
|
+
|
314
|
+
def test_root
|
315
|
+
new_ast = AST.parse("1").body[0]
|
316
|
+
|
317
|
+
assert_not_nil(new_ast)
|
318
|
+
assert(AST::Fixnum === new_ast)
|
319
|
+
end
|
320
|
+
|
321
|
+
def test_boolean
|
322
|
+
new_ast1 = AST.parse("true").body[0]
|
323
|
+
new_ast2 = AST.parse("false").body[0]
|
324
|
+
|
325
|
+
assert_not_nil(new_ast1)
|
326
|
+
assert_not_nil(new_ast2)
|
327
|
+
assert(AST::Boolean === new_ast1)
|
328
|
+
assert(AST::Boolean === new_ast2)
|
329
|
+
assert(new_ast1.literal)
|
330
|
+
assert(!new_ast2.literal)
|
331
|
+
end
|
332
|
+
|
333
|
+
def test_return
|
334
|
+
new_ast = AST.parse("return 1").body[0]
|
335
|
+
|
336
|
+
assert_not_nil(new_ast)
|
337
|
+
assert(AST::Return === new_ast)
|
338
|
+
assert(AST::Fixnum === new_ast.value)
|
339
|
+
assert(1, new_ast.value.literal)
|
340
|
+
end
|
341
|
+
|
342
|
+
def test_empty_array
|
343
|
+
new_ast = AST.parse("int[5]").body[0]
|
344
|
+
|
345
|
+
assert_not_nil(new_ast)
|
346
|
+
assert(AST::EmptyArray === new_ast)
|
347
|
+
assert_equal(5, new_ast.size.literal)
|
348
|
+
assert_equal(AST.type(:int), new_ast.inferred_type)
|
349
|
+
end
|
350
|
+
|
351
|
+
def test_block_comment
|
352
|
+
new_ast = AST.parse("/* foo\nbar*/1").body[0]
|
353
|
+
|
354
|
+
assert_not_nil(new_ast)
|
355
|
+
assert_equal("Fixnum(1)", new_ast.inspect)
|
356
|
+
assert(AST::Fixnum === new_ast)
|
357
|
+
assert_equal(1, new_ast.literal)
|
358
|
+
end
|
359
|
+
end
|
@@ -0,0 +1,112 @@
|
|
1
|
+
require 'mirah'
|
2
|
+
require 'mirah/compiler'
|
3
|
+
require 'test/unit'
|
4
|
+
require 'jruby'
|
5
|
+
|
6
|
+
class TestAst < Test::Unit::TestCase
|
7
|
+
include Duby
|
8
|
+
|
9
|
+
class MockCompiler
|
10
|
+
attr_accessor :calls
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
@calls = []
|
14
|
+
end
|
15
|
+
def compile(ast)
|
16
|
+
ast.compile(self, true)
|
17
|
+
end
|
18
|
+
|
19
|
+
def line(num)
|
20
|
+
# ignore newlines
|
21
|
+
end
|
22
|
+
|
23
|
+
def method_missing(sym, *args, &block)
|
24
|
+
calls << [sym, *args]
|
25
|
+
block.call if block
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class ClassComparison
|
30
|
+
def initialize(klass)
|
31
|
+
@class = klass
|
32
|
+
end
|
33
|
+
|
34
|
+
def ==(other)
|
35
|
+
other.kind_of?(@class)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def a(klass)
|
40
|
+
ClassComparison.new(klass)
|
41
|
+
end
|
42
|
+
|
43
|
+
def setup
|
44
|
+
@compiler = MockCompiler.new
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_fixnum
|
48
|
+
new_ast = AST.parse("1").body[0]
|
49
|
+
|
50
|
+
new_ast.compile(@compiler, true)
|
51
|
+
|
52
|
+
assert_equal([[:fixnum, nil, 1]], @compiler.calls)
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_string
|
56
|
+
new_ast = AST.parse("'foo'").body[0]
|
57
|
+
|
58
|
+
new_ast.compile(@compiler, true)
|
59
|
+
|
60
|
+
assert_equal([[:string, "foo"]], @compiler.calls)
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_float
|
64
|
+
new_ast = AST.parse("1.0").body[0]
|
65
|
+
|
66
|
+
new_ast.compile(@compiler, true)
|
67
|
+
|
68
|
+
assert_equal([[:float, nil, 1.0]], @compiler.calls)
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_boolean
|
72
|
+
new_ast = AST.parse("true").body[0]
|
73
|
+
|
74
|
+
new_ast.compile(@compiler, true)
|
75
|
+
|
76
|
+
assert_equal([[:boolean, true]], @compiler.calls)
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_local
|
80
|
+
new_ast = AST.parse("a = 1").body[0]
|
81
|
+
|
82
|
+
new_ast.compile(@compiler, true)
|
83
|
+
|
84
|
+
assert_equal([[:local_assign, a(Duby::AST::StaticScope), "a", nil, true, AST.fixnum(nil, nil, 1)]], @compiler.calls)
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_local_typed
|
88
|
+
new_ast = AST.parse("a = 1").body[0]
|
89
|
+
typer = Typer::Simple.new(:bar)
|
90
|
+
new_ast.infer(typer)
|
91
|
+
new_ast.compile(@compiler, true)
|
92
|
+
|
93
|
+
assert_equal([[:local_assign, a(Duby::AST::StaticScope), "a", AST.type(nil, :fixnum), true, AST.fixnum(nil, nil, 1)]], @compiler.calls)
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_return
|
97
|
+
new_ast = AST.parse("return 1").body[0]
|
98
|
+
new_ast.compile(@compiler, true)
|
99
|
+
|
100
|
+
assert_equal([[:return, new_ast]], @compiler.calls)
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_empty_array
|
104
|
+
new_ast = AST.parse("int[5]").body[0]
|
105
|
+
new_ast.compile(@compiler, true)
|
106
|
+
|
107
|
+
assert_equal(1, @compiler.calls.size)
|
108
|
+
size = @compiler.calls[0].pop
|
109
|
+
assert_equal([[:empty_array, nil]], @compiler.calls)
|
110
|
+
assert_equal(5, size.literal)
|
111
|
+
end
|
112
|
+
end
|