Class: Ruby2Basic::ZXSpectrum::Transpiler

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby2basic/zxspectrum/transpiler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTranspiler

Returns a new instance of Transpiler.



9
10
11
# File 'lib/ruby2basic/zxspectrum/transpiler.rb', line 9

def initialize
  @oneline = false
end

Instance Attribute Details

#onelineObject

Returns the value of attribute oneline.



7
8
9
# File 'lib/ruby2basic/zxspectrum/transpiler.rb', line 7

def oneline
  @oneline
end

Instance Method Details

#call(code) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ruby2basic/zxspectrum/transpiler.rb', line 22

def call(code)
  reset
  @result = Prism.parse(code)
  @comments = @result.comments.map do |c| 
    { line: c.location.start_line, text: c.location.slice.gsub(/^#\s*/, "") }
  end
  
  return "Error de sintaxis" unless @result.success?
  process_nodes(@result.value.statements.body)
    
  output = []
  @arrays.each { |dim| output << "#{@line_num} #{dim}"; @line_num += 10 }
  output += @lines
  
  unless @oneline
    output << "#{@line_num} STOP"
    @line_num += 10
    output += @subs
  end
  output.join("\n")
end

#resetObject



13
14
15
16
17
18
19
20
# File 'lib/ruby2basic/zxspectrum/transpiler.rb', line 13

def reset
  @lines = []
  @line_num = 10
  @subs = []
  @labels = {}
  @arrays = []
  @string_vars = []
end