Module: TEALrb::CmdLine::TEAL2TEALrb

Defined in:
lib/tealrb/cmd_line/teal2tealrb.rb

Class Method Summary collapse

Class Method Details

.convert(input) ⇒ Object



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
# File 'lib/tealrb/cmd_line/teal2tealrb.rb', line 7

def self.convert(input)
  output = StringIO.new

  input.each_line do |line|
    line.strip!
    next if line[/^#/]

    comment = line[%r{//.*}]
    comment&.sub!('//', '# //')
    line.sub!(%r{//.*}, '')

    opcode = line.split.first

    opcode_mappings = TEALrb::Opcodes::BINARY_OPCODE_METHOD_MAPPING
    opcode_mappings.merge TEALrb::Opcodes::UNARY_OPCODE_METHOD_MAPPING

    opcode_mappings.each do |op, method|
      line.sub!(opcode, method) if opcode == op.to_s
    end

    if line[/^(b|bz|bnz|callsub) /]
      label = line.split.last
      line.gsub!(label, ":#{label}")
    elsif line[/\w+:/]
      line = ":#{line[0..-2]}"
    elsif opcode == 'return'
      line = 'teal_return'
    elsif opcode == 'method'
      line.sub!('method', 'method_signature')
    end

    if line.count(' ').positive? && !%w[byte pushbytes method].include?(opcode)
      args = line.split[1..]
      args.map! do |arg|
        arg_as_int = Integer(arg, exception: false)
        if arg[/^:/]
          arg
        else
          arg_as_int || "'#{arg}'"
        end
      end

      line = "#{opcode} #{args.join(', ')}"
    end

    if line.empty?
      output.puts comment
    else
      output.puts "#{line} #{comment}"
    end
  end

  output.string
end