]> git.mdlowis.com Git - proto/sclpl.git/commitdiff
Implemented basic value definitions
authorMichael D. Lowis <mike@mdlowis.com>
Tue, 13 Oct 2015 02:29:26 +0000 (22:29 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Tue, 13 Oct 2015 02:29:26 +0000 (22:29 -0400)
source/ast.c
source/gc.c
source/grammar.c
source/lexer.c
source/parser.c
source/pprint.c
source/sclpl.h

index aa289044570173c42ae479858cf116b355537033..336a1a1eb045deb6dc1ead9894443d793d368e0f 100644 (file)
@@ -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;
+//}
 
index 0e1ebbc7fc1dd3d83ea62b9cee1d31e3060eacbb..6a8eec2246bacdc6a99daa769a16f1422608c955 100644 (file)
@@ -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);
index f79e934bbdddc1eb737c4883e4459b6d722e6150..b9c0326f74bb161fb8260645644073dfd6487ddb 100644 (file)
@@ -7,9 +7,9 @@
 #include <sclpl.h>
 
 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);
index 7dd08c26dc3a7d34b3dd55d3d4cc20fc6652bbf5..127feda3f9ee51557c358ec09c6c6b15b7a8165d 100644 (file)
@@ -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;
 }
 
index 374948cdcc502413ce40e97108843862cb20fdfe..2f4572cfe4ad645306d62f35ad5b17061634ee53 100644 (file)
@@ -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;
 }
 
index 18de8ec6595eec38ef4ebcd66ebf07973732007e..77cff04d6312e50ed6d1b5abd47452a8ebdf12a8 100644 (file)
@@ -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;
index e5f4c9a5e66d576797e427d9e6a3aa5066eb099c..757812dcc84cb265862e81abdab4915d7ce85f21 100644 (file)
@@ -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);