hyc

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

commit f1ce05258d1bf04b543b0855026fa8d0ddfc7ccc
parent db1c15947bb7d8c02b3310eea04fe0e33bfdd217
Author: kocotian <kocotian@kocotian.pl>
Date:   Mon, 26 Jul 2021 09:14:18 +0000

Return statement

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

diff --git a/ast.c b/ast.c @@ -132,6 +132,24 @@ tokenstoASTStatementCompound(Tokenizer *t) } static ASTStatement +tokenstoASTStatementReturn(Tokenizer *t) +{ + /* return <expr>; */ + ASTStatement stat; + Token *tok; + + tok = enextTokenType(t, TokenIdentifier); + if (Strccmp(tok->str, "return")) + error(tok, "expected 'return' keyword"); + + *(stat.Return.expr = malloc(sizeof *stat.Return.expr)) = tokenstoASTExpression(t); + + tok = enextTokenType(t, TokenSemicolon); + + return stat; +} + +static ASTStatement tokenstoASTStatement(Tokenizer *t) { ASTStatement stat; @@ -145,6 +163,9 @@ tokenstoASTStatement(Tokenizer *t) } else if (tok->type == TokenOpeningBrace) { prevToken(t); stat = tokenstoASTStatementCompound(t); + } else if (tok->type == TokenIdentifier && !Strccmp(tok->str, "return")) { + prevToken(t); + stat = tokenstoASTStatementReturn(t); } return stat;