#include <sclpl.h>
-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;
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;
}
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;
}
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;
}
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;
}
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;
}
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)
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)
void ifexpr_set_else(AST* ifexpr, AST* belse)
{
- ifexpr->value.ifexpr.belse = (AST*)gc_addref(belse);
+ ifexpr->value.ifexpr.belse = belse;
}
AST* Func(void)
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;
}
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)
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;
}
void let_set_body(AST* let, AST* body)
{
- let->value.let.body = (AST*)gc_addref(body);
+ let->value.let.body = body;
}
AST* TempVar(void)
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;
%%
-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));
}
*****************************************************************************/
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;
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);
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);
{
Tok* tok = peek(parser);
if (tok->type == type) {
- gc_swapref((void**)&(parser->tok), NULL);
+ parser->tok = NULL;
return tok;
}
return NULL;
static Tok* expect(Parser* parser, TokType type)
{
Tok* tok = accept(parser, type);
- if (tok == NULL) {
+ if (tok == NULL)
error(parser, "Unexpected token");
- }
return tok;
}
#include <assert.h>
#include <setjmp.h>
-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 {