gettc 1.10 → 2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/gettc +60 -69
- data/dist/config.yml +1 -1
- data/dist/include/cpp/engine.rb +78 -86
- data/dist/include/cpp/topcoder +236 -236
- data/dist/include/go/engine.rb +53 -61
- data/dist/include/haskell/engine.rb +112 -122
- data/dist/include/java/engine.rb +187 -184
- data/dist/include/javascript/engine.rb +26 -30
- data/dist/include/javascript/topcoder.js +3 -3
- data/dist/include/javascript/topcoder/errors.js +5 -5
- data/dist/include/javascript/topcoder/reader.js +188 -165
- data/dist/include/javascript/topcoder/writer.js +37 -33
- data/dist/include/python/engine.rb +43 -52
- data/dist/include/python/topcoder/__init__.pyc +0 -0
- data/dist/include/python/topcoder/__pycache__/__init__.cpython-34.pyc +0 -0
- data/dist/include/python/topcoder/__pycache__/errors.cpython-34.pyc +0 -0
- data/dist/include/python/topcoder/__pycache__/reader.cpython-34.pyc +0 -0
- data/dist/include/python/topcoder/__pycache__/writer.cpython-34.pyc +0 -0
- data/dist/include/python/topcoder/errors.pyc +0 -0
- data/dist/include/python/topcoder/reader.pyc +0 -0
- data/dist/include/python/topcoder/writer.pyc +0 -0
- data/dist/include/ruby/engine.rb +47 -52
- data/dist/include/ruby/topcoder/reader.rb +205 -193
- data/dist/include/ruby/topcoder/writer.rb +39 -37
- data/dist/template/bin/runner.rb +146 -151
- data/dist/template/bin/runner.sh +96 -96
- data/dist/template/prob/{name}.html +1 -1
- data/dist/template/solve/cpp/{name}.cpp +3 -4
- data/dist/template/solve/cpp/{name}Solver.cpp +14 -14
- data/dist/template/solve/go/{name}/{name}.go +3 -3
- data/dist/template/solve/go/{name}Solver.go +2 -2
- data/dist/template/solve/haskell/{name}.hs +6 -6
- data/dist/template/solve/haskell/{name}Solver.hs +10 -10
- data/dist/template/solve/java/{name}.java +4 -4
- data/dist/template/solve/java/{name}Solver.java +4 -4
- data/dist/template/solve/javascript/{name}.js +4 -6
- data/dist/template/solve/javascript/{name}Solver.js +11 -9
- data/dist/template/solve/python/{name}.py +1 -1
- data/dist/template/solve/python/{name}Solver.py +2 -2
- data/dist/template/solve/ruby/{name}.rb +4 -4
- data/dist/template/solve/ruby/{name}Solver.rb +14 -17
- data/dist/template/util/check/check.cpp +19 -19
- data/{core/lib → lib}/gettc.rb +0 -0
- data/lib/gettc/account.rb +14 -0
- data/lib/gettc/download.rb +211 -0
- data/lib/gettc/generate.rb +156 -0
- data/lib/gettc/parse.rb +237 -0
- data/lib/gettc/print.rb +54 -0
- data/lib/gettc/problem.rb +39 -0
- data/lib/gettc/signature.rb +63 -0
- data/lib/gettc/types.rb +93 -0
- data/lib/version.rb +3 -0
- data/test/gettc/download_test.rb +61 -0
- data/test/gettc/generate_test.rb +70 -0
- data/test/gettc/parse_test.rb +78 -0
- data/test/gettc/signature_test.rb +71 -0
- data/test/gettc/types_test.rb +31 -0
- metadata +28 -23
- data/core/lib/gettc/download.rb +0 -130
- data/core/lib/gettc/generate.rb +0 -145
- data/core/lib/gettc/parse.rb +0 -233
- data/core/lib/gettc/print.rb +0 -56
- data/core/lib/gettc/problem.rb +0 -33
- data/core/lib/gettc/signature.rb +0 -55
- data/core/lib/gettc/types.rb +0 -83
- data/core/lib/version.rb +0 -3
- data/core/test/gettc/download_test.rb +0 -29
- data/core/test/gettc/generate_test.rb +0 -31
- data/core/test/gettc/parse_test.rb +0 -104
- data/core/test/gettc/signature_test.rb +0 -54
- data/core/test/gettc/types_test.rb +0 -28
data/dist/include/go/engine.rb
CHANGED
@@ -1,70 +1,62 @@
|
|
1
|
-
require "gettc/types"
|
1
|
+
require "gettc/types"
|
2
2
|
|
3
3
|
module Gettc
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
return "[]#{subtype.to_go}"
|
8
|
-
end
|
4
|
+
class Type
|
5
|
+
def to_go
|
6
|
+
return "[]#{subtype.to_go}" if self.is_a?(TArray)
|
9
7
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
def dumb_go
|
30
|
-
if is_a? TArray
|
31
|
-
return to_go + " {}"
|
32
|
-
end
|
8
|
+
case self
|
9
|
+
when TInt
|
10
|
+
"int"
|
11
|
+
when TLong
|
12
|
+
"int64"
|
13
|
+
when TFloat
|
14
|
+
"float32"
|
15
|
+
when TDouble
|
16
|
+
"float64"
|
17
|
+
when TChar
|
18
|
+
"byte"
|
19
|
+
when TString
|
20
|
+
"string"
|
21
|
+
when TBoolean
|
22
|
+
"bool"
|
23
|
+
else
|
24
|
+
"unknown"
|
25
|
+
end
|
26
|
+
end
|
33
27
|
|
34
|
-
|
35
|
-
|
36
|
-
return "0"
|
37
|
-
when TChar
|
38
|
-
return "'$'"
|
39
|
-
when TString
|
40
|
-
return '"$"'
|
41
|
-
when TBoolean
|
42
|
-
return "true"
|
43
|
-
end
|
28
|
+
def dumb_go
|
29
|
+
return "#{self.to_go} {}" if self.is_a?(TArray)
|
44
30
|
|
45
|
-
|
46
|
-
|
31
|
+
case self
|
32
|
+
when TInt, TLong, TDouble, TFloat
|
33
|
+
"0"
|
34
|
+
when TChar
|
35
|
+
"'$'"
|
36
|
+
when TString
|
37
|
+
'"$"'
|
38
|
+
when TBoolean
|
39
|
+
"true"
|
40
|
+
else
|
41
|
+
"nil"
|
42
|
+
end
|
47
43
|
end
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
44
|
+
end
|
45
|
+
|
46
|
+
class Signature
|
47
|
+
def to_go
|
48
|
+
"#{@name} #{@type.to_go}"
|
52
49
|
end
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
temp = temp.map do |name|
|
64
|
-
"&" + name
|
65
|
-
end
|
66
|
-
@input = temp.join ", "
|
67
|
-
@func_name = func.name[0, 1].upcase + func.name[1..-1]
|
68
|
-
end
|
50
|
+
end
|
51
|
+
|
52
|
+
class GoEngine
|
53
|
+
attr_reader :declare, :input, :output, :func_name
|
54
|
+
|
55
|
+
def initialize(func, vars)
|
56
|
+
@declare = vars.map(&:to_go)
|
57
|
+
@output = vars.map(&:name).join(", ")
|
58
|
+
@input = vars.map { |var| "&#{var.name}" }.join(", ")
|
59
|
+
@func_name = func.name[0, 1].upcase + func.name[1..-1]
|
69
60
|
end
|
61
|
+
end
|
70
62
|
end
|
@@ -1,128 +1,118 @@
|
|
1
|
-
require "gettc/types"
|
1
|
+
require "gettc/types"
|
2
2
|
|
3
3
|
module Gettc
|
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
|
-
end
|
50
|
-
|
51
|
-
return "unknown"
|
52
|
-
end
|
53
|
-
def dumb_haskell
|
54
|
-
if is_a? TArray
|
55
|
-
return "[]"
|
56
|
-
end
|
57
|
-
|
58
|
-
case self
|
59
|
-
when TInt, TLong, TFloat, TDouble
|
60
|
-
return "0"
|
61
|
-
when TChar
|
62
|
-
return "'$'"
|
63
|
-
when TString
|
64
|
-
return '"$"'
|
65
|
-
when TBoolean
|
66
|
-
return "True"
|
67
|
-
end
|
68
|
-
|
69
|
-
return "Nil"
|
70
|
-
end
|
4
|
+
class Type
|
5
|
+
def to_haskell
|
6
|
+
return "[#{subtype.to_haskell}]" if self.is_a?(TArray)
|
7
|
+
|
8
|
+
case self
|
9
|
+
when TInt
|
10
|
+
"Int"
|
11
|
+
when TLong
|
12
|
+
"Integer"
|
13
|
+
when TFloat
|
14
|
+
"Float"
|
15
|
+
when TDouble
|
16
|
+
"Double"
|
17
|
+
when TChar
|
18
|
+
"Char"
|
19
|
+
when TString
|
20
|
+
"String"
|
21
|
+
when TBoolean
|
22
|
+
"Bool"
|
23
|
+
else
|
24
|
+
"Unknown"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def get_haskell_parser
|
29
|
+
return "(TC.parseList #{subtype.get_haskell_parser})" if self.is_a?(TArray)
|
30
|
+
|
31
|
+
case self
|
32
|
+
when TInt
|
33
|
+
"TC.parseInt"
|
34
|
+
when TLong
|
35
|
+
"TC.parseLong"
|
36
|
+
when TFloat
|
37
|
+
"TC.parseFloat"
|
38
|
+
when TDouble
|
39
|
+
"TC.parseDouble"
|
40
|
+
when TChar
|
41
|
+
"TC.parseChar"
|
42
|
+
when TString
|
43
|
+
"TC.parseString"
|
44
|
+
when TBoolean
|
45
|
+
"TC.parseBool"
|
46
|
+
else
|
47
|
+
"unknown"
|
48
|
+
end
|
71
49
|
end
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
50
|
+
|
51
|
+
def dumb_haskell
|
52
|
+
return "[]" if self.is_a?(TArray)
|
53
|
+
|
54
|
+
case self
|
55
|
+
when TInt, TLong, TFloat, TDouble
|
56
|
+
"0"
|
57
|
+
when TChar
|
58
|
+
"'$'"
|
59
|
+
when TString
|
60
|
+
'"$"'
|
61
|
+
when TBoolean
|
62
|
+
"True"
|
63
|
+
else
|
64
|
+
"Nil"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
class HaskellEngine
|
70
|
+
attr_reader :func, :vars
|
71
|
+
|
72
|
+
def initialize(func, vars)
|
73
|
+
@func = Signature.new(func.type, uncapitalize(func.name))
|
74
|
+
@vars = vars.map { |var| Signature.new(var.type, uncapitalize(var.name)) }
|
75
|
+
end
|
76
|
+
|
77
|
+
def declare
|
78
|
+
ret = ""
|
79
|
+
ret << @func.name << " :: "
|
80
|
+
ret << @vars.map { |var| var.type.to_haskell }.join(" -> ")
|
81
|
+
ret << " -> " << @func.type.to_haskell << "\n"
|
82
|
+
ret << @func.name << " "
|
83
|
+
ret << @vars.map(&:name).join(" ")
|
84
|
+
ret << " = " << @func.type.dumb_haskell
|
85
|
+
end
|
86
|
+
|
87
|
+
def input
|
88
|
+
ret = "getVars :: TC.Parser ("
|
89
|
+
ret << @vars.map { |var| var.type.to_haskell }.join(", ") << ")\n"
|
90
|
+
|
91
|
+
temp = "getVars = do "
|
92
|
+
indent = " " * temp.size
|
93
|
+
ret << temp
|
94
|
+
|
95
|
+
ret << @vars.map do |var|
|
96
|
+
x = ""
|
97
|
+
x << var.name
|
98
|
+
x << " <- TC.spaces >> "
|
99
|
+
x << var.type.get_haskell_parser
|
100
|
+
end.join(" ; TC.spaces >> TC.next\n#{indent}")
|
101
|
+
|
102
|
+
ret << "\n"
|
103
|
+
ret << indent << "return ("
|
104
|
+
ret << @vars.map(&:name).join(", ")
|
105
|
+
ret << ")"
|
106
|
+
end
|
107
|
+
|
108
|
+
def output
|
109
|
+
@func.name + " " + @vars.map(&:name).join(" ")
|
110
|
+
end
|
111
|
+
|
83
112
|
private
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
def compute_declare
|
88
|
-
ret = ""
|
89
|
-
ret << func.name << " :: "
|
90
|
-
|
91
|
-
temp = vars.map do |var| var.type.to_haskell end
|
92
|
-
ret << temp.join(" -> ") << " -> " << func.type.to_haskell << "\n"
|
93
|
-
|
94
|
-
ret << func.name << " "
|
95
|
-
temp = vars.map do |var| var.name end
|
96
|
-
ret << temp.join(" ") << " = " << func.type.dumb_haskell
|
97
|
-
|
98
|
-
@declare = ret
|
99
|
-
end
|
100
|
-
def compute_input
|
101
|
-
ret = "getVars :: TC.Parser ("
|
102
|
-
temp = @vars.map do |var| var.type.to_haskell end
|
103
|
-
ret << temp.join(", ") << ")\n"
|
104
|
-
temp = "getVars = do "
|
105
|
-
ret << temp
|
106
|
-
indent = " " * temp.size
|
107
|
-
temp = @vars.map do |var|
|
108
|
-
x = ""
|
109
|
-
x << var.name
|
110
|
-
x << " <- TC.spaces >> "
|
111
|
-
x << var.type.get_haskell_parser
|
112
|
-
end
|
113
|
-
ret << temp.join(" ; TC.spaces >> TC.next\n#{indent}") << "\n"
|
114
|
-
ret << indent << "return ("
|
115
|
-
temp = @vars.map do |var| var.name end
|
116
|
-
ret << temp.join(", ")
|
117
|
-
ret << ")"
|
118
|
-
@input = ret
|
119
|
-
end
|
120
|
-
def compute_output
|
121
|
-
ret = ""
|
122
|
-
ret << @func.name << " "
|
123
|
-
temp = vars.map do |var| var.name end
|
124
|
-
ret << temp.join(" ")
|
125
|
-
@output = ret
|
126
|
-
end
|
113
|
+
|
114
|
+
def uncapitalize str
|
115
|
+
str[0, 1].downcase + str[1..-1]
|
127
116
|
end
|
117
|
+
end
|
128
118
|
end
|
data/dist/include/java/engine.rb
CHANGED
@@ -1,193 +1,196 @@
|
|
1
|
-
require "gettc/types"
|
1
|
+
require "gettc/types"
|
2
2
|
|
3
3
|
module Gettc
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
end
|
8
|
-
def to_java_boxed
|
9
|
-
if is_a? TArray then
|
10
|
-
return "List<#{subtype.to_java_boxed}>"
|
11
|
-
end
|
12
|
-
|
13
|
-
case self
|
14
|
-
when TInt
|
15
|
-
return "Integer"
|
16
|
-
when TLong
|
17
|
-
return "Long"
|
18
|
-
when TFloat
|
19
|
-
return "Float"
|
20
|
-
when TDouble
|
21
|
-
return "Double"
|
22
|
-
when TChar
|
23
|
-
return "Character"
|
24
|
-
when TString
|
25
|
-
return "String"
|
26
|
-
when TBoolean
|
27
|
-
return "Boolean"
|
28
|
-
end
|
29
|
-
|
30
|
-
return "Unknown"
|
31
|
-
end
|
32
|
-
def dumb_java
|
33
|
-
if self.is_a? TArray then
|
34
|
-
return "new #{subtype.to_java}[1]"
|
35
|
-
end
|
36
|
-
|
37
|
-
case self
|
38
|
-
when TInt, TLong, TFloat, TDouble
|
39
|
-
return "0"
|
40
|
-
when TChar
|
41
|
-
return "'$'"
|
42
|
-
when TString
|
43
|
-
return '"$"'
|
44
|
-
when TBoolean
|
45
|
-
return "true"
|
46
|
-
end
|
47
|
-
|
48
|
-
return "Nil"
|
49
|
-
end
|
4
|
+
class Type
|
5
|
+
def to_java
|
6
|
+
to_s
|
50
7
|
end
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
8
|
+
|
9
|
+
def to_java_boxed
|
10
|
+
return "List<#{subtype.to_java_boxed}>" if self.is_a?(TArray)
|
11
|
+
|
12
|
+
case self
|
13
|
+
when TInt
|
14
|
+
"Integer"
|
15
|
+
when TLong
|
16
|
+
"Long"
|
17
|
+
when TFloat
|
18
|
+
"Float"
|
19
|
+
when TDouble
|
20
|
+
"Double"
|
21
|
+
when TChar
|
22
|
+
"Character"
|
23
|
+
when TString
|
24
|
+
"String"
|
25
|
+
when TBoolean
|
26
|
+
"Boolean"
|
27
|
+
else
|
28
|
+
"Unknown"
|
29
|
+
end
|
55
30
|
end
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
@depth = self.class.get_depth sig.type
|
73
|
-
@elem_type = self.class.get_elem_type(sig.type).to_java
|
74
|
-
end
|
75
|
-
def unbox_code
|
76
|
-
counters = @depth.times.map do |i| "_" + ("i"[0].ord + i).chr end
|
77
|
-
indents = 0.upto(@depth).map do |i| " " * 4 * i end
|
78
|
-
sizes = @depth.times.map do |i|
|
79
|
-
@boxed_name + (".get(0)" * i) + ".size()"
|
80
|
-
end
|
81
|
-
ret = ""
|
82
|
-
|
83
|
-
ret << @type << " " << @name << " = new " << @elem_type;
|
84
|
-
@depth.times do |i| ret << "[" << sizes[i] << "]" end
|
85
|
-
ret << ";\n"
|
86
|
-
|
87
|
-
@depth.times do |i|
|
88
|
-
ret << indents[i] << "for (int " << counters[i] << " = 0; "
|
89
|
-
ret << counters[i] << " < " << sizes[i]
|
90
|
-
ret << "; ++" << counters[i] << ")\n"
|
91
|
-
end
|
92
|
-
|
93
|
-
ret << indents[@depth] << @name
|
94
|
-
@depth.times do |i| ret << "[" << counters[i] << "]" end
|
95
|
-
ret << " = "
|
96
|
-
ret << @boxed_name
|
97
|
-
@depth.times do |i| ret << ".get(" << counters[i] << ")" end
|
98
|
-
ret << ";"
|
99
|
-
|
100
|
-
return ret
|
101
|
-
end
|
102
|
-
def box_code_recurse boxed_type, parent, boxed_name, depth, counters
|
103
|
-
ret = ""
|
104
|
-
index = counters.map do |counter| "[" + counter + "]" end
|
105
|
-
index = index.join
|
106
|
-
if depth == 0
|
107
|
-
ret << parent << ".add(" << @name << index << ");\n"
|
108
|
-
else
|
109
|
-
type_str = boxed_type.to_java_boxed
|
110
|
-
ret << type_str << " " << boxed_name
|
111
|
-
ret << " = new Array" << type_str << "();" << "\n"
|
112
|
-
|
113
|
-
counter = "_" + ("i"[0].ord + counters.size).chr
|
114
|
-
ret << "for (int " << counter << " = 0; "
|
115
|
-
ret << counter << " < " << @name << index << ".length; "
|
116
|
-
ret << "++" << counter << ") {\n"
|
117
|
-
|
118
|
-
ret << box_code_recurse(boxed_type.subtype,
|
119
|
-
boxed_name,
|
120
|
-
boxed_name + counter,
|
121
|
-
depth - 1, counters << counter)
|
122
|
-
ret << "}\n"
|
123
|
-
|
124
|
-
unless parent.nil?
|
125
|
-
ret << parent << ".add("
|
126
|
-
ret << boxed_name << ");\n"
|
127
|
-
end
|
128
|
-
end
|
129
|
-
ret.gsub! /^/, " " * 4 unless parent.nil?
|
130
|
-
return ret
|
131
|
-
end
|
132
|
-
def box_code
|
133
|
-
box_code_recurse @sig.type, nil, @boxed_name, @depth, []
|
134
|
-
end
|
31
|
+
|
32
|
+
def dumb_java
|
33
|
+
return "new #{subtype.to_java}[1]" if self.is_a?(TArray)
|
34
|
+
|
35
|
+
case self
|
36
|
+
when TInt, TLong, TFloat, TDouble
|
37
|
+
"0"
|
38
|
+
when TChar
|
39
|
+
"'$'"
|
40
|
+
when TString
|
41
|
+
'"$"'
|
42
|
+
when TBoolean
|
43
|
+
"true"
|
44
|
+
else
|
45
|
+
"Nil"
|
46
|
+
end
|
135
47
|
end
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
48
|
+
end
|
49
|
+
|
50
|
+
class Signature
|
51
|
+
def to_java
|
52
|
+
"#{type.to_java} #{name}"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
class JavaArray
|
57
|
+
attr_reader :name, :boxed_name, :type, :boxed_type
|
58
|
+
|
59
|
+
def self.get_depth(type)
|
60
|
+
type.is_a?(TArray) ? 1 + get_depth(type.subtype) : 0
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.get_elem_type(type)
|
64
|
+
type.is_a?(TArray) ? get_elem_type(type.subtype) : type
|
65
|
+
end
|
66
|
+
|
67
|
+
def initialize(sig)
|
68
|
+
@sig = sig
|
69
|
+
@name = sig.name
|
70
|
+
@boxed_name = "#{@name}Boxed"
|
71
|
+
@type = sig.type.to_java
|
72
|
+
@boxed_type = sig.type.to_java_boxed
|
73
|
+
@depth = self.class.get_depth(sig.type)
|
74
|
+
@elem_type = self.class.get_elem_type(sig.type).to_java
|
75
|
+
end
|
76
|
+
|
77
|
+
def unbox_code
|
78
|
+
ret = ""
|
79
|
+
|
80
|
+
counters = @depth.times.map { |i| "_" + ("i"[0].ord + i).chr }
|
81
|
+
indents = 0.upto(@depth).map { |i| " " * 4 * i }
|
82
|
+
sizes = @depth.times.map { |i| @boxed_name + (".get(0)" * i) + ".size()" }
|
83
|
+
|
84
|
+
ret << @type << " " << @name << " = new " << @elem_type
|
85
|
+
@depth.times { |i| ret << "[" << sizes[i] << "]" }
|
86
|
+
ret << ";\n"
|
87
|
+
|
88
|
+
@depth.times do |i|
|
89
|
+
ret << indents[i] << "for (int " << counters[i] << " = 0; "
|
90
|
+
ret << counters[i] << " < " << sizes[i]
|
91
|
+
ret << "; ++" << counters[i] << ")\n"
|
92
|
+
end
|
93
|
+
|
94
|
+
ret << indents[@depth] << @name
|
95
|
+
@depth.times { |i| ret << "[" << counters[i] << "]" }
|
96
|
+
ret << " = "
|
97
|
+
|
98
|
+
ret << @boxed_name
|
99
|
+
@depth.times { |i| ret << ".get(" << counters[i] << ")" }
|
100
|
+
ret << ";"
|
101
|
+
|
102
|
+
ret
|
103
|
+
end
|
104
|
+
|
105
|
+
def box_code_recurse(boxed_type, parent, boxed_name, depth, counters)
|
106
|
+
ret = ""
|
107
|
+
|
108
|
+
index = counters.map { |counter| "[#{counter}]" }.join
|
109
|
+
|
110
|
+
if depth == 0
|
111
|
+
ret << parent << ".add(" << @name << index << ");\n"
|
112
|
+
else
|
113
|
+
type_str = boxed_type.to_java_boxed
|
114
|
+
ret << type_str << " " << boxed_name
|
115
|
+
ret << " = new Array" << type_str << "();" << "\n"
|
116
|
+
|
117
|
+
counter = "_" + ("i"[0].ord + counters.size).chr
|
118
|
+
ret << "for (int " << counter << " = 0; "
|
119
|
+
ret << counter << " < " << @name << index << ".length; "
|
120
|
+
ret << "++" << counter << ") {\n"
|
121
|
+
|
122
|
+
ret << box_code_recurse(boxed_type.subtype,
|
123
|
+
boxed_name,
|
124
|
+
boxed_name + counter,
|
125
|
+
depth - 1, counters << counter)
|
126
|
+
ret << "}\n"
|
127
|
+
|
128
|
+
unless parent.nil?
|
129
|
+
ret << parent << ".add("
|
130
|
+
ret << boxed_name << ");\n"
|
176
131
|
end
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
132
|
+
end
|
133
|
+
|
134
|
+
parent.nil? ? ret : ret.gsub(/^/, " " * 4)
|
135
|
+
end
|
136
|
+
|
137
|
+
def box_code
|
138
|
+
box_code_recurse(@sig.type, nil, @boxed_name, @depth, [])
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
class JavaEngine
|
143
|
+
def initialize func, vars
|
144
|
+
@func = func
|
145
|
+
@vars = vars
|
146
|
+
end
|
147
|
+
|
148
|
+
def declare
|
149
|
+
ret = "public "
|
150
|
+
ret << @func.to_java << "("
|
151
|
+
indent = " " * ret.size
|
152
|
+
ret << @vars.map(&:to_java).join(",\n#{indent}")
|
153
|
+
ret << ")"
|
154
|
+
end
|
155
|
+
|
156
|
+
def input
|
157
|
+
@vars.map do |var|
|
158
|
+
ret = ""
|
159
|
+
if var.type.is_a?(TArray)
|
160
|
+
arr = JavaArray.new(var)
|
161
|
+
ret << arr.boxed_type
|
162
|
+
ret << " " << arr.boxed_name
|
163
|
+
ret << " = (" << arr.boxed_type << ") "
|
164
|
+
ret << "reader.next(new TypeRef<" << arr.boxed_type << ">"
|
165
|
+
ret << "(){}.getType());\n"
|
166
|
+
ret << arr.unbox_code
|
167
|
+
else
|
168
|
+
ret << var.to_java
|
169
|
+
ret << " = (" << var.type.to_java_boxed << ") "
|
170
|
+
ret << "reader.next("
|
171
|
+
ret << var.type.to_java_boxed
|
172
|
+
ret << ".class);"
|
191
173
|
end
|
174
|
+
ret
|
175
|
+
end.join("\nreader.next();\n\n")
|
176
|
+
end
|
177
|
+
|
178
|
+
def output
|
179
|
+
ret = ""
|
180
|
+
|
181
|
+
solver = "solver." + @func.name + "("
|
182
|
+
solver << @vars.map(&:name).join(", ") << ")"
|
183
|
+
|
184
|
+
if @func.type.is_a?(TArray)
|
185
|
+
ret << @func.type.to_java << " result = " << solver << ";\n"
|
186
|
+
arr = JavaArray.new(Signature.new(@func.type, "result"))
|
187
|
+
ret << arr.box_code
|
188
|
+
ret << "writer.write(resultBoxed);"
|
189
|
+
else
|
190
|
+
ret << "writer.write(" << solver << ");"
|
191
|
+
end
|
192
|
+
|
193
|
+
ret
|
192
194
|
end
|
195
|
+
end
|
193
196
|
end
|