Class: Postsvg::Source::AstBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/postsvg/source/ast_builder.rb

Overview

Walks a token stream from the Lexer and produces a typed Model::Program. Procedures, arrays, and dictionaries are kept as Model literal nodes; operators are constructed via the Model::Operators registry.

Constant Summary collapse

MAX_PROC_DEPTH =
64

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tokens) ⇒ AstBuilder

Returns a new instance of AstBuilder.



14
15
16
17
18
19
20
21
# File 'lib/postsvg/source/ast_builder.rb', line 14

def initialize(tokens)
  @tokens = tokens
  @stack = OperandStack.new
  @body = []
  @dict_stack = [{}] # global dictionary
  @header = Model::Program::Header.new
  @recursion_depth = 0
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



10
11
12
# File 'lib/postsvg/source/ast_builder.rb', line 10

def body
  @body
end

#dict_stackObject (readonly)

Returns the value of attribute dict_stack.



10
11
12
# File 'lib/postsvg/source/ast_builder.rb', line 10

def dict_stack
  @dict_stack
end

#headerObject (readonly)

Returns the value of attribute header.



10
11
12
# File 'lib/postsvg/source/ast_builder.rb', line 10

def header
  @header
end

#recursion_depthObject (readonly)

Returns the value of attribute recursion_depth.



10
11
12
# File 'lib/postsvg/source/ast_builder.rb', line 10

def recursion_depth
  @recursion_depth
end

#stackObject (readonly)

Returns the value of attribute stack.



10
11
12
# File 'lib/postsvg/source/ast_builder.rb', line 10

def stack
  @stack
end

#tokensObject (readonly)

Returns the value of attribute tokens.



10
11
12
# File 'lib/postsvg/source/ast_builder.rb', line 10

def tokens
  @tokens
end

Class Method Details

.build(tokens) ⇒ Object



23
24
25
# File 'lib/postsvg/source/ast_builder.rb', line 23

def self.build(tokens)
  new(tokens).build
end

Instance Method Details

#buildObject



27
28
29
30
31
# File 'lib/postsvg/source/ast_builder.rb', line 27

def build
  Model::Operators.load_all!
  consume_until(tokens.length)
  Model::Program.new(header: @header, body: @body)
end