From e9f799c191bb5b6013634cd8b85a35c30ea60fd2 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Sat, 19 May 2018 15:43:15 -0400 Subject: [PATCH] removed remnants of GC --- Makefile | 1 - source/ast.c | 82 ++++++++++++------------------------------------- source/lexer.l | 15 ++------- source/parser.c | 21 +++---------- source/sclpl.h | 25 +-------------- source/vec.c | 6 ++-- 6 files changed, 28 insertions(+), 122 deletions(-) diff --git a/Makefile b/Makefile index 92902d7..1924618 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,6 @@ ARFLAGS = rcs #------------------------------------------------------------------------------ BIN = sclpl OBJS = source/main.o \ - source/gc.o \ source/vec.o \ source/pprint.o \ source/parser.o \ diff --git a/source/ast.c b/source/ast.c index 7f2100f..4917d2c 100644 --- a/source/ast.c +++ b/source/ast.c @@ -1,51 +1,8 @@ #include -static void ast_free(void* ptr) -{ - AST* ast = (AST*)ptr; - switch(ast->type) { - case AST_REQ: - case AST_IDENT: - case AST_STRING: - case AST_SYMBOL: - gc_delref(ast->value.text); - break; - - case AST_DEF: - gc_delref(ast->value.def.name); - gc_delref(ast->value.def.value); - break; - - case AST_IF: - gc_delref(ast->value.ifexpr.cond); - gc_delref(ast->value.ifexpr.bthen); - gc_delref(ast->value.ifexpr.belse); - break; - - case AST_FUNC: - vec_deinit(&(ast->value.func.args)); - gc_delref(ast->value.func.body); - break; - - case AST_FNAPP: - gc_delref(ast->value.fnapp.fn); - vec_deinit(&(ast->value.fnapp.args)); - break; - - case AST_LET: - break; - - case AST_TEMP: - break; - - default: - break; - } -} - static AST* ast(ASTType type) { - AST* tree = gc_alloc(sizeof(AST), &ast_free); + AST* tree = emalloc(sizeof(AST)); memset(tree, 0, sizeof(AST)); tree->type = type; return tree; @@ -54,7 +11,7 @@ static AST* ast(ASTType type) AST* String(Tok* val) { AST* node = ast(AST_STRING); - node->value.text = (char*)gc_addref(val->value.text); + node->value.text = val->value.text; return node; } @@ -68,7 +25,7 @@ char* string_value(AST* val) AST* Symbol(Tok* val) { AST* node = ast(AST_SYMBOL); - node->value.text = (char*)gc_addref(val->value.text); + node->value.text = val->value.text; return node; } @@ -145,7 +102,7 @@ bool bool_value(AST* val) AST* Ident(Tok* val) { AST* node = ast(AST_IDENT); - node->value.text = (char*)gc_addref(val->value.text); + node->value.text = val->value.text; return node; } @@ -159,7 +116,7 @@ char* ident_value(AST* val) AST* Require(Tok* name) { AST* node = ast(AST_REQ); - node->value.text = (char*)gc_addref(name->value.text); + node->value.text = name->value.text; return node; } @@ -173,8 +130,8 @@ char* require_name(AST* req) AST* Def(Tok* name, AST* value) { AST* node = ast(AST_DEF); - node->value.def.name = (char*)gc_addref(name->value.text); - node->value.def.value = (AST*)gc_addref(value); + node->value.def.name = name->value.text; + node->value.def.value = value; return node; } @@ -204,7 +161,7 @@ AST* ifexpr_cond(AST* ifexpr) void ifexpr_set_cond(AST* ifexpr, AST* cond) { - ifexpr->value.ifexpr.cond = (AST*)gc_addref(cond); + ifexpr->value.ifexpr.cond = cond; } AST* ifexpr_then(AST* ifexpr) @@ -214,7 +171,7 @@ AST* ifexpr_then(AST* ifexpr) void ifexpr_set_then(AST* ifexpr, AST* bthen) { - ifexpr->value.ifexpr.bthen = (AST*)gc_addref(bthen); + ifexpr->value.ifexpr.bthen = bthen; } AST* ifexpr_else(AST* ifexpr) @@ -224,7 +181,7 @@ AST* ifexpr_else(AST* ifexpr) void ifexpr_set_else(AST* ifexpr, AST* belse) { - ifexpr->value.ifexpr.belse = (AST*)gc_addref(belse); + ifexpr->value.ifexpr.belse = belse; } AST* Func(void) @@ -247,18 +204,18 @@ AST* func_body(AST* func) void func_add_arg(AST* func, AST* arg) { - vec_push_back(func_args(func), gc_addref(arg)); + vec_push_back(func_args(func), arg); } void func_set_body(AST* func, AST* body) { - func->value.func.body = (AST*)gc_addref(body); + func->value.func.body = body; } AST* FnApp(AST* fnapp) { AST* node = ast(AST_FNAPP); - node->value.fnapp.fn = (AST*)gc_addref(fnapp); + node->value.fnapp.fn = fnapp; vec_init(&(node->value.fnapp.args)); return node; } @@ -266,8 +223,7 @@ AST* FnApp(AST* fnapp) void fnapp_set_fn(AST* fnapp, AST* fn) { AST* old = fnapp->value.fnapp.fn; - fnapp->value.fnapp.fn = (AST*)gc_addref(fn); - gc_delref(old); + fnapp->value.fnapp.fn = fn; } AST* fnapp_fn(AST* fnapp) @@ -282,15 +238,15 @@ vec_t* fnapp_args(AST* fnapp) void fnapp_add_arg(AST* fnapp, AST* arg) { - vec_push_back(&(fnapp->value.fnapp.args), gc_addref(arg)); + vec_push_back(&(fnapp->value.fnapp.args), arg); } AST* Let(AST* temp, AST* val, AST* body) { AST* node = ast(AST_LET); - node->value.let.temp = (AST*)gc_addref(temp); - node->value.let.value = (AST*)gc_addref(val); - node->value.let.body = (AST*)gc_addref(body); + node->value.let.temp = temp; + node->value.let.value = val; + node->value.let.body = body; return node; } @@ -311,7 +267,7 @@ AST* let_body(AST* let) void let_set_body(AST* let, AST* body) { - let->value.let.body = (AST*)gc_addref(body); + let->value.let.body = body; } AST* TempVar(void) diff --git a/source/lexer.l b/source/lexer.l index a076a4c..d590817 100644 --- a/source/lexer.l +++ b/source/lexer.l @@ -11,7 +11,7 @@ static union { static char* dupstring(const char* old) { size_t length = strlen(old); - char* str = (char*)gc_alloc(length+1, NULL); + char* str = emalloc(length+1); memcpy(str, old, length); str[length] = '\0'; return str; @@ -124,23 +124,12 @@ false { %% -static void token_free(void* obj) -{ - Tok* tok = (Tok*)obj; - if ((tok->type != T_BOOL) && - (tok->type != T_CHAR) && - (tok->type != T_INT) && - (tok->type != T_FLOAT) && - (NULL != tok->value.text)) - gc_delref(tok->value.text); -} - Tok* gettoken(Parser* ctx) { Tok* tok = NULL; int type = yylex(); if (type != T_END_FILE) { - tok = (Tok*)gc_alloc(sizeof(Tok), &token_free); + tok = emalloc(sizeof(Tok)); tok->type = type; memcpy(&(tok->value), &Value, sizeof(Value)); } diff --git a/source/parser.c b/source/parser.c index 4a98936..dc930d8 100644 --- a/source/parser.c +++ b/source/parser.c @@ -209,7 +209,7 @@ static void type_annotation(Parser* p) *****************************************************************************/ Parser* parser_new(char* prompt, FILE* input) { - Parser* parser = (Parser*)gc_alloc(sizeof(Parser), &parser_free); + Parser* parser = emalloc(sizeof(Parser)); parser->line = NULL; parser->index = 0; parser->lineno = 0; @@ -219,16 +219,6 @@ Parser* parser_new(char* prompt, FILE* input) return parser; } -static void parser_free(void* obj) -{ - Parser* parser = (Parser*)obj; - if ((NULL != parser->tok) && (&tok_eof != parser->tok)) { - gc_delref(parser->tok); - } - if (parser->line != NULL) - free(parser->line); -} - static void fetch(Parser* parser) { parser->tok = gettoken(parser); @@ -250,10 +240,8 @@ static bool parser_eof(Parser* parser) static void parser_resume(Parser* parser) { - if ((NULL != parser->tok) && (&tok_eof != parser->tok)) { - gc_delref(parser->tok); + if ((NULL != parser->tok) && (&tok_eof != parser->tok)) parser->tok = NULL; - } /* We ignore the rest of the current line and attempt to start parsing * again on the next line */ fetchline(parser); @@ -275,7 +263,7 @@ static Tok* accept(Parser* parser, TokType type) { Tok* tok = peek(parser); if (tok->type == type) { - gc_swapref((void**)&(parser->tok), NULL); + parser->tok = NULL; return tok; } return NULL; @@ -284,9 +272,8 @@ static Tok* accept(Parser* parser, TokType type) static Tok* expect(Parser* parser, TokType type) { Tok* tok = accept(parser, type); - if (tok == NULL) { + if (tok == NULL) error(parser, "Unexpected token"); - } return tok; } diff --git a/source/sclpl.h b/source/sclpl.h index a2b20cc..7edd081 100644 --- a/source/sclpl.h +++ b/source/sclpl.h @@ -9,40 +9,17 @@ #include #include -typedef void (*destructor_t)(void*); - static void fatal(char* estr) { perror(estr); exit(1); } -static void* gc_alloc(size_t size, destructor_t destructor) -{ +static void* emalloc(size_t size) { void* ptr = malloc(size); - //fprintf(stderr, "%d\n", size); if (!ptr) fatal("malloc()"); return ptr; } -static void* gc_addref(void* ptr) -{ - return ptr; -} - -static void gc_delref(void* ptr) -{ -} - -static void gc_swapref(void** dest, void* newref) -{ - void* oldref = *dest; - *dest = gc_addref(newref); - gc_delref(oldref); -} - -/* Garbage Collection - *****************************************************************************/ - /* Vector Implementation *****************************************************************************/ typedef struct { diff --git a/source/vec.c b/source/vec.c index 8881267..ac545ef 100644 --- a/source/vec.c +++ b/source/vec.c @@ -59,7 +59,7 @@ static void vec_resize(vec_t* vec, size_t count, void* fillval) if (count > vec->count) { vec_reserve(vec, vec_next_capacity(count+1)); for (; vec->count < count; vec->count++) - vec->buffer[vec->count] = gc_addref(fillval); + vec->buffer[vec->count] = fillval; } else if (count < vec->count) { vec->count = count; } @@ -73,13 +73,11 @@ void vec_push_back(vec_t* vec, void* data) void vec_set(vec_t* vec, size_t index, void* data) { assert(index < vec->count); - gc_swapref((void**)&(vec->buffer[index]), data); + vec->buffer[index] = data; } void vec_clear(vec_t* vec) { - for (size_t i = 0; i < vec->count; i++) - gc_delref(vec->buffer[i]); vec->count = 0; } -- 2.54.0