From dd6f24990394605cfa1424d600d780496674731d Mon Sep 17 00:00:00 2001 From: "Mike D. Lowis" Date: Tue, 13 Oct 2015 20:24:44 -0400 Subject: [PATCH] Added ast functions for if statments and blocks --- source/ast.c | 40 +++++++++++++++++++++++++++++++++++++--- source/grammar.c | 19 ++++++++++--------- source/parser.c | 11 +++++++++++ source/sclpl.h | 17 +++++++++++------ 4 files changed, 69 insertions(+), 18 deletions(-) diff --git a/source/ast.c b/source/ast.c index 822c042..45120d7 100644 --- a/source/ast.c +++ b/source/ast.c @@ -181,14 +181,48 @@ AST* def_value(AST* def) return def->value.def.value; } +AST* IfExpr(void) +{ + return ast(AST_IF); +} + +AST* ifexpr_cond(AST* ifexpr) +{ + return ifexpr->value.ifexpr.cond; +} + +void ifexpr_set_cond(AST* ifexpr, AST* cond) +{ + ifexpr->value.ifexpr.cond = (AST*)gc_addref(cond); +} + +AST* ifexpr_then(AST* ifexpr) +{ + return ifexpr->value.ifexpr.bthen; +} + +void ifexpr_set_then(AST* ifexpr, AST* bthen) +{ + ifexpr->value.ifexpr.cond = (AST*)gc_addref(bthen); +} + +AST* ifexpr_else(AST* ifexpr) +{ + return ifexpr->value.ifexpr.belse; +} + +void ifexpr_set_else(AST* ifexpr, AST* belse) +{ + ifexpr->value.ifexpr.cond = (AST*)gc_addref(belse); +} + AST* Block(void) { - return NULL; + return ast(AST_BLOCK); } void block_append(AST* block, AST* expr) { - } size_t block_size(AST* block) @@ -196,7 +230,7 @@ size_t block_size(AST* block) return 0; } -AST* block_get(size_t index) +AST* block_get(AST* block, size_t index) { return NULL; } diff --git a/source/grammar.c b/source/grammar.c index 958ab42..83acff6 100644 --- a/source/grammar.c +++ b/source/grammar.c @@ -17,7 +17,7 @@ static AST* token_to_tree(Tok* tok); AST* toplevel(Parser* p) { AST* ret = NULL; - if (peek(p)->type != T_END_FILE) { + if (!match(p, T_END_FILE)) { if (accept_str(p, T_ID, "require")) ret = require(p); else if (accept_str(p, T_ID, "def")) @@ -55,7 +55,7 @@ static AST* require(Parser* p) static AST* expression(Parser* p) { - if (peek(p)->type == T_ID) { + if (match(p, T_ID)) { return Ident(expect(p,T_ID)); //if (peek(p)->type == T_LPAR) { // arglist(p); @@ -82,12 +82,13 @@ static AST* expression(Parser* p) static AST* if_stmnt(Parser* p) { - //AST* ifexpr = IfExpr(); - //ifexpr_set_condition( expression(p) ); - //ifexpr_set_branch_then( expr_block(p) ); - //expect(p,T_END); - //return ifexpr; - return NULL; + AST* ifexpr = IfExpr(); + ifexpr_set_cond( ifexpr, expression(p) ); + ifexpr_set_then( ifexpr, expr_block(p) ); + if (accept_str(p, T_ID, "else")) + ifexpr_set_else( ifexpr, expr_block(p) ); + expect(p,T_END); + return ifexpr; } static AST* literal(Parser* p) @@ -113,7 +114,7 @@ static AST* expr_block(Parser* p) AST* block = Block(); do { block_append(block, expression(p)); - } while(!accept_str(p, T_ID, "else") && !accept(p, T_END)); + } while(!match(p, T_END) && !match_str(p, T_ID, "else")); return block; } diff --git a/source/parser.c b/source/parser.c index d252131..a058bf8 100644 --- a/source/parser.c +++ b/source/parser.c @@ -64,6 +64,17 @@ void error(Parser* parser, const char* text) exit(1); } +bool match(Parser* parser, TokType type) +{ + return (peek(parser)->type == type); +} + +bool match_str(Parser* parser, TokType type, const char* text) +{ + return (match(parser, type) && + (0 == strcmp((char*)(peek(parser)->value.text), text))); +} + Tok* accept(Parser* parser, TokType type) { Tok* tok = peek(parser); diff --git a/source/sclpl.h b/source/sclpl.h index 8982f33..b13c4c1 100644 --- a/source/sclpl.h +++ b/source/sclpl.h @@ -73,7 +73,7 @@ typedef struct { *****************************************************************************/ typedef enum ASTType { AST_STRING = 0, AST_SYMBOL, AST_CHAR, AST_INT, AST_FLOAT, AST_BOOL, AST_IDENT, - AST_REQ, AST_DEF, AST_ANN, AST_IF, AST_FUNC, + AST_REQ, AST_DEF, AST_ANN, AST_IF, AST_FUNC, AST_BLOCK } ASTType; typedef struct AST { @@ -155,16 +155,19 @@ char* def_name(AST* def); AST* def_value(AST* def); /* If Expression */ -AST* IfExpr(AST* cond, AST* bthen, AST* belse); -AST* ifexpr_condition(AST* ifexpr); -AST* ifexpr_branch_then(AST* ifexpr); -AST* ifexpr_branch_else(AST* ifexpr); +AST* IfExpr(void); +AST* ifexpr_cond(AST* ifexpr); +void ifexpr_set_cond(AST* ifexpr, AST* cond); +AST* ifexpr_then(AST* ifexpr); +void ifexpr_set_then(AST* ifexpr, AST* bthen); +AST* ifexpr_else(AST* ifexpr); +void ifexpr_set_else(AST* ifexpr, AST* belse); /* Code Block */ AST* Block(void); void block_append(AST* block, AST* expr); size_t block_size(AST* block); -AST* block_get(size_t index); +AST* block_get(AST* block, size_t index); @@ -202,6 +205,8 @@ 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 match(Parser* parser, TokType type); +bool match_str(Parser* parser, TokType type, const char* 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); -- 2.54.0