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.
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
@@ -1,132 +1,147 @@
1
1
  module Duby::AST
2
2
  class Arguments < Node
3
- attr_accessor :args, :opt_args, :rest_arg, :block_arg
4
-
3
+ child :args
4
+ child :opt_args
5
+ child :rest_arg
6
+ child :block_arg
7
+
5
8
  def initialize(parent, line_number, &block)
6
9
  super(parent, line_number, &block)
7
- @args, @opt_args, @rest_arg, @block_arg = children
8
10
  end
9
-
11
+
10
12
  def infer(typer)
11
- unless @inferred_type
13
+ unless resolved?
12
14
  @inferred_type = args ? args.map {|arg| typer.infer(arg)} : []
15
+ if @inferred_type.all?
16
+ resolved!
17
+ else
18
+ typer.defer(self)
19
+ end
13
20
  end
21
+ @inferred_type
14
22
  end
15
23
  end
16
-
24
+
17
25
  class Argument < Node
18
26
  include Typed
27
+
28
+ def resolved!(typer)
29
+ typer.learn_local_type(containing_scope, name, @inferred_type)
30
+ super
31
+ end
19
32
  end
20
-
33
+
21
34
  class RequiredArgument < Argument
22
35
  include Named
23
36
  include Scoped
24
-
37
+
25
38
  def initialize(parent, line_number, name)
26
39
  super(parent, line_number)
27
40
 
28
41
  @name = name
29
42
  end
30
-
43
+
31
44
  def infer(typer)
32
- unless @inferred_type
33
- # if not already typed, check parent of parent (MethodDefinition) for signature info
45
+ resolve_if(typer) do
46
+ scope.static_scope << name
47
+ # if not already typed, check parent of parent (MethodDefinition)
48
+ # for signature info
34
49
  method_def = parent.parent
35
50
  signature = method_def.signature
36
51
 
37
52
  # if signature, search for this argument
38
- if signature[name.intern]
39
- @inferred_type = typer.learn_local_type(scope, name, signature[name.intern])
40
- else
41
- @inferred_type = typer.local_type(scope, name)
42
- end
43
-
44
- unless @inferred_type
45
- typer.defer(self)
46
- end
53
+ signature[name.intern] || typer.local_type(scope, name)
47
54
  end
48
-
49
- @inferred_type
50
55
  end
51
56
  end
52
-
57
+
53
58
  class OptionalArgument < Argument
54
59
  include Named
55
60
  include Scoped
56
- attr_accessor :child
57
-
61
+ child :child
62
+
58
63
  def initialize(parent, line_number, name, &block)
59
64
  super(parent, line_number, &block)
60
- @child = children[0]
61
65
  @name = name
62
66
  end
63
67
 
64
68
  def infer(typer)
65
- unless @inferred_type
66
- # if not already typed, check parent of parent (MethodDefinition) for signature info
69
+ resolve_if(typer) do
70
+ scope.static_scope << name
71
+ # if not already typed, check parent of parent (MethodDefinition)
72
+ # for signature info
67
73
  method_def = parent.parent
68
74
  signature = method_def.signature
69
75
 
70
- # if signature, search for this argument
71
- @inferred_type = child.infer(typer)
72
- if @inferred_type
73
- typer.learn_local_type(scope, name, @inferred_type)
74
- signature[name.intern] = @inferred_type
75
- else
76
- typer.defer(self)
77
- end
76
+ signature[name.intern] = child.infer(typer)
78
77
  end
79
-
80
- @inferred_type
81
78
  end
82
79
  end
83
-
80
+
84
81
  class RestArgument < Argument
85
82
  include Named
86
83
  include Scoped
87
-
84
+
88
85
  def initialize(parent, line_number, name)
89
86
  super(parent, line_number)
90
87
 
91
88
  @name = name
92
89
  end
90
+
91
+ def infer(typer)
92
+ scope.static_scope << name
93
+ super
94
+ end
93
95
  end
94
-
96
+
95
97
  class BlockArgument < Argument
96
98
  include Named
97
-
99
+ include Scoped
100
+ attr_accessor :optional
101
+ alias optional? optional
102
+
98
103
  def initialize(parent, line_number, name)
99
104
  super(parent, line_number)
100
105
 
101
106
  @name = name
102
107
  end
108
+
109
+ def infer(typer)
110
+ scope.static_scope << name
111
+ super
112
+ end
103
113
  end
104
-
114
+
105
115
  class MethodDefinition < Node
106
116
  include Annotated
107
117
  include Named
108
118
  include Scope
109
- attr_accessor :signature, :arguments, :body, :defining_class
110
-
119
+ include Binding
120
+
121
+ child :signature
122
+ child :arguments
123
+ child :body
124
+
125
+ attr_accessor :defining_class
126
+
111
127
  def initialize(parent, line_number, name, annotations=[], &block)
112
128
  @annotations = annotations
113
129
  super(parent, line_number, &block)
114
- @signature, @arguments, @body = children
115
130
  @name = name
116
131
  end
117
132
 
118
133
  def name
119
134
  super
120
135
  end
121
-
136
+
122
137
  def infer(typer)
123
138
  @defining_class ||= typer.self_type
124
139
  typer.infer(arguments)
125
140
  typer.infer_signature(self)
126
141
  forced_type = signature[:return]
127
142
  inferred_type = body ? typer.infer(body) : typer.no_type
128
-
129
- if !inferred_type
143
+
144
+ if !(inferred_type && arguments.inferred_type.all?)
130
145
  typer.defer(self)
131
146
  else
132
147
  actual_type = if forced_type.nil?
@@ -137,7 +152,7 @@ module Duby::AST
137
152
  if actual_type.unreachable?
138
153
  actual_type = typer.no_type
139
154
  end
140
-
155
+
141
156
  if !abstract? &&
142
157
  forced_type != typer.no_type &&
143
158
  !actual_type.is_parent(inferred_type)
@@ -164,10 +179,10 @@ module Duby::AST
164
179
 
165
180
  signature[:return] = @inferred_type
166
181
  end
167
-
182
+
168
183
  @inferred_type
169
184
  end
170
-
185
+
171
186
  def abstract?
172
187
  node = parent
173
188
  while node && !node.kind_of?(Scope)
@@ -175,46 +190,47 @@ module Duby::AST
175
190
  end
176
191
  InterfaceDeclaration === node
177
192
  end
178
-
193
+
179
194
  def static?
180
195
  false
181
196
  end
182
197
  end
183
-
198
+
184
199
  class StaticMethodDefinition < MethodDefinition
185
200
  def defining_class
186
201
  @defining_class.meta
187
202
  end
188
-
203
+
189
204
  def static?
190
205
  true
191
206
  end
192
207
  end
193
-
208
+
194
209
  class ConstructorDefinition < MethodDefinition
195
210
  attr_accessor :delegate_args, :calls_super
196
-
211
+
197
212
  def initialize(*args)
198
213
  super
199
214
  extract_delegate_constructor
200
215
  end
201
-
216
+
202
217
  def first_node
203
- if @body.kind_of? Body
204
- @body.children[0]
218
+ if body.kind_of? Body
219
+ body.children[0]
205
220
  else
206
- @body
221
+ body
207
222
  end
208
223
  end
209
-
224
+
210
225
  def first_node=(new_node)
211
- if @body.kind_of? Body
212
- @body.children[0] = new_node
226
+ if body.kind_of? Body
227
+ new_node.parent = body
228
+ body.children[0] = new_node
213
229
  else
214
- @body = children[2] = new_node
230
+ self.body = new_node
215
231
  end
216
232
  end
217
-
233
+
218
234
  def extract_delegate_constructor
219
235
  # TODO verify that this constructor exists during type inference.
220
236
  possible_delegate = first_node
@@ -233,7 +249,7 @@ module Duby::AST
233
249
  end
234
250
  self.first_node = Noop.new(self, position) if @delegate_args
235
251
  end
236
-
252
+
237
253
  def infer(typer)
238
254
  unless @inferred_type
239
255
  delegate_args.each {|a| typer.infer(a)} if delegate_args
@@ -3,59 +3,141 @@ module Duby::AST
3
3
  def initialize(parent, line_number, &block)
4
4
  super(parent, line_number, &block)
5
5
  end
6
-
6
+
7
7
  # Type of a block is the type of its final element
8
8
  def infer(typer)
9
9
  unless @inferred_type
10
10
  if children.size == 0
11
- @inferred_type = typer.default_type
11
+ @inferred_type = typer.no_type
12
12
  else
13
13
  children.each {|child| @inferred_type = typer.infer(child)}
14
14
  end
15
-
16
- unless @inferred_type
15
+
16
+ if @inferred_type
17
+ resolved!
18
+ else
17
19
  typer.defer(self)
18
20
  end
19
21
  end
20
22
 
21
23
  @inferred_type
22
24
  end
23
-
24
- def <<(node)
25
- @children << node
26
- node.parent = self
27
- end
28
-
29
- def empty?
30
- @children.empty?
31
- end
32
25
  end
33
-
26
+
27
+ class ScopedBody < Body
28
+ include Scope
29
+ end
30
+
34
31
  class Block < Node
35
- attr_accessor :args, :body
32
+ include Scoped
33
+ include Scope
34
+ child :args
35
+ child :body
36
36
 
37
37
  def initialize(parent, position, &block)
38
- super(parent, position, &block)
39
- @args, @body = children
38
+ super(parent, position) do
39
+ static_scope.parent = scope.static_scope
40
+ yield(self) if block_given?
41
+ end
42
+ end
43
+
44
+ def prepare(typer, method)
45
+ duby = typer.transformer
46
+ interface = method.argument_types[-1]
47
+ outer_class = scope.defining_class
48
+ binding = scope.binding_type(duby)
49
+ name = "#{outer_class.name}$#{duby.tmp}"
50
+ klass = duby.define_closure(position, name, outer_class)
51
+ klass.interfaces = [interface]
52
+ klass.define_constructor(position,
53
+ ['binding', binding]) do |c|
54
+ duby.eval("@binding = binding", '-', c, 'binding')
55
+ end
56
+
57
+ # find all methods which would not otherwise be on java.lang.Object
58
+ impl_methods = find_methods(interface).select do |m|
59
+ begin
60
+ obj_m = java.lang.Object.java_class.java_method m.name, *m.parameter_types
61
+ rescue NameError
62
+ # not found on Object
63
+ next true
64
+ end
65
+ # found on Object
66
+ next false
67
+ end
68
+
69
+ # TODO: find a nice way to closure-impl multiple methods
70
+ # perhaps something like
71
+ # Collections.sort(list) do
72
+ # def equals(other); self == other; end
73
+ # def compareTo(x,y); Comparable(x).compareTo(y); end
74
+ # end
75
+ raise "Multiple abstract methods found; cannot use block" if impl_methods.size > 1
76
+ impl_methods.each do |method|
77
+ mdef = klass.define_method(position,
78
+ method.name,
79
+ method.actual_return_type,
80
+ args.dup)
81
+ mdef.static_scope = static_scope
82
+ mdef.body = body.dup
83
+ mdef.binding_type = binding
84
+ end
85
+ call = parent
86
+ instance = Call.new(call, position, 'new')
87
+ instance.target = Constant.new(call, position, name)
88
+ instance.parameters = [
89
+ BindingReference.new(instance, position, binding)
90
+ ]
91
+ call.parameters << instance
92
+ call.block = nil
93
+ typer.infer(klass)
94
+ typer.infer(instance)
95
+ end
96
+
97
+ def find_methods(interface)
98
+ methods = []
99
+ interfaces = [interface]
100
+ until interfaces.empty?
101
+ interface = interfaces.pop
102
+ methods += interface.declared_instance_methods.select {|m| m.abstract?}
103
+ interfaces.concat(interface.interfaces)
104
+ end
105
+ methods
106
+ end
107
+ end
108
+
109
+ class BindingReference < Node
110
+ def initialize(parent, position, type)
111
+ super(parent, position)
112
+ @inferred_type = type
113
+ end
114
+
115
+ def infer(typer)
116
+ resolved! unless resolved?
117
+ @inferred_type
40
118
  end
41
119
  end
42
-
120
+
43
121
  class Noop < Node
44
122
  def infer(typer)
123
+ resolved!
45
124
  @inferred_type ||= typer.no_type
46
125
  end
47
126
  end
48
-
127
+
49
128
  class Script < Node
50
129
  include Scope
51
- attr_accessor :body
52
-
130
+ include Binding
131
+ child :body
132
+
133
+ attr_accessor :defining_class
134
+
53
135
  def initialize(parent, line_number, &block)
54
136
  super(parent, line_number, children, &block)
55
- @body = children[0]
56
137
  end
57
-
138
+
58
139
  def infer(typer)
140
+ @defining_class ||= typer.self_type
59
141
  @inferred_type ||= typer.infer(body) || (typer.defer(self); nil)
60
142
  end
61
143
  end