Class: Duby::Transform::Transformer

Inherits:
Object
  • Object
show all
Defined in:
lib/duby/transform.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state) ⇒ Transformer

Returns a new instance of Transformer.



24
25
26
27
28
29
30
31
# File 'lib/duby/transform.rb', line 24

def initialize(state)
  @errors = []
  @tmp_count = 0
  @annotations = []
  @scopes = []
  @extra_body = nil
  @state = state
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



23
24
25
# File 'lib/duby/transform.rb', line 23

def errors
  @errors
end

#stateObject (readonly)

Returns the value of attribute state.



23
24
25
# File 'lib/duby/transform.rb', line 23

def state
  @state
end

Instance Method Details

#add_annotation(annotation) ⇒ Object



46
47
48
49
# File 'lib/duby/transform.rb', line 46

def add_annotation(annotation)
  @annotations << annotation
  Duby::AST::Noop.new(annotation.parent, annotation.position)
end

#annotationsObject



41
42
43
44
# File 'lib/duby/transform.rb', line 41

def annotations
  result, @annotations = @annotations, []
  return result
end

#append_node(node) ⇒ Object



126
127
128
129
# File 'lib/duby/transform.rb', line 126

def append_node(node)
  @extra_body << node
  node
end

#captured?(node) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
97
98
99
100
101
# File 'lib/duby/transform.rb', line 93

def captured?(node)
  depth = node.depth
  scope = @scopes[-1]
  while depth > 0
    depth -= 1
    scope = scope.enclosing_scope
  end
  scope.isCaptured(node.index)
end

#define_class(position, name, &block) ⇒ Object



131
132
133
# File 'lib/duby/transform.rb', line 131

def define_class(position, name, &block)
  append_node Duby::AST::ClassDefinition.new(nil, position, name, &block)
end

#define_closure(position, name, enclosing_type) ⇒ Object



135
136
137
138
# File 'lib/duby/transform.rb', line 135

def define_closure(position, name, enclosing_type)
  append_node(Duby::AST::ClosureDefinition.new(
      nil, position, name, enclosing_type))
end

#destinationObject



33
34
35
# File 'lib/duby/transform.rb', line 33

def destination
  @state.destination
end

#eval(src, filename = '-', parent = nil, *vars) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/duby/transform.rb', line 103

def eval(src, filename='-', parent=nil, *vars)
  unless vars.empty?
    src = "#{vars.join ','} = [];begin;#{src};end"
  end
  node = Duby::AST.parse_ruby(src, filename)
  duby_node = transform(node, nil).body
  unless vars.empty?
    # We have
    # (Script (Body ({vars} (NewlineNode (BeginNode ({src}))))))
    duby_node = duby_node.children[1]
  end
  duby_node.parent = parent
  duby_node
end

#expand(fvcall, parent) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/duby/transform.rb', line 118

def expand(fvcall, parent)
  result = yield self, fvcall, parent
  unless AST::Node === result
    raise Error.new('Invalid macro result', fvcall.position)
  end
  result
end

#tmp(format = "__xform_tmp_%d") ⇒ Object



51
52
53
# File 'lib/duby/transform.rb', line 51

def tmp(format="__xform_tmp_%d")
  format % [@tmp_count += 1]
end

#transform(node, parent) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/duby/transform.rb', line 55

def transform(node, parent)
  begin
    scope = node.getScope if node.respond_to? :getScope
    if scope
      @scopes << scope
    end
    top = @extra_body.nil?
    if top
      @extra_body = Duby::AST::Body.new(nil, node.position)
    end
    begin
      result = node.transform(self, parent)
      if top
        body = result.body
        if body.kind_of?(Duby::AST::Body) && @extra_body.empty?
          @extra_body = body
        else
          result.body = @extra_body
          body.parent = @extra_body
          @extra_body.children.insert(0, body)
        end
      end
      return result
    ensure
      if scope
        @scopes.pop
      end
    end
  rescue Error => ex
    @errors << ex
    Duby::AST::ErrorNode.new(parent, ex)
  rescue Exception => ex
    error = Error.new(ex.message, node.position, ex)
    @errors << error
    Duby::AST::ErrorNode.new(parent, error)
  end
end

#verbose?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/duby/transform.rb', line 37

def verbose?
  @state.verbose
end