hyc

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

commit dc56f9e11afe084505f0cf4fd450faa025f9a20a
parent 8657048a41ff49ff812675afb19d6a5a06e6ffbb
Author: kocotian <kocotian@kocotian.pl>
Date:   Fri, 30 Jul 2021 21:19:00 +0000

Basic function-call argument parsing in AST

Diffstat:
Mast.c | 40++++++++++++++++++++++++++++++++++------
Minclude/ast.h | 8++++++++
2 files changed, 42 insertions(+), 6 deletions(-)

diff --git a/ast.c b/ast.c @@ -38,6 +38,16 @@ static Token *nextTokenType(Tokenizer *t, TokenType type); static Token *enextToken(Tokenizer *t); static Token *enextTokenType(Tokenizer *t, TokenType type); +static ASTExpression tokenstoASTExpressionLiteral(Tokenizer *t); +static ASTExpression tokenstoASTExpressionFunctionArgumentList(Tokenizer *t); +static ASTExpression tokenstoASTExpression(Tokenizer *t); + +static ASTStatement tokenstoASTStatementCompound(Tokenizer *t); +static ASTStatement tokenstoASTStatementConditional(Tokenizer *t); +static ASTStatement tokenstoASTStatementReturn(Tokenizer *t); +static ASTStatement tokenstoASTStatementExpression(Tokenizer *t); +static ASTStatement tokenstoASTStatementInlineAssembly(Tokenizer *t); +static ASTStatement tokenstoASTStatementVariableDeclaration(Tokenizer *t); static ASTStatement tokenstoASTStatement(Tokenizer *t); static ASTGlobal tokenstoASTGlobalFunction(Tokenizer *t); @@ -91,11 +101,6 @@ enextTokenType(Tokenizer *t, TokenType type) /* Expressions */ static ASTExpression -tokenstoASTExpressionFunctionCall(Tokenizer *t) -{ -} - -static ASTExpression tokenstoASTExpressionLiteral(Tokenizer *t) { ASTExpression expr; @@ -117,6 +122,25 @@ tokenstoASTExpressionLiteral(Tokenizer *t) } static ASTExpression +tokenstoASTExpressionFunctionArgumentList(Tokenizer *t) +{ + ASTExpression expr; + Token *tok; + + newVector(expr.FunctionArgumentList); + enextTokenType(t, TokenOpeningParenthesis); + + if ((tok = enextToken(t))->type != TokenClosingParenthesis) { + prevToken(t); + do { + pushVector(expr.FunctionArgumentList, tokenstoASTExpression(t)); + } while ((tok = enextToken(t))->type == TokenComma); + prevToken(t); + enextTokenType(t, TokenClosingParenthesis); + } +} + +static ASTExpression tokenstoASTExpression(Tokenizer *t) { ASTExpression expr; @@ -155,15 +179,19 @@ tokenstoASTExpression(Tokenizer *t) } else if (tok->type == TokenMinusMinus) { expr.type = ASTExpressionUnaryPredecrement_T; new(expr.Unary.expr) = tokenstoASTExpression(t); + } else { + error(tok, "unexpected token: '%s'", strTokenType(tok->type)); } tok = enextToken(t); if (tok->type == TokenOpeningParenthesis) { /* function call */ ASTExpression callexpr = expr; + prevToken(t); expr.type = ASTExpressionFunctionCall_T; new(expr.FunctionCall.callexpr) = callexpr; - enextTokenType(t, TokenClosingParenthesis); + new(expr.FunctionCall.argv) = + tokenstoASTExpressionFunctionArgumentList(t).FunctionArgumentList; } else { prevToken(t); } diff --git a/include/ast.h b/include/ast.h @@ -53,6 +53,7 @@ typedef enum ASTExpressionType { ASTExpressionUnaryValuefrom_T, ASTExpressionUnaryPreincrement_T, ASTExpressionUnaryPredecrement_T, + ASTExpressionFunctionArgumentList_T, ASTExpressionFunctionCall_T, ASTExpression_Count, } ASTExpressionType; @@ -71,9 +72,15 @@ typedef struct ASTExpressionUnary { union ASTExpression *expr; } ASTExpressionUnary; +typedef struct ASTExpressionFunctionArgumentList { + union ASTExpression *data; + size_t len; +} ASTExpressionFunctionArgumentList; + typedef struct ASTExpressionFunctionCall { enum ASTExpressionType type; union ASTExpression *callexpr; + struct ASTExpressionFunctionArgumentList *argv; } ASTExpressionFunctionCall; typedef union ASTExpression { @@ -87,6 +94,7 @@ typedef union ASTExpression { struct ASTExpressionUnary UnaryValuefrom; struct ASTExpressionUnary UnaryPreincrement; struct ASTExpressionUnary UnaryPredecrement; + struct ASTExpressionFunctionArgumentList FunctionArgumentList; struct ASTExpressionFunctionCall FunctionCall; } ASTExpression;