From cc9b38e85afdb28ad553e0bf1436b8a8cb434e16 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Mon, 18 Mar 2019 22:05:54 -0400 Subject: [PATCH] started building and printing the AST nodes in the parser --- source/ast.c | 11 +++++----- source/parser.c | 53 ++++++++++++++++++++++++++----------------------- source/pprint.c | 9 +++------ source/sclpl.h | 7 +++---- 4 files changed, 40 insertions(+), 40 deletions(-) diff --git a/source/ast.c b/source/ast.c index 2a8afbe..10171ec 100644 --- a/source/ast.c +++ b/source/ast.c @@ -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); } diff --git a/source/parser.c b/source/parser.c index ba77231..865ff1e 100644 --- a/source/parser.c +++ b/source/parser.c @@ -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; } } diff --git a/source/pprint.c b/source/pprint.c index 26c02ab..c28a709 100644 --- a/source/pprint.c +++ b/source/pprint.c @@ -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: diff --git a/source/sclpl.h b/source/sclpl.h index 28ed79c..e6bb4ab 100644 --- a/source/sclpl.h +++ b/source/sclpl.h @@ -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); -- 2.52.0