]> git.mdlowis.com Git - proto/sclpl.git/commitdiff
removed typing from initial AST creation process. Will be done from the type checker...
authorMichael D. Lowis <mike.lowis@gentex.com>
Wed, 20 Mar 2019 19:50:35 +0000 (15:50 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Wed, 20 Mar 2019 19:50:35 +0000 (15:50 -0400)
source/ast.c
source/parser.c
source/pprint.c
source/sclpl.h

index 10171ec7c603055c4634e21c4236c378d69190bd..8da9dba21eb879abd9e09f6c1e1bd0a8ff10c179 100644 (file)
@@ -64,7 +64,7 @@ bool bool_value(AST* val) {
 
 AST* Ident(Tok* val) {
     AST* node = ast(AST_IDENT);
-    node->value.text = val->value.text;
+    node->value.text = val->text;
     return node;
 }
 
index 865ff1e25dc9203961cf91f8fa4e35bb2422360d..9ab5c25c26ffd1e6a40a6f5cae757c2c024ce368 100644 (file)
@@ -15,9 +15,6 @@ static AST* expression_block(Parser* p);
 static AST* if_expression(Parser* p);
 static AST* identifier(Parser* p);
 static AST* func_expr_list(Parser* p);
-static Type* get_typedef(Parser* p, char* typename);
-static AST* add_type(Parser* p, AST* ast, char* typename);
-static AST* token_to_tree(Parser* p, Tok* tok);
 
 /* Parsing Routines
  *****************************************************************************/
@@ -31,7 +28,7 @@ static void error(Parser* parser, const char* fmt, ...) {
     Tok* tok = peek(parser);
     va_list args;
     va_start(args, fmt);
-    fprintf(stderr, "<file>:%zu:%zu: error: ", tok->line, tok->col);
+    fprintf(stderr, "<file>:%zu: error: ", tok->offset);
     vfprintf(stderr, fmt, args);
     fprintf(stderr, "\n");
     va_end(args);
@@ -133,7 +130,7 @@ static AST* type_definition(Parser* p) {
     return Var(str, NULL, type, SF_TYPEDEF);
 }
 
-static AST* func_definition(Parser* p) {
+static AST* func_definition(Parser* p) { // TODO: Function AST nodes
     expect(p, T_FUN);
     char* str = strdup(expect_val(p, T_ID)->text);
     expect(p, '(');
@@ -181,21 +178,19 @@ static AST* expression(Parser* p) {
 }
 
 static AST* constant(Parser* p) {
-    AST* ret = NULL;
+    AST* tree = NULL;
     Tok* tok = peek(p);
     switch (tok->type) {
-        case T_BOOL:
-        case T_CHAR:
-        case T_STRING:
-        case T_INT:
-        case T_FLOAT:
-            ret = token_to_tree(p, tok);
-            tok->type = T_NONE;
-            break;
-        default:
-            error(p, "expected an expression");
+        case T_BOOL:   tree = Bool(tok);
+        case T_CHAR:   tree = Char(tok);
+        case T_STRING: tree = String(tok);
+        case T_INT:    tree = Integer(tok);
+        case T_FLOAT:  tree = Float(tok);
+        case T_ID:     tree = Ident(tok);
+        default:       error(p, "expected an expression");
     }
-    return ret;
+    accept(p, tok->type);
+    return tree;
 }
 
 /* Type Expressions
@@ -286,28 +281,3 @@ static AST* func_expr_list(Parser* p) {
     return NULL;
 }
 
-/* Helper Functions for Constants
- ****************************************************************************/
-static Type* get_typedef(Parser* p, char* typename) {
-   Sym* sym = sym_get(&(p->syms), typename);
-    if (!sym) error(p, "unknown type '%s'", typename);
-    return sym->type;
-}
-
-static AST* add_type(Parser* p, AST* ast, char* typename) {
-    ast->datatype = get_typedef(p, typename);
-    return ast;
-}
-
-static AST* token_to_tree(Parser* p, Tok* tok) {
-    switch (tok->type) {
-        case T_BOOL:   return add_type(p, Bool(tok), "bool");
-        case T_CHAR:   return add_type(p, Char(tok), "char");
-        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->text);
-        default:       return NULL;
-    }
-}
-
index c28a709906db4c7f2a1828c4ec597be2ecc3824e..969c2af02f215fd15b5d38505fe442606c99c203 100644 (file)
@@ -83,8 +83,7 @@ static void pprint_token_value(FILE* file, Tok* token) {
 void pprint_token(FILE* file, Tok* token, bool print_loc)
 {
     if (print_loc) {
-        fprintf(file, "%zu:", token->line);
-        fprintf(file, "%zu:", token->col);
+        fprintf(file, "%zu:", token->offset);
     }
     pprint_token_type(file, token);
     if (token->type > 256) {
index e6bb4ab203efd44eb779cceb7040ea1e0bf6e165..a380ebc95ae7f2d80c3588e5cb06b1dc102b20a7 100644 (file)
@@ -33,13 +33,10 @@ typedef enum {
 
 typedef struct {
     const char* file;
-    size_t line;
-    size_t col;
+    long offset;
     TokType type;
     char* text;
-    long offset;
-  union {
-        char* text;
+    union {
         long long integer;
         double floating;
     } value;