Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add attr_{reader,writer,accessor} builtins.

  • Loading branch information...
commit afe5384066db9b0f63cb84bdcd4706cc55fc4d88 1 parent 8e31256
@ribrdb ribrdb authored
View
45 src/org/mirah/builtins/object_extensions.mirah
@@ -15,6 +15,8 @@
package org.mirah.builtins
+import mirah.lang.ast.*
+
class ObjectExtensions
macro def puts(node)
quote {System.out.println(`node`)}
@@ -35,4 +37,47 @@ class ObjectExtensions
macro def self.loop(block:Block)
quote { while true do `block.body` end }
end
+
+ macro def self.attr_accessor(hash:Hash)
+ args = [hash]
+ quote do
+ attr_reader `args`
+ attr_writer `args`
+ end
+ end
+
+ macro def self.attr_reader(hash:Hash)
+ methods = NodeList.new
+ i = 0
+ size = hash.size
+ while i < size
+ e = hash.get(i)
+ i += 1
+ method = quote do
+ def `e.key`:`e.value` #`
+ @`e.key`
+ end
+ end
+ methods.add(method)
+ end
+ methods
+ end
+
+ macro def self.attr_writer(hash:Hash)
+ methods = NodeList.new
+ i = 0
+ size = hash.size
+ while i < size
+ e = hash.get(i)
+ i += 1
+ name = Identifier(e.key).identifier + "_set"
+ method = quote do
+ def `name`(value:`e.value`):void #`
+ @`e.key` = value
+ end
+ end
+ methods.add(method)
+ end
+ methods
+ end
end
View
8 src/org/mirah/typer/typer.mirah
@@ -135,7 +135,9 @@ class Typer < SimpleNodeVisitor
def visitVCall(call, expression)
scope = @scopes.getScope(call)
- methodType = CallFuture.new(@types, scope, scope.selfType, Collections.emptyList, call)
+ targetType = scope.selfType
+ targetType = @types.getMetaType(targetType) if scope.context.kind_of?(ClassDefinition)
+ methodType = CallFuture.new(@types, scope, targetType, Collections.emptyList, call)
fcall = FunctionalCall.new(call.position, Identifier(call.name.clone), nil, nil)
fcall.setParent(call.parent)
@futures[fcall] = methodType
@@ -180,7 +182,9 @@ class Typer < SimpleNodeVisitor
parameters = inferAll(call.parameters)
parameters.add(infer(call.block, true)) if call.block
delegate = DelegateFuture.new
- methodType = CallFuture.new(@types, scope, scope.selfType, parameters, call)
+ targetType = scope.selfType
+ targetType = @types.getMetaType(targetType) if scope.context.kind_of?(ClassDefinition)
+ methodType = CallFuture.new(@types, scope, targetType, parameters, call)
delegate.type = methodType
typer = self
methodType.onUpdate do |x, resolvedType|
View
20 test/jvm/macros_test.rb
@@ -131,7 +131,7 @@ def function
def test_unquote_method_definitions_with_main
script, cls = compile(<<-EOF)
class UnquoteMacros
- macro def make_attr(name_node:Identifier, type:TypeName)
+ macro def self.make_attr(name_node:Identifier, type:TypeName)
name = name_node.identifier
quote do
def `name`
@@ -236,7 +236,7 @@ def test_block_parameter_uses_outer_scope
def test_method_def_after_macro_def_with_same_name_raises_error
assert_raises Mirah::MirahError do
compile(<<-EOF)
- macro def foo
+ macro def self.foo
quote { System.out.println :z }
end
def foo
@@ -250,7 +250,7 @@ def foo
def test_macro_def_unquote_named_method_without_main
cls, = compile <<-EOF
class FooHaver
- macro def null_method(name)
+ macro def self.null_method(name)
quote {
def `name`
end
@@ -261,4 +261,18 @@ def `name`
EOF
assert_equal nil, cls.new.testing
end
+
+ def test_attr_accessor
+ script, cls = compile(<<-EOF)
+ class AttrAccessorTest
+ attr_accessor foo: int
+ end
+
+ x = AttrAccessorTest.new
+ System.out.println x.foo
+ x.foo = 3
+ System.out.println x.foo
+ EOF
+ assert_output("0\n3\n") {script.main(nil)}
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.