hyc

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

commit 57ebabe95de124d41c4cf4efea4ebf5fbd414741
parent c5a48525698470f5894a87b3730387f0fe9e2970
Author: kocotian <kocotian@kocotian.pl>
Date:   Mon, 26 Jul 2021 14:23:42 +0000

Unary expressions

Diffstat:
Mast.c | 23+++++++++++++++++++++++
Minclude/ast.h | 18++++++++++++++++++
2 files changed, 41 insertions(+), 0 deletions(-)

diff --git a/ast.c b/ast.c @@ -29,6 +29,8 @@ typedef struct { ssize_t pos; } Tokenizer; +#define new(PTR) (*((PTR) = malloc(sizeof *(PTR)))) + static Token *prevToken(Tokenizer *t); static Token *nextToken(Tokenizer *t); @@ -106,6 +108,27 @@ tokenstoASTExpression(Tokenizer *t) } else if (tok->type == TokenString) { expr.type = ASTExpressionLiteralString_T; expr.Literal.value = Strdup(tok->str).data; + } else if (tok->type == TokenOpeningParenthesis) { + expr = tokenstoASTExpression(t); + enextTokenType(t, TokenClosingParenthesis); + } else if (tok->type == TokenExclamationMark) { + expr.type = ASTExpressionUnaryNegation_T; + new(expr.Unary.expr) = tokenstoASTExpression(t); + } else if (tok->type == TokenMinus) { + expr.type = ASTExpressionUnarySignChange_T; + new(expr.Unary.expr) = tokenstoASTExpression(t); + } else if (tok->type == TokenAmperstand) { + expr.type = ASTExpressionUnaryAddressof_T; + new(expr.Unary.expr) = tokenstoASTExpression(t); + } else if (tok->type == TokenAsterisk) { + expr.type = ASTExpressionUnaryValuefrom_T; + new(expr.Unary.expr) = tokenstoASTExpression(t); + } else if (tok->type == TokenPlusPlus) { + expr.type = ASTExpressionUnaryPreincrement_T; + new(expr.Unary.expr) = tokenstoASTExpression(t); + } else if (tok->type == TokenMinusMinus) { + expr.type = ASTExpressionUnaryPredecrement_T; + new(expr.Unary.expr) = tokenstoASTExpression(t); } return expr; diff --git a/include/ast.h b/include/ast.h @@ -47,6 +47,12 @@ typedef enum ASTExpressionType { ASTExpressionLiteralIdentifier_T, ASTExpressionLiteralInteger_T, ASTExpressionLiteralString_T, + ASTExpressionUnaryNegation_T, + ASTExpressionUnarySignChange_T, + ASTExpressionUnaryAddressof_T, + ASTExpressionUnaryValuefrom_T, + ASTExpressionUnaryPreincrement_T, + ASTExpressionUnaryPredecrement_T, ASTExpression_Count, } ASTExpressionType; @@ -59,10 +65,22 @@ typedef struct ASTExpressionLiteral { char *value; } ASTExpressionLiteral; +typedef struct ASTExpressionUnary { + enum ASTExpressionType type; + union ASTExpression *expr; +} ASTExpressionUnary; + typedef union ASTExpression { enum ASTExpressionType type; struct ASTExpressionAny Any; struct ASTExpressionLiteral Literal; + struct ASTExpressionUnary Unary; + struct ASTExpressionUnary UnaryNegation; + struct ASTExpressionUnary UnarySignChange; + struct ASTExpressionUnary UnaryAddressof; + struct ASTExpressionUnary UnaryValuefrom; + struct ASTExpressionUnary UnaryPreincrement; + struct ASTExpressionUnary UnaryPredecrement; } ASTExpression; /**/