Class: LadderConverter::Mel2Kv

Inherits:
Object
  • Object
show all
Defined in:
lib/ladder_converter/mel2kv.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mel2Kv

Returns a new instance of Mel2Kv.



14
15
16
17
18
19
20
# File 'lib/ladder_converter/mel2kv.rb', line 14

def initialize options={}
  @src = options[:src]
  @dst = options[:dst]
  @logging_file = options[:logging_file]
  @ignore_unknown = options[:ignore_unknown]
  @codes = nil
end

Instance Attribute Details

#codesObject (readonly)

Returns the value of attribute codes.



7
8
9
# File 'lib/ladder_converter/mel2kv.rb', line 7

def codes
  @codes
end

#convertedObject (readonly)

Returns the value of attribute converted.



8
9
10
# File 'lib/ladder_converter/mel2kv.rb', line 8

def converted
  @converted
end

#dstObject (readonly)

Returns the value of attribute dst.



9
10
11
# File 'lib/ladder_converter/mel2kv.rb', line 9

def dst
  @dst
end

#ignore_unknownObject (readonly)

Returns the value of attribute ignore_unknown.



12
13
14
# File 'lib/ladder_converter/mel2kv.rb', line 12

def ignore_unknown
  @ignore_unknown
end

#logging_fileObject (readonly)

Returns the value of attribute logging_file.



10
11
12
# File 'lib/ladder_converter/mel2kv.rb', line 10

def logging_file
  @logging_file
end

#srcObject (readonly)

Returns the value of attribute src.



9
10
11
# File 'lib/ladder_converter/mel2kv.rb', line 9

def src
  @src
end

Instance Method Details

#convertObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ladder_converter/mel2kv.rb', line 60

def convert
  load
  @converted = <<EOS
DEVICE:132
;MODULE:Main
;MODULE_TYPE:0
EOS
  # collect subrouten labels
  sb_labels = codes.select{|c| c.mnemonic == "CALL"}.map{|c| c.devices.first}
  # replace mnemonic LABLE to SBN if it's for subroutin.
  codes.select{|c| c.mnemonic == "LABEL" && sb_labels.include?(c.device)}.each{|c| c.becone_subroutin_label }

  # add kv codes string
  @converted += codes.map(&:to_s).join("\n")
  @converted += "\nENDH\n"
end

#loadObject



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
# File 'lib/ladder_converter/mel2kv.rb', line 22

def load
  return false unless File.exist? @src
  return true if @codes

  begin
    logfile = File.open(logging_file, "w") rescue nil

    @codes = []
    has_end = false
    CSV.open(@src, "rb:BOM|UTF-16:UTF-8", headers:true, skip_lines:Regexp.new(/^[^\t]+$|PC情報:/), col_sep:"\t").each_with_index do |row, i|
      begin
        mnemonic = row["命令"]
        device = row["I/O(デバイス)"]
        case mnemonic
        when 'NOP'
          # Skip
        when ""
          @codes.last.add_device device
        when 'END', 'FEND'
          unless has_end
            @codes << KvCode.new(mnemonic, [device])
            has_end = true
          end
        else
          @codes << KvCode.new(mnemonic, [device])
        end
      rescue UnknownCodeError => e
        mes = "[WARN] SKIPPED! : line #{i+(3+1)} : #{e}"
        STDERR.puts mes
        logfile.puts mes if logfile
        raise unless ignore_unknown
      end
    end
  ensure
    logfile.close if logfile
  end
end

#saveObject



77
78
79
80
81
# File 'lib/ladder_converter/mel2kv.rb', line 77

def save
  return unless dst
  convert
  File.write dst, converted
end