mirah 0.0.7-java → 0.0.8-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (182) hide show
  1. data/History.txt +181 -0
  2. data/README.txt +6 -10
  3. data/Rakefile +86 -9
  4. data/bin/mirah +2 -0
  5. data/bin/mirahc +2 -0
  6. data/bin/mirahp +2 -0
  7. data/{bin/dubyp → examples/interfaces.mirah} +16 -9
  8. data/examples/macros/square.mirah +12 -0
  9. data/examples/macros/square_int.mirah +12 -0
  10. data/examples/macros/string-each-char.mirah +14 -0
  11. data/examples/maven/README.txt +2 -0
  12. data/examples/maven/pom.xml +23 -0
  13. data/examples/maven/src/main/mirah/hello_mirah.mirah +9 -0
  14. data/examples/rosettacode/100-doors.mirah +44 -0
  15. data/examples/rosettacode/99-bottles-of-beer.mirah +13 -0
  16. data/examples/rosettacode/README.txt +9 -0
  17. data/examples/rosettacode/boolean-values.mirah +29 -0
  18. data/examples/rosettacode/comments.mirah +2 -0
  19. data/examples/rosettacode/copy-a-string.mirah +10 -0
  20. data/examples/rosettacode/count-occurrences-of-a-substring.mirah +40 -0
  21. data/examples/rosettacode/create-a-file.mirah +6 -0
  22. data/examples/rosettacode/empty-string.mirah +9 -0
  23. data/examples/rosettacode/factorial.mirah +10 -0
  24. data/examples/rosettacode/fibonacci.mirah +21 -0
  25. data/examples/rosettacode/file-size.mirah +5 -0
  26. data/examples/rosettacode/fizz-buzz.mirah +21 -0
  27. data/examples/rosettacode/flatten-a-list.mirah +24 -0
  28. data/examples/rosettacode/guess-the-number.mirah +21 -0
  29. data/examples/rosettacode/is-string-numeric.mirah +127 -0
  30. data/examples/rosettacode/palindrome.mirah +14 -0
  31. data/examples/rosettacode/repeat-a-string.mirah +9 -0
  32. data/examples/rosettacode/reverse-a-string.mirah +6 -0
  33. data/examples/rosettacode/rot-13.mirah +20 -0
  34. data/examples/rosettacode/user-input.mirah +4 -0
  35. data/examples/sort_closure.mirah +1 -1
  36. data/javalib/dynalink-0.2.jar +0 -0
  37. data/javalib/mirah-bootstrap.jar +0 -0
  38. data/lib/mirah.rb +7 -16
  39. data/lib/mirah/ast.rb +22 -92
  40. data/lib/mirah/ast/call.rb +41 -9
  41. data/lib/mirah/ast/class.rb +34 -6
  42. data/lib/mirah/ast/flow.rb +17 -5
  43. data/lib/mirah/ast/intrinsics.rb +50 -8
  44. data/lib/mirah/ast/literal.rb +7 -0
  45. data/lib/mirah/ast/local.rb +9 -1
  46. data/lib/mirah/ast/method.rb +21 -8
  47. data/lib/mirah/ast/scope.rb +1 -1
  48. data/lib/mirah/ast/structure.rb +81 -15
  49. data/lib/mirah/ast/type.rb +4 -0
  50. data/{bin/dubyc → lib/mirah/commands.rb} +4 -11
  51. data/lib/mirah/commands/base.rb +54 -0
  52. data/lib/mirah/commands/compile.rb +39 -0
  53. data/{examples/wiki/Rakefile → lib/mirah/commands/parse.rb} +18 -17
  54. data/lib/mirah/commands/run.rb +73 -0
  55. data/lib/mirah/compiler.rb +37 -417
  56. data/lib/mirah/compiler/call.rb +45 -0
  57. data/lib/mirah/compiler/class.rb +81 -0
  58. data/lib/mirah/compiler/flow.rb +109 -0
  59. data/lib/mirah/compiler/literal.rb +130 -0
  60. data/lib/mirah/compiler/local.rb +59 -0
  61. data/lib/mirah/compiler/method.rb +44 -0
  62. data/lib/mirah/compiler/structure.rb +65 -0
  63. data/lib/mirah/compiler/type.rb +27 -0
  64. data/lib/mirah/env.rb +4 -6
  65. data/lib/mirah/generator.rb +61 -0
  66. data/lib/mirah/jvm/compiler.rb +8 -867
  67. data/lib/mirah/jvm/compiler/base.rb +270 -0
  68. data/lib/mirah/jvm/compiler/java_source.rb +779 -0
  69. data/lib/mirah/jvm/compiler/jvm_bytecode.rb +851 -0
  70. data/lib/mirah/jvm/method_lookup.rb +21 -2
  71. data/lib/mirah/jvm/source_generator/builder.rb +10 -13
  72. data/lib/mirah/jvm/source_generator/loops.rb +99 -93
  73. data/lib/mirah/jvm/source_generator/precompile.rb +3 -2
  74. data/lib/mirah/jvm/typer.rb +3 -3
  75. data/lib/mirah/jvm/types.rb +10 -426
  76. data/lib/mirah/jvm/types/array_type.rb +62 -0
  77. data/lib/mirah/jvm/types/basic_types.rb +1 -0
  78. data/lib/mirah/jvm/types/dynamic_type.rb +46 -0
  79. data/lib/mirah/jvm/types/factory.rb +23 -5
  80. data/lib/mirah/jvm/types/interface_definition.rb +20 -0
  81. data/lib/mirah/jvm/types/intrinsics.rb +15 -3
  82. data/lib/mirah/jvm/types/meta_type.rb +45 -0
  83. data/lib/mirah/jvm/types/methods.rb +12 -5
  84. data/lib/mirah/jvm/types/null_type.rb +27 -0
  85. data/lib/mirah/jvm/types/primitive_type.rb +38 -0
  86. data/lib/mirah/jvm/types/source_mirror.rb +266 -0
  87. data/lib/mirah/jvm/types/type.rb +173 -0
  88. data/lib/mirah/jvm/types/type_definition.rb +55 -0
  89. data/lib/mirah/jvm/types/unreachable_type.rb +27 -0
  90. data/lib/mirah/jvm/types/void_type.rb +19 -0
  91. data/lib/mirah/parser.rb +90 -0
  92. data/lib/mirah/plugin/gwt.rb +5 -5
  93. data/lib/mirah/plugin/java.rb +1 -1
  94. data/lib/mirah/transform.rb +4 -321
  95. data/lib/mirah/transform/ast_ext.rb +63 -0
  96. data/lib/mirah/transform/error.rb +13 -0
  97. data/lib/mirah/transform/helper.rb +761 -0
  98. data/lib/mirah/transform/transformer.rb +255 -0
  99. data/lib/mirah/typer.rb +2 -383
  100. data/{bin/duby → lib/mirah/typer/base.rb} +12 -10
  101. data/lib/mirah/typer/simple.rb +377 -0
  102. data/lib/mirah/util/argument_processor.rb +114 -0
  103. data/lib/mirah/util/class_loader.rb +37 -0
  104. data/lib/mirah/util/compilation_state.rb +51 -0
  105. data/lib/mirah/util/process_errors.rb +33 -0
  106. data/lib/mirah/version.rb +1 -1
  107. data/lib/mirah_task.rb +3 -2
  108. data/test/{test_ast.rb → core/test_ast.rb} +6 -0
  109. data/test/{test_compilation.rb → core/test_compilation.rb} +0 -0
  110. data/test/{test_env.rb → core/test_env.rb} +24 -25
  111. data/test/{test_macros.rb → core/test_macros.rb} +2 -4
  112. data/test/{test_typer.rb → core/test_typer.rb} +0 -3
  113. data/test/jvm/bytecode_test_helper.rb +181 -0
  114. data/test/{test_javac_compiler.rb → jvm/javac_test_helper.rb} +38 -22
  115. data/test/jvm/test_enumerable.rb +304 -0
  116. data/test/{test_java_typer.rb → jvm/test_java_typer.rb} +2 -4
  117. data/test/{test_jvm_compiler.rb → jvm/test_jvm_compiler.rb} +146 -443
  118. data/test/jvm/test_macros.rb +147 -0
  119. data/test/jvm/test_main_method.rb +15 -0
  120. data/test/{test_gwt.rb → plugins/test_gwt.rb} +0 -2
  121. metadata +103 -91
  122. data/bin/jrubyp +0 -52
  123. data/examples/wiki/src/org/mirah/wiki/MirahWiki.duby +0 -339
  124. data/examples/wiki/src/org/mirah/wiki/edit.eduby.html +0 -42
  125. data/examples/wiki/src/org/mirah/wiki/error.eduby.html +0 -2
  126. data/examples/wiki/src/org/mirah/wiki/layout.eduby.html +0 -69
  127. data/examples/wiki/src/org/mirah/wiki/parser.eduby.html +0 -7
  128. data/examples/wiki/src/org/mirah/wiki/view.eduby.html +0 -15
  129. data/examples/wiki/war/WEB-INF/classes/test/HeredocContext.class +0 -0
  130. data/examples/wiki/war/WEB-INF/classes/test/MirahParser.class +0 -0
  131. data/examples/wiki/war/WEB-INF/lib/appengine-api.jar +0 -0
  132. data/examples/wiki/war/WEB-INF/lib/dubydatastore.jar +0 -0
  133. data/examples/wiki/war/WEB-INF/lib/jmeta-runtime.jar +0 -0
  134. data/examples/wiki/war/WEB-INF/lib/pegdown-stubs.jar +0 -0
  135. data/examples/wiki/war/WEB-INF/pegdown.jar +0 -0
  136. data/examples/wiki/war/app.yaml +0 -21
  137. data/examples/wiki/war/public/favicon.ico +0 -0
  138. data/examples/wiki/war/public/images/appengine_duby.png +0 -0
  139. data/examples/wiki/war/public/images/back.gif +0 -0
  140. data/examples/wiki/war/public/images/dir.gif +0 -0
  141. data/examples/wiki/war/public/images/file.gif +0 -0
  142. data/examples/wiki/war/public/javascripts/prettify.js +0 -61
  143. data/examples/wiki/war/public/robots.txt +0 -0
  144. data/examples/wiki/war/public/stylesheets/main.css +0 -156
  145. data/examples/wiki/war/public/stylesheets/prettify.css +0 -1
  146. data/examples/wiki/war/public/stylesheets/sh_style.css +0 -66
  147. data/examples/wiki/war/public/stylesheets/source.css +0 -21
  148. data/examples/wiki/war/public/wmd/images/bg-fill.png +0 -0
  149. data/examples/wiki/war/public/wmd/images/bg.png +0 -0
  150. data/examples/wiki/war/public/wmd/images/blockquote.png +0 -0
  151. data/examples/wiki/war/public/wmd/images/bold.png +0 -0
  152. data/examples/wiki/war/public/wmd/images/code.png +0 -0
  153. data/examples/wiki/war/public/wmd/images/h1.png +0 -0
  154. data/examples/wiki/war/public/wmd/images/hr.png +0 -0
  155. data/examples/wiki/war/public/wmd/images/img.png +0 -0
  156. data/examples/wiki/war/public/wmd/images/italic.png +0 -0
  157. data/examples/wiki/war/public/wmd/images/link.png +0 -0
  158. data/examples/wiki/war/public/wmd/images/ol.png +0 -0
  159. data/examples/wiki/war/public/wmd/images/redo.png +0 -0
  160. data/examples/wiki/war/public/wmd/images/separator.png +0 -0
  161. data/examples/wiki/war/public/wmd/images/ul.png +0 -0
  162. data/examples/wiki/war/public/wmd/images/undo.png +0 -0
  163. data/examples/wiki/war/public/wmd/images/wmd-on.png +0 -0
  164. data/examples/wiki/war/public/wmd/images/wmd.png +0 -0
  165. data/examples/wiki/war/public/wmd/showdown.js +0 -421
  166. data/examples/wiki/war/public/wmd/wmd-base.js +0 -1799
  167. data/examples/wiki/war/public/wmd/wmd-plus.js +0 -311
  168. data/examples/wiki/war/public/wmd/wmd.js +0 -73
  169. data/examples/wiki/war/src/org/mirah/wiki/MirahWiki.duby +0 -339
  170. data/examples/wiki/war/src/org/mirah/wiki/edit.eduby.html +0 -42
  171. data/examples/wiki/war/src/org/mirah/wiki/error.eduby.html +0 -2
  172. data/examples/wiki/war/src/org/mirah/wiki/layout.eduby.html +0 -69
  173. data/examples/wiki/war/src/org/mirah/wiki/parser.eduby.html +0 -7
  174. data/examples/wiki/war/src/org/mirah/wiki/view.eduby.html +0 -15
  175. data/javalib/dynalink-0.1.jar +0 -0
  176. data/javalib/jsr292-mock.jar +0 -0
  177. data/lib/mirah/class_loader.rb +0 -35
  178. data/lib/mirah/compilation_state.rb +0 -28
  179. data/lib/mirah/impl.rb +0 -273
  180. data/lib/mirah/jvm/base.rb +0 -267
  181. data/lib/mirah/jvm/source_compiler.rb +0 -760
  182. data/lib/mirah/transform2.rb +0 -752
@@ -0,0 +1,173 @@
1
+ module Mirah
2
+ module JVM
3
+ module Types
4
+ class Type < AST::TypeReference
5
+ include Java::DubyLangCompiler::Class
6
+ include Mirah::JVM::MethodLookup
7
+
8
+ attr_writer :inner_class
9
+
10
+ def log(message)
11
+ puts "* [JVM::Types] #{message}" if Mirah::JVM::Compiler::JVMBytecode.verbose
12
+ end
13
+
14
+ def initialize(mirror_or_name)
15
+ if mirror_or_name.kind_of?(BiteScript::ASM::ClassMirror)
16
+ @type = mirror_or_name
17
+ name = mirror_or_name.type.class_name
18
+ else
19
+ name = mirror_or_name.to_s
20
+ end
21
+ super(name, false, false)
22
+ raise ArgumentError, "Bad type #{mirror_or_name}" if name =~ /Java::/
23
+ end
24
+
25
+ def full_name
26
+ desc = BiteScript::Signature.class_id(self)
27
+ BiteScript::ASM::Type.get_type(desc).class_name
28
+ end
29
+
30
+ def jvm_type
31
+ @type
32
+ end
33
+
34
+ def void?
35
+ false
36
+ end
37
+
38
+ def meta?
39
+ false
40
+ end
41
+
42
+ def array?
43
+ false
44
+ end
45
+
46
+ def primitive?
47
+ false
48
+ end
49
+
50
+ def interface?
51
+ # FIXME: Don't do rescue nil. Figure out a cleaner way to handle
52
+ # mirrors for all incoming types without blowing up on e.g. 'boolean' or 'int'
53
+ (@type || BiteScript::ASM::ClassMirror.for_name(@name)).interface? rescue nil
54
+ end
55
+
56
+ def dynamic?
57
+ false
58
+ end
59
+
60
+ def inner_class?
61
+ @inner_class
62
+ end
63
+
64
+ def is_parent(other)
65
+ assignable_from?(other)
66
+ end
67
+
68
+ def compatible?(other)
69
+ assignable_from?(other)
70
+ end
71
+
72
+ def assignable_from?(other)
73
+ return false if other.nil?
74
+ return true if !primitive? && other == Null
75
+ return true if other == self
76
+ return true if other.error? || other.unreachable?
77
+
78
+ # TODO should we allow more here?
79
+ return interface? if other.block?
80
+
81
+ return true if jvm_type && (jvm_type == other.jvm_type)
82
+
83
+ assignable_from?(other.superclass) ||
84
+ other.interfaces.any? {|i| assignable_from?(i)}
85
+ end
86
+
87
+ def iterable?
88
+ ['java.lang.Iterable',
89
+ 'java.util.Iterator',
90
+ 'java.util.Enumeration'].any? {|n| AST.type(nil, n).assignable_from(self)}
91
+ end
92
+
93
+ def component_type
94
+ AST.type(nil, 'java.lang.Object') if iterable?
95
+ end
96
+
97
+ def meta
98
+ @meta ||= MetaType.new(self)
99
+ end
100
+
101
+ def unmeta
102
+ self
103
+ end
104
+
105
+ def basic_type
106
+ self
107
+ end
108
+
109
+ def array_type
110
+ @array_type ||= Mirah::JVM::Types::ArrayType.new(self)
111
+ end
112
+
113
+ def prefix
114
+ 'a'
115
+ end
116
+
117
+ # is this a 64 bit type?
118
+ def wide?
119
+ false
120
+ end
121
+
122
+ def inspect(indent=0)
123
+ "#{' ' * indent}#<#{self.class.name} #{name}>"
124
+ end
125
+
126
+ def newarray(method)
127
+ method.anewarray(self)
128
+ end
129
+
130
+ def pop(method)
131
+ if wide?
132
+ method.pop2
133
+ else
134
+ method.pop
135
+ end
136
+ end
137
+
138
+ def superclass
139
+ raise "Incomplete type #{self}" unless jvm_type
140
+ AST.type(nil, jvm_type.superclass) if jvm_type.superclass
141
+ end
142
+
143
+ def interfaces
144
+ raise "Incomplete type #{self} (#{self.class})" unless jvm_type
145
+ @interfaces ||= begin
146
+ interfaces = jvm_type.interfaces.map {|i| AST.type(nil, i)}.to_set
147
+ if superclass
148
+ interfaces |= superclass.interfaces
149
+ end
150
+ interfaces.to_a
151
+ end
152
+ @interfaces
153
+ end
154
+
155
+ def astore(builder)
156
+ if primitive?
157
+ builder.send "#{name[0,1]}astore"
158
+ else
159
+ builder.aastore
160
+ end
161
+ end
162
+
163
+ def aload(builder)
164
+ if primitive?
165
+ builder.send "#{name[0,1]}aload"
166
+ else
167
+ builder.aaload
168
+ end
169
+ end
170
+ end
171
+ end
172
+ end
173
+ end
@@ -0,0 +1,55 @@
1
+ module Mirah
2
+ module JVM
3
+ module Types
4
+ class TypeDefinition < Type
5
+ attr_accessor :node
6
+
7
+ def initialize(name, node)
8
+ raise ArgumentError, "Bad name #{name}" if name[0,1] == '.'
9
+ raise ArgumentError, "Bad name #{name}" if name.include? ?/
10
+ @name = name
11
+ @node = node
12
+ raise ArgumentError, "Bad type #{name}" if self.name =~ /Java::/
13
+ end
14
+
15
+ def name
16
+ if @type
17
+ @type.name.tr('/', '.')
18
+ else
19
+ @name
20
+ end
21
+ end
22
+
23
+ def superclass
24
+ (node && node.superclass) || Object
25
+ end
26
+
27
+ def interfaces
28
+ if node
29
+ node.interfaces
30
+ else
31
+ []
32
+ end
33
+ end
34
+
35
+ def define(builder)
36
+ class_name = @name.tr('.', '/')
37
+ abstract = node && node.abstract
38
+ @type ||= builder.define_class(
39
+ class_name,
40
+ :visibility => :public,
41
+ :abstract => abstract,
42
+ :superclass => superclass,
43
+ :interfaces => interfaces)
44
+ end
45
+
46
+ def meta
47
+ @meta ||= TypeDefMeta.new(self)
48
+ end
49
+ end
50
+
51
+ class TypeDefMeta < MetaType
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,27 @@
1
+ module Mirah
2
+ module JVM
3
+ module Types
4
+ class UnreachableType < Type
5
+ def initialize
6
+ super('java.lang.Object')
7
+ end
8
+
9
+ def to_s
10
+ "Type(null)"
11
+ end
12
+
13
+ def unreachable?
14
+ true
15
+ end
16
+
17
+ def compatible?(other)
18
+ true
19
+ end
20
+
21
+ def assignable_from?(other)
22
+ true
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,19 @@
1
+ module Mirah
2
+ module JVM
3
+ module Types
4
+ class VoidType < PrimitiveType
5
+ def initialize
6
+ super('void', Java::JavaLang::Void)
7
+ end
8
+
9
+ def void?
10
+ true
11
+ end
12
+
13
+ def return(builder)
14
+ builder.returnvoid
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,90 @@
1
+ # Copyright (c) 2010 The Mirah project authors. All Rights Reserved.
2
+ # All contributing project authors may be found in the NOTICE file.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ require 'mirah/util/process_errors'
17
+ require 'mirah/transform'
18
+ require 'java'
19
+
20
+ module Mirah
21
+ class Parser
22
+ include Mirah::Util::ProcessErrors
23
+
24
+ def initialize(state, logging)
25
+ @transformer = Mirah::Transform::Transformer.new(state)
26
+ Java::MirahImpl::Builtin.initialize_builtins(@transformer)
27
+ @logging = logging
28
+ @verbose = state.verbose
29
+ end
30
+
31
+ attr_accessor :transformer, :logging
32
+
33
+ def parse_from_args(files_or_scripts)
34
+ nodes = []
35
+ inline = false
36
+ puts "Parsing..." if logging
37
+ expand_files(files_or_scripts).each do |script|
38
+ if script == '-e'
39
+ inline = true
40
+ next
41
+ elsif inline
42
+ nodes << parse_inline(script)
43
+ break
44
+ else
45
+ nodes << parse_file(script)
46
+ end
47
+ end
48
+ raise 'nothing to parse? ' + files_or_scripts.inspect unless nodes.length > 0
49
+ nodes
50
+ end
51
+
52
+ def parse_inline(source)
53
+ puts " <inline script>" if logging
54
+ parse_and_transform('DashE', source)
55
+ end
56
+
57
+ def parse_file(filename)
58
+ puts " #{filename}" if logging
59
+ parse_and_transform(filename, File.read(filename))
60
+ end
61
+
62
+ def parse_and_transform(filename, src)
63
+ parser_ast = Mirah::AST.parse_ruby(src, filename)
64
+
65
+ transformer.filename = filename
66
+ mirah_ast = transformer.transform(parser_ast, nil)
67
+ process_errors(transformer.errors)
68
+
69
+ mirah_ast
70
+ end
71
+
72
+ def expand_files(files_or_scripts)
73
+ expanded = []
74
+ files_or_scripts.each do |filename|
75
+ if File.directory?(filename)
76
+ Dir[File.join(filename, '*')].each do |child|
77
+ if File.directory?(child)
78
+ files_or_scripts << child
79
+ elsif child =~ /\.(duby|mirah)$/
80
+ expanded << child
81
+ end
82
+ end
83
+ else
84
+ expanded << filename
85
+ end
86
+ end
87
+ expanded
88
+ end
89
+ end
90
+ end
@@ -65,13 +65,13 @@ module Mirah::JavaSource
65
65
  end
66
66
  end
67
67
 
68
- module Mirah::Compiler
69
- class JVMCompilerBase
68
+ module Mirah::JVM::Compiler
69
+ class Base
70
+ alias :original_create_method_builder :create_method_builder
70
71
  # arg_types must be an Array
71
72
  def create_method_builder(name, node, static, exceptions, return_type, arg_types)
72
73
  unless node.class == Mirah::AST::JsniMethodDefinition
73
- @class.build_method(name.to_s, node.visibility, static,
74
- exceptions, return_type, *arg_types)
74
+ original_create_method_builder(name, node, static, exceptions, return_type, arg_types)
75
75
  else
76
76
  @class.build_jsni_method(name.to_s, node.visibility, static,
77
77
  exceptions, return_type, *arg_types)
@@ -79,7 +79,7 @@ module Mirah::Compiler
79
79
  end
80
80
  end
81
81
 
82
- class JavaSource < JVMCompilerBase
82
+ class JavaSource < Base
83
83
  def define_jsni_method(node)
84
84
  base_define_method(node, false) do |method, arg_types|
85
85
  with :method => method do
@@ -20,7 +20,7 @@ require 'java'
20
20
 
21
21
  module Mirah
22
22
  module Typer
23
- class JavaTyper < BaseTyper
23
+ class JavaTyper < Base
24
24
  include Mirah::JVM::MethodLookup
25
25
  include Mirah::JVM::Types
26
26
 
@@ -16,324 +16,7 @@
16
16
  require 'base64'
17
17
  require 'jruby'
18
18
  require 'mirah/errors'
19
-
20
- module Mirah
21
- module Transform
22
- class Error < Mirah::MirahError
23
- attr_reader :position
24
- def initialize(msg, position, cause=nil)
25
- position = position.position if position.respond_to? :position
26
- super(msg, position)
27
- self.cause = cause
28
- end
29
- end
30
-
31
- class Transformer
32
- begin
33
- include Java::DubyLangCompiler.Compiler
34
- rescue NameError
35
- $CLASSPATH << File.dirname(__FILE__) + '/../../javalib/mirah-bootstrap.jar'
36
- include Java::DubyLangCompiler.Compiler
37
- end
38
-
39
- attr_reader :errors, :state
40
- attr_accessor :filename
41
- def initialize(state)
42
- @errors = []
43
- @tmp_count = 0
44
- @annotations = []
45
- @scopes = []
46
- @extra_body = nil
47
- @state = state
48
- @helper = Mirah::AST::TransformHelper.new(self)
49
- end
50
-
51
- def destination
52
- @state.destination
53
- end
54
-
55
- def verbose?
56
- @state.verbose
57
- end
58
-
59
- def annotations
60
- result, @annotations = @annotations, []
61
- return result
62
- end
63
-
64
- def add_annotation(annotation)
65
- @annotations << annotation
66
- Mirah::AST::Noop.new(annotation.parent, annotation.position)
67
- end
68
-
69
- def tmp(format="__xform_tmp_%d")
70
- format % [@tmp_count += 1]
71
- end
72
-
73
- class JMetaPosition
74
- attr_accessor :start_line, :end_line, :start_offset, :end_offset, :file
75
- attr_accessor :startpos, :endpos, :start_col, :end_col
76
-
77
- def initialize(startpos, endpos)
78
- @startpos = startpos
79
- @endpos = endpos
80
- @file = startpos.filename
81
- @start_line = startpos.line
82
- @start_offset = startpos.pos
83
- @start_col = startpos.col
84
- @end_line = endpos.line
85
- @end_offset = endpos.pos
86
- @end_col = endpos.col
87
- end
88
-
89
- def +(other)
90
- JMetaPosition.new(@startpos, other.endpos)
91
- end
92
- end
93
-
94
- def position(node)
95
- JMetaPosition.new(node.start_position, node.end_position)
96
- end
97
-
98
- def camelize(name)
99
- name.gsub(/([a-z])([A-Z])/, '\1_\2').downcase
100
- end
101
-
102
- def transform(node, parent)
103
- begin
104
- top = @extra_body.nil?
105
- if top
106
- @extra_body = Mirah::AST::Body.new(nil, position(node))
107
- end
108
- method = "transform_#{camelize(node[0])}"
109
- result = @helper.send method, node, parent
110
- if top
111
- body = result.body
112
- if body.kind_of?(Mirah::AST::Body) && @extra_body.empty?
113
- @extra_body = body
114
- else
115
- result.body = @extra_body
116
- body.parent = @extra_body
117
- @extra_body.children.insert(0, body)
118
- end
119
- end
120
- return result
121
- rescue Error => ex
122
- @errors << ex
123
- Mirah::AST::ErrorNode.new(parent, ex)
124
- rescue Exception => ex
125
- error = Error.new(ex.message, position(node), ex)
126
- @errors << error
127
- Mirah::AST::ErrorNode.new(parent, error)
128
- end
129
- end
130
-
131
- def captured?(node)
132
- depth = node.depth
133
- scope = @scopes[-1]
134
- while depth > 0
135
- depth -= 1
136
- scope = scope.enclosing_scope
137
- end
138
- scope.isCaptured(node.index)
139
- end
140
-
141
- def eval(src, filename='-', parent=nil, *vars)
142
- node = Mirah::AST.parse_ruby(src, filename)
143
- duby_node = transform(node, nil).body
144
- duby_node.parent = parent
145
- duby_node
146
- end
147
-
148
- def dump_ast(node, call=nil)
149
- encoded = nil
150
- values = Mirah::AST::Unquote.extract_values do
151
- encoded = Base64.encode64(Marshal.dump(node))
152
- end
153
- scope = call.scope.static_scope if call
154
- result = Mirah::AST::Array.new(nil, node.position)
155
- if encoded.size < 65535
156
- result << Mirah::AST::String.new(result, node.position, encoded)
157
- else
158
- strings = Mirah::AST::StringConcat.new(result, node.position)
159
- result << strings
160
- while encoded.size >= 65535
161
- chunk = encoded[0, 65535]
162
- encoded[0, 65535] = ""
163
- strings << Mirah::AST::String.new(strings, node.position, chunk)
164
- end
165
- strings << Mirah::AST::String.new(strings, node.position, encoded)
166
- end
167
- values.each do |value|
168
- if call
169
- scoped_value = Mirah::AST::ScopedBody.new(result, value.position)
170
- scoped_value << value
171
- scoped_value.static_scope = scope
172
- else
173
- scoped_value = value
174
- end
175
- result << scoped_value
176
- end
177
- return result
178
- end
179
-
180
- def load_ast(args)
181
- nodes = args.to_a
182
- encoded = nodes.shift
183
- Mirah::AST::Unquote.inject_values(nodes) do
184
- result = Marshal.load(Base64.decode64(encoded))
185
- if Mirah::AST::UnquotedValue === result
186
- result.node
187
- else
188
- result
189
- end
190
- end
191
- end
192
-
193
- def __ruby_eval(code, arg)
194
- self.instance_eval(code)
195
- end
196
-
197
- def fixnum(value)
198
- node = eval("1")
199
- node.literal = value
200
- node
201
- end
202
-
203
- def constant(name, array=false)
204
- node = eval("Foo")
205
- node.name = name
206
- node.array = array
207
- node
208
- end
209
-
210
- def cast(type, value)
211
- if value.kind_of?(String)
212
- value = Mirah::AST::Local.new(@extra_body, @extra_body.position, value)
213
- end
214
- fcall = eval("Foo()")
215
- fcall.name = type
216
- fcall.parameters = [value]
217
- fcall
218
- end
219
-
220
- def string(value)
221
- node = eval('"Foo"')
222
- node.literal = value
223
- node
224
- end
225
-
226
- def empty_array(type_node, size_node)
227
- node = eval('int[0]')
228
- node.type_node = type_node
229
- node.size = size_node
230
- node
231
- end
232
-
233
- def find_class(name)
234
- AST.type(nil, name, false, false)
235
- end
236
-
237
- def expand(fvcall, parent)
238
- result = yield self, fvcall, parent
239
- unless AST::Node === result
240
- raise Error.new('Invalid macro result', fvcall.position)
241
- end
242
- result
243
- end
244
-
245
- def append_node(node)
246
- @extra_body << node
247
- node
248
- end
249
-
250
- def define_class(position, name, &block)
251
- append_node Mirah::AST::ClassDefinition.new(@extra_body, position, name, &block)
252
- end
253
-
254
- def defineClass(name, superclass=nil)
255
- define_class(@extra_body.position, name) do |class_def|
256
- superclass = constant(superclass)
257
- superclass.parent = class_def
258
- [superclass, body(class_def)]
259
- end
260
- end
261
-
262
- def body(parent=nil)
263
- parent ||= @extra_body
264
- Mirah::AST::Body.new(parent, parent.position)
265
- end
266
-
267
- def define_closure(position, name, enclosing_type)
268
- target = self
269
- parent = @extra_body
270
- enclosing_type = enclosing_type.unmeta
271
- if enclosing_type.respond_to?(:node) && enclosing_type.node
272
- parent = target = enclosing_type.node
273
- end
274
- target.append_node(Mirah::AST::ClosureDefinition.new(
275
- parent, position, name, enclosing_type))
276
- end
277
- end
278
- end
279
- TransformError = Transform::Error
280
-
281
- module AST
282
- begin
283
- java_import 'mirah.impl.MirahParser'
284
- rescue NameError
285
- $CLASSPATH << File.dirname(__FILE__) + '/../../javalib/mirah-parser.jar'
286
- java_import 'mirah.impl.MirahParser'
287
- end
288
- java_import 'jmeta.ErrorHandler'
289
-
290
- class MirahErrorHandler
291
- include ErrorHandler
292
- def warning(messages, positions)
293
- print "Warning: "
294
- messages.each_with_index do |message, i|
295
- jpos = positions[i]
296
- if jpos
297
- dpos = Mirah::Transform::Transformer::JMetaPosition.new(jpos, jpos)
298
- print "#{message} at "
299
- Mirah.print_error("", dpos)
300
- else
301
- print message
302
- end
303
- end
304
- end
305
- end
306
-
307
- def parse(src, filename='dash_e', raise_errors=false, transformer=nil)
308
- ast = parse_ruby(src, filename)
309
- transformer ||= Transform::Transformer.new(Mirah::CompilationState.new)
310
- transformer.filename = filename
311
- ast = transformer.transform(ast, nil)
312
- if raise_errors
313
- transformer.errors.each do |e|
314
- raise e.cause || e
315
- end
316
- end
317
- ast
318
- end
319
- module_function :parse
320
-
321
- def parse_ruby(src, filename='-')
322
- raise ArgumentError if src.nil?
323
- parser = MirahParser.new
324
- parser.filename = filename
325
- parser.errorHandler = MirahErrorHandler.new
326
- begin
327
- parser.parse(src)
328
- rescue => ex
329
- if ex.cause.respond_to? :position
330
- position = ex.cause.position
331
- Mirah.print_error(ex.cause.message, position)
332
- end
333
- raise ex
334
- end
335
- end
336
- module_function :parse_ruby
337
- end
338
- end
339
- require 'mirah/transform2'
19
+ require 'mirah/transform/error'
20
+ require 'mirah/transform/transformer'
21
+ require 'mirah/transform/ast_ext'
22
+ require 'mirah/transform/helper'