Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[dotnet] implement auto-generated accessors & mutators: has $.foo is rw
viviself containers don't happen yet, though.  jnthn, fix plz
  • Loading branch information
diakopter committed Nov 17, 2010
1 parent 3e62209 commit 24494b3
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 6 deletions.
14 changes: 12 additions & 2 deletions dotnet/compiler/Actions.pm
Expand Up @@ -366,10 +366,13 @@ method variable($/) {
)
);
}
elsif $<twigil>[0] eq '!' {
elsif $<twigil>[0] eq '!' || $<twigil>[0] eq '.' {
$past.push(PAST::Var.new( :name('self') ));
$past.scope('attribute');
$past.viviself( vivitype( $<sigil> ) );
if ($<twigil>[0] eq '.') {
$past.name(pir::substr($past.name, 0, 1) ~ '!' ~ pir::substr($past.name, 2));
}
}
}
make $past;
Expand Down Expand Up @@ -493,6 +496,9 @@ method variable_declarator($/) {
$/.CURSOR.panic("Redeclaration of symbol ", $name);
}
if $*SCOPE eq 'has' {
if $<variable><twigil>[0] eq '.' {
$name := pir::substr($name, 0, 1) ~ '!' ~ pir::substr($name, 2);
}
# Create and add a meta-attribute.
my $meta-attr-type := %*HOW-METAATTR{$*PKGDECL} || $*DEFAULT-METAATTR;
$*PACKAGE-SETUP.push(PAST::Op.new(
Expand All @@ -505,7 +511,11 @@ method variable_declarator($/) {
PAST::Op.new(
:pasttype('callmethod'), :name('new'),
PAST::Var.new( :name($meta-attr-type), :scope('lexical') ),
PAST::Val.new( :value($name), :named('name') )
PAST::Val.new( :value($name), :named('name') ),
PAST::Val.new( :value($<variable><twigil>[0] eq '.'
?? 1 !! 0), :named('has_accessor') ),
PAST::Val.new( :value($<declarator_is_rw>
?? 1 !! 0), :named('has_mutator') )
)
));
$past := PAST::Stmts.new();
Expand Down
6 changes: 4 additions & 2 deletions dotnet/compiler/Grammar.pm
Expand Up @@ -265,7 +265,7 @@ token variable {

token sigil { <[$@%&]> | '::' }

token twigil { <[*!?]> }
token twigil { <[*!?.]> }

proto token package_declarator { <...> }
token package_declarator:sym<module> { <sym> <package_def> }
Expand Down Expand Up @@ -319,7 +319,9 @@ token declarator {
| <routine_declarator>
}

token variable_declarator { <variable> }
token variable_declarator { <variable> <declarator_is_rw>?}

token declarator_is_rw { [<.ws> 'is' <.ws> 'rw'] }

proto token routine_declarator { <...> }
token routine_declarator:sym<sub> { <sym> <routine_def> }
Expand Down
28 changes: 26 additions & 2 deletions dotnet/runtime/Runtime/Ops/Metamodel.cs
Expand Up @@ -42,8 +42,8 @@ public static RakudoObject instance_of(ThreadContext TC, RakudoObject WHAT)
/// <param name="Obj"></param>
/// <returns></returns>
public static RakudoObject repr_defined(ThreadContext TC, RakudoObject Obj)
{
return Ops.box_int(TC, Obj.STable.REPR.defined(TC, Obj) ? 1 : 0, TC.DefaultBoolBoxType);
{
return Ops.box_int(TC, !(Obj is object) || Obj.STable.REPR.defined(TC, Obj) ? 1 : 0, TC.DefaultBoolBoxType);
}

/// <summary>
Expand All @@ -56,6 +56,18 @@ public static RakudoObject repr_defined(ThreadContext TC, RakudoObject Obj)
public static RakudoObject get_attr(ThreadContext TC, RakudoObject Object, RakudoObject Class, string Name)
{
return Object.STable.REPR.get_attribute(TC, Object, Class, Name);
}

/// <summary>
/// Gets the value of an attribute.
/// </summary>
/// <param name="Object"></param>
/// <param name="Class"></param>
/// <param name="Name"></param>
/// <returns></returns>
public static RakudoObject get_attr(ThreadContext TC, RakudoObject Object, RakudoObject Class, RakudoObject Name)
{
return Object.STable.REPR.get_attribute(TC, Object, Class, Ops.unbox_str(TC, Name));
}

/// <summary>
Expand All @@ -81,6 +93,18 @@ public static RakudoObject bind_attr(ThreadContext TC, RakudoObject Object, Raku
{
Object.STable.REPR.bind_attribute(TC, Object, Class, Name, Value);
return Value;
}

/// <summary>
/// Binds the value of an attribute to the given value.
/// </summary>
/// <param name="Object"></param>
/// <param name="Class"></param>
/// <param name="Name"></param>
public static RakudoObject bind_attr(ThreadContext TC, RakudoObject Object, RakudoObject Class, RakudoObject Name, RakudoObject Value)
{
Object.STable.REPR.bind_attribute(TC, Object, Class, Ops.unbox_str(TC, Name), Value);
return Value;
}

/// <summary>
Expand Down
28 changes: 28 additions & 0 deletions dotnet/runtime/Runtime/Ops/Primitive.cs
Expand Up @@ -214,6 +214,34 @@ public static RakudoObject bitwise_xor_num(ThreadContext TC, RakudoObject x, Rak
public static RakudoObject concat(ThreadContext TC, RakudoObject x, RakudoObject y)
{
return Ops.box_str(TC, Ops.unbox_str(TC, x) + Ops.unbox_str(TC, y), TC.DefaultStrBoxType);
}

/// <summary>
/// Performs a string substring.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="z"></param>
/// <param name="ResultType"></param>
/// <returns></returns>
public static RakudoObject substr(ThreadContext TC, RakudoObject x, RakudoObject y, RakudoObject z)
{
return Ops.box_str(TC, z.STable.REPR.defined(TC, z)
? Ops.unbox_str(TC, x).Substring(Ops.unbox_int(TC, y), Ops.unbox_int(TC, z))
: Ops.unbox_str(TC, x).Substring(Ops.unbox_int(TC, y)), TC.DefaultStrBoxType);
}

/// <summary>
/// Performs a string substring.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="z"></param>
/// <param name="ResultType"></param>
/// <returns></returns>
public static RakudoObject substr(ThreadContext TC, RakudoObject x, RakudoObject y)
{
return Ops.box_str(TC, Ops.unbox_str(TC, x).Substring(Ops.unbox_int(TC, y)), TC.DefaultStrBoxType);
}
}
}

0 comments on commit 24494b3

Please sign in to comment.