Skip to content
Browse files

Refactor the Variable AST node into two different nodes, and fix a bu…

…g where all instances of Lupin::Types::Table had the same #hash result.
  • Loading branch information...
1 parent ce08e10 commit 04d0bf87d0b7f17ee3fcadab947cfe0b8e751962 @Twisol committed Feb 24, 2011
View
2 lib/lupin/ast/table.rb
@@ -15,7 +15,7 @@ def bytecode (g)
g.dup_top
k.bytecode(g)
v.bytecode(g)
- g.set_variable
+ g.table_set
g.pop
end
end
View
41 lib/lupin/ast/variable.rb
@@ -1,37 +1,34 @@
module Lupin::AST
class Variable
- def initialize (parent, identifier)
- @parent = parent
+ def initialize (identifier)
@name = identifier
end
def bytecode (g)
- push_parent(g)
+ g.push_environment
@name.bytecode(g)
- g.get_variable
- end
-
- def set_value (g)
- push_parent(g)
- swap_stack
-
- @name.bytecode(g)
- swap_stack
-
- g.set_variable
+ g.table_get
end
def sexp
- [:variable, @parent ? @parent.sexp : nil, @name.sexp]
+ [:variable, @name.sexp]
end
+ end
- protected
- def push_parent(g)
- if @parent == nil
- g.push_environment
- else
- @parent.bytecode(g)
- end
+ class Indexer
+ def initialize (container, key)
+ @container = container
+ @key = key
+ end
+
+ def bytecode (g)
+ @container.bytecode(g)
+ @key.bytecode(g)
+ g.table_get
+ end
+
+ def sexp
+ [:[], @container.sexp, @key.sexp]
end
end
end
View
4 lib/lupin/generator.rb
@@ -94,11 +94,11 @@ def neq
eq
end
- def get_variable
+ def table_get
g.send :[], 1
end
- def set_variable
+ def table_set
g.send :[]=, 2
end
View
4 lib/lupin/parser/parser.citrus
@@ -135,7 +135,7 @@ grammar Lupin::Parser::Lua
| (WS? table) { [table.value] }
| (WS? string) { [string] }
)
- | op:('') { Lupin::AST::Variable }
+ | op:('') { Lupin::AST::Indexer }
rhs:( (WS? '[' WS? expression WS? ']') { expression.value }
| (WS? '.' WS? identifier) { identifier.value }
)
@@ -150,7 +150,7 @@ grammar Lupin::Parser::Lua
end
rule variable
- ('' identifier) { Lupin::AST::Variable.new(nil, identifier.value) }
+ ('' identifier) { Lupin::AST::Variable.new(identifier.value) }
end
rule identifier
View
4 lib/lupin/types/table.rb
@@ -25,5 +25,9 @@ def to_s
str << @hash.to_a.map {|k, v| "[#{k}] = #{v}"}.join(', ')
str << '}'
end
+
+ def value
+ @hash
+ end
end
end
View
16 lib/lupin/types/value.rb
@@ -4,24 +4,24 @@ class Value
attr_reader :value
protected :value
- def initialize (value)
- @value = value
+ def initialize (val)
+ @value = val
end
def to_s
- @value.inspect
+ value.inspect
end
def to_bool
- @value ? true : false
+ value ? true : false
end
def hash
- @value.hash
+ value.hash
end
def eql? (val)
- val.eql? @value
+ val.eql? value
end
def + (other)
@@ -97,5 +97,9 @@ def > (other)
def concatenate (other)
Nil
end
+
+ def value
+ @value
+ end
end
end

0 comments on commit 04d0bf8

Please sign in to comment.
Something went wrong with that request. Please try again.