diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | ast.c | 32 | ||||
-rw-r--r-- | ast.h | 5 | ||||
-rw-r--r-- | grammar.peg | 30 |
4 files changed, 54 insertions, 15 deletions
@@ -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) @@ -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; + } + +} @@ -5,9 +5,9 @@ #include <stddef.h> 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 <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; } |