Skip to content

Commit

Permalink
Rename Object to Dict
Browse files Browse the repository at this point in the history
It's more like Dict already than like Object.

There are a few things to straighten out here that need to be
fixed before this is mergeable to master -- notably, the whole
`new` syntax needs to work with objects, not with dicts.

But this is an encouraging start.

Closes #184.
  • Loading branch information
Carl Masak committed Feb 11, 2019
1 parent a2c754d commit 2aca4a7
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 139 deletions.
8 changes: 4 additions & 4 deletions examples/in.007
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ func infix:<in>(value, container) {
}
return false;
}
else if container ~~ Object {
else if container ~~ Dict {
return container.has(value);
}
else if container ~~ Str {
return container.contains(~value);
}
else {
throw new Exception {
message: "Wrong type to infix:<in>. Expected Array or Object or Str, was " ~ type(container),
message: "Wrong type to infix:<in>. Expected Array or Dict or Str, was " ~ type(container),
};
}
}
Expand All @@ -29,15 +29,15 @@ func infix:<not in>(value, container) {
}
return true;
}
else if container ~~ Object {
else if container ~~ Dict {
return !container.has(value);
}
else if container ~~ Str {
return !container.contains(~value);
}
else {
throw new Exception {
message: "Wrong type to infix:<not in>. Expected Array or Object or Str, was " ~ type(container),
message: "Wrong type to infix:<not in>. Expected Array or Dict or Str, was " ~ type(container),
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/_007/Builtins.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ my &parameter = { Q::Parameter.new(:identifier(Q::Identifier.new(:name(Val::Str.
default { die "Unknown type {.value.^name}" }
});

my $builtins-pad = Val::Object.new;
my $builtins-pad = Val::Dict.new;
for @builtins -> Pair (:key($name), :$value) {
$builtins-pad.properties{$name} = $value;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/_007/Equal.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ multi equal-value(Val::Array $l, Val::Array $r) {
[&&] $l.elements == $r.elements,
|(^$l.elements).map(&equal-at-index);
}
multi equal-value(Val::Object $l, Val::Object $r) {
multi equal-value(Val::Dict $l, Val::Dict $r) {
if %*equality-seen{$l.WHICH} && %*equality-seen{$r.WHICH} {
return $l === $r;
}
Expand Down
27 changes: 15 additions & 12 deletions lib/_007/Parser/Actions.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ class _007::Parser::Actions {
}

if $expansion ~~ Q::StatementList {
$*runtime.enter($*runtime.current-frame, Val::Object.new, $expansion);
$*runtime.enter($*runtime.current-frame, Val::Dict.new, $expansion);
$expansion = Q::Block.new(
:parameterlist(Q::ParameterList.new())
:statementlist($expansion));
Expand Down Expand Up @@ -340,7 +340,7 @@ class _007::Parser::Actions {
my $declname = $decltype.^name.subst(/ .* '::'/, "").lc;
die X::Assignment::ReadOnly.new(:$declname, :$symbol)
unless $decltype.is-assignable;
%*assigned{$frame.id ~ $symbol}++;
%*assigned{$frame.WHICH ~ $symbol}++;
}
}
}
Expand Down Expand Up @@ -656,11 +656,10 @@ class _007::Parser::Actions {
my $type-obj = $type.type;
my $name = $type-obj.^name.subst("::", ".", :g);

if $type-obj !=== Val::Object {
if $type-obj !=== Val::Dict {
if is-role($type-obj) {
die X::Uninstantiable.new(:$name);
}

sub aname($attr) { $attr.name.substr(2) }
my %known-properties = $type-obj.attributes.map({ aname($_) => 1 });
for $<propertylist>.ast.properties.elements -> $p {
Expand All @@ -678,12 +677,16 @@ class _007::Parser::Actions {
}
}

make Q::Term::Object.new(:$type, :propertylist($<propertylist>.ast));
make Q::Term::Dict.new(
:$type,
:propertylist($<propertylist>.ast));
}

method term:object ($/) {
make Q::Term::Object.new(
:type(Val::Type.of(Val::Object)),
method term:dict ($/) {
my $type = Val::Type.of(Val::Dict);

make Q::Term::Dict.new(
:$type,
:propertylist($<propertylist>.ast));
}

Expand Down Expand Up @@ -846,7 +849,7 @@ sub check(Q $ast, $runtime) is export {
:statementlist($func.block.statementlist),
:$outer-frame
);
$runtime.enter($outer-frame, Val::Object.new, $func.block.statementlist, $val);
$runtime.enter($outer-frame, Val::Dict.new, $func.block.statementlist, $val);
handle($func.block);
$runtime.leave();

Expand All @@ -861,7 +864,7 @@ sub check(Q $ast, $runtime) is export {
:statementlist($macro.block.statementlist),
:$outer-frame
);
$runtime.enter($outer-frame, Val::Object.new, $macro.block.statementlist, $val);
$runtime.enter($outer-frame, Val::Dict.new, $macro.block.statementlist, $val);
handle($macro.block);
$runtime.leave();

Expand All @@ -881,14 +884,14 @@ sub check(Q $ast, $runtime) is export {
}

multi handle(Q::Block $block) {
$runtime.enter($runtime.current-frame, Val::Object.new, Q::StatementList.new);
$runtime.enter($runtime.current-frame, Val::Dict.new, Q::StatementList.new);
handle($block.parameterlist);
handle($block.statementlist);
$block.static-lexpad = $runtime.current-frame.properties<pad>;
$runtime.leave();
}

multi handle(Q::Term::Object $object) {
multi handle(Q::Term::Dict $object) {
handle($object.propertylist);
}

Expand Down
8 changes: 4 additions & 4 deletions lib/_007/Parser/Syntax.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ grammar _007::Parser::Syntax {
token newpad { <?> {
$*parser.push-opscope;
@*declstack.push(@*declstack ?? @*declstack[*-1].clone !! {});
$*runtime.enter($*runtime.current-frame, Val::Object.new, Q::StatementList.new);
$*runtime.enter($*runtime.current-frame, Val::Dict.new, Q::StatementList.new);
} }

token finishpad { <?> {
Expand All @@ -40,7 +40,7 @@ grammar _007::Parser::Syntax {
if $*runtime.declared-locally($symbol);
my $frame = $*runtime.current-frame();
die X::Redeclaration::Outer.new(:$symbol)
if %*assigned{$frame.id ~ $symbol};
if %*assigned{$frame.WHICH ~ $symbol};
my $identifier = Q::Identifier.new(
:name(Val::Str.new(:value($symbol))),
:$frame);
Expand Down Expand Up @@ -223,7 +223,7 @@ grammar _007::Parser::Syntax {
|| "<" <.ws> $<qtype>=["Q.PropertyList"] ">" <.ws> '{' <.ws> <propertylist> <.ws> '}'
|| "<" <.ws> $<qtype>=["Q.Term"] ">" <.ws> '{' <.ws> <term> <.ws> '}'
|| "<" <.ws> $<qtype>=["Q.Term.Array"] ">" <.ws> '{' <.ws> <term:array> <.ws> '}'
|| "<" <.ws> $<qtype>=["Q.Term.Object"] ">" <.ws> '{' <.ws> <term:object> <.ws> '}'
|| "<" <.ws> $<qtype>=["Q.Term.Dict"] ">" <.ws> '{' <.ws> <term:object> <.ws> '}'
|| "<" <.ws> $<qtype>=["Q.Term.Quasi"] ">" <.ws> '{' <.ws> <term:quasi> <.ws> '}'
|| "<" <.ws> $<qtype>=["Q.Trait"] ">" <.ws> '{' <.ws> <trait> <.ws> '}'
|| "<" <.ws> $<qtype>=["Q.TraitList"] ">" <.ws> '{' <.ws> <traitlist> <.ws> '}'
Expand Down Expand Up @@ -251,7 +251,7 @@ grammar _007::Parser::Syntax {
}> <.ws>
'{' ~ '}' <propertylist>
}
token term:object {
token term:dict {
'{' ~ '}' <propertylist>
}
token term:identifier {
Expand Down
17 changes: 8 additions & 9 deletions lib/_007/Q.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,11 @@ class Q::Term::Array does Q::Term {
}
}

### ### Q::Term::Object
### ### Q::Term::Dict
###
### An object. Object terms consist of an optional *type*, and a property list
### with zero or more key/value pairs.
### A dict. Dict terms consist of an entry list with zero or more key/value pairs.
###
class Q::Term::Object does Q::Term {
class Q::Term::Dict does Q::Term {
has Val::Type $.type;
has $.propertylist;

Expand Down Expand Up @@ -391,7 +390,7 @@ class Q::Term::Func does Q::Term does Q::Declaration {
class Q::Block does Q {
has $.parameterlist;
has $.statementlist;
has Val::Object $.static-lexpad is rw = Val::Object.new;
has Val::Dict $.static-lexpad is rw = Val::Dict.new;
# XXX
has $.frame is rw;
Expand Down Expand Up @@ -530,7 +529,7 @@ class Q::Postfix::Index is Q::Postfix {
if $index.value < 0;
return .elements[$index.value];
}
when Val::Object | Val::Func | Q {
when Val::Dict | Val::Func | Q {
my $property = $.index.eval($runtime);
die X::Subscript::NonString.new
if $property !~~ Val::Str;
Expand All @@ -553,7 +552,7 @@ class Q::Postfix::Index is Q::Postfix {
if $index.value < 0;
.elements[$index.value] = $value;
}
when Val::Object | Q {
when Val::Dict | Q {
my $property = $.index.eval($runtime);
die X::Subscript::NonString.new
if $property !~~ Val::Str;
Expand Down Expand Up @@ -602,7 +601,7 @@ class Q::Postfix::Property is Q::Postfix {

method put-value($value, $runtime) {
given $.operand.eval($runtime) {
when Val::Object | Q {
when Val::Dict | Q {
my $propname = $.property.name.value;
$runtime.put-property($_, $propname, $value);
}
Expand Down Expand Up @@ -666,7 +665,7 @@ class Q::Term::Quasi does Q::Term {
if $thing ~~ Val::Array;

return $thing.new(:properties(%($thing.properties.map({ .key => interpolate(.value) }))))
if $thing ~~ Val::Object;
if $thing ~~ Val::Dict;

return $thing
if $thing ~~ Val;
Expand Down
20 changes: 10 additions & 10 deletions lib/_007/Runtime.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use _007::Q;
use _007::Builtins;
use _007::Equal;

constant NO_OUTER = Val::Object.new;
constant NO_OUTER = Val::Dict.new;
constant RETURN_TO = Q::Identifier.new(
:name(Val::Str.new(:value("--RETURN-TO--"))),
:frame(NONE));
Expand All @@ -23,7 +23,7 @@ class _007::Runtime {

submethod BUILD(:$!input, :$!output, :@!arguments) {
$!builtin-opscope = opscope();
$!builtin-frame = Val::Object.new(:properties(
$!builtin-frame = Val::Dict.new(:properties(
:outer-frame(NO_OUTER),
:pad(builtins-pad()))
);
Expand Down Expand Up @@ -76,7 +76,7 @@ class _007::Runtime {
}

method enter($outer-frame, $static-lexpad, $statementlist, $routine?) {
my $frame = Val::Object.new(:properties(:$outer-frame, :pad(Val::Object.new)));
my $frame = Val::Dict.new(:properties(:$outer-frame, :pad(Val::Dict.new)));
@!frames.push($frame);
for $static-lexpad.properties.kv -> $name, $value {
my $identifier = Q::Identifier.new(
Expand Down Expand Up @@ -162,7 +162,7 @@ class _007::Runtime {

method declare-var(Q::Identifier $identifier, $value?) {
my $name = $identifier.name.value;
my Val::Object $frame = $identifier.frame ~~ Val::None
my Val::Dict $frame = $identifier.frame ~~ Val::None
?? self.current-frame
!! $identifier.frame;
$frame.properties<pad>.properties{$name} = $value // NONE;
Expand Down Expand Up @@ -258,7 +258,7 @@ class _007::Runtime {
if $thing ~~ Val::Array;
return $thing.new(:properties(%($thing.properties.map(.key => interpolate(.value)))))
if $thing ~~ Val::Object;
if $thing ~~ Val::Dict;
return $thing
if $thing ~~ Val;
Expand Down Expand Up @@ -376,7 +376,7 @@ class _007::Runtime {
return Val::Str.new(:value($obj.elements.join($sep.value.Str)));
});
}
elsif $obj ~~ Val::Object && $propname eq "size" {
elsif $obj ~~ Val::Dict && $propname eq "size" {
return builtin(sub size() {
return Val::Int.new(:value($obj.properties.elems));
});
Expand Down Expand Up @@ -512,7 +512,7 @@ class _007::Runtime {
elsif $obj ~~ Val::Func && $propname eq any <outer-frame static-lexpad parameterlist statementlist> {
return $obj."$propname"();
}
elsif $obj ~~ (Q | Val::Object) && ($obj.properties{$propname} :exists) {
elsif $obj ~~ (Q | Val::Dict) && ($obj.properties{$propname} :exists) {
return $obj.properties{$propname};
}
elsif $propname eq "get" {
Expand All @@ -532,7 +532,7 @@ class _007::Runtime {
# XXX: problem: we're not lying hard enough here. we're missing
# both Q objects, which are still hard-coded into the
# substrate, and the special-cased properties
# <get has extend update id>
# <get has extend update>
my $value = $obj.properties{$prop.value} :exists;
return Val::Bool.new(:$value);
});
Expand Down Expand Up @@ -638,8 +638,8 @@ class _007::Runtime {
if $obj ~~ Q {
die "We don't handle assigning to Q object properties yet";
}
elsif $obj !~~ Val::Object {
die "We don't handle assigning to non-Val::Object types yet";
elsif $obj !~~ Val::Dict {
die "We don't handle assigning to non-Val::Dict types yet";
}
else {
$obj.properties{$propname} = $newvalue;
Expand Down
Loading

0 comments on commit 2aca4a7

Please sign in to comment.