hyc

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

commit db1c15947bb7d8c02b3310eea04fe0e33bfdd217
parent 135b166a85ba2aaf255543f33b8a3c8e5a21976d
Author: kocotian <kocotian@kocotian.pl>
Date:   Mon, 26 Jul 2021 08:50:21 +0000

Compound statement

Diffstat:
Mast.c | 30++++++++++++++++++++++++++++++
1 file changed, 30 insertions(+), 0 deletions(-)

diff --git a/ast.c b/ast.c @@ -29,7 +29,10 @@ typedef struct { ssize_t pos; } Tokenizer; +static Token *prevToken(Tokenizer *t); + static Token *nextToken(Tokenizer *t); +static Token *nextTokenType(Tokenizer *t, TokenType type); static Token *enextToken(Tokenizer *t); static Token *enextTokenType(Tokenizer *t, TokenType type); @@ -38,6 +41,12 @@ static ASTStatement tokenstoASTStatement(Tokenizer *t); static ASTGlobal tokenstoASTGlobalFunction(Tokenizer *t); static Token * +prevToken(Tokenizer *t) +{ + return (--t->pos >= 0) ? &(t->data[t->pos]) : NULL; +} + +static Token * nextToken(Tokenizer *t) { return ((unsigned)(++t->pos) < t->len) ? &(t->data[t->pos]) : NULL; @@ -105,6 +114,24 @@ tokenstoASTExpression(Tokenizer *t) /* Statements */ static ASTStatement +tokenstoASTStatementCompound(Tokenizer *t) +{ + ASTStatement stat; + Token *tok; + + tok = enextTokenType(t, TokenOpeningBrace); + stat.type = ASTStatementCompound_T; + newVector(stat.Compound); + + while ((tok = enextToken(t))->type != TokenClosingBrace) { + prevToken(t); + pushVector(stat.Compound, tokenstoASTStatement(t)); + } + + return stat; +} + +static ASTStatement tokenstoASTStatement(Tokenizer *t) { ASTStatement stat; @@ -115,6 +142,9 @@ tokenstoASTStatement(Tokenizer *t) if (0) { } else if (tok->type == TokenSemicolon) { stat.type = ASTStatementNoOp_T; + } else if (tok->type == TokenOpeningBrace) { + prevToken(t); + stat = tokenstoASTStatementCompound(t); } return stat;