Class: Malline::Compiler
- Inherits:
-
Object
- Object
- Malline::Compiler
- Defined in:
- lib/malline/compiler.rb
Defined Under Namespace
Classes: InvalidKind
Constant Summary collapse
- VAR =
'_malline_'
- OUT =
VAR + '_out'
Instance Attribute Summary collapse
-
#out ⇒ Object
readonly
Returns the value of attribute out.
Instance Method Summary collapse
- #cmd_if(str, lines, id) ⇒ Object
- #code(out, cmd, str, lines) ⇒ Object
- #combine ⇒ Object
- #compile ⇒ Object
- #data(out, str, lines) ⇒ Object
-
#initialize(parsed) ⇒ Compiler
constructor
A new instance of Compiler.
- #resolve(out_org) ⇒ Object
- #resolve_cmd_if(line_nr, cmp, out) ⇒ Object
Constructor Details
#initialize(parsed) ⇒ Compiler
Returns a new instance of Compiler.
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/malline/compiler.rb', line 7 def initialize parsed @parsed = parsed @out = nil @cmd = { :if => { :now => 0, :line => [], :id => [], }, } end |
Instance Attribute Details
#out ⇒ Object (readonly)
Returns the value of attribute out.
6 7 8 |
# File 'lib/malline/compiler.rb', line 6 def out @out end |
Instance Method Details
#cmd_if(str, lines, id) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/malline/compiler.rb', line 56 def cmd_if str, lines, id name = VAR+'if_'+id.to_s (lines.first .. lines.last).each do |line| @cmd[:if][:line][line] ||= [] @cmd[:if][:line][line] << id @cmd[:if][:id][id] = [name, str] end name end |
#code(out, cmd, str, lines) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/malline/compiler.rb', line 45 def code out, cmd, str, lines lines = (lines.first .. lines.last).to_a str.lines.each_with_index do |line, index| linenr = lines[index] line = cmd_if(line, lines, @cmd[:if][:now]+=1) if cmd == :if line = '%s.concat %s' % [OUT, line] unless cmd == :s out << ["#{line}.to_s", linenr] end end |
#combine ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/malline/compiler.rb', line 23 def combine out = [] @parsed.each do |e| case kind=e.shift when :data then data(out, *e) when :code then code(out, *e) else raise InvalidKind, "'#{kind}' was not recognized" end end out end |
#compile ⇒ Object
19 20 21 |
# File 'lib/malline/compiler.rb', line 19 def compile @out = resolve(combine) end |
#data(out, str, lines) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/malline/compiler.rb', line 36 def data out, str, lines lines = (lines.first .. lines.last).to_a str.lines.each_with_index do |line, index| linenr = lines[index] line = '%s.concat \'%s\'' % [OUT, line.gsub(/'/, %q(\\\'))] out << [line, linenr] end end |
#resolve(out_org) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/malline/compiler.rb', line 66 def resolve out_org out = ["#{OUT}='';"] cmp = [] seen = [] out_org.each do |line, line_nr| resolve_cmd_if(line_nr, cmp, out) unless seen.include? line_nr seen << line_nr if not cmp.empty? line = line + ' if ' + cmp.join(' and ') end out << line + ';' end out << "#{OUT}" out.join "\n" end |
#resolve_cmd_if(line_nr, cmp, out) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/malline/compiler.rb', line 82 def resolve_cmd_if line_nr, cmp, out cmp.clear set = [] [@cmd[:if][:line][line_nr]].flatten.compact.each do |cmd_id| varname, varvalue = @cmd[:if][:id][cmd_id] set << [varname,varvalue] cmp << varname end set.each do |name, value| out << name + '=' + value + ';' end end |