From 931b2378f550e7d068d4955c5f0b2a27313e86c8 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Mon, 12 Oct 2015 22:29:26 -0400 Subject: [PATCH] Implemented basic value definitions --- source/ast.c | 256 ++++++++++++++++++++++------------------------- source/gc.c | 4 +- source/grammar.c | 72 ++++++------- source/lexer.c | 8 +- source/parser.c | 40 ++++---- source/pprint.c | 4 + source/sclpl.h | 35 ++++--- 7 files changed, 199 insertions(+), 220 deletions(-) diff --git a/source/ast.c b/source/ast.c index aa28904..336a1a1 100644 --- a/source/ast.c +++ b/source/ast.c @@ -12,235 +12,223 @@ static AST* ast(ASTType type) return tree; } -AST* String(char* val) +AST* String(Tok* val) { AST* node = ast(AST_STRING); - node->value.text = val; + node->value.text = (char*)gc_addref(val->value.text); return node; } char* string_value(AST* val) { + assert(val != NULL); assert(val->type == AST_STRING); return val->value.text; } -AST* Symbol(char* val) +AST* Symbol(Tok* val) { AST* node = ast(AST_SYMBOL); - node->value.text = val; + node->value.text = (char*)gc_addref(val->value.text); return node; } char* symbol_value(AST* val) { + assert(val != NULL); assert(val->type == AST_SYMBOL); return val->value.text; } -AST* Char(uint32_t val) +AST* Char(Tok* val) { AST* node = ast(AST_CHAR); - node->value.character = val; + node->value.character = val->value.character; return node; } uint32_t char_value(AST* val) { + assert(val != NULL); assert(val->type == AST_CHAR); return val->value.character; } -AST* Integer(intptr_t val) +AST* Integer(Tok* val) { AST* node = ast(AST_INT); - node->value.integer = val; + node->value.integer = val->value.integer; return node; } intptr_t integer_value(AST* val) { + assert(val != NULL); assert(val->type == AST_INT); return val->value.integer; } -AST* Float(double val) +AST* Float(Tok* val) { AST* node = ast(AST_FLOAT); - node->value.floating = val; + node->value.floating = val->value.floating; return node; } double float_value(AST* val) { + assert(val != NULL); assert(val->type == AST_FLOAT); return val->value.floating; } -AST* Bool(bool val) +AST* Bool(Tok* val) { AST* node = ast(AST_BOOL); - node->value.boolean = val; + node->value.boolean = val->value.boolean; return node; } bool bool_value(AST* val) { + assert(val != NULL); assert(val->type == AST_BOOL); return val->value.boolean; } -AST* Ident(char* val) +AST* Ident(Tok* val) { AST* node = ast(AST_IDENT); - node->value.text = val; + node->value.text = (char*)gc_addref(val->value.text); return node; } char* ident_value(AST* val) { + assert(val != NULL); assert(val->type == AST_IDENT); return val->value.text; } -AST* Require(char* name) +AST* Require(Tok* name) { AST* node = ast(AST_REQ); - node->value.text = name; + node->value.text = (char*)gc_addref(name->value.text); return node; } char* require_name(AST* req) { + assert(req != NULL); assert(req->type == AST_REQ); return req->value.text; } - - - - - - - - - - - - - - - - - - - - - - - -AST* Def(char* name, AST* value) +AST* Def(Tok* name, AST* value) { - (void)name; - (void)value; - return NULL; + AST* node = ast(AST_DEF); + node->value.def.name = (char*)gc_addref(name->value.text); + node->value.def.value = (AST*)gc_addref(value); + return node; } char* def_name(AST* def) { - (void)def; - return NULL; + assert(def != NULL); + assert(def->type == AST_DEF); + return def->value.def.name; } AST* def_value(AST* def) { - (void)def; - return NULL; -} - -AST* Ann(char* name, AST* value) -{ - (void)name; - (void)value; - return NULL; -} - -char* ann_name(AST* ann) -{ - (void)ann; - return NULL; -} - -AST* ann_value(AST* ann) -{ - (void)ann; - return NULL; -} - -AST* IfExpr(AST* cond, AST* bthen, AST* belse) -{ - (void)cond; - (void)bthen; - (void)belse; - return NULL; -} - -AST* ifexpr_condition(AST* ifexpr) -{ - (void)ifexpr; - return NULL; -} - -AST* ifexpr_branch_then(AST* ifexpr) -{ - (void)ifexpr; - return NULL; -} - -AST* ifexpr_branch_else(AST* ifexpr) -{ - (void)ifexpr; - return NULL; -} - -AST* Func(AST* args, AST* body) -{ - (void)args; - (void)body; - return NULL; -} - -AST* func_args(AST* func) -{ - (void)func; - return NULL; -} - -AST* func_body(AST* func) -{ - (void)func; - return NULL; -} - -AST* Block(void) -{ - return NULL; -} - -void block_append(AST* expr) -{ - (void)expr; -} - -size_t block_size(AST* block) -{ - (void)block; - return 0; -} - -AST* block_get(size_t index) -{ - (void)index; - return NULL; -} + assert(def != NULL); + assert(def->type == AST_DEF); + return def->value.def.value; +} + +//AST* Ann(char* name, AST* value) +//{ +// (void)name; +// (void)value; +// return NULL; +//} +// +//char* ann_name(AST* ann) +//{ +// (void)ann; +// return NULL; +//} +// +//AST* ann_value(AST* ann) +//{ +// (void)ann; +// return NULL; +//} +// +//AST* IfExpr(AST* cond, AST* bthen, AST* belse) +//{ +// (void)cond; +// (void)bthen; +// (void)belse; +// return NULL; +//} +// +//AST* ifexpr_condition(AST* ifexpr) +//{ +// (void)ifexpr; +// return NULL; +//} +// +//AST* ifexpr_branch_then(AST* ifexpr) +//{ +// (void)ifexpr; +// return NULL; +//} +// +//AST* ifexpr_branch_else(AST* ifexpr) +//{ +// (void)ifexpr; +// return NULL; +//} +// +//AST* Func(AST* args, AST* body) +//{ +// (void)args; +// (void)body; +// return NULL; +//} +// +//AST* func_args(AST* func) +//{ +// (void)func; +// return NULL; +//} +// +//AST* func_body(AST* func) +//{ +// (void)func; +// return NULL; +//} +// +//AST* Block(void) +//{ +// return NULL; +//} +// +//void block_append(AST* expr) +//{ +// (void)expr; +//} +// +//size_t block_size(AST* block) +//{ +// (void)block; +// return 0; +//} +// +//AST* block_get(size_t index) +//{ +// (void)index; +// return NULL; +//} diff --git a/source/gc.c b/source/gc.c index 0e1ebbc..6a8eec2 100644 --- a/source/gc.c +++ b/source/gc.c @@ -249,7 +249,7 @@ void* gc_addref(void* ptr) if (ptr != NULL) { obj->refs++; if (obj->refs == 1) { - lookup.object = obj; + lookup.object = ptr; entry = hash_del(&Zero_Count_Table, &lookup); assert(entry != NULL); hash_set(&Multi_Ref_Table, entry); @@ -266,7 +266,7 @@ void gc_delref(void* ptr) if (ptr != NULL) { obj->refs--; if (obj->refs == 0) { - lookup.object = obj; + lookup.object = ptr; entry = hash_del(&Multi_Ref_Table, &lookup); assert(entry != NULL); hash_set(&Zero_Count_Table, entry); diff --git a/source/grammar.c b/source/grammar.c index f79e934..b9c0326 100644 --- a/source/grammar.c +++ b/source/grammar.c @@ -7,9 +7,9 @@ #include static AST* require(Parser* p); +static AST* definition(Parser* p); static AST* expression(Parser* p); static AST* literal(Parser* p); -static AST* expect_lit(Parser* p, TokType); static AST* token_to_tree(Tok* tok); AST* toplevel(Parser* p) @@ -18,6 +18,8 @@ AST* toplevel(Parser* p) if (peek(p)->type != T_END_FILE) { if (accept_str(p, T_ID, "require")) ret = require(p); + else if (accept_str(p, T_ID, "def")) + ret = definition(p); else ret = expression(p); } @@ -33,10 +35,30 @@ AST* toplevel(Parser* p) return ret; } +static AST* definition(Parser* p) +{ + Tok* id = expect(p, T_ID);//expect_lit(p, T_ID); + AST* expr; + //if (peek(p)->type == T_LPAR) + // expr = function(p); + //else + expr = expression(p); + expect(p, T_END); + return Def(id, expr); +} + +static AST* require(Parser* p) +{ + Tok* tok = expect(p, T_STRING); + AST* ast = Require(tok); + expect(p, T_END); + return ast; +} + static AST* expression(Parser* p) { if (peek(p)->type == T_ID) { - return expect_lit(p, T_ID); + return Ident(expect(p,T_ID)); //expect_lit(p, T_ID); //if (peek(p)->type == T_LPAR) { // arglist(p); //} @@ -63,15 +85,6 @@ static AST* expression(Parser* p) //} } -static AST* require(Parser* p) -{ - AST* ret = expect_lit(p, T_STRING); - if (ret != NULL) - ret = Require(string_value(ret)); - expect(p, T_END); - return ret; -} - static AST* literal(Parser* p) { AST* ret = NULL; @@ -82,8 +95,7 @@ static AST* literal(Parser* p) case T_STRING: case T_INT: case T_FLOAT: - ret = token_to_tree(tok); - expect(p, tok->type); + ret = token_to_tree(expect(p, tok->type)); break; default: error(p, "Expected a literal"); @@ -94,23 +106,16 @@ static AST* literal(Parser* p) static AST* token_to_tree(Tok* tok) { switch (tok->type) { - case T_BOOL: return Bool(tok->value.boolean); - case T_CHAR: return Char(tok->value.character); - case T_STRING: return String(tok->value.text); - case T_INT: return Integer(tok->value.integer); - case T_FLOAT: return Float(tok->value.floating); - case T_ID: return Ident(tok->value.text); + case T_BOOL: return Bool(tok); + case T_CHAR: return Char(tok); + case T_STRING: return String(tok); + case T_INT: return Integer(tok); + case T_FLOAT: return Float(tok); + case T_ID: return Ident(tok); default: return NULL; } } -static AST* expect_lit(Parser* p, TokType type) -{ - Tok* tok = peek(p); - expect(p, type); - return token_to_tree(tok); -} - @@ -185,21 +190,6 @@ static AST* function(Parser* p) { return NULL; } -static AST* definition(Parser* p) -{ - ////size_t mrk = mark(p); - //expect(p,T_ID); - //if (peek(p)->type == T_LPAR) { - // //insert(p, T_ID, lexer_dup("fn")); - // fn_stmnt(p); - //} else { - // expression(p); - // expect(p,T_END); - //} - ////reduce(p, mrk); - return NULL; -} - static AST* arglist(Parser* p) { ////size_t mrk = mark(p); diff --git a/source/lexer.c b/source/lexer.c index 7dd08c2..127feda 100644 --- a/source/lexer.c +++ b/source/lexer.c @@ -37,7 +37,7 @@ static Tok* Token(TokType type) static Tok* TextTok(TokType type, char* text) { Tok* tok = Token(type); - tok->value.text = dupstring(text); + tok->value.text = (char*)gc_addref(dupstring(text)); return tok; } @@ -349,7 +349,7 @@ static Tok* classify(const char* file, size_t line, size_t col, char* text) tok = punctuation(text); } else if ('"' == text[0]) { text[strlen(text)-1] = '\0'; - tok = TextTok(T_STRING, dupstring(&text[1])); + tok = TextTok(T_STRING, &text[1]); } else if (text[0] == '\\') { tok = character(text); } else if ((text[0] == '0') && char_oneof("bodh",text[1])) { @@ -374,8 +374,10 @@ Tok* gettoken(Parser* ctx) Tok* tok = NULL; size_t line, col; char* text = scan(ctx, &line, &col); - if (text != NULL) + if (text != NULL) { tok = classify(NULL, line, col, text); + free(text); + } return tok; } diff --git a/source/parser.c b/source/parser.c index 374948c..2f4572c 100644 --- a/source/parser.c +++ b/source/parser.c @@ -66,45 +66,41 @@ void error(Parser* parser, const char* text) exit(1); } -bool accept(Parser* parser, TokType type) +Tok* accept(Parser* parser, TokType type) { - bool ret = false; - if (peek(parser)->type == type) { + Tok* tok = peek(parser); + if (tok->type == type) { gc_swapref((void**)&(parser->tok), NULL); - ret = true; + return tok; } - return ret; + return NULL; } -bool accept_str(Parser* parser, TokType type, const char* text) +Tok* accept_str(Parser* parser, TokType type, const char* text) { - bool ret = false; - if ((peek(parser)->type == type) && (0 == strcmp((char*)(parser->tok->value.text), text))) { + Tok* tok = peek(parser); + if ((tok->type == type) && (0 == strcmp((char*)(tok->value.text), text))) { gc_swapref((void**)&(parser->tok), NULL); - ret = true; + return tok; } - return ret; + return NULL; } -bool expect(Parser* parser, TokType type) +Tok* expect(Parser* parser, TokType type) { - bool ret = false; - if (accept(parser, type)) { - ret = true; - } else { + Tok* tok = accept(parser, type); + if (tok == NULL) { error(parser, "Unexpected token"); } - return ret; + return tok; } -bool expect_str(Parser* parser, TokType type, const char* text) +Tok* expect_str(Parser* parser, TokType type, const char* text) { - bool ret = false; - if (accept_str(parser, type, text)) { - ret = true; - } else { + Tok* tok = accept_str(parser, type, text); + if (tok == NULL) { error(parser, "Unexpected token"); } - return ret; + return tok; } diff --git a/source/pprint.c b/source/pprint.c index 18de8ec..77cff04 100644 --- a/source/pprint.c +++ b/source/pprint.c @@ -121,6 +121,10 @@ void pprint_tree(FILE* file, AST* tree, int depth) pprint_literal(file, tree, depth); } else if (tree->type == AST_REQ) { printf("(require \"%s\")", require_name(tree)); + } else if (tree->type == AST_DEF) { + printf("(def %s ", def_name(tree)); + pprint_tree(file, def_value(tree), depth); + printf(")"); } else { //fputs("(tree", file); //vec_t* p_vec = tree->ptr.vec; diff --git a/source/sclpl.h b/source/sclpl.h index e5f4c9a..757812d 100644 --- a/source/sclpl.h +++ b/source/sclpl.h @@ -102,48 +102,47 @@ typedef struct AST { } AST; /* String */ -AST* String(char* val); +AST* String(Tok* val); char* string_value(AST* val); /* Symbol */ -AST* Symbol(char* val); +AST* Symbol(Tok* val); char* symbol_value(AST* val); /* Character */ -AST* Char(uint32_t val); +AST* Char(Tok* val); uint32_t char_value(AST* val); /* Integer */ -AST* Integer(intptr_t val); +AST* Integer(Tok* val); intptr_t integer_value(AST* val); /* Float */ -AST* Float(double val); +AST* Float(Tok* val); double float_value(AST* val); /* Bool */ -AST* Bool(bool val); +AST* Bool(Tok* val); bool bool_value(AST* val); /* Ident */ -AST* Ident(char* val); +AST* Ident(Tok* val); char* ident_value(AST* val); /* Require */ -AST* Require(char* name); +AST* Require(Tok* name); char* require_name(AST* req); +/* Definition */ +AST* Def(Tok* name, AST* value); +char* def_name(AST* def); +AST* def_value(AST* def); + -// -///* Definition */ -//AST* Def(char* name, AST* value); -//char* def_name(AST* def); -//AST* def_value(AST* def); -// ///* Annotation */ //AST* Ann(char* name, AST* value); //char* ann_name(AST* def); @@ -188,10 +187,10 @@ Tok* peek(Parser* p_parser); bool parser_eof(Parser* p_parser); void parser_resume(Parser* p_parser); void error(Parser* p_parser, const char* p_text); -bool accept(Parser* p_parser, TokType type); -bool accept_str(Parser* p_parser, TokType type, const char* p_text); -bool expect(Parser* p_parser, TokType type); -bool expect_str(Parser* p_parser, TokType type, const char* p_text); +Tok* accept(Parser* p_parser, TokType type); +Tok* accept_str(Parser* p_parser, TokType type, const char* p_text); +Tok* expect(Parser* p_parser, TokType type); +Tok* expect_str(Parser* p_parser, TokType type, const char* p_text); // Grammar Routines AST* toplevel(Parser* p); -- 2.52.0