diff options
author | ubq323 <ubq323@ubq323.website> | 2023-07-11 00:36:32 +0100 |
---|---|---|
committer | ubq323 <ubq323@ubq323.website> | 2023-07-11 00:36:32 +0100 |
commit | 1e19e4c6e052909bf76a30b26f63dcf32576e994 (patch) | |
tree | 3632bd275f0735636aec5a8b9cf5f85796719b8a /grammar.peg | |
parent | 86c9a7e5c47c5c3eb70e06ea5e278de8022fd5b2 (diff) |
parsing sexprs
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; } |