duby 0.0.2-java → 0.0.3-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. data/History.txt +7 -0
  2. data/README.txt +18 -7
  3. data/Rakefile +72 -0
  4. data/examples/ant/example-build.xml +7 -0
  5. data/examples/appengine/Rakefile +8 -67
  6. data/examples/appengine/Readme +4 -3
  7. data/examples/appengine/lib/duby/appengine_tasks.rb +173 -0
  8. data/examples/appengine/lib/duby/plugin/datastore.rb +92 -31
  9. data/examples/appengine/lib/duby_task.rb +61 -0
  10. data/examples/appengine/src/com/ribrdb/DubyApp.duby +32 -6
  11. data/examples/appengine/src/com/ribrdb/list.dhtml +2 -2
  12. data/examples/appengine/{config.ru → src/config.ru} +0 -0
  13. data/examples/bintrees.duby +66 -0
  14. data/examples/dynamic.duby +17 -0
  15. data/examples/fib.duby +3 -11
  16. data/examples/fields.duby +3 -3
  17. data/examples/fractal.duby +1 -3
  18. data/examples/sort_closure.duby +7 -0
  19. data/examples/swing.duby +11 -11
  20. data/javalib/duby-bootstrap.jar +0 -0
  21. data/javalib/dynalang-invoke-0.1.jar +0 -0
  22. data/lib/duby.rb +168 -35
  23. data/lib/duby/ast.rb +224 -27
  24. data/lib/duby/ast/call.rb +85 -25
  25. data/lib/duby/ast/class.rb +112 -28
  26. data/lib/duby/ast/flow.rb +65 -44
  27. data/lib/duby/ast/intrinsics.rb +223 -21
  28. data/lib/duby/ast/literal.rb +67 -16
  29. data/lib/duby/ast/local.rb +36 -40
  30. data/lib/duby/ast/method.rb +83 -67
  31. data/lib/duby/ast/structure.rb +105 -23
  32. data/lib/duby/compiler.rb +83 -28
  33. data/lib/duby/env.rb +33 -0
  34. data/lib/duby/jvm/base.rb +210 -0
  35. data/lib/duby/jvm/compiler.rb +293 -219
  36. data/lib/duby/jvm/method_lookup.rb +77 -67
  37. data/lib/duby/jvm/source_compiler.rb +250 -157
  38. data/lib/duby/jvm/source_generator/builder.rb +53 -49
  39. data/lib/duby/jvm/source_generator/loops.rb +9 -9
  40. data/lib/duby/jvm/source_generator/precompile.rb +35 -25
  41. data/lib/duby/jvm/typer.rb +19 -10
  42. data/lib/duby/jvm/types.rb +127 -68
  43. data/lib/duby/jvm/types/basic_types.rb +26 -13
  44. data/lib/duby/jvm/types/enumerable.rb +6 -4
  45. data/lib/duby/jvm/types/factory.rb +49 -13
  46. data/lib/duby/jvm/types/floats.rb +16 -0
  47. data/lib/duby/jvm/types/integers.rb +63 -2
  48. data/lib/duby/jvm/types/intrinsics.rb +43 -21
  49. data/lib/duby/jvm/types/methods.rb +326 -86
  50. data/lib/duby/jvm/types/number.rb +3 -0
  51. data/lib/duby/nbcompiler.rb +1 -1
  52. data/lib/duby/plugin/edb.rb +1 -1
  53. data/lib/duby/plugin/java.rb +10 -1
  54. data/lib/duby/transform.rb +134 -46
  55. data/lib/duby/typer.rb +75 -50
  56. data/test/test_ast.rb +106 -106
  57. data/test/test_compilation.rb +46 -32
  58. data/test/test_env.rb +42 -0
  59. data/test/test_java_typer.rb +35 -51
  60. data/test/test_javac_compiler.rb +4 -1
  61. data/test/test_jvm_compiler.rb +564 -133
  62. data/test/test_typer.rb +68 -92
  63. metadata +37 -21
  64. data/examples/README +0 -16
  65. data/lib/duby/c/compiler.rb +0 -134
  66. data/lib/duby/old/compiler_old.rb +0 -845
  67. data/lib/duby/old/declaration.rb +0 -72
  68. data/lib/duby/old/mapper.rb +0 -72
  69. data/lib/duby/old/signature.rb +0 -52
  70. data/lib/duby/old/typer_old.rb +0 -163
  71. data/lib/duby/plugin/math.rb +0 -84
  72. data/test/test_math_plugin.rb +0 -87
@@ -3,19 +3,84 @@ module Duby::AST
3
3
  include Annotated
4
4
  include Named
5
5
  include Scope
6
- attr_accessor :superclass, :body, :interfaces
7
-
8
- def initialize(parent, line_number, name, annotations=[], &block)
6
+ attr_accessor :interfaces
7
+
8
+ child :superclass
9
+ child :body
10
+
11
+ def initialize(parent, position, name, annotations=[], &block)
9
12
  @annotations = annotations
10
13
  @interfaces = []
11
14
  @name = name
12
15
  if Duby::AST.type_factory.respond_to? :declare_type
13
16
  Duby::AST.type_factory.declare_type(self)
14
17
  end
15
- super(parent, line_number, &block)
16
- @superclass, @body = children
18
+ # We need somewhere to collect nodes that get appended during
19
+ # the transform phase.
20
+ @extra_body = Body.new(self, position)
21
+ super(parent, position, &block)
22
+ if body
23
+ @extra_body.insert(0, body)
24
+ end
25
+ self.body = @extra_body
26
+ end
27
+
28
+ def append_node(node)
29
+ @extra_body << node
30
+ node
31
+ end
32
+
33
+ def define_inner_class(position, name, &block)
34
+ name = "#{self.name}$#{name}"
35
+ append_node ClassDefinition.new(nil, position, name, &block)
36
+ end
37
+
38
+ def define_method(position, name, type, *args)
39
+ append_node(_define_method(MethodDefinition, position, name, type, args))
40
+ end
41
+
42
+ def define_static_method(position, name, type, *args)
43
+ append_node(
44
+ _define_method(StaticMethodDefinition, position, name, type, args))
45
+ end
46
+
47
+ def define_constructor(position, *args, &block)
48
+ append_node(_define_method(
49
+ ConstructorDefinition, position, 'initialize', nil, args, &block))
50
+ end
51
+
52
+ def _define_method(klass, position, name, type, args)
53
+ klass.new(nil, position, name) do |method|
54
+ signature = {:return => type}
55
+ if Arguments === args[0]
56
+ args_node = args[0]
57
+ args_node.parent = method
58
+ else
59
+ args_node = Arguments.new(method, position) do |args_node|
60
+ arg_list = args.map do |arg_name, arg_type, arg_position|
61
+ signature[arg_name.intern] = arg_type
62
+ arg_position ||= position
63
+ RequiredArgument.new(args_node, arg_position, arg_name)
64
+ end
65
+ [arg_list, nil, nil, nil]
66
+ end
67
+ end
68
+ [
69
+ signature,
70
+ args_node,
71
+ if block_given?
72
+ yield(method)
73
+ end
74
+ ]
75
+ end
76
+ end
77
+
78
+ def declare_field(position, name, type)
79
+ field = FieldDeclaration.new(nil, position || self.position, name)
80
+ field.type = type.dup
81
+ append_node(field)
17
82
  end
18
-
83
+
19
84
  def infer(typer)
20
85
  unless resolved?
21
86
  @inferred_type ||= typer.define_type(name, superclass, @interfaces) do
@@ -25,35 +90,52 @@ module Duby::AST
25
90
  typer.no_type
26
91
  end
27
92
  end
28
- @inferred_type ? resolved! : typer.defer(self)
93
+ if @inferred_type
94
+ resolved!
95
+ else
96
+ typer.defer(self)
97
+ end
29
98
  end
30
99
 
31
100
  @inferred_type
32
101
  end
33
-
102
+
34
103
  def implements(*types)
35
104
  raise ArgumentError if types.any? {|x| x.nil?}
36
105
  @interfaces.concat types
37
106
  end
38
107
  end
39
-
108
+
40
109
  defmacro('implements') do |transformer, fcall, parent|
41
110
  interfaces = fcall.args_node.child_nodes.map do |interface|
42
111
  interface.type_reference(parent)
43
112
  end
44
- parent.parent.implements(*interfaces)
113
+ klass = parent
114
+ klass = klass.parent unless ClassDefinition === klass
115
+ klass.implements(*interfaces)
45
116
  Noop.new(parent, fcall.position)
46
117
  end
47
118
 
48
119
  class InterfaceDeclaration < ClassDefinition
49
- attr_accessor :superclass, :body, :interfaces
50
-
51
- def initialize(parent, line_number, name, annotations)
52
- super(parent, line_number, name, annotations) {|p| }
53
- @interfaces = []
120
+ attr_accessor :superclass
121
+ child :interfaces
122
+ child :body
123
+
124
+ def initialize(parent, position, name, annotations)
125
+ super(parent, position, name, annotations) {|p| }
54
126
  @name = name
127
+ @children = [[], nil]
55
128
  @children = yield(self)
56
- @interfaces, @body = children
129
+ end
130
+ end
131
+
132
+ class ClosureDefinition < ClassDefinition
133
+ attr_accessor :enclosing_type
134
+ def initialize(parent, position, name, enclosing_type)
135
+ super(parent, position, name, []) do
136
+ [nil, nil]
137
+ end
138
+ @enclosing_type = enclosing_type
57
139
  end
58
140
  end
59
141
 
@@ -84,11 +166,12 @@ module Duby::AST
84
166
  include ClassScoped
85
167
  include Typed
86
168
 
87
- def initialize(parent, line_number, name, annotations=[], &block)
169
+ child :type
170
+
171
+ def initialize(parent, position, name, annotations=[], &block)
88
172
  @annotations = annotations
89
- super(parent, line_number, &block)
173
+ super(parent, position, &block)
90
174
  @name = name
91
- @type = children[0]
92
175
  end
93
176
 
94
177
  def infer(typer)
@@ -105,17 +188,18 @@ module Duby::AST
105
188
  @inferred_type
106
189
  end
107
190
  end
108
-
191
+
109
192
  class FieldAssignment < Node
110
193
  include Annotated
111
194
  include Named
112
195
  include Valued
113
196
  include ClassScoped
114
-
115
- def initialize(parent, line_number, name, annotations=[], &block)
197
+
198
+ child :value
199
+
200
+ def initialize(parent, position, name, annotations=[], &block)
116
201
  @annotations = annotations
117
- super(parent, line_number, &block)
118
- @value = children[0]
202
+ super(parent, position, &block)
119
203
  @name = name
120
204
  end
121
205
 
@@ -129,15 +213,15 @@ module Duby::AST
129
213
  @inferred_type
130
214
  end
131
215
  end
132
-
216
+
133
217
  class Field < Node
134
218
  include Annotated
135
219
  include Named
136
220
  include ClassScoped
137
-
138
- def initialize(parent, line_number, name, annotations=[], &block)
221
+
222
+ def initialize(parent, position, name, annotations=[], &block)
139
223
  @annotations = annotations
140
- super(parent, line_number, &block)
224
+ super(parent, position, &block)
141
225
  @name = name
142
226
  end
143
227
 
@@ -1,11 +1,10 @@
1
1
  module Duby
2
2
  module AST
3
3
  class Condition < Node
4
- attr_accessor :predicate
4
+ child :predicate
5
5
 
6
6
  def initialize(parent, line_number, &block)
7
7
  super(parent, line_number, &block)
8
- @predicate = children[0]
9
8
  end
10
9
 
11
10
  def infer(typer)
@@ -13,10 +12,10 @@ module Duby
13
12
  @inferred_type = typer.infer(predicate)
14
13
  if @inferred_type && !@inferred_type.primitive?
15
14
  call = Call.new(parent, position, '!=') do |call|
16
- @predicate.parent = call
17
- [@predicate, [Null.new(call, position)]]
15
+ predicate.parent = call
16
+ [predicate, [Null.new(call, position)]]
18
17
  end
19
- @predicate = children[0] = call
18
+ self.predicate = call
20
19
  @inferred_type = typer.infer(predicate)
21
20
  end
22
21
 
@@ -28,11 +27,12 @@ module Duby
28
27
  end
29
28
 
30
29
  class If < Node
31
- attr_accessor :condition, :body, :else
30
+ child :condition
31
+ child :body
32
+ child :else
32
33
 
33
34
  def initialize(parent, line_number, &block)
34
35
  super(parent, line_number, &block)
35
- @condition, @body, @else = children
36
36
  end
37
37
 
38
38
  def infer(typer)
@@ -56,7 +56,11 @@ module Duby
56
56
  else
57
57
  # we have else but not then, defer only then and use else type for now
58
58
  @inferred_type = else_type
59
- typer.defer(self)
59
+ if body
60
+ typer.defer(self)
61
+ else
62
+ resolved! if condition_type
63
+ end
60
64
  end
61
65
  else
62
66
  # no then type could be inferred and no else body, defer for now
@@ -93,17 +97,28 @@ module Duby
93
97
  end
94
98
 
95
99
  class Loop < Node
96
- attr_accessor :init, :condition, :pre, :post, :body
100
+ child :init
101
+ child :condition
102
+ child :pre
103
+ child :body
104
+ child :post
97
105
  attr_accessor :check_first, :negative, :redo
98
106
 
99
107
  def initialize(parent, position, check_first, negative, &block)
100
108
  @check_first = check_first
101
109
  @negative = negative
102
- @init = Body.new(self, position)
103
- @pre = Body.new(self, position)
104
- @post = Body.new(self, position)
105
- super(parent, position, &block)
106
- @condition, @body = @children
110
+
111
+ @children = [
112
+ Body.new(self, position),
113
+ nil,
114
+ Body.new(self, position),
115
+ nil,
116
+ Body.new(self, position),
117
+ ]
118
+ super(parent, position) do |l|
119
+ condition, body = yield(l)
120
+ [self.init, condition, self.pre, body, self.post]
121
+ end
107
122
  end
108
123
 
109
124
  def infer(typer)
@@ -125,10 +140,6 @@ module Duby
125
140
 
126
141
  @inferred_type
127
142
  end
128
-
129
- def children
130
- [init, condition, pre, post, body]
131
- end
132
143
 
133
144
  def check_first?; @check_first; end
134
145
  def negative?; @negative; end
@@ -154,15 +165,15 @@ module Duby
154
165
  end
155
166
 
156
167
  def init?
157
- @init && !(@init.kind_of?(Body) && @init.empty?)
168
+ init && !(init.kind_of?(Body) && init.empty?)
158
169
  end
159
170
 
160
171
  def pre?
161
- @pre && !(@pre.kind_of?(Body) && @pre.empty?)
172
+ pre && !(pre.kind_of?(Body) && pre.empty?)
162
173
  end
163
-
174
+
164
175
  def post?
165
- @post && !(@post.kind_of?(Body) && @post.empty?)
176
+ post && !(post.kind_of?(Body) && post.empty?)
166
177
  end
167
178
 
168
179
  def to_s
@@ -179,9 +190,10 @@ module Duby
179
190
  class Return < Node
180
191
  include Valued
181
192
 
193
+ child :value
194
+
182
195
  def initialize(parent, line_number, &block)
183
196
  super(parent, line_number, &block)
184
- @value = children[0]
185
197
  end
186
198
 
187
199
  def infer(typer)
@@ -201,16 +213,19 @@ module Duby
201
213
  resolved!
202
214
  @inferred_type = typer.null_type
203
215
  end
216
+ @inferred_type
204
217
  end
205
218
  end
206
-
219
+
207
220
  class Next < Break; end
208
-
221
+
209
222
  class Redo < Break; end
210
223
 
211
224
  class Raise < Node
212
225
  include Valued
213
226
 
227
+ child :exception
228
+
214
229
  def initialize(parent, line_number, &block)
215
230
  super(parent, line_number, &block)
216
231
  end
@@ -220,13 +235,12 @@ module Duby
220
235
  @inferred_type = AST.unreachable_type
221
236
  throwable = AST.type('java.lang.Throwable')
222
237
  if children.size == 1
223
- arg_type = typer.infer(children[0])
238
+ arg_type = typer.infer(self.exception)
224
239
  unless arg_type
225
240
  typer.defer(self)
226
241
  return
227
242
  end
228
243
  if throwable.assignable_from?(arg_type) && !arg_type.meta?
229
- @exception = children[0]
230
244
  resolved!
231
245
  return @inferred_type
232
246
  end
@@ -241,12 +255,12 @@ module Duby
241
255
  else
242
256
  klass = Constant.new(self, position, 'RuntimeException')
243
257
  end
244
- @exception = Call.new(self, position, 'new') do
258
+ exception = Call.new(self, position, 'new') do
245
259
  [klass, children, nil]
246
260
  end
247
261
  resolved!
248
- @children = [@exception]
249
- typer.infer(@exception)
262
+ @children = [exception]
263
+ typer.infer(exception)
250
264
  end
251
265
  end
252
266
  @inferred_type
@@ -265,38 +279,45 @@ module Duby
265
279
 
266
280
  class RescueClause < Node
267
281
  include Scoped
268
- attr_accessor :types, :body, :name, :type
269
-
282
+ attr_accessor :name, :type
283
+ child :types
284
+ child :body
285
+
270
286
  def initialize(parent, position, &block)
271
287
  super(parent, position, &block)
272
- @types, @body = children
273
288
  end
274
289
 
275
290
  def infer(typer)
276
291
  unless resolved?
277
292
  if name
278
- orig_type = typer.local_type(scope, name)
293
+ scope.static_scope << name
294
+ orig_type = typer.local_type(containing_scope, name)
279
295
  # TODO find the common parent Throwable
280
296
  @type = types.size == 1 ? types[0] : AST.type('java.lang.Throwable')
281
- typer.learn_local_type(scope, name, @type)
297
+ typer.learn_local_type(containing_scope, name, @type)
282
298
  end
283
299
  @inferred_type = typer.infer(body)
284
300
 
301
+ if (@inferred_type && !body.resolved?)
302
+ puts "#{body} not resolved"
303
+ end
304
+
285
305
  (@inferred_type && body.resolved?) ? resolved! : typer.defer(self)
286
- typer.local_type_hash(scope)[name] = orig_type if name
306
+ typer.local_type_hash(containing_scope)[name] = orig_type if name
287
307
  end
288
308
 
289
309
  @inferred_type
290
310
  end
291
311
  end
292
-
312
+
293
313
  class Rescue < Node
294
- attr_accessor :body, :clauses
314
+ child :body
315
+ child :clauses
295
316
  def initialize(parent, position, &block)
296
317
  super(parent, position, &block)
297
318
  @body, @clauses = children
298
319
  end
299
-
320
+
300
321
  def infer(typer)
301
322
  unless resolved?
302
323
  types = [typer.infer(body)] + clauses.map {|c| typer.infer(c)}
@@ -311,16 +332,16 @@ module Duby
311
332
  @inferred_type
312
333
  end
313
334
  end
314
-
335
+
315
336
  class Ensure < Node
316
- attr_reader :body, :clause
337
+ child :body
338
+ child :clause
317
339
  attr_accessor :state # Used by the some compilers.
318
-
340
+
319
341
  def initialize(parent, position, &block)
320
342
  super(parent, position, &block)
321
- @body, @clause = children
322
343
  end
323
-
344
+
324
345
  def infer(typer)
325
346
  resolve_if(typer) do
326
347
  typer.infer(clause)