forked from mchung/ruby2ruby_rubyconf08
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruby2java.rb
92 lines (74 loc) · 1.61 KB
/
ruby2java.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
require 'rubygems'
require 'parse_tree'
require 'pp'
# This code is evil
# Please avert your eyes right now
# Dark side this leads to
class Ruby2Java < SexpProcessor
def initialize
super
@indent = " "
self.auto_shift_type = true
self.strict = true
self.expected = String
#@unsupported_checked = false
#@warn_on_default = true
@require_empty = false
# self.debug[:defn] = /zsuper/
end
def self.translate(klass)
sexp = ParseTree.new.parse_tree(JavaClass).first
Ruby2Java.new.process(sexp)
end
def process_class(exp)
"public class #{exp.shift} {\n #{next_token(exp, true)} \n}"
end
def process_const(exp)
"extends #{exp.shift}"
end
def process_defs(exp)
rv = "public #{method_sig(exp)} #{method_name(exp)}"
return rv
end
def method_sig(exp)
if exp[0].first == :self
exp.shift
"static void"
end
end
def method_name(exp)
name = exp.shift
"#{name}(String argv[]) {\n#{method_body(exp)}\n }"
end
def method_body(exp)
# Look, you get the point right? :-D
exp = exp.first.last.last#.last.last.last
process(exp)
end
def process_fcall(exp)
process_puts(exp)
end
def process_puts(exp)
if exp.first == :puts
" System.out.println(\"#{process(exp.last)}\");"
end
end
def process_array(exp)
process(exp.shift)
end
def process_str(exp)
exp.first
end
def next_token(exp, is_class = false)
if is_class
const = exp.shift
process(const)
end
process(exp.first)
end
def dump(exp)
puts "======"
pp exp
puts "======"
end
end