From 1e19e4c6e052909bf76a30b26f63dcf32576e994 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Tue, 11 Jul 2023 00:36:32 +0100 Subject: parsing sexprs --- Makefile | 2 +- ast.c | 32 ++++++++++++++++++++++++++++++-- ast.h | 5 ++++- grammar.peg | 30 +++++++++++++++++++----------- 4 files changed, 54 insertions(+), 15 deletions(-) diff --git a/Makefile b/Makefile index 012f152..1e6f6ef 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CS=parser.c ast.c HS=ast.h parser.h -CFLAGS=-Wall -Wpedantic -std=c99 +CFLAGS=-Wall -Wpedantic badthing: $(CS) $(HS) $(CC) $(CFLAGS) -o badthing $(CS) diff --git a/ast.c b/ast.c index 94c0a66..7474d45 100644 --- a/ast.c +++ b/ast.c @@ -16,7 +16,7 @@ AstVec astvec_new() { void astvec_append(AstVec *v, AstNode val) { if (v->len == v->cap) { size_t newcap = v->cap * 2; - printf("%ld to %ld\n", v->cap, newcap); + // printf("%ld to %ld\n", v->cap, newcap); v->vals = realloc(v->vals, newcap * sizeof(AstNode)); v->cap = newcap; } @@ -25,7 +25,7 @@ void astvec_append(AstVec *v, AstNode val) { } void astnode_append(AstNode *l, AstNode val) { - printf(" astnode_append: %d\n",l->ty); + // printf(" astnode_append: %d\n",l->ty); assert(l->ty == AST_LIST); astvec_append(&l->as.list, val); } @@ -48,3 +48,31 @@ AstNode astnode_new_list() { }; } +AstNode astnode_new_symbol(char *s) { + return (AstNode){ + .ty =AST_SYMBOL, + .as = { + .str = s + } + }; +} + +void astnode_disp(AstNode *a) { + switch (a->ty) { + case AST_NUM: + printf("n:%d ",a->as.num); + break; + case AST_LIST:; + AstVec *v = &a->as.list; + printf("l:("); + for (int i = 0; i < v->len; i++) { + astnode_disp(&v->vals[i]); + } + printf(")"); + break; + case AST_SYMBOL:; + printf("s:%s ",a->as.str); + break; + } + +} diff --git a/ast.h b/ast.h index f352992..3647a7b 100644 --- a/ast.h +++ b/ast.h @@ -5,9 +5,9 @@ #include typedef enum { - AST_NOTHING, AST_LIST, AST_NUM, + AST_SYMBOL, } AstTy; struct _astnode; @@ -24,6 +24,7 @@ struct _astnode { union { int num; AstVec list; + char *str; } as; }; @@ -34,7 +35,9 @@ void astnode_append(AstNode *l, AstNode val); AstNode astnode_new_num(int n); AstNode astnode_new_list(); +AstNode astnode_new_symbol(char *s); +void astnode_disp(AstNode *a); 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