]> git.mdlowis.com Git - proto/sclpl.git/commitdiff
started building and printing the AST nodes in the parser
authorMichael D. Lowis <mike.lowis@gentex.com>
Tue, 19 Mar 2019 02:05:54 +0000 (22:05 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Tue, 19 Mar 2019 02:05:54 +0000 (22:05 -0400)
source/ast.c
source/parser.c
source/pprint.c
source/sclpl.h

index 2a8afbe5c1d5c985c2752874b7b6bf20fb038226..10171ec7c603055c4634e21c4236c378d69190bd 100644 (file)
@@ -9,7 +9,7 @@ static AST* ast(ASTType type) {
 
 AST* String(Tok* val) {
     AST* node = ast(AST_STRING);
-    node->value.text = val->value.text;
+    node->value.text = val->text;
     return node;
 }
 
@@ -73,11 +73,12 @@ char* ident_value(AST* val) {
     return val->value.text;
 }
 
-AST* Var(Tok* name, AST* value, bool constant) {
+AST* Var(char* name, AST* value, AST* type, int flags) {
     AST* node = ast(AST_VAR);
-    node->value.var.name = name->value.text;
+    node->value.var.name = name;
     node->value.var.value = value;
-    node->value.var.constant = constant;
+    node->value.var.type = type;
+    node->value.var.flags = flags;
     return node;
 }
 
@@ -93,5 +94,5 @@ AST* var_value(AST* var) {
 
 bool var_const(AST* var) {
     assert(var->nodetype == AST_VAR);
-    return var->value.var.constant;
+    return (var->value.var.flags == SF_CONSTANT);
 }
index ba772319eee56efc50e1b1c1e7c4c0a1e2244271..865ff1e25dc9203961cf91f8fa4e35bb2422360d 100644 (file)
@@ -3,7 +3,7 @@
 
 static void require_list(Parser* p);
 static void provide_list(Parser* p);
-static AST* definition_list(Parser* p);
+static void definition_list(Parser* p);
 static AST* definition(Parser* p);
 static AST* type_definition(Parser* p);
 static AST* func_definition(Parser* p);
@@ -97,43 +97,45 @@ static void provide_list(Parser* p) {
     expect(p, ')');
 }
 
-static AST* definition_list(Parser* p) {
+static void definition_list(Parser* p) {
     while (!matches(p, T_END_FILE)) {
+        AST* def = NULL;
         if (matches(p, T_LET) || matches(p, T_VAR)) {
-            definition(p);
+            def = definition(p);
         } else if (matches(p, T_TYPE)) {
-            type_definition(p);
+            def = type_definition(p);
         } else if (matches(p, T_FUN)) {
-            func_definition(p);
+            def = func_definition(p);
         } else {
             error(p, "only definitions are allowed at the top level");
         }
-        pkg_add_definition(&(p->pkg), NULL);
+        pprint_tree(stdin, def, 0);
+        pkg_add_definition(&(p->pkg), def);
     }
-    return NULL;
 }
 
 static AST* definition(Parser* p) {
+    bool is_const = matches(p, T_LET);
     if (!accept(p, T_LET) && !accept(p, T_VAR))
         error(p, "constant or variable definition expected");
-    expect(p, T_ID);
-    type_expression(p);
+    char* str = strdup(expect_val(p, T_ID)->text);
+    AST* type = type_expression(p);
     expect(p, '=');
-    expression(p);
-    return NULL;
+    AST* val = expression(p);
+    return Var(str, val, type, SF_CONSTANT);
 }
 
 static AST* type_definition(Parser* p) {
     expect(p, T_TYPE);
-    expect(p, T_ID);
+    char* str = strdup(expect_val(p, T_ID)->text);
     expect(p, '=');
-    type_expression(p);
-    return NULL;
+    AST* type = type_expression(p);
+    return Var(str, NULL, type, SF_TYPEDEF);
 }
 
 static AST* func_definition(Parser* p) {
     expect(p, T_FUN);
-    expect(p, T_ID);
+    char* str = strdup(expect_val(p, T_ID)->text);
     expect(p, '(');
     if (!matches(p, ')')) {
         while (true) {
@@ -146,35 +148,36 @@ static AST* func_definition(Parser* p) {
         }
     }
     expect(p, ')');
-    type_expression(p);
+    AST* type = type_expression(p);
     expression_block(p);
-    return NULL;
+    return Var(str, NULL, type, SF_CONSTANT);
 }
 
 static AST* expression(Parser* p) {
+    AST* exp;
     if (matches(p, '(')) {
         expect(p, '(');
-        expression(p);
+        exp = expression(p);
         expect(p, ')');
     } else if (matches(p, '{')) {
-        expression_block(p);
+        exp = expression_block(p);
     } else if (matches(p, T_IF)) {
-        if_expression(p);
+        exp = if_expression(p);
     } else if (matches(p, T_ID)) {
-        identifier(p);
+        exp = identifier(p);
     } else {
-        constant(p);
+        exp = constant(p);
     }
 
     /* determine if this is a function call */
-    if (matches(p, '(')) {
+    if (matches(p, '(')) { // TODO
         func_expr_list(p);
     } else if (accept(p, '.')) {
         identifier(p);
         func_expr_list(p);
     }
 
-    return NULL;
+    return exp;
 }
 
 static AST* constant(Parser* p) {
@@ -303,7 +306,7 @@ static AST* token_to_tree(Parser* p, Tok* tok) {
         case T_STRING: return add_type(p, String(tok), "string");
         case T_INT:    return add_type(p, Integer(tok), "int");
         case T_FLOAT:  return add_type(p, Float(tok), "float");
-        case T_ID:     return add_type(p, Ident(tok), tok->value.text);
+        case T_ID:     return add_type(p, Ident(tok), tok->text);
         default:       return NULL;
     }
 }
index 26c02abf929d64e1b04195c43dc96f67b663d870..c28a709906db4c7f2a1828c4ec597be2ecc3824e 100644 (file)
@@ -48,14 +48,14 @@ static void print_char(FILE* file, char ch) {
     if (i == 5) fprintf(file, "\\%c", ch);
 }
 
-void pprint_token_type(FILE* file, Tok* token) {
+static void pprint_token_type(FILE* file, Tok* token) {
     if (token->type > 256)
         fprintf(file, "%s", token_type_to_string(token->type));
     else
         fprintf(file, "%c", token->type);
 }
 
-void pprint_token_value(FILE* file, Tok* token) {
+static void pprint_token_value(FILE* file, Tok* token) {
     #define TOK(name) case (name): fprintf(file, "%s", #name); break
     switch(token->type) {
         /* value tokens */
@@ -127,10 +127,7 @@ static void pprint_literal(FILE* file, AST* tree, int depth)
 
 void pprint_tree(FILE* file, AST* tree, int depth)
 {
-    puts("tree");
-    if (tree == NULL) {
-        return;
-    }
+    if (tree == NULL) return;
     print_indent(file, depth);
     switch (tree->nodetype) {
         case AST_VAR:
index 28ed79c1f2f4e7ff0edfb338d2fd6b9bfd9707b2..e6bb4ab203efd44eb779cceb7040ea1e0bf6e165 100644 (file)
@@ -107,8 +107,9 @@ typedef struct AST {
         /* Definition Node */
         struct {
             char* name;
+            int flags;
             struct AST* value;
-            bool constant;
+            struct AST* type;
         } var;
         /* String, Symbol, Identifier */
         char* text;
@@ -148,7 +149,7 @@ AST* Ident(Tok* val);
 char* ident_value(AST* val);
 
 /* Definition */
-AST* Var(Tok* name, AST* value, bool constant);
+AST* Var(char* name, AST* value, AST* type, int flags);
 char* var_name(AST* var);
 AST* var_value(AST* var);
 bool var_const(AST* var);
@@ -185,8 +186,6 @@ void pkg_add_definition(Package* p, AST* ast);
 
 /* Pretty Printing
  *****************************************************************************/
-void pprint_token_type(FILE* file, Tok* token);
-void pprint_token_value(FILE* file, Tok* token);
 void pprint_token(FILE* file, Tok* token, bool print_loc);
 void pprint_tree(FILE* file, AST* tree, int depth);