Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement new Nil model
  • Loading branch information
sorear committed Aug 3, 2011
1 parent 7888fe2 commit 6b9d707
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 9 deletions.
2 changes: 0 additions & 2 deletions TODO
Expand Up @@ -63,8 +63,6 @@ HARD

*Finish roles.

Study Nil and find the correct way to integrate it into Niecza.

Add a JVM backend.

Add a Parrot backend.
Expand Down
21 changes: 14 additions & 7 deletions lib/CORE.setting
Expand Up @@ -434,11 +434,11 @@ my class Submethod is Routine { }
my class WhateverCode is Block { }
my class ClassHOW {
method isa(Mu $obj, Mu $type) { Q:CgOp {
method isa(\$obj, \$type) { Q:CgOp {
(box Bool (obj_isa (@ {$obj}) (obj_llhow (@ {$type}))))
} }
method does(Mu $obj, Mu $role) { self.isa($obj, $role) } #no roles yet
method can(Mu $obj, $name) { Q:CgOp {
method does(\$obj, \$role) { self.isa($obj, $role) } #no roles yet
method can(\$obj, $name) { Q:CgOp {
(box Bool (obj_can (@ {$obj}) (obj_getstr {$name})))
} }
}
Expand Down Expand Up @@ -802,6 +802,13 @@ my class EMPTY { }
my class List { ... }
my class Array { ... }
my class Nil is Cool {
method new() { Nil }
method iterator() { ().iterator }
method gist() { 'Nil' }
method Str() { '' }
}
my class Parcel is Cool {
method flat() { self.iterator.flat }
method list() { self.iterator.list }
Expand All @@ -817,8 +824,10 @@ my class Parcel is Cool {
method unwrap-single(@self:) { Q:CgOp {
(letn p (unbox fvarlist (@ {@self}))
(ternary (== (i 1) (fvarlist_length (l p)))
(fvarlist_item (i 0) (l p)) {@self}))
l (fvarlist_length (l p))
(ternary (== (i 0) (l l)) {Nil}
(ternary (== (i 1) (l l))
(fvarlist_item (i 0) (l p)) {@self})))
} }
method Capture () {
Expand Down Expand Up @@ -856,8 +865,6 @@ my class Parcel is Cool {
}
}
constant Nil = Q:CgOp { (newrwlistvar (@ (box Parcel (fvarlist_new)))) };
my class List is Cool {
has @!items;
has @!rest;
Expand Down
17 changes: 17 additions & 0 deletions lib/Kernel.cs
Expand Up @@ -127,6 +127,9 @@ public sealed class SimpleVariable: Variable {
if (!rw) {
throw new NieczaException("Writing to readonly scalar");
}
if (v == Kernel.NilP) {
v = type.initObject;
}
if (!v.Does(type)) {
throw new NieczaException("Nominal type check failed for scalar store; got " + v.mo.name + ", needed " + type.name + " or subtype");
}
Expand Down Expand Up @@ -643,6 +646,11 @@ public sealed class RuntimeUnit {

mo.fixups_from = from;

if (this.name == "CORE" && name == "Nil") {
// this anomalous type object is iterable
mo.typeVar = Kernel.NewRWListVar(mo.typeObject);
}

return mo;
}

Expand Down Expand Up @@ -1006,6 +1014,7 @@ public class SubInfo {
src = pos[posc++];
goto gotit;
}
get_default:
if ((flags & SIG_F_HASDEFAULT) != 0) {
Frame thn = Kernel.GetInferiorRoot()
.MakeChild(th, (SubInfo) rbuf[rbase + 1 + names],
Expand Down Expand Up @@ -1047,6 +1056,13 @@ public class SubInfo {
bool rw = ((flags & SIG_F_READWRITE) != 0) && !islist;
P6any srco = src.Fetch();

// XXX: in order for calling methods on Nil to work,
// self needs to be ignored here.
if (srco == Kernel.NilP && obj_src != -1 &&
(flags & SIG_F_INVOCANT) == 0) {
obj_src = -1;
goto get_default;
}
if (!srco.Does(type)) {
if (quiet) return null;
if (srco.mo.HasMRO(Kernel.JunctionMO) && obj_src != -1) {
Expand Down Expand Up @@ -2981,6 +2997,7 @@ public class Kernel {
public static STable CaptureMO;
public static STable GatherIteratorMO;
public static STable IterCursorMO;
public static P6any NilP;
public static P6any AnyP;
public static P6any ArrayP;
public static P6any EMPTYP;
Expand Down
1 change: 1 addition & 0 deletions src/NieczaPassSimplifier.pm6
Expand Up @@ -85,6 +85,7 @@ sub do_builtin($name, $expect) { sub ($body, $nv, $invname, $op) { #OK not used
sub do_return_take($body, $nv, $invname, $op) { #OK not used
return $op unless defined my $args = no_named_params($op);
my $parcel = ($args == 1 ?? $args[0] !!
$args == 0 ?? ::Op::Lexical.new(name => 'Nil') !!
::Op::CallSub.new(invocant => ::Op::Lexical.new(name => '&infix:<,>'),
positionals => [@$args]));
return ($invname eq '&take' ??
Expand Down
15 changes: 15 additions & 0 deletions src/niecza
Expand Up @@ -157,6 +157,21 @@ class Op::IndirectVar is Op {


augment class NieczaActions {
method circumfix:sym<( )> ($/) {
my @kids = @( $<semilist>.ast );
if @kids == 1 && @kids[0].^isa(::Op::WhateverCode) {
# XXX in cases like * > (2 + *), we *don't* want the parens to disable
# syntactic specialization, since they're required for grouping
make @kids[0];
} elsif !@kids {
# an empty StatementList returns Nil, but () needs to be defined...
make ::Op::Paren.new(|node($/), inside =>
::Op::SimpleParcel.new(items => []));
} else {
make ::Op::StatementList.new(|node($/), children => @kids);
}
}

method process_name($/, :$declaring, :$defer, :$clean) {
return () unless defined $/;

Expand Down
1 change: 1 addition & 0 deletions t/spectest.data
Expand Up @@ -10,6 +10,7 @@ S02-builtin_data_types/bool.t
S02-builtin_data_types/catch_type_cast_mismatch.t
S02-builtin_data_types/flattening.t
S02-builtin_data_types/nested_pairs.t
S02-builtin_data_types/nil.t
S02-builtin_data_types/parsing-bool.t
S02-builtin_data_types/subscripts_and_context.t
S02-lexical-conventions/unicode.t
Expand Down

0 comments on commit 6b9d707

Please sign in to comment.