Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add constant lookup like Noscript.AST.Whatever

  • Loading branch information...
commit cf933474b0f4aabe202b4ff1024956cca93a9d74 1 parent b034f03
@txus authored
View
6 lib/noscript/ast.rb
@@ -163,7 +163,11 @@ class SlotGet < Node
def initialize(line, receiver, name)
super(line)
@receiver = receiver
- @name = name.name.to_sym
+ @name = name
+ end
+
+ def constant?
+ @name.constant?
end
end
View
10 lib/noscript/compiler.rb
@@ -207,8 +207,14 @@ def visit_LocalVariableAccess(o)
def visit_SlotGet(o)
set_line(o)
o.receiver.accept(self)
- g.push_literal o.name
- g.send :get, 1
+
+ if o.receiver.constant? && o.name.constant? # Constant lookup like Rubinius.Compiler
+ g.find_const o.name.name.to_sym
+ else
+ g.push_literal o.name.name.to_sym
+ g.send :get, 1
+ end
+
g.raise_if_empty NameError, "Object has no slot named #{o.name}"
end
View
8 test/compiler_test.rb
@@ -34,6 +34,14 @@ def test_compile_constant_assignment
assert_equal 1, compile("Foo = 1; Foo")
end
+ def test_compile_constant_lookup
+ assert_equal Rubinius, compile("Rubinius")
+ end
+
+ def test_compile_constant_lookup_with_namespace
+ assert_equal Noscript::AST::Identifier, compile("Noscript.AST.Identifier")
+ end
+
def test_compile_array_literal
assert_equal 1, compile("[1,2].at(0)")
end
View
2  test/parser/function_call_test.rb
@@ -8,7 +8,7 @@ def test_slot_get
assert_kind_of SlotGet, invocation
assert_equal "a", invocation.receiver.name
- assert_equal :foo, invocation.name
+ assert_equal :foo, invocation.name.name.to_sym
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.