Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Slot assignment works

  • Loading branch information...
commit 6f9baae3dbba4d2c09cf664704197f6795a9fdcf 1 parent 4c66c90
Josep M. Bach authored
24 lib/noscript/ast/script.rb
View
@@ -96,23 +96,23 @@ def initialize(line, name, value)
@value = value
end
end
-#
-#
-# class SlotAssignNode < Node
-# attr_reader :receiver, :slot, :value
-# def initialize(receiver, slot, value)
-# @receiver = receiver
-# @slot = slot
-# @value = value
-# end
-# end
-#
+
+ class SlotAssign < Node
+ attr_reader :receiver, :name, :value
+ def initialize(line, receiver, name, value)
+ super(line)
+ @receiver = receiver
+ @name = name.name.to_sym
+ @value = value
+ end
+ end
+
class SlotGet < Node
attr_reader :receiver, :name
def initialize(line, receiver, name)
super(line)
@receiver = receiver
- @name = name.to_sym
+ @name = name.name.to_sym
end
end
#
2  lib/noscript/bootstrap.rb
View
@@ -32,7 +32,7 @@ class ObjectType < Rubinius::LookupTable
attr_accessor :prototype
def initialize
- @prototype = Object
+ @prototype = Runtime::Object
end
noscript_def("clone") do |*args|
8 lib/noscript/compiler.rb
View
@@ -157,6 +157,14 @@ def visit_SlotGet(o)
g.send :get, 1
end
+ def visit_SlotAssign(o)
+ set_line(o)
+ o.receiver.accept(self)
+ g.push_literal o.name
+ o.value.accept(self)
+ g.send :put, 2
+ end
+
def finalize
g.local_names = s.variables
g.local_count = s.variables.size
4 lib/noscript/parser/noscript.y
View
@@ -153,13 +153,13 @@ rule
SlotAssign:
# receiver.slot = 123
- Expression '.' Identifier '=' Expression { result = SlotAssignNode.new(val[0], val[2], val[4]); result.pos(filename, lineno) }
+ Expression '.' Identifier '=' Expression { result = SlotAssign.new(lineno, val[0], val[2], val[4]) }
;
# Get a slot from an object
SlotGet:
# receiver.slot
- | Expression '.' Identifier { result = SlotGet.new(lineno, val[0], val[2].name) }
+ | Expression '.' Identifier { result = SlotGet.new(lineno, val[0], val[2]) }
;
# Function call
4 lib/noscript/parser/parser.rb
View
@@ -765,7 +765,7 @@ def _reduce_51(val, _values, result)
module_eval(<<'.,.,', 'noscript.y', 155)
def _reduce_52(val, _values, result)
- result = SlotAssignNode.new(val[0], val[2], val[4]); result.pos(filename, lineno)
+ result = SlotAssign.new(lineno, val[0], val[2], val[4])
result
end
.,.,
@@ -774,7 +774,7 @@ def _reduce_52(val, _values, result)
module_eval(<<'.,.,', 'noscript.y', 161)
def _reduce_54(val, _values, result)
- result = SlotGet.new(lineno, val[0], val[2].name)
+ result = SlotGet.new(lineno, val[0], val[2])
result
end
.,.,
7 test/new_test.rb
View
@@ -82,9 +82,10 @@ def test_compile_slot_get
assert_equal 1, @compiler.compile("obj = Object.clone({a: 1}); obj.a").call
end
- # def test_compile_slot_assign
- # assert_equal [:assign_slot], @compiler.compile("foo.a = 3")
- # end
+ def test_compile_slot_assign
+ obj = @compiler.compile("foo = Object.clone(); foo.a = 3; foo").call
+ assert_equal 3, obj.get(:a)
+ end
# def test_compile_slot_get
# assert_equal [:get_slot], @compiler.compile("foo.a")
Please sign in to comment.
Something went wrong with that request. Please try again.