Add alternation destructuring operator ?//

This is a first pass to show the implementation.
It needs tests and evaluation, but doesn't break any existing tests.

NOT READY FOR MERGING
This commit is contained in:
William Langford authored and Nicolas Williams committed 2017-03-26 05:36:22 -05:00
1 parent b142d484d5
commit 3a8c8f4747
7 files changed
+206 -27

No files matched your search

+22 -10
View File
@@ -83,6 +83,7 @@ struct lexer_param;
%token SETDEFINEDOR "//="
%token LESSEQ "<="
%token GREATEREQ ">="
%token ALTERNATION "?//"
%token QQSTRING_START
%token <literal> QQSTRING_TEXT
@@ -118,7 +119,7 @@ struct lexer_param;
%type <blk> FuncDef FuncDefs
%type <blk> Module Import Imports ImportWhat ImportFrom
%type <blk> Param Params Arg Args
%type <blk> Pattern ArrayPats ObjPats ObjPat
%type <blk> Patterns RepPatterns Pattern ArrayPats ObjPats ObjPat
%type <literal> Keyword
%{
#include "lexer.h"
@@ -176,10 +177,6 @@ static jv check_object_key(block k) {
return jv_invalid();
}
static block gen_dictpair(block k, block v) {
return BLOCK(gen_subexp(k), gen_subexp(v), gen_op_simple(INSERT));
}
static block gen_index(block obj, block key) {
return BLOCK(gen_subexp(key), obj, gen_op_simple(INDEX));
}
@@ -342,19 +339,18 @@ FuncDef Exp %prec FUNCDEF {
$$ = block_bind_referenced($1, $2, OP_IS_CALL_PSEUDO);
} |
Term "as" Pattern '|' Exp {
Term "as" Patterns '|' Exp {
$$ = gen_destructure($1, $3, $5);
} |
"reduce" Term "as" Pattern '(' Exp ';' Exp ')' {
"reduce" Term "as" Patterns '(' Exp ';' Exp ')' {
$$ = gen_reduce($2, $4, $6, $8);
} |
"foreach" Term "as" Pattern '(' Exp ';' Exp ';' Exp ')' {
"foreach" Term "as" Patterns '(' Exp ';' Exp ';' Exp ')' {
$$ = gen_foreach($2, $4, $6, $8, $10);
} |
"foreach" Term "as" Pattern '(' Exp ';' Exp ')' {
"foreach" Term "as" Patterns '(' Exp ';' Exp ')' {
$$ = gen_foreach($2, $4, $6, $8, gen_noop());
} |
@@ -778,6 +774,22 @@ Exp {
$$ = gen_lambda($1);
}
RepPatterns:
RepPatterns "?//" Pattern {
$$ = BLOCK($1, gen_destructure_alt($3));
} |
Pattern {
$$ = gen_destructure_alt($1);
}
Patterns:
RepPatterns "?//" Pattern {
$$ = BLOCK($1, $3);
} |
Pattern {
$$ = $1;
}
Pattern:
'$' IDENT {
$$ = gen_op_unbound(STOREV, jv_string_value($2));