hyc

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

commit c1fb8034c47791a7fd7f5508fd61db099926a451
parent 740c7afb30a5eca969f36762dad1b7c97d6766ba
Author: kocotian <kocotian@kocotian.pl>
Date:   Mon, 26 Jul 2021 19:00:56 +0000

Function calls in AST

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

diff --git a/ast.c b/ast.c @@ -91,15 +91,18 @@ enextTokenType(Tokenizer *t, TokenType type) /* Expressions */ static ASTExpression -tokenstoASTExpression(Tokenizer *t) +tokenstoASTExpressionFunctionCall(Tokenizer *t) +{ +} + +static ASTExpression +tokenstoASTExpressionLiteral(Tokenizer *t) { ASTExpression expr; Token *tok; tok = enextToken(t); - if (0) { - /* Literals: */ - } else if (tok->type == TokenIdentifier) { + if (tok->type == TokenIdentifier) { expr.type = ASTExpressionLiteralIdentifier_T; expr.Literal.value = Strdup(tok->str).data; } else if (tok->type == TokenInteger) { @@ -107,7 +110,30 @@ tokenstoASTExpression(Tokenizer *t) expr.Literal.value = Strdup(tok->str).data; } else if (tok->type == TokenString) { expr.type = ASTExpressionLiteralString_T; - expr.Literal.value = Strdup((String){ tok->str.data + 1, tok->str.len - 2}).data; + expr.Literal.value = Strdup(tok->str).data; + } + + return expr; +} + +static ASTExpression +tokenstoASTExpression(Tokenizer *t) +{ + ASTExpression expr; + Token *tok; + tok = enextToken(t); + + if (0) { + /* Literals: */ + } else if (tok->type == TokenIdentifier) { + prevToken(t); + expr = tokenstoASTExpressionLiteral(t); + } else if (tok->type == TokenInteger) { + prevToken(t); + expr = tokenstoASTExpressionLiteral(t); + } else if (tok->type == TokenString) { + prevToken(t); + expr = tokenstoASTExpressionLiteral(t); } else if (tok->type == TokenOpeningParenthesis) { expr = tokenstoASTExpression(t); enextTokenType(t, TokenClosingParenthesis); @@ -131,6 +157,17 @@ tokenstoASTExpression(Tokenizer *t) new(expr.Unary.expr) = tokenstoASTExpression(t); } + tok = enextToken(t); + if (tok->type == TokenOpeningParenthesis) { /* function call */ + ASTExpression callexpr = expr; + + expr.type = ASTExpressionFunctionCall_T; + new(expr.FunctionCall.callexpr) = callexpr; + enextTokenType(t, TokenClosingParenthesis); + } else { + prevToken(t); + } + return expr; } @@ -199,7 +236,7 @@ tokenstoASTStatementExpression(Tokenizer *t) stat.type = ASTStatementExpression_T; *(stat.Expression.expr = malloc(sizeof *stat.Expression.expr)) - = tokenstoASTExpression(t); + = tokenstoASTExpressionLiteral(t); return stat; } @@ -216,7 +253,7 @@ tokenstoASTStatementInlineAssembly(Tokenizer *t) if (Strccmp(tok->str, "asm")) error(tok, "expected 'asm' keyword"); - stat.InlineAssembly.expr = tokenstoASTExpression(t).Literal; + stat.InlineAssembly.expr = tokenstoASTExpressionLiteral(t).Literal; tok = enextTokenType(t, TokenSemicolon); @@ -263,7 +300,7 @@ tokenstoASTGlobalFunction(Tokenizer *t) Token *tok; global.type = ASTGlobalFunction_T; - global.Function.name = tokenstoASTExpression(t).Literal; + global.Function.name = tokenstoASTExpressionLiteral(t).Literal; tok = enextTokenType(t, TokenOpeningParenthesis); while ((tok = enextToken(t)) != NULL) { diff --git a/include/ast.h b/include/ast.h @@ -53,6 +53,7 @@ typedef enum ASTExpressionType { ASTExpressionUnaryValuefrom_T, ASTExpressionUnaryPreincrement_T, ASTExpressionUnaryPredecrement_T, + ASTExpressionFunctionCall_T, ASTExpression_Count, } ASTExpressionType; @@ -70,6 +71,11 @@ typedef struct ASTExpressionUnary { union ASTExpression *expr; } ASTExpressionUnary; +typedef struct ASTExpressionFunctionCall { + enum ASTExpressionType type; + union ASTExpression *callexpr; +} ASTExpressionFunctionCall; + typedef union ASTExpression { enum ASTExpressionType type; struct ASTExpressionAny Any; @@ -81,6 +87,7 @@ typedef union ASTExpression { struct ASTExpressionUnary UnaryValuefrom; struct ASTExpressionUnary UnaryPreincrement; struct ASTExpressionUnary UnaryPredecrement; + struct ASTExpressionFunctionCall FunctionCall; } ASTExpression; /**/