Class: TestML::Compiler::Pegex::AST

Inherits:
Pegex::Tree
  • Object
show all
Defined in:
lib/testml/compiler/pegex/ast.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAST

Returns a new instance of AST.



10
11
12
13
# File 'lib/testml/compiler/pegex/ast.rb', line 10

def initialize
  @points = []
  @function = TestML::Function.new
end

Instance Attribute Details

#functionObject

Returns the value of attribute function.



8
9
10
# File 'lib/testml/compiler/pegex/ast.rb', line 8

def function
  @function
end

#pointsObject

Returns the value of attribute points.



7
8
9
# File 'lib/testml/compiler/pegex/ast.rb', line 7

def points
  @points
end

Instance Method Details

#got_assertion_call(call) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/testml/compiler/pegex/ast.rb', line 67

def got_assertion_call(call)
  name, expr = nil, nil
  %w( eq has ok ).each do |a|
    if expr = call["assertion_#{a}"]
      name = a.upcase
      expr =
        expr.fetch("assertion_operator_#{a}", [])[0] ||
        expr.fetch("assertion_function_#{a}", [])[0]
      break
    end
  end
  return TestML::Assertion.new(name, expr)
end

#got_assertion_function_ok(ok) ⇒ Object



81
82
83
# File 'lib/testml/compiler/pegex/ast.rb', line 81

def got_assertion_function_ok(ok)
  return { 'assertion_function_ok' => [] }
end

#got_assignment_statement(match) ⇒ Object



19
20
21
# File 'lib/testml/compiler/pegex/ast.rb', line 19

def got_assignment_statement(match)
  return TestML::Assignment.new(match[0], match[1])
end

#got_block_point(point) ⇒ Object



142
143
144
# File 'lib/testml/compiler/pegex/ast.rb', line 142

def got_block_point(point)
  return { point[0] => point[1] }
end

#got_call_argument_list(list) ⇒ Object



124
125
126
# File 'lib/testml/compiler/pegex/ast.rb', line 124

def got_call_argument_list(list)
  return list
end

#got_call_indicator(dummy) ⇒ Object



128
129
130
# File 'lib/testml/compiler/pegex/ast.rb', line 128

def got_call_indicator(dummy)
  return
end

#got_call_name(name) ⇒ Object



104
105
106
# File 'lib/testml/compiler/pegex/ast.rb', line 104

def got_call_name(name)
  return TestML::Call.new(name)
end

#got_call_object(object) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/testml/compiler/pegex/ast.rb', line 108

def got_call_object(object)
  call = object[0]
  args = object[1] && object[1][-1]
  if args
    args = args.map do |arg|
      (arg.kind_of?(TestML::Expression) and arg.calls.size == 1 and
      (
         arg.calls[0].kind_of?(TestML::Point) ||
         arg.calls[0].kind_of?(TestML::Object)
      )) ? arg.calls[0] : arg
    end
    call.args = args
  end
  return call
end

#got_code_expression(list) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/testml/compiler/pegex/ast.rb', line 41

def got_code_expression(list)
  calls = []
  calls.push(list.shift) if !list.empty?
  list = !list.empty? ? list.shift : []
  list.each do |e|
    call = e[1]   # XXX this is e[0] in perl
    calls.push(call)
  end
  return calls[0] if calls.size == 1
  return TestML::Expression.new(calls)
end

#got_code_section(code) ⇒ Object



15
16
17
# File 'lib/testml/compiler/pegex/ast.rb', line 15

def got_code_section(code)
  @function.statements = code
end

#got_code_statement(list) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/testml/compiler/pegex/ast.rb', line 23

def got_code_statement(list)
  expression, assertion = nil, nil
  points = @points
  @points = []
  list.each do |e|
    if e.kind_of? TestML::Assertion
      assertion = e
    else
      expression = e
    end
  end
  return TestML::Statement.new(
    expression,
    assertion,
    !points.empty? ? points : nil,
  )
end

#got_data_block(block) ⇒ Object



136
137
138
139
140
# File 'lib/testml/compiler/pegex/ast.rb', line 136

def got_data_block(block)
  label = block[0][0][0]
  points = block[1].inject({}){|r, h| r.merge!(h)}
  return TestML::Block.new(label, points)
end

#got_data_section(data) ⇒ Object



132
133
134
# File 'lib/testml/compiler/pegex/ast.rb', line 132

def got_data_section(data)
  @function.data = data
end

#got_function_object(object) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/testml/compiler/pegex/ast.rb', line 92

def got_function_object(object)
  function = @function
  @function = function.outer

  if object[0].kind_of? Array and object[0][0].kind_of? Array
    function.signature = object[0][0]
  end
  function.statements = object[-1]

  return function
end

#got_function_start(dummy) ⇒ Object



85
86
87
88
89
90
# File 'lib/testml/compiler/pegex/ast.rb', line 85

def got_function_start(dummy)
  function = TestML::Function.new
  function.outer = @function
  @function = function
  return true
end

#got_number_object(number) ⇒ Object



57
58
59
# File 'lib/testml/compiler/pegex/ast.rb', line 57

def got_number_object(number)
  return TestML::Num.new(number.to_i)
end

#got_point_object(point) ⇒ Object



61
62
63
64
65
# File 'lib/testml/compiler/pegex/ast.rb', line 61

def got_point_object(point)
  point.sub!(/^\*/, '') or fail
  @points.push(point)
  return TestML::Point.new(point)
end

#got_string_object(string) ⇒ Object



53
54
55
# File 'lib/testml/compiler/pegex/ast.rb', line 53

def got_string_object(string)
  return TestML::Str.new(string)
end