diff options
Diffstat (limited to 'grammar.peg')
-rw-r--r-- | grammar.peg | 30 |
1 files changed, 19 insertions, 11 deletions
diff --git a/grammar.peg b/grammar.peg index bbb5baa..9b8e882 100644 --- a/grammar.peg +++ b/grammar.peg @@ -2,6 +2,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> + } %common { @@ -10,13 +11,21 @@ %value "AstNode" -numberlist <- { $$ = astnode_new_list(); } - ( n:number _ { astnode_append(&$$, n); } ) + - EOL +expr <- + l:list { $$ = l; } + / n:number { $$ = n; } + / s:symbol { $$ = s; } + +list <- { $$ = astnode_new_list(); } + '(' + ( e:expr { astnode_append(&$$, e); } + )* + ')' _ + +number <- < [0-9]+ > _ { $$ = astnode_new_num(atoi($1)); } +symbol <- < [a-z]+ > _ { $$ = astnode_new_symbol(strdup($1)); } -number <- < [0-9]+ > { $$ = astnode_new_num(atoi($1)); } -EOL <- '\n' / '\r\n' / '\r' _ <- [ \t]* %% @@ -25,12 +34,11 @@ int main() { pcc_context_t *ctx = pcc_create(NULL); AstNode ret; memset(&ret, 0, sizeof ret); - while (pcc_parse(ctx, &ret)) ; - AstVec *v = &ret.as.list; - printf("%d: %ld %ld\n",ret.ty, v->len,v->cap); - for (int i = 0; i < v->len; i++) { - printf(" %d:[%d]\n", v->vals[i].ty, v->vals[i].as.num); - } + pcc_parse(ctx, &ret); + + astnode_disp(&ret); + putchar('\n'); + pcc_destroy(ctx); return 0; } |