Permalink
Browse files

add [], []= support to List

  • Loading branch information...
1 parent 1dc2d5c commit 32e16587450544b00823036c797aef942154f8e5 @baroquebobcat baroquebobcat committed Oct 9, 2012
@@ -21,6 +21,7 @@ import java.util.Collections
class Builtins
def self.initialize_builtins(mirah:Compiler)
mirah.type_system.extendClass('java.util.Map', MapExtensions.class)
+ mirah.type_system.extendClass('java.util.List', ListExtensions.class)
mirah.type_system.extendClass('java.lang.Object', ObjectExtensions.class)
mirah.type_system.extendClass('java.lang.Iterable', EnumerableExtensions.class)
mirah.type_system.extendClass('java.lang.Iterable', IterableExtensions.class)
@@ -0,0 +1,28 @@
+# Copyright (c) 2012 The Mirah project authors. All Rights Reserved.
+# All contributing project authors may be found in the NOTICE file.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+package org.mirah.builtins
+
+
+class ListExtensions
+ macro def [](index)
+ quote { `@call.target`.get `index` }
+ end
+
+ macro def []=(index, value)
+ quote { `@call.target`.add `index`, `value` }
+ end
+
+end
@@ -0,0 +1,23 @@
+class ListExtensionsTest < Test::Unit::TestCase
+ def test_bracket_getter
+ cls, = compile(<<-EOF)
+ x = [1,2]
+ puts x[0]
+ EOF
+ assert_output("1\n") do
+ cls.main(nil)
+ end
+ end
+
+ def test_bracket_assignment
+ cls, = compile(<<-EOF)
+ import java.util.ArrayList # literals are immutable
+ x = ArrayList.new
+ x[0]= "2"
+ puts x
+ EOF
+ assert_output("[2]\n") do
+ cls.main(nil)
+ end
+ end
+end

0 comments on commit 32e1658

Please sign in to comment.