// (tight binding, do last).
enum { PREC_BINARY, PREC_REVERSE, PREC_PREFIX, PREC_SUFFIX };
-#define MAX_PREC_TOKS 5
+#define MAX_PREC_TOKS 6
struct precedence {
int toks[MAX_PREC_TOKS];
int rule;
} PRECEDENCE[] = {
{ { TOK_SEMI, TOK_COMMA }, PREC_REVERSE },
- { { TOK_COLON }, PREC_BINARY },
+ { { TOK_ELLIPSIS }, PREC_SUFFIX },
{ { TOK_RETURN, TOK_BREAK, TOK_CONTINUE }, PREC_PREFIX },
- { { TOK_ASSIGN }, PREC_REVERSE },
+ { { TOK_ASSIGN, TOK_PLUSEQ, TOK_MINUSEQ,
+ TOK_MULEQ, TOK_DIVEQ, TOK_CATEQ }, PREC_REVERSE },
+ { { TOK_COLON, TOK_QUESTION }, PREC_REVERSE },
+ { { TOK_VAR }, PREC_PREFIX },
{ { TOK_OR }, PREC_BINARY },
{ { TOK_AND }, PREC_BINARY },
{ { TOK_EQ, TOK_NEQ }, PREC_BINARY },
{ { TOK_LT, TOK_LTE, TOK_GT, TOK_GTE }, PREC_BINARY },
- { { TOK_PLUS, TOK_MINUS, TOK_CAT }, PREC_REVERSE },
+ { { TOK_PLUS, TOK_MINUS, TOK_CAT }, PREC_BINARY },
{ { TOK_MUL, TOK_DIV }, PREC_BINARY },
{ { TOK_MINUS, TOK_NEG, TOK_NOT }, PREC_PREFIX },
{ { TOK_LPAR, TOK_LBRA }, PREC_SUFFIX },
t = start;
while(t) {
switch(t->type) {
- case TOK_ELSE: case TOK_FUNC:
+ case TOK_FUNC:
+ // Slurp an optional paren block containing an arglist, then
+ // fall through to parse the curlies...
+ if(t->next && t->next->type == TOK_LPAR) {
+ c = t->next;
+ addNewChild(t, c);
+ fixBlockStructure(p, c);
+ }
+ case TOK_ELSE: // and TOK_FUNC!
// These guys precede a single curly block
if(!t->next || t->next->type != TOK_LCURL) oops(p, t);
c = t->next;
addNewChild(t, c);
fixBlockStructure(p, c);
break;
- case TOK_FOR: case TOK_FOREACH: case TOK_WHILE:
+ case TOK_FOR: case TOK_FOREACH: case TOK_FORINDEX: case TOK_WHILE:
case TOK_IF: case TOK_ELSIF:
// Expect a paren and then a curly
if(!t->next || t->next->type != TOK_LPAR) oops(p, t);
|| t->prev->type == TOK_LCURL)
addSemi = 1;
break;
- case TOK_FOR: case TOK_FOREACH: case TOK_WHILE:
+ case TOK_FOR: case TOK_FOREACH: case TOK_FORINDEX: case TOK_WHILE:
addSemi = 1;
break;
case TOK_FUNC:
addSemi = 1;
break;
}
+ if(t->next && t->next->type == TOK_SEMI)
+ addSemi = 0; // don't bother if it's already there!
if(addSemi) {
struct Token* semi = emptyToken(p);
semi->type = TOK_SEMI;
{
return t == TOK_IF || t == TOK_ELSIF || t == TOK_ELSE
|| t == TOK_FOR || t == TOK_FOREACH || t == TOK_WHILE
- || t == TOK_FUNC;
+ || t == TOK_FUNC || t == TOK_FORINDEX;
}
static void precChildren(struct Parser* p, struct Token* t);
struct Parser p;
// Protect from garbage collection
- naVec_append(c->temps, srcFile);
+ naTempSave(c, srcFile);
// Catch parser errors here.
*errLine = 0;
p.tree.lastChild = t;
// Generate code!
- codeObj = naCodeGen(&p, &(p.tree));
+ codeObj = naCodeGen(&p, &(p.tree), 0);
// Clean up our mess
naParseDestroy(&p);
- naVec_append(c->temps, codeObj);
+ naTempSave(c, codeObj);
return codeObj;
}