duby 0.0.2-java → 0.0.3-java
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.
- data/History.txt +7 -0
- data/README.txt +18 -7
- data/Rakefile +72 -0
- data/examples/ant/example-build.xml +7 -0
- data/examples/appengine/Rakefile +8 -67
- data/examples/appengine/Readme +4 -3
- data/examples/appengine/lib/duby/appengine_tasks.rb +173 -0
- data/examples/appengine/lib/duby/plugin/datastore.rb +92 -31
- data/examples/appengine/lib/duby_task.rb +61 -0
- data/examples/appengine/src/com/ribrdb/DubyApp.duby +32 -6
- data/examples/appengine/src/com/ribrdb/list.dhtml +2 -2
- data/examples/appengine/{config.ru → src/config.ru} +0 -0
- data/examples/bintrees.duby +66 -0
- data/examples/dynamic.duby +17 -0
- data/examples/fib.duby +3 -11
- data/examples/fields.duby +3 -3
- data/examples/fractal.duby +1 -3
- data/examples/sort_closure.duby +7 -0
- data/examples/swing.duby +11 -11
- data/javalib/duby-bootstrap.jar +0 -0
- data/javalib/dynalang-invoke-0.1.jar +0 -0
- data/lib/duby.rb +168 -35
- data/lib/duby/ast.rb +224 -27
- data/lib/duby/ast/call.rb +85 -25
- data/lib/duby/ast/class.rb +112 -28
- data/lib/duby/ast/flow.rb +65 -44
- data/lib/duby/ast/intrinsics.rb +223 -21
- data/lib/duby/ast/literal.rb +67 -16
- data/lib/duby/ast/local.rb +36 -40
- data/lib/duby/ast/method.rb +83 -67
- data/lib/duby/ast/structure.rb +105 -23
- data/lib/duby/compiler.rb +83 -28
- data/lib/duby/env.rb +33 -0
- data/lib/duby/jvm/base.rb +210 -0
- data/lib/duby/jvm/compiler.rb +293 -219
- data/lib/duby/jvm/method_lookup.rb +77 -67
- data/lib/duby/jvm/source_compiler.rb +250 -157
- data/lib/duby/jvm/source_generator/builder.rb +53 -49
- data/lib/duby/jvm/source_generator/loops.rb +9 -9
- data/lib/duby/jvm/source_generator/precompile.rb +35 -25
- data/lib/duby/jvm/typer.rb +19 -10
- data/lib/duby/jvm/types.rb +127 -68
- data/lib/duby/jvm/types/basic_types.rb +26 -13
- data/lib/duby/jvm/types/enumerable.rb +6 -4
- data/lib/duby/jvm/types/factory.rb +49 -13
- data/lib/duby/jvm/types/floats.rb +16 -0
- data/lib/duby/jvm/types/integers.rb +63 -2
- data/lib/duby/jvm/types/intrinsics.rb +43 -21
- data/lib/duby/jvm/types/methods.rb +326 -86
- data/lib/duby/jvm/types/number.rb +3 -0
- data/lib/duby/nbcompiler.rb +1 -1
- data/lib/duby/plugin/edb.rb +1 -1
- data/lib/duby/plugin/java.rb +10 -1
- data/lib/duby/transform.rb +134 -46
- data/lib/duby/typer.rb +75 -50
- data/test/test_ast.rb +106 -106
- data/test/test_compilation.rb +46 -32
- data/test/test_env.rb +42 -0
- data/test/test_java_typer.rb +35 -51
- data/test/test_javac_compiler.rb +4 -1
- data/test/test_jvm_compiler.rb +564 -133
- data/test/test_typer.rb +68 -92
- metadata +37 -21
- data/examples/README +0 -16
- data/lib/duby/c/compiler.rb +0 -134
- data/lib/duby/old/compiler_old.rb +0 -845
- data/lib/duby/old/declaration.rb +0 -72
- data/lib/duby/old/mapper.rb +0 -72
- data/lib/duby/old/signature.rb +0 -52
- data/lib/duby/old/typer_old.rb +0 -163
- data/lib/duby/plugin/math.rb +0 -84
- data/test/test_math_plugin.rb +0 -87
data/lib/duby/typer.rb
CHANGED
@@ -10,7 +10,7 @@ module Duby
|
|
10
10
|
puts "* [#{name}] #{message}" if Typer.verbose
|
11
11
|
end
|
12
12
|
end
|
13
|
-
|
13
|
+
|
14
14
|
class InferenceError < Exception
|
15
15
|
attr_accessor :node
|
16
16
|
def initialize(msg, node = nil)
|
@@ -18,23 +18,23 @@ module Duby
|
|
18
18
|
@node = node
|
19
19
|
end
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
class BaseTyper
|
23
23
|
include Duby
|
24
24
|
|
25
25
|
def log(message); Typer.log(message); end
|
26
|
-
|
26
|
+
|
27
27
|
def to_s
|
28
28
|
name
|
29
29
|
end
|
30
30
|
end
|
31
|
-
|
31
|
+
|
32
32
|
class Simple < BaseTyper
|
33
|
-
attr_accessor :known_types, :errors
|
33
|
+
attr_accessor :known_types, :errors, :last_chance
|
34
34
|
|
35
35
|
def initialize(self_type)
|
36
36
|
@known_types = {}
|
37
|
-
|
37
|
+
|
38
38
|
@known_types["self"] = type_reference(self_type)
|
39
39
|
@known_types["fixnum"] = type_reference("fixnum")
|
40
40
|
@known_types["float"] = type_reference("float")
|
@@ -42,11 +42,11 @@ module Duby
|
|
42
42
|
@known_types["boolean"] = type_reference("boolean")
|
43
43
|
@errors = []
|
44
44
|
end
|
45
|
-
|
45
|
+
|
46
46
|
def name
|
47
47
|
"Simple"
|
48
48
|
end
|
49
|
-
|
49
|
+
|
50
50
|
def self_type
|
51
51
|
known_types["self"]
|
52
52
|
end
|
@@ -54,19 +54,19 @@ module Duby
|
|
54
54
|
def default_type
|
55
55
|
nil
|
56
56
|
end
|
57
|
-
|
57
|
+
|
58
58
|
def fixnum_type
|
59
59
|
known_types["fixnum"]
|
60
60
|
end
|
61
|
-
|
61
|
+
|
62
62
|
def float_type
|
63
63
|
known_types["float"]
|
64
64
|
end
|
65
|
-
|
65
|
+
|
66
66
|
def string_type
|
67
67
|
known_types["string"]
|
68
68
|
end
|
69
|
-
|
69
|
+
|
70
70
|
def boolean_type
|
71
71
|
known_types["boolean"]
|
72
72
|
end
|
@@ -89,36 +89,44 @@ module Duby
|
|
89
89
|
AST::TypeReference::NullType
|
90
90
|
end
|
91
91
|
|
92
|
+
def known_type(name)
|
93
|
+
@known_types[name]
|
94
|
+
end
|
95
|
+
|
92
96
|
def define_type(name, superclass, interfaces)
|
93
97
|
log "New type defined: '#{name}' < '#{superclass}'"
|
94
98
|
known_types[name] = type_definition(name, superclass, interfaces)
|
95
|
-
|
99
|
+
|
96
100
|
old_self, known_types["self"] = known_types["self"], known_types[name]
|
97
101
|
yield
|
98
102
|
known_types["self"] = old_self
|
99
|
-
|
103
|
+
|
100
104
|
known_types[name]
|
101
105
|
end
|
102
|
-
|
106
|
+
|
103
107
|
def learn_local_type(scope, name, type)
|
104
|
-
|
108
|
+
existing_type = local_type_hash(scope)[name]
|
109
|
+
if existing_type
|
110
|
+
# TODO check for compatibility?
|
111
|
+
existing_type
|
112
|
+
elsif type
|
113
|
+
log "Learned local type under #{scope} : #{name} = #{type}"
|
114
|
+
|
115
|
+
local_type_hash(scope)[name] = known_types[type] || type
|
116
|
+
end
|
117
|
+
end
|
105
118
|
|
106
|
-
|
107
|
-
local_type_hash(scope)[name]
|
119
|
+
def local_type(scope, name)
|
120
|
+
type = local_type_hash(scope)[name]
|
121
|
+
log "Retrieved local type in #{scope} : #{name} = #{type}" if type
|
108
122
|
|
109
123
|
type
|
110
124
|
end
|
111
|
-
|
112
|
-
def local_type(scope, name)
|
113
|
-
log "Retrieved local type in #{scope} : #{name} = #{local_type_hash(scope)[name]}"
|
114
125
|
|
115
|
-
local_type_hash(scope)[name]
|
116
|
-
end
|
117
|
-
|
118
126
|
def local_types
|
119
127
|
@local_types ||= {}
|
120
128
|
end
|
121
|
-
|
129
|
+
|
122
130
|
def local_type_hash(scope)
|
123
131
|
local_types[scope] ||= {}
|
124
132
|
end
|
@@ -146,7 +154,7 @@ module Duby
|
|
146
154
|
def field_type(cls, name)
|
147
155
|
field_type_hash(cls)[name]
|
148
156
|
end
|
149
|
-
|
157
|
+
|
150
158
|
def learn_method_type(target_type, name, parameter_types, type, exceptions)
|
151
159
|
log "Learned method #{name} (#{parameter_types}) on #{target_type} = #{type}"
|
152
160
|
|
@@ -156,7 +164,7 @@ module Duby
|
|
156
164
|
imported_types = parameter_types.map {|param| known_types[param] || param}
|
157
165
|
get_method_type_hash(target_type, name, imported_types)[:type] = type
|
158
166
|
end
|
159
|
-
|
167
|
+
|
160
168
|
def method_type(target_type, name, parameter_types)
|
161
169
|
if (target_type && target_type.error?) ||
|
162
170
|
parameter_types.any? {|t| t && t.error?}
|
@@ -191,7 +199,7 @@ module Duby
|
|
191
199
|
simple_type
|
192
200
|
end
|
193
201
|
end
|
194
|
-
|
202
|
+
|
195
203
|
def plugins
|
196
204
|
if cycling?
|
197
205
|
Duby.typer_plugins.each do |plugin|
|
@@ -201,18 +209,18 @@ module Duby
|
|
201
209
|
return result if result
|
202
210
|
end
|
203
211
|
end
|
204
|
-
|
212
|
+
|
205
213
|
nil
|
206
214
|
end
|
207
|
-
|
215
|
+
|
208
216
|
def cycling?
|
209
217
|
@cycling
|
210
218
|
end
|
211
|
-
|
219
|
+
|
212
220
|
def cycling=(c)
|
213
221
|
@cycling = c
|
214
222
|
end
|
215
|
-
|
223
|
+
|
216
224
|
def cycle(count)
|
217
225
|
@cycling = true
|
218
226
|
count.times do |i|
|
@@ -226,23 +234,23 @@ module Duby
|
|
226
234
|
ensure
|
227
235
|
@cycling = false
|
228
236
|
end
|
229
|
-
|
237
|
+
|
230
238
|
def method_types
|
231
239
|
@method_types ||= {}
|
232
240
|
end
|
233
|
-
|
241
|
+
|
234
242
|
def get_method_type_hash(target_type, name, parameter_types)
|
235
243
|
method_types[target_type] ||= {}
|
236
244
|
method_types[target_type][name] ||= {}
|
237
245
|
method_types[target_type][name][parameter_types.size] ||= {}
|
238
|
-
|
246
|
+
|
239
247
|
current = method_types[target_type][name][parameter_types.size]
|
240
248
|
|
241
249
|
parameter_types.each {|type| current[type] ||= {}; current = current[type]}
|
242
250
|
|
243
251
|
current
|
244
252
|
end
|
245
|
-
|
253
|
+
|
246
254
|
def type_reference(name, array=false, meta=false)
|
247
255
|
AST::TypeReference.new(name, array, meta)
|
248
256
|
end
|
@@ -257,7 +265,7 @@ module Duby
|
|
257
265
|
end
|
258
266
|
|
259
267
|
def deferred_nodes
|
260
|
-
@deferred_nodes ||=
|
268
|
+
@deferred_nodes ||= {}
|
261
269
|
end
|
262
270
|
|
263
271
|
def infer(node)
|
@@ -294,27 +302,31 @@ module Duby
|
|
294
302
|
else
|
295
303
|
return if deferred_nodes.include? node
|
296
304
|
log "Deferring inference for #{node}"
|
297
|
-
|
298
|
-
deferred_nodes
|
305
|
+
|
306
|
+
deferred_nodes[node] = self_type
|
299
307
|
end
|
300
308
|
end
|
301
309
|
|
302
310
|
def resolve(raise = false)
|
303
311
|
count = deferred_nodes.size + 1
|
304
|
-
|
312
|
+
|
305
313
|
log "Entering type inference cycle"
|
306
|
-
|
314
|
+
|
307
315
|
retried = false
|
308
316
|
cycle(count) do |i|
|
309
317
|
old_deferred = @deferred_nodes
|
310
|
-
@deferred_nodes =
|
318
|
+
@deferred_nodes = {}
|
319
|
+
old_deferred.each do |node, saved_type|
|
320
|
+
known_types["self"] = saved_type
|
311
321
|
type = infer(node)
|
312
322
|
|
313
323
|
log "[Cycle #{i}]: Inferred type for #{node}: #{type || 'FAILED'}"
|
314
324
|
|
315
|
-
type == default_type
|
325
|
+
if type == default_type
|
326
|
+
@deferred_nodes[node] = saved_type
|
327
|
+
end
|
316
328
|
end
|
317
|
-
|
329
|
+
|
318
330
|
if @deferred_nodes.size == 0
|
319
331
|
log "[Cycle #{i}]: Resolved all types, exiting"
|
320
332
|
break
|
@@ -322,31 +334,44 @@ module Duby
|
|
322
334
|
if @error_next || retried
|
323
335
|
log "[Cycle #{i}]: Made no progress, bailing out"
|
324
336
|
break
|
325
|
-
|
337
|
+
elsif @last_chance
|
326
338
|
# Retry this iteration, and mark the first deferred
|
327
339
|
# type as an error.
|
328
340
|
retried = true
|
329
341
|
@error_next = true
|
330
342
|
redo
|
343
|
+
else
|
344
|
+
# This is a hack for default constructor support. The right fix
|
345
|
+
# is probably to check the AST for constructors. Instead we
|
346
|
+
# tell the plugins that we're near the end of inference so they
|
347
|
+
# can assume no new constructors are being added. You could
|
348
|
+
# easily write some circular constructors that would compile
|
349
|
+
# with this technique but fail to run.
|
350
|
+
@last_chance = true
|
351
|
+
redo
|
331
352
|
end
|
332
353
|
end
|
333
354
|
retried = false
|
334
355
|
end
|
335
356
|
|
336
|
-
# done with n sweeps, if any remain
|
337
|
-
error_nodes = @errors.map {|e| e.node}
|
357
|
+
# done with n sweeps, if any remain mark them as errors
|
358
|
+
error_nodes = @errors.map {|e| e.node}
|
359
|
+
(deferred_nodes.keys - error_nodes).each do |deferred_node|
|
360
|
+
error_nodes << deferred_node
|
361
|
+
error(deferred_node)
|
362
|
+
end
|
338
363
|
if raise && !error_nodes.empty?
|
339
364
|
msg = "Could not infer typing for nodes:"
|
340
365
|
error_nodes.map do |e|
|
341
366
|
msg << "\n "
|
342
|
-
msg << "#{e} at line #{e.line_number} (child of #{e.parent})"
|
367
|
+
msg << "#{e.inspect} at line #{e.line_number} (child of #{e.parent})"
|
343
368
|
end
|
344
369
|
raise InferenceError.new(msg)
|
345
370
|
end
|
346
371
|
end
|
347
372
|
end
|
348
373
|
end
|
349
|
-
|
374
|
+
|
350
375
|
def self.typer_plugins
|
351
376
|
@typer_plugins ||= []
|
352
377
|
end
|
@@ -363,7 +388,7 @@ if __FILE__ == $0
|
|
363
388
|
rescue Duby::Typer::InferenceError => e
|
364
389
|
puts e.message
|
365
390
|
end
|
366
|
-
|
391
|
+
|
367
392
|
puts "\nAST:"
|
368
393
|
p ast
|
369
394
|
end
|
data/test/test_ast.rb
CHANGED
@@ -4,15 +4,15 @@ require 'jruby'
|
|
4
4
|
|
5
5
|
class TestAst < Test::Unit::TestCase
|
6
6
|
include Duby
|
7
|
-
|
7
|
+
|
8
8
|
def test_args
|
9
|
-
new_ast = AST.parse("def foo(a, *c, &d); end").body
|
9
|
+
new_ast = AST.parse("def foo(a, *c, &d); end").body[0]
|
10
10
|
arguments = new_ast.arguments
|
11
|
-
|
11
|
+
|
12
12
|
assert_not_nil(arguments)
|
13
13
|
inspected = "Arguments\n RequiredArgument(a)\n RestArgument(c)\n BlockArgument(d)"
|
14
14
|
assert_equal(inspected, arguments.inspect)
|
15
|
-
|
15
|
+
|
16
16
|
assert(AST::Arguments === arguments)
|
17
17
|
children = arguments.children
|
18
18
|
assert_not_nil(children)
|
@@ -28,41 +28,41 @@ class TestAst < Test::Unit::TestCase
|
|
28
28
|
assert_equal("d", children[3].name)
|
29
29
|
assert_equal(arguments, children[3].parent)
|
30
30
|
end
|
31
|
-
|
31
|
+
|
32
32
|
def test_locals
|
33
33
|
new_ast = AST.parse("a = 1; a").body
|
34
|
-
|
34
|
+
|
35
35
|
assert_not_nil(new_ast)
|
36
36
|
assert(AST::Body === new_ast)
|
37
|
-
inspected = "Body\n LocalAssignment(name = a, scope = Script, captured = false)\n Fixnum(1)\n
|
37
|
+
inspected = "Body\n LocalAssignment(name = a, scope = Script, captured = false)\n Fixnum(1)\n FunctionalCall(a)"
|
38
38
|
assert_equal(inspected, new_ast.inspect)
|
39
39
|
assert(!new_ast.newline)
|
40
|
-
|
40
|
+
|
41
41
|
asgn = new_ast[0]
|
42
42
|
var = new_ast[1]
|
43
|
-
|
43
|
+
|
44
44
|
assert(AST::LocalAssignment === asgn)
|
45
45
|
assert(asgn.newline)
|
46
46
|
assert_equal("a", asgn.name)
|
47
47
|
assert(AST::Fixnum === asgn.value)
|
48
48
|
assert(!asgn.value.newline)
|
49
|
-
assert(AST::
|
49
|
+
assert(AST::FunctionalCall === var)
|
50
50
|
assert(var.newline)
|
51
51
|
assert_equal("a", var.name)
|
52
52
|
end
|
53
|
-
|
53
|
+
|
54
54
|
def test_fields
|
55
55
|
new_ast = AST.parse("@a = 1; @a").body
|
56
|
-
|
56
|
+
|
57
57
|
assert_not_nil(new_ast)
|
58
58
|
assert(AST::Body === new_ast)
|
59
59
|
inspected = "Body\n FieldAssignment(@a)\n Fixnum(1)\n Field(@a)"
|
60
60
|
assert_equal(inspected, new_ast.inspect)
|
61
61
|
assert(!new_ast.newline)
|
62
|
-
|
62
|
+
|
63
63
|
asgn = new_ast[0]
|
64
64
|
var = new_ast[1]
|
65
|
-
|
65
|
+
|
66
66
|
assert(AST::FieldAssignment === asgn)
|
67
67
|
assert(asgn.newline)
|
68
68
|
assert_equal("@a", asgn.name)
|
@@ -72,52 +72,52 @@ class TestAst < Test::Unit::TestCase
|
|
72
72
|
assert(var.newline)
|
73
73
|
assert_equal("@a", var.name)
|
74
74
|
end
|
75
|
-
|
75
|
+
|
76
76
|
def test_array
|
77
|
-
new_ast = AST.parse("[a = 1, 1]").body
|
78
|
-
|
77
|
+
new_ast = AST.parse("[a = 1, 1]").body[0]
|
78
|
+
|
79
79
|
assert_not_nil(new_ast)
|
80
80
|
assert(AST::Array === new_ast)
|
81
81
|
assert_equal("Array\n LocalAssignment(name = a, scope = Script, captured = false)\n Fixnum(1)\n Fixnum(1)", new_ast.inspect)
|
82
|
-
|
82
|
+
|
83
83
|
assert(AST::LocalAssignment === new_ast[0])
|
84
84
|
assert(AST::Fixnum === new_ast[1])
|
85
85
|
end
|
86
|
-
|
86
|
+
|
87
87
|
def test_call
|
88
|
-
new_ast = AST.parse("1.foo(1)").body
|
89
|
-
|
88
|
+
new_ast = AST.parse("1.foo(1)").body[0]
|
89
|
+
|
90
90
|
assert_not_nil(new_ast)
|
91
91
|
assert(AST::Call === new_ast)
|
92
92
|
assert_equal("Call(foo)\n Fixnum(1)\n Fixnum(1)", new_ast.inspect)
|
93
|
-
|
93
|
+
|
94
94
|
assert_equal("foo", new_ast.name)
|
95
95
|
assert(AST::Fixnum === new_ast.target)
|
96
96
|
assert_not_nil(new_ast.parameters)
|
97
97
|
assert_equal(1, new_ast.parameters.size)
|
98
98
|
assert(AST::Fixnum === new_ast.parameters[0])
|
99
99
|
end
|
100
|
-
|
100
|
+
|
101
101
|
def test_fcall
|
102
|
-
new_ast = AST.parse("foo(1)").body
|
103
|
-
|
102
|
+
new_ast = AST.parse("foo(1)").body[0]
|
103
|
+
|
104
104
|
assert_not_nil(new_ast)
|
105
105
|
assert(AST::FunctionalCall === new_ast)
|
106
106
|
assert_equal("FunctionalCall(foo)\n Fixnum(1)", new_ast.inspect)
|
107
|
-
|
107
|
+
|
108
108
|
assert_equal("foo", new_ast.name)
|
109
109
|
assert_not_nil(new_ast.parameters)
|
110
110
|
assert_equal(1, new_ast.parameters.size)
|
111
111
|
assert(AST::Fixnum === new_ast.parameters[0])
|
112
112
|
end
|
113
|
-
|
113
|
+
|
114
114
|
def test_if
|
115
|
-
new_ast = AST.parse("if 1; 2; elsif !3; 4; else; 5; end").body
|
116
|
-
|
115
|
+
new_ast = AST.parse("if 1; 2; elsif !3; 4; else; 5; end").body[0]
|
116
|
+
|
117
117
|
assert_not_nil(new_ast)
|
118
118
|
assert(AST::If === new_ast)
|
119
119
|
assert_equal("If\n Condition\n Fixnum(1)\n Fixnum(2)\n If\n Condition\n Not\n Fixnum(3)\n Fixnum(4)\n Fixnum(5)", new_ast.inspect)
|
120
|
-
|
120
|
+
|
121
121
|
assert(AST::Condition === new_ast.condition)
|
122
122
|
assert(AST::Fixnum === new_ast.condition.predicate)
|
123
123
|
assert(AST::Fixnum === new_ast.body)
|
@@ -127,74 +127,74 @@ class TestAst < Test::Unit::TestCase
|
|
127
127
|
assert(AST::Fixnum === new_ast.else.body)
|
128
128
|
assert(AST::Fixnum === new_ast.else.else)
|
129
129
|
end
|
130
|
-
|
130
|
+
|
131
131
|
def test_begin
|
132
132
|
new_ast = AST.parse("begin; 1; 2; end").body
|
133
|
-
|
133
|
+
|
134
134
|
assert_not_nil(new_ast)
|
135
135
|
assert_equal("Body\n Fixnum(1)\n Fixnum(2)", new_ast.inspect)
|
136
136
|
assert(AST::Body === new_ast)
|
137
137
|
assert(AST::Fixnum === new_ast[0])
|
138
|
-
|
139
|
-
new_ast = AST.parse("begin; 1; end").body
|
138
|
+
|
139
|
+
new_ast = AST.parse("begin; 1; end").body[0]
|
140
140
|
assert(AST::Fixnum === new_ast)
|
141
|
-
|
142
|
-
new_ast = AST.parse("begin; end").body
|
141
|
+
|
142
|
+
new_ast = AST.parse("begin; end").body[0]
|
143
143
|
assert(AST::Noop === new_ast)
|
144
144
|
end
|
145
|
-
|
145
|
+
|
146
146
|
def test_block
|
147
147
|
new_ast = AST.parse("1; 2").body
|
148
|
-
|
148
|
+
|
149
149
|
assert_not_nil(new_ast)
|
150
150
|
assert_equal("Body\n Fixnum(1)\n Fixnum(2)", new_ast.inspect)
|
151
151
|
assert(AST::Body === new_ast)
|
152
152
|
assert(AST::Fixnum === new_ast[0])
|
153
|
-
|
154
|
-
new_ast = AST.parse("1").body
|
153
|
+
|
154
|
+
new_ast = AST.parse("1").body[0]
|
155
155
|
assert(AST::Fixnum === new_ast)
|
156
156
|
end
|
157
|
-
|
157
|
+
|
158
158
|
def test_fixnum
|
159
|
-
new_ast = AST.parse("1").body
|
160
|
-
|
159
|
+
new_ast = AST.parse("1").body[0]
|
160
|
+
|
161
161
|
assert_not_nil(new_ast)
|
162
162
|
assert_equal("Fixnum(1)", new_ast.inspect)
|
163
163
|
assert(AST::Fixnum === new_ast)
|
164
164
|
assert_equal(1, new_ast.literal)
|
165
165
|
end
|
166
|
-
|
166
|
+
|
167
167
|
def test_float
|
168
|
-
new_ast = AST.parse("1.0").body
|
169
|
-
|
168
|
+
new_ast = AST.parse("1.0").body[0]
|
169
|
+
|
170
170
|
assert_not_nil(new_ast)
|
171
171
|
assert_equal("Float(1.0)", new_ast.inspect)
|
172
172
|
assert(AST::Float === new_ast)
|
173
173
|
assert_equal(1.0, new_ast.literal)
|
174
174
|
end
|
175
|
-
|
175
|
+
|
176
176
|
def test_class
|
177
|
-
new_ast = AST.parse("class Foo < Bar; 1; 2; end").body
|
178
|
-
|
177
|
+
new_ast = AST.parse("class Foo < Bar; 1; 2; end").body[0]
|
178
|
+
|
179
179
|
assert_not_nil(new_ast)
|
180
|
-
assert_equal("ClassDefinition(Foo)\n Type(Bar)\n Body\n Fixnum(1)\n
|
180
|
+
assert_equal("ClassDefinition(Foo)\n Type(Bar)\n Body\n Body\n Fixnum(1)\n Fixnum(2)", new_ast.inspect)
|
181
181
|
assert(AST::ClassDefinition === new_ast)
|
182
182
|
assert_equal("Foo", new_ast.name)
|
183
|
-
|
183
|
+
|
184
184
|
assert(AST::TypeReference === new_ast.superclass)
|
185
185
|
assert(AST::Body === new_ast.body)
|
186
|
-
assert(AST::Fixnum === new_ast.body[0])
|
186
|
+
assert(AST::Fixnum === new_ast.body[0][0])
|
187
187
|
|
188
|
-
new_ast = AST.parse("class Foo < Bar; def foo; end; end").body
|
188
|
+
new_ast = AST.parse("class Foo < Bar; def foo; end; end").body[0]
|
189
189
|
|
190
190
|
assert_not_nil(new_ast)
|
191
|
-
assert_equal("ClassDefinition(Foo)\n Type(Bar)\n MethodDefinition(foo)\n
|
191
|
+
assert_equal("ClassDefinition(Foo)\n Type(Bar)\n Body\n MethodDefinition(foo)\n {:return=>nil}\n Arguments", new_ast.inspect)
|
192
192
|
assert_equal(new_ast, new_ast.body.parent)
|
193
193
|
end
|
194
|
-
|
194
|
+
|
195
195
|
def test_defn
|
196
|
-
new_ast = AST.parse("def foo(a, b); 1; end").body
|
197
|
-
|
196
|
+
new_ast = AST.parse("def foo(a, b); 1; end").body[0]
|
197
|
+
|
198
198
|
assert_not_nil(new_ast)
|
199
199
|
assert_equal("MethodDefinition(foo)\n {:return=>nil}\n Arguments\n RequiredArgument(a)\n RequiredArgument(b)\n Fixnum(1)", new_ast.inspect)
|
200
200
|
assert(AST::MethodDefinition === new_ast)
|
@@ -204,9 +204,9 @@ class TestAst < Test::Unit::TestCase
|
|
204
204
|
assert(nil === new_ast.signature[:return])
|
205
205
|
assert(AST::Arguments === new_ast.arguments)
|
206
206
|
assert(AST::Fixnum === new_ast.body)
|
207
|
-
|
208
|
-
new_ast = AST.parse("def foo; end").body
|
209
|
-
|
207
|
+
|
208
|
+
new_ast = AST.parse("def foo; end").body[0]
|
209
|
+
|
210
210
|
assert_not_nil(new_ast)
|
211
211
|
assert_equal("MethodDefinition(foo)\n {:return=>nil}\n Arguments", new_ast.inspect)
|
212
212
|
assert_not_nil(new_ast.arguments)
|
@@ -216,10 +216,10 @@ class TestAst < Test::Unit::TestCase
|
|
216
216
|
assert_equal(nil, new_ast.arguments.block_arg)
|
217
217
|
assert_nil(new_ast.body)
|
218
218
|
end
|
219
|
-
|
219
|
+
|
220
220
|
def test_defs
|
221
|
-
new_ast = AST.parse("def self.foo(a, b); 1; end").body
|
222
|
-
|
221
|
+
new_ast = AST.parse("def self.foo(a, b); 1; end").body[0]
|
222
|
+
|
223
223
|
assert_not_nil(new_ast)
|
224
224
|
inspected = "StaticMethodDefinition(foo)\n {:return=>nil}\n Arguments\n RequiredArgument(a)\n RequiredArgument(b)\n Fixnum(1)"
|
225
225
|
assert_equal(inspected, new_ast.inspect)
|
@@ -230,9 +230,9 @@ class TestAst < Test::Unit::TestCase
|
|
230
230
|
assert(nil === new_ast.signature[:return])
|
231
231
|
assert(AST::Arguments === new_ast.arguments)
|
232
232
|
assert(AST::Fixnum === new_ast.body)
|
233
|
-
|
234
|
-
new_ast = AST.parse("def self.foo; end").body
|
235
|
-
|
233
|
+
|
234
|
+
new_ast = AST.parse("def self.foo; end").body[0]
|
235
|
+
|
236
236
|
assert_not_nil(new_ast)
|
237
237
|
assert_equal("StaticMethodDefinition(foo)\n {:return=>nil}\n Arguments", new_ast.inspect)
|
238
238
|
assert_not_nil(new_ast.arguments)
|
@@ -242,10 +242,10 @@ class TestAst < Test::Unit::TestCase
|
|
242
242
|
assert_equal(nil, new_ast.arguments.block_arg)
|
243
243
|
assert_nil(new_ast.body)
|
244
244
|
end
|
245
|
-
|
245
|
+
|
246
246
|
def test_signature
|
247
|
-
new_ast = AST.parse("def self.foo(a, b); {a => :foo, b => :bar, :return => :baz}; 1; end").body
|
248
|
-
|
247
|
+
new_ast = AST.parse("def self.foo(a, b); {a => :foo, b => :bar, :return => :baz}; 1; end").body[0]
|
248
|
+
|
249
249
|
assert_not_nil(new_ast.signature)
|
250
250
|
inspected = "StaticMethodDefinition(foo)\n {:return=>Type(baz), :a=>Type(foo), :b=>Type(bar)}\n Arguments\n RequiredArgument(a)\n RequiredArgument(b)\n Body\n Noop\n Fixnum(1)"
|
251
251
|
assert_equal(inspected, new_ast.inspect)
|
@@ -258,10 +258,10 @@ class TestAst < Test::Unit::TestCase
|
|
258
258
|
assert(AST::TypeReference === signature[:b])
|
259
259
|
assert_equal("bar", signature[:b].name)
|
260
260
|
end
|
261
|
-
|
261
|
+
|
262
262
|
def test_type_reference
|
263
263
|
signature = AST.parse_ruby("{a => :foo, b => java.lang.Object, :return => ArrayList}").child_nodes[0].signature(nil)
|
264
|
-
|
264
|
+
|
265
265
|
inspected = "{:return=>Type(ArrayList), :a=>Type(foo), :b=>Type(java.lang.Object)}"
|
266
266
|
assert_equal(inspected, signature.inspect)
|
267
267
|
assert_equal(3, signature.size)
|
@@ -272,97 +272,97 @@ class TestAst < Test::Unit::TestCase
|
|
272
272
|
assert(AST::TypeReference === signature[:b])
|
273
273
|
assert_equal("java.lang.Object", signature[:b].name)
|
274
274
|
end
|
275
|
-
|
275
|
+
|
276
276
|
def test_return
|
277
|
-
new_ast = AST.parse("return 1").body
|
278
|
-
|
277
|
+
new_ast = AST.parse("return 1").body[0]
|
278
|
+
|
279
279
|
assert_not_nil(new_ast)
|
280
280
|
inspected = "Return\n Fixnum(1)"
|
281
281
|
assert_equal(inspected, new_ast.inspect)
|
282
282
|
assert(AST::Return === new_ast)
|
283
283
|
assert(AST::Fixnum === new_ast.value)
|
284
284
|
end
|
285
|
-
|
285
|
+
|
286
286
|
def test_vcall
|
287
|
-
new_ast = AST.parse("foo").body
|
288
|
-
|
287
|
+
new_ast = AST.parse("foo").body[0]
|
288
|
+
|
289
289
|
assert_not_nil(new_ast)
|
290
290
|
assert(AST::FunctionalCall === new_ast)
|
291
291
|
assert_equal("FunctionalCall(foo)", new_ast.inspect)
|
292
|
-
|
292
|
+
|
293
293
|
assert_equal("foo", new_ast.name)
|
294
294
|
assert_not_nil(new_ast.parameters)
|
295
295
|
assert_equal(0, new_ast.parameters.size)
|
296
296
|
end
|
297
|
-
|
297
|
+
|
298
298
|
def test_while
|
299
|
-
new_ast = AST.parse("while 1; 2; end").body
|
300
|
-
|
299
|
+
new_ast = AST.parse("while 1; 2; end").body[0]
|
300
|
+
|
301
301
|
assert_not_nil(new_ast)
|
302
302
|
assert(AST::Loop === new_ast)
|
303
|
-
assert_equal("Loop(check_first = true, negative = false)\n Body\n Condition\n Fixnum(1)\n Body\n
|
303
|
+
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)
|
304
304
|
assert(new_ast.check_first?)
|
305
305
|
assert(!new_ast.negative?)
|
306
306
|
assert(AST::Condition === new_ast.condition)
|
307
307
|
assert(AST::Fixnum === new_ast.condition.predicate)
|
308
308
|
assert(AST::Fixnum === new_ast.body)
|
309
|
-
|
310
|
-
new_ast = AST.parse("begin; 2; end while 1").body
|
311
|
-
|
309
|
+
|
310
|
+
new_ast = AST.parse("begin; 2; end while 1").body[0]
|
311
|
+
|
312
312
|
assert_not_nil(new_ast)
|
313
313
|
assert(AST::Loop === new_ast)
|
314
|
-
assert_equal("Loop(check_first = false, negative = false)\n Body\n Condition\n Fixnum(1)\n Body\n
|
314
|
+
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)
|
315
315
|
assert(!new_ast.check_first?)
|
316
316
|
assert(!new_ast.negative?)
|
317
317
|
assert(AST::Condition === new_ast.condition)
|
318
318
|
assert(AST::Fixnum === new_ast.condition.predicate)
|
319
319
|
assert(AST::Fixnum === new_ast.body)
|
320
320
|
end
|
321
|
-
|
321
|
+
|
322
322
|
def test_until
|
323
|
-
new_ast = AST.parse("until 1; 2; end").body
|
324
|
-
|
323
|
+
new_ast = AST.parse("until 1; 2; end").body[0]
|
324
|
+
|
325
325
|
assert_not_nil(new_ast)
|
326
326
|
assert(AST::Loop === new_ast)
|
327
|
-
assert_equal("Loop(check_first = true, negative = true)\n Body\n Condition\n Fixnum(1)\n Body\n
|
327
|
+
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)
|
328
328
|
assert(new_ast.check_first?)
|
329
329
|
assert(new_ast.negative?)
|
330
330
|
assert(AST::Condition === new_ast.condition)
|
331
331
|
assert(AST::Fixnum === new_ast.condition.predicate)
|
332
332
|
assert(AST::Fixnum === new_ast.body)
|
333
|
-
|
334
|
-
new_ast = AST.parse("begin; 2; end until 1").body
|
335
|
-
|
333
|
+
|
334
|
+
new_ast = AST.parse("begin; 2; end until 1").body[0]
|
335
|
+
|
336
336
|
assert_not_nil(new_ast)
|
337
337
|
assert(AST::Loop === new_ast)
|
338
|
-
assert_equal("Loop(check_first = false, negative = true)\n Body\n Condition\n Fixnum(1)\n Body\n
|
338
|
+
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)
|
339
339
|
assert(!new_ast.check_first?)
|
340
340
|
assert(new_ast.negative?)
|
341
341
|
assert(AST::Condition === new_ast.condition)
|
342
342
|
assert(AST::Fixnum === new_ast.condition.predicate)
|
343
343
|
assert(AST::Fixnum === new_ast.body)
|
344
344
|
end
|
345
|
-
|
345
|
+
|
346
346
|
def test_string
|
347
|
-
new_ast = AST.parse("'foo'").body
|
348
|
-
|
347
|
+
new_ast = AST.parse("'foo'").body[0]
|
348
|
+
|
349
349
|
assert_not_nil(new_ast)
|
350
350
|
assert(AST::String === new_ast)
|
351
351
|
assert_equal("String(\"foo\")", new_ast.inspect)
|
352
352
|
assert_equal("foo", new_ast.literal)
|
353
353
|
end
|
354
|
-
|
354
|
+
|
355
355
|
def test_root
|
356
|
-
new_ast = AST.parse("1").body
|
357
|
-
|
356
|
+
new_ast = AST.parse("1").body[0]
|
357
|
+
|
358
358
|
assert_not_nil(new_ast)
|
359
359
|
assert(AST::Fixnum === new_ast)
|
360
360
|
end
|
361
|
-
|
361
|
+
|
362
362
|
def test_boolean
|
363
|
-
new_ast1 = AST.parse("true").body
|
364
|
-
new_ast2 = AST.parse("false").body
|
365
|
-
|
363
|
+
new_ast1 = AST.parse("true").body[0]
|
364
|
+
new_ast2 = AST.parse("false").body[0]
|
365
|
+
|
366
366
|
assert_not_nil(new_ast1)
|
367
367
|
assert_not_nil(new_ast2)
|
368
368
|
assert(AST::Boolean === new_ast1)
|
@@ -372,7 +372,7 @@ class TestAst < Test::Unit::TestCase
|
|
372
372
|
end
|
373
373
|
|
374
374
|
def test_return
|
375
|
-
new_ast = AST.parse("return 1").body
|
375
|
+
new_ast = AST.parse("return 1").body[0]
|
376
376
|
|
377
377
|
assert_not_nil(new_ast)
|
378
378
|
assert(AST::Return === new_ast)
|
@@ -381,7 +381,7 @@ class TestAst < Test::Unit::TestCase
|
|
381
381
|
end
|
382
382
|
|
383
383
|
def test_empty_array
|
384
|
-
new_ast = AST.parse("int[5]").body
|
384
|
+
new_ast = AST.parse("int[5]").body[0]
|
385
385
|
|
386
386
|
assert_not_nil(new_ast)
|
387
387
|
assert(AST::EmptyArray === new_ast)
|