duby 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. data/History.txt +4 -0
  2. data/Manifest.txt +68 -0
  3. data/README.txt +31 -0
  4. data/Rakefile +37 -0
  5. data/bin/duby +9 -0
  6. data/bin/dubyc +9 -0
  7. data/bin/dubyp +9 -0
  8. data/examples/appengine/src/com/google/appengine/ext/duby/db/Model.duby +132 -0
  9. data/examples/appengine/src/com/ribrdb/DubyApp.duby +28 -0
  10. data/examples/bench_fractal.duby +57 -0
  11. data/examples/construction.duby +8 -0
  12. data/examples/edb.duby +3 -0
  13. data/examples/fib.duby +24 -0
  14. data/examples/fields.duby +22 -0
  15. data/examples/java_thing.duby +13 -0
  16. data/examples/simple_class.duby +12 -0
  17. data/examples/swing.duby +20 -0
  18. data/examples/tak.duby +15 -0
  19. data/javalib/JRubyParser.jar +0 -0
  20. data/lib/duby.rb +170 -0
  21. data/lib/duby/ast.rb +340 -0
  22. data/lib/duby/ast/call.rb +73 -0
  23. data/lib/duby/ast/class.rb +145 -0
  24. data/lib/duby/ast/flow.rb +328 -0
  25. data/lib/duby/ast/intrinsics.rb +46 -0
  26. data/lib/duby/ast/literal.rb +93 -0
  27. data/lib/duby/ast/local.rb +77 -0
  28. data/lib/duby/ast/method.rb +187 -0
  29. data/lib/duby/ast/structure.rb +44 -0
  30. data/lib/duby/ast/type.rb +93 -0
  31. data/lib/duby/c/compiler.rb +134 -0
  32. data/lib/duby/compiler.rb +261 -0
  33. data/lib/duby/jvm/compiler.rb +684 -0
  34. data/lib/duby/jvm/method_lookup.rb +185 -0
  35. data/lib/duby/jvm/source_compiler.rb +516 -0
  36. data/lib/duby/jvm/source_generator/builder.rb +368 -0
  37. data/lib/duby/jvm/source_generator/loops.rb +132 -0
  38. data/lib/duby/jvm/source_generator/precompile.rb +154 -0
  39. data/lib/duby/jvm/source_generator/typer.rb +11 -0
  40. data/lib/duby/jvm/typer.rb +118 -0
  41. data/lib/duby/jvm/types.rb +326 -0
  42. data/lib/duby/jvm/types/basic_types.rb +18 -0
  43. data/lib/duby/jvm/types/boolean.rb +11 -0
  44. data/lib/duby/jvm/types/factory.rb +151 -0
  45. data/lib/duby/jvm/types/floats.rb +70 -0
  46. data/lib/duby/jvm/types/integers.rb +110 -0
  47. data/lib/duby/jvm/types/intrinsics.rb +157 -0
  48. data/lib/duby/jvm/types/literals.rb +82 -0
  49. data/lib/duby/jvm/types/methods.rb +344 -0
  50. data/lib/duby/jvm/types/number.rb +92 -0
  51. data/lib/duby/nbcompiler.rb +29 -0
  52. data/lib/duby/old/compiler_old.rb +845 -0
  53. data/lib/duby/old/declaration.rb +72 -0
  54. data/lib/duby/old/mapper.rb +72 -0
  55. data/lib/duby/old/signature.rb +52 -0
  56. data/lib/duby/old/typer_old.rb +163 -0
  57. data/lib/duby/plugin/edb.rb +25 -0
  58. data/lib/duby/plugin/java.rb +42 -0
  59. data/lib/duby/plugin/math.rb +84 -0
  60. data/lib/duby/transform.rb +908 -0
  61. data/lib/duby/typer.rb +359 -0
  62. data/test/test_ast.rb +391 -0
  63. data/test/test_compilation.rb +98 -0
  64. data/test/test_java_typer.rb +199 -0
  65. data/test/test_javac_compiler.rb +57 -0
  66. data/test/test_jvm_compiler.rb +1459 -0
  67. data/test/test_math_plugin.rb +87 -0
  68. data/test/test_typer.rb +246 -0
  69. metadata +155 -0
@@ -0,0 +1,154 @@
1
+ require 'duby/ast'
2
+
3
+ module Duby::AST
4
+ class TempValue
5
+ def initialize(node, compiler=nil, value=nil)
6
+ if compiler.nil?
7
+ @tempname = node
8
+ else
9
+ @tempname = compiler.temp(node, value)
10
+ @tempvalue = value || node
11
+ end
12
+ end
13
+
14
+ def compile(compiler, expression)
15
+ if expression
16
+ compiler.method.print @tempname
17
+ end
18
+ end
19
+
20
+ def reload(compiler)
21
+ compiler.assign(@tempname, @tempvalue)
22
+ end
23
+ end
24
+
25
+ class Node
26
+ def expr?(compiler)
27
+ true
28
+ end
29
+
30
+ def precompile(compiler)
31
+ if expr?(compiler)
32
+ self
33
+ else
34
+ temp(compiler)
35
+ end
36
+ end
37
+
38
+ def temp(compiler, value=nil)
39
+ TempValue.new(self, compiler, value)
40
+ end
41
+ end
42
+
43
+ class Body
44
+ def expr?(compiler)
45
+ false
46
+ end
47
+ end
48
+
49
+ class If
50
+ def expr?(compiler)
51
+ return false unless condition.predicate.expr?(compiler)
52
+ return false unless body.nil? || body.expr?(compiler)
53
+ return false unless self.else.nil? || self.else.expr?(compiler)
54
+ true
55
+ end
56
+ end
57
+
58
+ class Loop
59
+ def expr?(compiler)
60
+ false
61
+ end
62
+
63
+ def precompile(compiler)
64
+ compile(compiler, false)
65
+ temp(compiler, 'null')
66
+ end
67
+ end
68
+
69
+ class Call
70
+ def method(compiler=nil)
71
+ @method ||= begin
72
+ arg_types = parameters.map {|p| p.inferred_type}
73
+ target.inferred_type.get_method(name, arg_types)
74
+ end
75
+ end
76
+
77
+ def expr?(compiler)
78
+ target.expr?(compiler) &&
79
+ parameters.all? {|p| p.expr?(compiler)} &&
80
+ !method.actual_return_type.void?
81
+ end
82
+ end
83
+
84
+ class FunctionalCall
85
+ def method(compiler)
86
+ @method ||= begin
87
+ arg_types = parameters.map {|p| p.inferred_type}
88
+ compiler.self_type.get_method(name, arg_types)
89
+ end
90
+ end
91
+
92
+ def expr?(compiler)
93
+ parameters.all? {|p| p.expr?(compiler)} &&
94
+ (cast? || !method(compiler).actual_return_type.void?)
95
+ end
96
+ end
97
+
98
+ class EmtpyArray
99
+ def expr?(compiler)
100
+ size.expr?(compiler)
101
+ end
102
+ end
103
+
104
+ class LocalAssignment
105
+ def expr?(compiler)
106
+ compiler.method.local?(name) && value.expr?(compiler)
107
+ end
108
+
109
+ def precompile(compiler)
110
+ if expr?(compiler)
111
+ self
112
+ else
113
+ compile(compiler, false)
114
+ TempValue.new(name)
115
+ end
116
+ end
117
+ end
118
+
119
+ class Return
120
+ def expr?(compiler)
121
+ false
122
+ end
123
+ end
124
+
125
+ class Raise
126
+ def expr?(compiler)
127
+ false
128
+ end
129
+ end
130
+
131
+ class Rescue
132
+ def expr?(compiler)
133
+ false
134
+ end
135
+ end
136
+
137
+ class Ensure
138
+ def expr?(compiler)
139
+ false
140
+ end
141
+ end
142
+
143
+ class FieldAssignment
144
+ def expr?(compiler)
145
+ false
146
+ end
147
+ end
148
+
149
+ class LocalAssignment
150
+ def expr?(compiler)
151
+ false
152
+ end
153
+ end
154
+ end
@@ -0,0 +1,11 @@
1
+ require 'duby/jvm/typer'
2
+ require 'duby/jvm/source_generator/builder'
3
+
4
+ module Duby
5
+ module Typer
6
+ class JavaSource < JVM
7
+ include Duby::JVM::Types
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,118 @@
1
+ require 'duby/typer'
2
+ require 'duby/jvm/types'
3
+ require 'duby/jvm/types/factory'
4
+
5
+ module Duby
6
+ module Typer
7
+ class JVM < Simple
8
+ include Duby::JVM::Types
9
+
10
+ def initialize(filename)
11
+ @factory = AST.type_factory
12
+ unless @factory.kind_of? TypeFactory
13
+ raise "TypeFactory not installed"
14
+ end
15
+ @known_types = @factory.known_types
16
+ classname = File.basename(filename, '.duby')
17
+ main_class = @factory.declare_type(classname)
18
+ @known_types['self'] = main_class.meta
19
+ @errors = []
20
+ end
21
+
22
+ def type_reference(name, array=false, meta=false)
23
+ @factory.type(name, array, meta)
24
+ end
25
+
26
+ def alias_types(short, long)
27
+ @known_types[short] = type_reference(long)
28
+ end
29
+
30
+ def name
31
+ "JVM"
32
+ end
33
+
34
+ def type_definition(name, superclass, interfaces)
35
+ @known_types[name]
36
+ end
37
+
38
+ def null_type
39
+ Null
40
+ end
41
+
42
+ def no_type
43
+ Void
44
+ end
45
+
46
+ def learn_method_type(target_type, name, parameter_types, type, exceptions)
47
+ static = target_type.meta?
48
+ unless target_type.unmeta.kind_of?(TypeDefinition)
49
+ raise "Method defined on #{target_type}"
50
+ end
51
+ if static
52
+ target_type.unmeta.declare_static_method(name, parameter_types, type, exceptions)
53
+ else
54
+ target_type.declare_method(name, parameter_types, type, exceptions)
55
+ end
56
+ super
57
+ end
58
+
59
+ def infer_signature(method_def)
60
+ signature = method_def.signature
61
+ sig_args = signature.dup
62
+ return_type = sig_args.delete(:return)
63
+ exceptions = sig_args.delete(:throws)
64
+ args = method_def.arguments.args || []
65
+ static = method_def.kind_of? Duby::AST::StaticMethodDefinition
66
+ if sig_args.size != args.size
67
+ # If the superclass declares one method with the same name and
68
+ # same number of arguments, assume we're overriding it.
69
+ found = nil
70
+ ambiguous = false
71
+ classes = [self_type.superclass] + self_type.interfaces
72
+ while classes.size > 0
73
+ cls = classes.pop
74
+ if static
75
+ methods = cls.declared_class_methods
76
+ else
77
+ methods = cls.declared_instance_methods
78
+ end
79
+ methods.each do |method|
80
+ if method.name == method_def.name &&
81
+ method.argument_types.size == args.size
82
+ if found && found.argument_types != method.argument_types
83
+ ambiguous = true
84
+ else
85
+ found ||= method
86
+ end
87
+ end
88
+ end
89
+ classes << cls.superclass if cls.superclass
90
+ end
91
+ if found && !ambiguous
92
+ signature[:return] = found.actual_return_type
93
+ signature[:throws] = found.exceptions
94
+ args.zip(found.argument_types) do |arg, type|
95
+ signature[arg.name.intern] = type
96
+ end
97
+ end
98
+ elsif signature[:return].nil? && !static
99
+ arg_types = args.map do |arg|
100
+ signature[arg.name.intern]
101
+ end
102
+ method = self_type.find_method(
103
+ self_type, method_def.name, arg_types, false)
104
+ interfaces = self_type.interfaces.dup
105
+ until method || interfaces.empty?
106
+ interface = interfaces.pop
107
+ method = interface.find_method(
108
+ interface, method_def.name, arg_types, false)
109
+ end
110
+ if method
111
+ signature[:return] = method.actual_return_type
112
+ signature[:throws] = method.exceptions
113
+ end
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,326 @@
1
+ require 'bitescript'
2
+ require 'duby/ast'
3
+ require 'duby/jvm/method_lookup'
4
+
5
+ class Object
6
+ def class_builder?
7
+ self.class.name =~ /::ClassBuilder$/
8
+ end
9
+ end
10
+
11
+ module Duby
12
+ module JVM
13
+ module Types
14
+ class Type < AST::TypeReference
15
+ include Duby::JVM::MethodLookup
16
+
17
+ def initialize(java_type)
18
+ orig_type = java_type
19
+ if !(java_type.kind_of?(Java::JavaClass) ||
20
+ java_type.class_builder?)
21
+ java_type = java_type.java_class
22
+ end
23
+ super(java_type.name, false, false)
24
+ raise ArgumentError, "Bad type #{orig_type}" if name =~ /Java::/
25
+ @type = java_type
26
+ end
27
+
28
+ def jvm_type
29
+ @type
30
+ end
31
+
32
+ def void?
33
+ false
34
+ end
35
+
36
+ def meta?
37
+ false
38
+ end
39
+
40
+ def array?
41
+ false
42
+ end
43
+
44
+ def primitive?
45
+ false
46
+ end
47
+
48
+ def interface?
49
+ @type.interface?
50
+ end
51
+
52
+ def is_parent(other)
53
+ assignable_from?(other)
54
+ end
55
+
56
+ def compatible?(other)
57
+ assignable_from?(other)
58
+ end
59
+
60
+ def assignable_from?(other)
61
+ return true if !primitive? && other == Null
62
+ return true if other == self
63
+ return true if other.error? || other.unreachable?
64
+ begin
65
+ jvm_type.assignable_from?(other.jvm_type)
66
+ rescue
67
+ assignable_from?(other.superclass) ||
68
+ other.interfaces.any? {|i| assignable_from?(i)}
69
+ end
70
+ end
71
+
72
+ def iterable?
73
+ ['java.lang.Iterable',
74
+ 'java.util.Iterator',
75
+ 'java.util.Enumeration'].any? {|n| AST.type(n).assignable_from(self)}
76
+ end
77
+
78
+ def component_type
79
+ AST.type('java.lang.Object') if iterable?
80
+ end
81
+
82
+ def meta
83
+ @meta ||= MetaType.new(self)
84
+ end
85
+
86
+ def unmeta
87
+ self
88
+ end
89
+
90
+ def basic_type
91
+ self
92
+ end
93
+
94
+ def array_type
95
+ @array_type ||= ArrayType.new(self)
96
+ end
97
+
98
+ def prefix
99
+ 'a'
100
+ end
101
+
102
+ # is this a 64 bit type?
103
+ def wide?
104
+ false
105
+ end
106
+
107
+ def inspect(indent=0)
108
+ "#{' ' * indent}#<#{self.class.name} #{name}>"
109
+ end
110
+
111
+ def newarray(method)
112
+ method.anewarray(self)
113
+ end
114
+
115
+ def superclass
116
+ raise "Incomplete type #{self}" unless jvm_type
117
+ AST.type(jvm_type.superclass) if jvm_type.superclass
118
+ end
119
+
120
+ def interfaces
121
+ @interfaces ||= jvm_type.interfaces.map do |interface|
122
+ AST.type(interface)
123
+ end
124
+ end
125
+
126
+ def astore(builder)
127
+ if primitive?
128
+ builder.send "#{name[0,1]}astore"
129
+ else
130
+ builder.aastore
131
+ end
132
+ end
133
+
134
+ def aload(builder)
135
+ if primitive?
136
+ builder.send "#{name[0,1]}aload"
137
+ else
138
+ builder.aaload
139
+ end
140
+ end
141
+ end
142
+
143
+ class PrimitiveType < Type
144
+ def initialize(type, wrapper)
145
+ @wrapper = wrapper
146
+ super(type)
147
+ end
148
+
149
+ def primitive?
150
+ true
151
+ end
152
+
153
+ def primitive_type
154
+ @wrapper::TYPE
155
+ end
156
+
157
+ def newarray(method)
158
+ method.send "new#{name}array"
159
+ end
160
+
161
+ def interfaces
162
+ []
163
+ end
164
+
165
+ def convertible_to?(type)
166
+ return true if type == self
167
+ a, b = TYPE_ORDERING.index(self), TYPE_ORDERING.index(type)
168
+ a && b && b > a
169
+ end
170
+ end
171
+
172
+ class MetaType < Type
173
+ attr_reader :unmeta
174
+
175
+ def initialize(unmeta)
176
+ @name = unmeta.name
177
+ @unmeta = unmeta
178
+ end
179
+
180
+ def basic_type
181
+ @unmeta.basic_type
182
+ end
183
+
184
+ def meta?
185
+ true
186
+ end
187
+
188
+ def meta
189
+ self
190
+ end
191
+
192
+ def superclass
193
+ @unmeta.superclass.meta if @unmeta.superclass
194
+ end
195
+
196
+ def interfaces
197
+ []
198
+ end
199
+
200
+ def jvm_type
201
+ unmeta.jvm_type
202
+ end
203
+ end
204
+
205
+ class NullType < Type
206
+ def initialize
207
+ super(java.lang.Object)
208
+ end
209
+
210
+ def compatible?(other)
211
+ !other.primitive?
212
+ end
213
+ end
214
+
215
+ class VoidType < PrimitiveType
216
+ def initialize
217
+ super(Java::JavaLang::Void, Java::JavaLang::Void)
218
+ @name = "void"
219
+ end
220
+
221
+ def void?
222
+ true
223
+ end
224
+
225
+ def return(builder)
226
+ builder.returnvoid
227
+ end
228
+ end
229
+
230
+ class ArrayType < Type
231
+ attr_reader :component_type
232
+
233
+ def initialize(component_type)
234
+ @component_type = component_type
235
+ @name = component_type.name
236
+ end
237
+
238
+ def jvm_type
239
+ @component_type.jvm_type
240
+ end
241
+
242
+ def array?
243
+ true
244
+ end
245
+
246
+ def iterable?
247
+ true
248
+ end
249
+
250
+ def basic_type
251
+ component_type.basic_type
252
+ end
253
+
254
+ def superclass
255
+ Object
256
+ end
257
+ end
258
+
259
+ class TypeDefinition < Type
260
+ attr_accessor :node
261
+
262
+ def initialize(name, node)
263
+ if name.class_builder?
264
+ super(name)
265
+ else
266
+ raise ArgumentError, "Bad name #{name}" if name[0,1] == '.'
267
+ @name = name
268
+ end
269
+ @node = node
270
+ raise ArgumentError, "Bad type #{name}" if self.name =~ /Java::/
271
+ end
272
+
273
+ def name
274
+ if @type
275
+ @type.name
276
+ else
277
+ @name
278
+ end
279
+ end
280
+
281
+ def superclass
282
+ (node && node.superclass) || Object
283
+ end
284
+
285
+ def interfaces
286
+ if node
287
+ node.interfaces
288
+ else
289
+ []
290
+ end
291
+ end
292
+
293
+ def define(builder)
294
+ class_name = @name.tr('.', '/')
295
+ @type ||= builder.public_class(class_name, superclass, *interfaces)
296
+ end
297
+
298
+ def meta
299
+ @meta ||= TypeDefMeta.new(self)
300
+ end
301
+ end
302
+
303
+ class InterfaceDefinition < TypeDefinition
304
+ def initialize(name, node)
305
+ super(name, node)
306
+ end
307
+
308
+ def define(builder)
309
+ @type ||= builder.public_interface(@name, *interfaces)
310
+ end
311
+ end
312
+
313
+ class TypeDefMeta < MetaType
314
+ end
315
+ end
316
+ end
317
+ end
318
+
319
+ require 'duby/jvm/types/intrinsics'
320
+ require 'duby/jvm/types/methods'
321
+ require 'duby/jvm/types/number'
322
+ require 'duby/jvm/types/integers'
323
+ require 'duby/jvm/types/boolean'
324
+ require 'duby/jvm/types/floats'
325
+ require 'duby/jvm/types/basic_types'
326
+ require 'duby/jvm/types/literals'