Class: MicroCisc::Compile::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/micro_cisc/compile/compiler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ Compiler

Returns a new instance of Compiler.



6
7
8
9
10
# File 'lib/micro_cisc/compile/compiler.rb', line 6

def initialize(text)
  @text = text
  @command_count = 0
  parse
end

Instance Attribute Details

#command_countObject (readonly)

Returns the value of attribute command_count.



4
5
6
# File 'lib/micro_cisc/compile/compiler.rb', line 4

def command_count
  @command_count
end

Instance Method Details

#parseObject



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
# File 'lib/micro_cisc/compile/compiler.rb', line 12

def parse
  line_number = 1
  @instructions = []
  address = 0
  @labels = {}
  @indexed_vars = {}
  @equivalents = {}
  errors = []
  lgen = MicroCisc::Compile::LabelGenerator.new
  @command_count = 0
  @text.each_line do |line|
    begin
      statement = MicroCisc::Compile::Statement.new(lgen, line, @indexed_vars, @equivalents)
      command = false
      statement.parse.each do |instruction|
        if instruction.label?
          @labels[instruction.label] = address
        elsif instruction.instruction?
          @instructions << [line_number, instruction]
          command = true
          address += 1
        elsif instruction.data?
          @instructions += instruction.data.map { |d| [line_number, d] }
          line_string = []
          word_counts = instruction.data.map do |d|
            if d.is_a?(String)
              line_string << d.unpack("S*").map { |w| '%04x' % w }.join
              d.size / 2
            else
              line_string << d.join('.')
              1 # 1 16-bit word reference
            end
          end
          address += word_counts.sum
        end
      end
      @command_count += 1 if command
    rescue ArgumentError => e
      MicroCisc.logger.error("Error on line #{line_number}: #{e.message}\n  #{line}")
      errors << [line_number, e, line]
    end
    line_number += 1
  end

  if errors.size > 0
    errors = errors.map do |error|
      "#{error[0]}: #{error[1]}\n  #{error[2]}"
    end
    MicroCisc.logger.error("\n\nErrors found:\n\n#{errors.join("\n")}")
    exit(1)
  end
end

#serialize(file = nil) ⇒ Object



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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/micro_cisc/compile/compiler.rb', line 65

def serialize(file = nil)
  @serialize ||=
    begin
      words = []
      dstart = nil
      MicroCisc.logger.info("ADDRESS: INS-WORD  LINE#: SOURCE")
      @instructions.each do |(line_number, ins)|
        if ins.is_a?(String)
          address = words.length
          ins_words = ins.unpack("S*")
          dstart ||= address
          words += ins_words
        elsif ins.is_a?(Array)
          dstart ||= address
          # Address reference in data
          label_address = @labels[ins.first]
          if ins.last == 'disp'
            words << label_address - address
          elsif ins.last == 'imm'
            words << (label_address & 0xFFFF)
          end
        else
          address = words.length
          if dstart
            MicroCisc.logger.info(" 0x#{'%04x' % dstart}: #{(address - dstart)} words of data")
            dstart = nil
          end
          MicroCisc.logger.info(" 0x#{'%04x' % address}: 0x#{'%04x' % ins.encoded(@labels, address)}   #{'% 6d' % line_number}: " + ins.original.gsub("\n", ""))
          words << ins.encoded(@labels, address)
        end
      end
      if dstart
        address = words.length
        MicroCisc.logger.info(" 0x#{'%04x' % dstart}: #{(address - dstart)} words of data")
      end
      words
    end

  File.open(file, 'w') do |file|
    file.write(@serialize.pack("S*"))
  end if file

  @serialize
end