Skip to content

Commit

Permalink
Start working on object literals.
Browse files Browse the repository at this point in the history
Only two forms work ATM:
 - {"a": 1}
 - {a}

(obviously, the empty object, {}, parses as well)
  • Loading branch information
vendethiel committed Oct 20, 2015
1 parent 9cca1dd commit 9b1657f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/_007/Parser/Actions.pm
Expand Up @@ -411,10 +411,22 @@ class _007::Parser::Actions {
make Q::Quasi.new($<statements>.ast);
}

method term:object ($/) {
make Q::Literal::Object.new($<pair>».ast);
}

method unquote ($/) {
make Q::Unquote.new($<EXPR>.ast);
}

method pair:quoted ($/) {
make Q::Property.new($<key>.made, $<value>.made);
}

method pair:sym ($/) {
make Q::Property.new($<identifier>.made, $<identifier>.made);
}

method infix($/) {
make $*parser.oplevel.ops<infix>{~$/};
}
Expand Down
5 changes: 5 additions & 0 deletions lib/_007/Parser/Syntax.pm
Expand Up @@ -179,9 +179,14 @@ grammar _007::Parser::Syntax {
}
}
token term:quasi { quasi >> [<.ws> '{' ~ '}' <statements> || <.panic("quasi")>] }
token term:object { '{' ~ '}' <pair>* % [\h* ',' \h*] }

token unquote { '{{{' <EXPR> '}}}' }

proto token pair {*}
rule pair:quoted { <key=term> ':' <value=term> }
token pair:sym { <identifier> }

method infix {
my @ops = $*parser.oplevel.ops<infix>.keys;
if /@ops/(self) -> $cur {
Expand Down
26 changes: 26 additions & 0 deletions lib/_007/Q.pm
Expand Up @@ -97,6 +97,32 @@ role Q::Literal::Array does Q::Literal {
}
}

role Q::Literal::Object does Q::Literal {
has @.elements;
method new(*@elements) {
self.bless(elements => @elements)
}
method Str { "Object" ~ children(@.elements) }

method eval($runtime) {
... 'Val::Object NYI'
}
}

role Q::Property does Q::Literal {
has $.key;
has $.value;
method new($key, $value) {
self.bless(:$key, :$value);
}

method Str { "Property" }

method eval($runtime) {
... 'Property NYI'
}
}

role Q::Block does Q {
has $.parameters;
has $.statements;
Expand Down
2 changes: 2 additions & 0 deletions lib/_007/Test.pm
Expand Up @@ -8,6 +8,8 @@ sub read(Str $ast) is export {
int => Q::Literal::Int,
str => Q::Literal::Str,
array => Q::Literal::Array,
object => Q::Literal::Object,
property => Q::Property,

'-' => Q::Prefix::Minus,

Expand Down

0 comments on commit 9b1657f

Please sign in to comment.