Class: Prattle::AST::Variable

Inherits:
Node
  • Object
show all
Defined in:
lib/prattle/ast/variable.rb

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from Node

#run

Constructor Details

- (Variable) initialize(name)

Returns a new instance of Variable



10
11
12
# File 'lib/prattle/ast/variable.rb', line 10

def initialize(name)
  @name = name
end

Instance Attribute Details

- (Object) name (readonly)

Returns the value of attribute name



14
15
16
# File 'lib/prattle/ast/variable.rb', line 14

def name
  @name
end

Class Method Details

+ (Object) grammar(g)



16
17
18
19
20
# File 'lib/prattle/ast/variable.rb', line 16

def self.grammar(g)
  g.variable = g.lit(/[a-zA-Z][a-zA-Z0-9_]*/) do |str|
    Variable.new(str)
  end
end

+ (Object) rule_name



6
7
8
# File 'lib/prattle/ast/variable.rb', line 6

def self.rule_name
  "variable"
end

Instance Method Details

- (Object) bytecode(g)



22
23
24
25
26
27
28
29
# File 'lib/prattle/ast/variable.rb', line 22

def bytecode(g)
  if /^[A-Z]/.match(@name)
    g.push_const @name.to_sym
  else
    depth, slot = g.state.scope.find_variable @name
    g.push_local slot
  end
end