Class: Twostroke::Compiler::TSASM

Inherits:
Object
  • Object
show all
Defined in:
lib/twostroke/compiler/tsasm.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ast, prefix = nil) ⇒ TSASM

Returns a new instance of TSASM.



4
5
6
7
# File 'lib/twostroke/compiler/tsasm.rb', line 4

def initialize(ast, prefix = nil)
  @ast = ast
  @prefix = prefix
end

Instance Attribute Details

#astObject

Returns the value of attribute ast.



2
3
4
# File 'lib/twostroke/compiler/tsasm.rb', line 2

def ast
  @ast
end

#bytecodeObject

Returns the value of attribute bytecode.



2
3
4
# File 'lib/twostroke/compiler/tsasm.rb', line 2

def bytecode
  @bytecode
end

#prefixObject

Returns the value of attribute prefix.



2
3
4
# File 'lib/twostroke/compiler/tsasm.rb', line 2

def prefix
  @prefix
end

Instance Method Details

#compile(node = nil, *opt_args) ⇒ Object



9
10
11
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
# File 'lib/twostroke/compiler/tsasm.rb', line 9

def compile(node = nil, *opt_args)
  if node
    if node.respond_to? :each
      # hoist named functions to top
      node.select { |n| n.is_a?(Twostroke::AST::Function) && n.name }.each { |n| compile n }
      node.reject { |n| n.is_a?(Twostroke::AST::Function) && n.name }.each { |n| compile n }
    elsif node.is_a? Symbol
      send node
    else
      output :".line", @current_line = node.line if node.line and node.line > @current_line
      @node_stack.push node
      send type(node), node, *opt_args if node
      @node_stack.pop
    end
  else
    @indent = 0
    @bytecode = Hash.new { |h,k| h[k] = [] }
    @current_section = :"#{prefix}main"
    @auto_inc = 0
    @sections = [:"#{prefix}main"]
    @break_stack = []
    @continue_stack = []
    @node_stack = []
    @labels = {}
    @current_line = 0
          
    ast.each { |node| hoist node }
    ast.each { |node| compile node }
    output :undefined
    output :ret
    
    fix_labels
  end
end