Method: MicroCisc::Compile::Statement#parse

Defined in:
lib/micro_cisc/compile/statement.rb

#parseObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/micro_cisc/compile/statement.rb', line 96

def parse
  instruction = nil
  names = []
  op = nil
  if FUNCTION_REGEX =~ @minimal
    return parse_function_call
  elsif SUGAR_REGEX =~ @minimal
    match = SUGAR_REGEX.match(@minimal)
    op = match['op']
    names = match['names'].split(',').map(&:strip)

    @minimal = match['param']
    if minimal.start_with?('copy') || minimal.start_with?('compute')
      instruction = Instruction.new(@label_generator, @minimal, original, self)
    else
      var = match['param']
      imm_match = IMM_REGEX.match(var)
      imm_val = imm_match ? imm_match['imm_val'].to_i : 0
      var = var.to_s.gsub(IMM_REGEX, '').strip
      @minimal = "#{get_var(var)}#{" #{imm_val}.imm" if imm_val != 0}"
    end
  else
    instruction = Instruction.new(@label_generator, @minimal, original, self)
  end
  if instruction && instruction.instruction?
    dest = instruction.dest
    if instruction.inc && instruction.inc > 0
      if dest > 4
        # pop
        update_variable("#{dest - 4}.mem", -1)
      else
        # push
        update_variable("#{dest}.mem", 1)
      end
    end
    if minimal.start_with?('copy') && dest > 4 && instruction.src == dest
      # Manually modifying a register
      update_variable("#{dest - 4}.mem", -1 * instruction.immediates.first)
    end
    dest -= 4 if dest > 4
    @minimal = "#{dest}.mem"
  end
  names.each_with_index do |name, index|
    if op == 'as'
      create_equivalent(name, @minimal, index)
    elsif op == '='
      create_variable(name, @minimal, index)
    end
  end
  [instruction].compact
end