diff options
-rw-r--r-- | Makefile | 11 | ||||
-rw-r--r-- | ast.c | 127 | ||||
-rw-r--r-- | ast.h | 51 | ||||
-rw-r--r-- | com.c | 7 | ||||
-rw-r--r-- | g.peg | 52 |
5 files changed, 3 insertions, 245 deletions
@@ -1,19 +1,14 @@ -CS=ast.c com.c dis.c ht.c lib.c mem.c prs.c read.c state.c val.c vm.c -HS=ast.h chunk.h com.h dis.h ht.h lib.h mem.h prs.h read.h state.h util.h val.h vm.h +CS= com.c dis.c ht.c lib.c mem.c read.c state.c val.c vm.c +HS=chunk.h com.h dis.h ht.h lib.h mem.h read.h state.h util.h val.h vm.h CFLAGS=$(EXTRA_CFLAGS) -g -lm -Wall -Wpedantic -Werror=implicit-function-declaration bth: $(CS) $(HS) Makefile $(CC) $(CFLAGS) -o bth $(CS) -prs.c: g.peg - packcc -o prs g.peg -prs.h: prs.c - - test: bth ./run_tests.sh clean: - rm bth prs.c prs.h + rm bth .PHONY: clean test @@ -1,127 +0,0 @@ -#include "ast.h" -#include <stdlib.h> -#include <stddef.h> -#include <stdio.h> -#include <assert.h> -#include <string.h> - -AstVec astvec_new() { - AstNode *vals = malloc(2 * sizeof(AstNode)); - return (AstVec){ - .len = 0, - .cap = 2, - vals = vals - }; -} - -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); - v->vals = realloc(v->vals, newcap * sizeof(AstNode)); - v->cap = newcap; - } - v->vals[v->len] = val; - v->len ++; -} - -void astnode_append(AstNode *l, AstNode val) { - // printf(" astnode_append: %d\n",l->ty); - assert(l->ty == AST_LIST || l->ty == AST_ARR); - astvec_append(&l->as.list, val); -} - -AstNode astnode_new_num(int n) { - return (AstNode){ - .ty = AST_NUM, - .as = { - .num = n, - }, - }; -} - -AstNode astnode_new_list() { - return (AstNode){ - .ty = AST_LIST, - .as = { - .list = astvec_new() - } - }; -} -AstNode astnode_new_arr() { - return (AstNode){ - .ty = AST_ARR, - .as = { - .list = astvec_new() - } - }; -} - -AstNode astnode_new_ident(const char *s) { - return (AstNode){ - .ty =AST_IDENT, - .as = { - .str = strdup(s) - } - }; -} - -AstNode astnode_new_string(const char *s) { - return (AstNode){ - .ty =AST_STRING, - .as = { - .str = strdup(s) - } - }; -} - -void astnode_free(AstNode *a) { - switch (a->ty) { - case AST_NUM: break; - case AST_IDENT: - case AST_STRING: - free(a->as.str); - break; - case AST_LIST: - case AST_ARR:; - AstVec *v = &a->as.list; - for (int i = 0; i < v->len; i++) { - astnode_free(&v->vals[i]); - } - free(v->vals); - v->vals = NULL; - v->len = 0; - v->cap = 0; - break; - } -} - -void astnode_disp(AstNode *a) { - switch (a->ty) { - case AST_NUM: - printf("%s:%d ",ast_ty_to_str(a->ty),a->as.num); - break; - case AST_LIST: - case AST_ARR:; - AstVec *v = &a->as.list; - printf("%s:(",ast_ty_to_str(a->ty)); - for (int i = 0; i < v->len; i++) { - astnode_disp(&v->vals[i]); - } - printf(")"); - break; - case AST_IDENT: - case AST_STRING: - printf("%s:%s ",ast_ty_to_str(a->ty), a->as.str); - break; - - } -} - -const char* ty_names[] = { - "list", "num", "ident", "string", "arr", -}; -const char *ast_ty_to_str(AstTy ty) { - return ty_names[ty]; -} - @@ -1,51 +0,0 @@ -#ifndef _ast_h -#define _ast_h - -#include <stdlib.h> -#include <stddef.h> - -typedef enum { - AST_LIST, - AST_NUM, - AST_IDENT, - AST_STRING, - AST_ARR, -} AstTy; - -struct _astnode; -typedef struct _astnode AstNode; - -typedef struct { - size_t len; - size_t cap; - AstNode *vals; -} AstVec; - -struct _astnode { - AstTy ty; - union { - int num; - AstVec list; - char *str; - } as; -}; - - -AstVec astvec_new(); -void astvec_append(AstVec *v, AstNode val); -void astnode_append(AstNode *l, AstNode val); - -AstNode astnode_new_num(int n); -AstNode astnode_new_list(); -AstNode astnode_new_arr(); -AstNode astnode_new_ident(const char *s); -AstNode astnode_new_string(const char *s); - -void astnode_disp(AstNode *a); -void astnode_free(AstNode *a); - -const char *ast_ty_to_str(AstTy ty); - - - -#endif @@ -6,7 +6,6 @@ #include "com.h" #include "mem.h" #include "chunk.h" -#include "ast.h" #include "util.h" #include "prs.h" #include "lib.h" @@ -697,7 +696,6 @@ int main(int argc, char **argv) { exit(1); } - fread(buf, 1, 8192, infile); buf[8192] = '\0'; ObjArr *top = read_exprs(S, buf); @@ -706,11 +704,6 @@ int main(int argc, char **argv) { println_val(VAL_OBJ(top)); cpl_body(&com, top, 0, 0); - - - // pcc_destroy(parser); - // compile_body(&com, top.as.list, 0, 0); - // astnode_free(&top); } @@ -1,52 +0,0 @@ -%source { -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - - -#ifdef DO_PARSE_DEBUG -static const char *dbg_str[] = { "Evaluating rule", "Matched rule", "Abandoning rule" }; -#define PCC_DEBUG(auxil, event, rule, level, pos, buffer, length) \ - fprintf(stderr, "%*s%s %s @%zu [%.*s]\n", (int)((level) * 2), "", dbg_str[event], rule, pos, (int)(length), buffer) -#endif - -#define PCC_GETCHAR(auxil) fgetc(auxil) - -} - - - -%common { -#include "ast.h" - -} - -%value "AstNode" -%auxil "FILE *" - -expr <- - l:list { $$ = l; } - / n:number { $$ = n; } - / i:ident { $$ = i; } - / t:string { $$ = t; } - / a:array { $$ = a; } - -list <- { $$ = astnode_new_list(); } - '(' _ - ( e:expr { astnode_append(&$$, e); } - )* - ')' _ -array <- { $$ = astnode_new_arr(); } - '[' _ - ( e:expr { astnode_append(&$$, e); } - )* - ']' _ - -number <- < [0-9]+ > (! ident_char) _ { $$ = astnode_new_num(atoi($1)); } -ident <- < ident_char+ > _ { $$ = astnode_new_ident($1); } -string <- '"' < [^"]* > '"' _ { $$ = astnode_new_string($1); } - -ident_char <- [-_a-zA-Z'+*/\\%=0-9<>!,] -_ <- [ \t\n]* - - |