hyc

Hydrogen Compiler written in C
git clone git://git.kocotian.pl/hyc.git
Log | Files | Refs | README | LICENSE

commit 60a0bdf7e7239cd2f30b47b6af4d14bb13f59919
parent 4627d489ab918418699b2fdc8a7035bf7e0f8b43
Author: kocotian <kocotian@kocotian.pl>
Date:   Mon, 26 Jul 2021 13:50:02 +0000

Expression statement

Diffstat:
Mast.c | 29+++++++++++++++++++++--------
Minclude/ast.h | 7+++++++
2 files changed, 28 insertions(+), 8 deletions(-)

diff --git a/ast.c b/ast.c @@ -170,6 +170,18 @@ tokenstoASTStatementReturn(Tokenizer *t) } static ASTStatement +tokenstoASTStatementExpression(Tokenizer *t) +{ + /* <expr>; */ + ASTStatement stat; + + *(stat.Expression.expr = malloc(sizeof *stat.Expression.expr)) + = tokenstoASTExpression(t); + + return stat; +} + +static ASTStatement tokenstoASTStatement(Tokenizer *t) { ASTStatement stat; @@ -183,14 +195,15 @@ tokenstoASTStatement(Tokenizer *t) } else if (tok->type == TokenOpeningBrace) { prevToken(t); stat = tokenstoASTStatementCompound(t); - } else if (tok->type == TokenIdentifier) { - if (!Strccmp(tok->str, "if")) { - prevToken(t); - stat = tokenstoASTStatementConditional(t); - } else if (!Strccmp(tok->str, "return")) { - prevToken(t); - stat = tokenstoASTStatementReturn(t); - } + } else if (tok->type == TokenIdentifier && !Strccmp(tok->str, "if")) { + prevToken(t); + stat = tokenstoASTStatementConditional(t); + } else if (tok->type == TokenIdentifier && !Strccmp(tok->str, "return")) { + prevToken(t); + stat = tokenstoASTStatementReturn(t); + } else { + prevToken(t); + stat = tokenstoASTStatementExpression(t); } return stat; diff --git a/include/ast.h b/include/ast.h @@ -71,6 +71,7 @@ typedef enum ASTStatementType { ASTStatementNoOp_T = 0, ASTStatementCompound_T, ASTStatementReturn_T, + ASTStatementExpression_T, ASTStatement_Count, } ASTStatementType; @@ -95,12 +96,18 @@ typedef struct ASTStatementReturn { union ASTExpression *expr; } ASTStatementReturn; +typedef struct ASTStatementExpression { + enum ASTStatementType type; + union ASTExpression *expr; +} ASTStatementExpression; + typedef union ASTStatement { enum ASTStatementType type; struct ASTStatementAny Any; struct ASTStatementCompound Compound; struct ASTStatementConditional Conditional; struct ASTStatementReturn Return; + struct ASTStatementExpression Expression; } ASTStatement; /**/