From f79aca85785b0fdb35adf49299b89ad0aad4d2a3 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Wed, 20 Mar 2019 15:50:35 -0400 Subject: [PATCH] removed typing from initial AST creation process. Will be done from the type checker/inferrer instead. --- source/ast.c | 2 +- source/parser.c | 54 +++++++++++-------------------------------------- source/pprint.c | 3 +-- source/sclpl.h | 7 ++----- 4 files changed, 16 insertions(+), 50 deletions(-) diff --git a/source/ast.c b/source/ast.c index 10171ec..8da9dba 100644 --- a/source/ast.c +++ b/source/ast.c @@ -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; } diff --git a/source/parser.c b/source/parser.c index 865ff1e..9ab5c25 100644 --- a/source/parser.c +++ b/source/parser.c @@ -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, ":%zu:%zu: error: ", tok->line, tok->col); + fprintf(stderr, ":%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; - } -} - diff --git a/source/pprint.c b/source/pprint.c index c28a709..969c2af 100644 --- a/source/pprint.c +++ b/source/pprint.c @@ -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) { diff --git a/source/sclpl.h b/source/sclpl.h index e6bb4ab..a380ebc 100644 --- a/source/sclpl.h +++ b/source/sclpl.h @@ -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; -- 2.54.0