| 1 |
|
| 2 |
|
| 3 |
require_relative 'to_ruby/compiled_code' |
| 4 |
require_relative 'to_ruby/operator' |
| 5 |
require_relative 'to_ruby/generic_proc' |
| 6 |
|
| 7 |
module MIKU |
| 8 |
module ToRuby |
| 9 |
STRING_LITERAL_ESCAPE_MAP = { '\\' => '\\\\', "'" => "\\'" }.freeze |
| 10 |
STRING_LITERAL_ESCAPE_MATCHER = Regexp.union(STRING_LITERAL_ESCAPE_MAP.keys).freeze |
| 11 |
OPERATOR_DICT = { |
| 12 |
:< => OPERATOR_GREATER_THAN, |
| 13 |
:> => OPERATOR_LESS_THAN, |
| 14 |
:<= => OPERATOR_GREATER_THAN_OR_EQUAL, |
| 15 |
:>= => OPERATOR_LESS_THAN_OR_EQUAL, |
| 16 |
:eql => OPERATOR_EQUAL, |
| 17 |
equal: OPERATOR_SAME, |
| 18 |
and: OPERATOR_LOGICAL_AND, |
| 19 |
or: OPERATOR_LOGICAL_OR, |
| 20 |
:== => OPERATOR_EQUAL, |
| 21 |
eq: Operator.new('equal?', priority: -1), |
| 22 |
:+ => OPERATOR_ADDITION, |
| 23 |
:- => OPERATOR_SUBTRACTION, |
| 24 |
:* => OPERATOR_PRODUCT, |
| 25 |
:/ => OPERATOR_DIVISION |
| 26 |
}.freeze |
| 27 |
|
| 28 |
class << self |
| 29 |
def progn(list, quoted: false, use_result: true) |
| 30 |
if options[:use_result] |
| 31 |
*progn_expr, return_expr = list |
| 32 |
progn_codes = progn_expr.map { |n| to_ruby(n, use_result: false) } |
| 33 |
return_code = to_ruby(return_expr, use_result: :to_return) |
| 34 |
codes = [*progn_codes, return_code] |
| 35 |
CompiledCode.new( |
| 36 |
codes.join("\n"), |
| 37 |
taint: codes.any?(&:taint?), |
| 38 |
affect: codes.any?(&:affect?), |
| 39 |
type: return_code.type |
| 40 |
) |
| 41 |
else |
| 42 |
codes = list.map { to_ruby(_1, use_result: false) } |
| 43 |
CompiledCode.new( |
| 44 |
codes.join("\n"), |
| 45 |
taint: codes.any?(&:taint?), |
| 46 |
affect: codes.any?(&:affect?), |
| 47 |
type: codes.last.type |
| 48 |
) |
| 49 |
end |
| 50 |
end |
| 51 |
|
| 52 |
換して返す |
| 53 |
|
| 54 |
|
| 55 |
インスタンス) |
| 56 |
値を持つHash |
| 57 |
ト内。シンボルが変数にならず、シンボル定数になる |
| 58 |
ストの戻り値として使うだけなら、 :to_return を指定 |
| 59 |
def to_ruby(sexp, options={ quoted: false, use_result: true }) |
| 60 |
expanded = sexp |
| 61 |
case expanded |
| 62 |
when :true, true |
| 63 |
CompiledCode.new('true', taint: false, affect: false, type: TRUE_TYPE) |
| 64 |
when :false, false |
| 65 |
CompiledCode.new('false', taint: false, affect: false, type: FALSE_TYPE) |
| 66 |
when :nil, nil |
| 67 |
CompiledCode.new('nil', taint: false, affect: false, type: NIL_TYPE) |
| 68 |
when Symbol |
| 69 |
if options[:quoted] |
| 70 |
CompiledCode.new( |
| 71 |
":#{expanded}", |
| 72 |
taint: false, |
| 73 |
affect: false, |
| 74 |
type: Type.new(Symbol, freeze: true) |
| 75 |
) |
| 76 |
else |
| 77 |
CompiledCode.new( |
| 78 |
expanded, |
| 79 |
taint: true, |
| 80 |
affect: true, |
| 81 |
type: ANY |
| 82 |
) |
| 83 |
end |
| 84 |
when Numeric |
| 85 |
CompiledCode.new(expanded, taint: false, affect: false, type: Type.new(expanded.class, freeze: true)) |
| 86 |
when String |
| 87 |
string_literal(expanded) |
| 88 |
when List |
| 89 |
if options[:quoted] |
| 90 |
codes = expanded.map { |node| to_ruby(node, quoted: true, use_result: true) } |
| 91 |
CompiledCode.new( |
| 92 |
"[#{codes.join(', ')}]", |
| 93 |
taint: codes.any?(&:taint?), |
| 94 |
affect: codes.any?(&:affect?), |
| 95 |
type: Type.new(Array, freeze: false) |
| 96 |
) |
| 97 |
else |
| 98 |
call_method(expanded) |
| 99 |
end |
| 100 |
else |
| 101 |
CompiledCode.new(expanded.to_s, taint: true, affect: true) |
| 102 |
end |
| 103 |
end |
| 104 |
|
| 105 |
def string_literal(str) |
| 106 |
escaped = str.gsub(STRING_LITERAL_ESCAPE_MATCHER, STRING_LITERAL_ESCAPE_MAP) |
| 107 |
CompiledCode.new( |
| 108 |
"'#{escaped}'", |
| 109 |
taint: false, |
| 110 |
affect: false, |
| 111 |
type: Type.new(String, freeze: true) |
| 112 |
) |
| 113 |
end |
| 114 |
|
| 115 |
def miracle_defarg(argsym) |
| 116 |
case argsym |
| 117 |
in [] |
| 118 |
'' |
| 119 |
in [*args, [:rest, Symbol => rest]] |
| 120 |
x = +'(' |
| 121 |
x << args.join(', ') |
| 122 |
"#{x}, *#{rest})".freeze |
| 123 |
in [*args] |
| 124 |
"(#{args.join(', ')})" |
| 125 |
end |
| 126 |
end |
| 127 |
|
| 128 |
def ife(cond_expr, then_expr, *else_progn_expr, else_result_expr, quoted: false, use_result: true) |
| 129 |
pattern = [ |
| 130 |
to_ruby(cond_expr, use_result: true), |
| 131 |
to_ruby(then_expr, use_result:), |
| 132 |
*else_progn_expr.map { to_ruby(_1, use_result: false) }, |
| 133 |
to_ruby(else_result_expr, use_result:) |
| 134 |
] |
| 135 |
case pattern |
| 136 |
in [code, {pure: true, type: TRUE_TYPE}, {pure: true, type: FALSY_TYPE}] |
| 137 |
code |
| 138 |
in [cond_code, then_code, {pure: true, type: FALSY_TYPE}] => codes |
| 139 |
cond_code.attach_paren if cond_code.priority > OPERATOR_LOGICAL_AND.priority |
| 140 |
then_code.attach_paren if then_code.priority > OPERATOR_LOGICAL_AND.priority |
| 141 |
CompiledCode.new( |
| 142 |
"#{cond_code} && #{then_code}", |
| 143 |
taint: codes.any?(&:taint?), |
| 144 |
affect: codes.any?(&:affect?), |
| 145 |
type: then_code.type | NIL_TYPE, |
| 146 |
priority: [cond_code.priority, then_code.priority, OPERATOR_LOGICAL_AND].max |
| 147 |
) |
| 148 |
in [cond_code, {pure: true, type: TRUE_TYPE}, else_code] |
| 149 |
cond_code.attach_paren if cond_code.priority > OPERATOR_LOGICAL_OR.priority |
| 150 |
else_code.attach_paren if else_code.priority > OPERATOR_LOGICAL_OR.priority |
| 151 |
CompiledCode.new( |
| 152 |
"#{cond_code} || #{else_code}", |
| 153 |
taint: cond_code.taint? || else_code.taint?, |
| 154 |
affect: cond_code.affect? || else_code.affect?, |
| 155 |
type: cond_code.type | else_code.type, |
| 156 |
priority: [cond_code.priority, else_code.priority, OPERATOR_LOGICAL_OR].max |
| 157 |
) |
| 158 |
in [cond_code, then_code, *else_code] => codes |
| 159 |
CompiledCode.new( |
| 160 |
[ |
| 161 |
"if #{cond_code}", |
| 162 |
then_code.indent, |
| 163 |
'else', |
| 164 |
*else_code.map(&:indent), |
| 165 |
'end' |
| 166 |
].join("\n"), |
| 167 |
taint: codes.any?(&:taint?), |
| 168 |
affect: codes.any?(&:affect?), |
| 169 |
type: then_code.type | else_code.last.type |
| 170 |
) |
| 171 |
end |
| 172 |
end |
| 173 |
|
| 174 |
def call_method(expanded, quoted: false, use_result: true) |
| 175 |
case expanded |
| 176 |
in [:quote, expr] |
| 177 |
to_ruby(expr, quoted: true, use_result: true) |
| 178 |
in [(:eq | :eql | :equal | :and | :or | :==), expr] |
| 179 |
to_ruby(expr, use_result:) |
| 180 |
in [:eq => operator, left, right] |
| 181 |
receiver = to_ruby(left, use_result:) |
| 182 |
arg = to_ruby(right, use_result:) |
| 183 |
CompiledCode.new( |
| 184 |
"#{receiver}.equal?(#{arg})", |
| 185 |
taint: receiver.taint? || arg.taint?, |
| 186 |
affect: receiver.affect? || arg.affect?, |
| 187 |
type: BOOLEAN_TYPE |
| 188 |
) |
| 189 |
in [(:< | :> | :<= | :>= | :eql | :equal | :and | :or | :==) => operator, left, right] |
| 190 |
operator = OPERATOR_DICT[operator] |
| 191 |
left_code = to_ruby(left, use_result: true) |
| 192 |
right_code = to_ruby(right, use_result: true) |
| 193 |
[left_code, right_code].select { _1.priority > operator.priority }.map(&:attach_paren) |
| 194 |
CompiledCode.new( |
| 195 |
[left_code, operator, right_code].join(' '), |
| 196 |
taint: left_code.taint? || right_code.taint?, |
| 197 |
affect: left_code.affect? || right_code.affect?, |
| 198 |
type: BOOLEAN_TYPE, |
| 199 |
priority: [left_code, right_code, operator].map(&:priority).max |
| 200 |
) |
| 201 |
in [(:< | :> | :<= | :>= | :eq | :eql | :equal | :==) => operator, *exprs] if exprs.size >= 3 |
| 202 |
codes = exprs.map { to_ruby(_1, use_result: true) } |
| 203 |
args = codes.join(', ') |
| 204 |
CompiledCode.new( |
| 205 |
"[#{args}].each_cons(2).inject(&:#{OPERATOR_DICT[operator]})", |
| 206 |
taint: codes.any?(&:taint?), |
| 207 |
affect: codes.any?(&:affect?), |
| 208 |
type: BOOLEAN_TYPE |
| 209 |
) |
| 210 |
in [(:and | :or | :+ | :- | :* | :/) => operator, *exprs] if exprs.size >= 2 |
| 211 |
codes = exprs.map { to_ruby(_1, use_result: true) } |
| 212 |
operator = OPERATOR_DICT[operator] |
| 213 |
codes.select { _1.priority > operator.priority }.map(&:attach_paren) |
| 214 |
CompiledCode.new( |
| 215 |
codes.join(" #{operator} "), |
| 216 |
taint: codes.any?(&:taint?), |
| 217 |
affect: codes.any?(&:affect?), |
| 218 |
type: ANY, |
| 219 |
priority: [*codes.map(&:priority), operator.priority].max |
| 220 |
) |
| 221 |
in [:not, expr] |
| 222 |
code = to_ruby(expanded[1], use_result:) |
| 223 |
priority_breaking = code.priority > OPERATOR_NOT.priority |
| 224 |
CompiledCode.new( |
| 225 |
priority_breaking ? "!(#{code})" : "!#{code}", |
| 226 |
taint: code.taint?, |
| 227 |
affect: code.affect?, |
| 228 |
type: BOOLEAN_TYPE, |
| 229 |
priority: priority_breaking ? -1 : 2 |
| 230 |
) |
| 231 |
in [:progn, *exprs] |
| 232 |
code = progn(exprs, use_result:) |
| 233 |
CompiledCode.new( |
| 234 |
"begin\n#{code.indent}\nend", |
| 235 |
taint: code.taint?, |
| 236 |
affect: code.affect?, |
| 237 |
type: code.type |
| 238 |
) |
| 239 |
in [:if, cond, then_expr] |
| 240 |
ife(cond, then_expr, false, use_result:) |
| 241 |
in [:if, cond, then_expr, *else_progn, else_result] |
| 242 |
ife(cond, then_expr, *else_progn, else_result, use_result:) |
| 243 |
in [:lambda, [*argsym], body] |
| 244 |
return_code = to_ruby(body, use_result: :to_return) |
| 245 |
CompiledCode.new( |
| 246 |
"->#{miracle_defarg(argsym)} { #{return_code} }", |
| 247 |
taint: return_code.taint?, |
| 248 |
affect: return_code.affect?, |
| 249 |
type: Type.new(GenericProc.new(return_code.type)) |
| 250 |
) |
| 251 |
in [:lambda, [*argsym], *body, return_body] if body.size >= 2 |
| 252 |
body_codes = body.map { to_ruby(_1, use_result: false) } |
| 253 |
return_code = to_ruby(return_body, use_result: :to_return) |
| 254 |
codes = [*body_codes, return_code] |
| 255 |
CompiledCode.new( |
| 256 |
[ |
| 257 |
"->#{miracle_defarg(argsym)} do", |
| 258 |
*codes.map(&:indent), |
| 259 |
'end' |
| 260 |
].join("\n"), |
| 261 |
taint: codes.any?(&:taint?), |
| 262 |
affect: codes.any?(&:affect?), |
| 263 |
type: Type.new(GenericProc.new(return_code.type)) |
| 264 |
) |
| 265 |
in [Symbol | String => method, _ => receiver] |
| 266 |
receiver_code = to_ruby(receiver, use_result: true) |
| 267 |
CompiledCode.new( |
| 268 |
"#{receiver_code}.#{method}", |
| 269 |
taint: true, |
| 270 |
affect: true |
| 271 |
) |
| 272 |
in [Symbol | String => method, _ => receiver, *args] |
| 273 |
receiver_code = to_ruby(receiver, use_result: true) |
| 274 |
arg_codes = args.map(&method(:to_ruby)) |
| 275 |
buf = "#{receiver_code}.#{method}(" |
| 276 |
buf += arg_codes.join(', ') |
| 277 |
CompiledCode.new( |
| 278 |
"#{buf})", |
| 279 |
taint: true, |
| 280 |
affect: true |
| 281 |
) |
| 282 |
in [func, *args] |
| 283 |
func = to_ruby(func, use_result: true).attach_paren |
| 284 |
arg_codes = args.map { to_ruby(_1, use_result: true) } |
| 285 |
arg_single_line = arg_codes.all?(&:single_line?) |
| 286 |
case func.type.types.to_a |
| 287 |
in [GenericProc => proc_type] |
| 288 |
if arg_single_line |
| 289 |
CompiledCode.new( |
| 290 |
"#{func}.(#{arg_codes.join(', ')})", |
| 291 |
taint: func.taint? || arg_codes.any?(&:taint?), |
| 292 |
affect: func.affect? || arg_codes.any?(&:affect?), |
| 293 |
type: proc_type.return_type |
| 294 |
) |
| 295 |
else |
| 296 |
[ |
| 297 |
"#{func}.(", |
| 298 |
*arg_codes.map { " #{_1}," }, |
| 299 |
')' |
| 300 |
].join("\n") |
| 301 |
end |
| 302 |
else |
| 303 |
CompiledCode.new( |
| 304 |
[ |
| 305 |
"#{func}.then do |__func|", |
| 306 |
' if __func.respond_to?(:call)', |
| 307 |
*if arg_single_line |
| 308 |
" __func.(#{arg_codes.join(', ')})" |
| 309 |
else |
| 310 |
[ |
| 311 |
' __func.(', |
| 312 |
*arg_codes.map { " #{_1}," }, |
| 313 |
' )' |
| 314 |
] |
| 315 |
end, |
| 316 |
' elsif __func.is_a?(Symbol)', |
| 317 |
*if arg_codes.first.single_line? |
| 318 |
" __receiver = #{arg_codes.first}" |
| 319 |
else |
| 320 |
ac1, *acrest = arg_codes.first.each_line.map(&:chomp) |
| 321 |
[ |
| 322 |
" __receiver = #{ac1}", |
| 323 |
*acrest.map { " #{_1}" } |
| 324 |
] |
| 325 |
end, |
| 326 |
' if __receiver.respond_to?(__func)', |
| 327 |
*if arg_single_line |
| 328 |
" __receiver.public_send(__func, #{arg_codes.join(', ')})" |
| 329 |
else |
| 330 |
[ |
| 331 |
' __receiver.(', |
| 332 |
' __func,', |
| 333 |
*arg_codes.map { " #{_1}," }, |
| 334 |
' )' |
| 335 |
] |
| 336 |
end, |
| 337 |
' end', |
| 338 |
' end', |
| 339 |
'end' |
| 340 |
].join("\n"), |
| 341 |
taint: func.taint? || arg_codes.any?(&:taint?), |
| 342 |
affect: func.affect? || arg_codes.any?(&:affect?) |
| 343 |
) |
| 344 |
end |
| 345 |
end |
| 346 |
end |
| 347 |
end |
| 348 |
end |
| 349 |
end |