Class: Keisan::AST::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/keisan/ast/builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(string: nil, parser: nil, components: nil) ⇒ Builder

Build from parser



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/keisan/ast/builder.rb', line 5

def initialize(string: nil, parser: nil, components: nil)
  if [string, parser, components].select(&:nil?).size != 2
    raise Keisan::Exceptions::InternalError.new("Require one of string, parser or components")
  end

  if !string.nil?
    @components = Keisan::Parser.new(string: string).components
  elsif !parser.nil?
    @components = parser.components
  else
    @components = Array.wrap(components)
  end

  @nodes = nodes_split_by_operators(@components)
  @operators = @components.select {|component| component.is_a?(Keisan::Parsing::Operator)}

  consume_operators!

  unless @nodes.count == 1
    raise Keisan::Exceptions::ASTError.new("Should end up with a single node")
  end
end

Instance Method Details

#astObject



32
33
34
# File 'lib/keisan/ast/builder.rb', line 32

def ast
  node
end

#nodeObject



28
29
30
# File 'lib/keisan/ast/builder.rb', line 28

def node
  @nodes.first
end