From 1e19e4c6e052909bf76a30b26f63dcf32576e994 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Tue, 11 Jul 2023 00:36:32 +0100 Subject: parsing sexprs --- grammar.peg | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) (limited to 'grammar.peg') diff --git a/grammar.peg b/grammar.peg index bbb5baa..9b8e882 100644 --- a/grammar.peg +++ b/grammar.peg @@ -2,6 +2,7 @@ #include #include #include + } %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; } -- cgit v1.2.3