Removed "constant types"

This commit is contained in:
Mid 2023-08-27 21:01:09 +03:00
parent 945bb2a672
commit 62c5a0ff47
2 changed files with 3 additions and 38 deletions

View File

@ -66,18 +66,11 @@ typedef enum {
UNOP_REF = 3, UNOP_REF = 3,
} UnaryOp; } UnaryOp;
typedef enum {
EXPRESSION_CONSTANT_TRUTHY,
EXPRESSION_CONSTANT_FALSY,
EXPRESSION_NOT_CONSTANT
} ASTExprConstantType;
union AST; union AST;
typedef struct { typedef struct {
ASTKind nodeKind; ASTKind nodeKind;
Type *type; Type *type;
ASTExprConstantType constantType;
} ASTExpr; } ASTExpr;
typedef struct { typedef struct {

View File

@ -83,8 +83,6 @@ static ASTExprPrimitive *parse_prim(Parser *P) {
ret->val = strtol(str, NULL, base); ret->val = strtol(str, NULL, base);
ret->constantType = ret->val == 0 ? EXPRESSION_CONSTANT_FALSY : EXPRESSION_CONSTANT_TRUTHY;
return ret; return ret;
} }
@ -108,7 +106,6 @@ AST *nct_cast_expr(AST *what, Type *to) {
if(to->type == TYPE_TYPE_ARRAY && type_equal(primitive_parse("u8"), to->array.of) && to->array.length == what->exprStrLit.length) { if(to->type == TYPE_TYPE_ARRAY && type_equal(primitive_parse("u8"), to->array.of) && to->array.length == what->exprStrLit.length) {
ASTExprArray *ret = malloc(sizeof(*ret)); ASTExprArray *ret = malloc(sizeof(*ret));
ret->nodeKind = AST_EXPR_ARRAY; ret->nodeKind = AST_EXPR_ARRAY;
ret->constantType = what->expression.constantType;
ret->items = malloc(sizeof(*ret->items) * to->array.length); ret->items = malloc(sizeof(*ret->items) * to->array.length);
ret->type = to; ret->type = to;
@ -117,7 +114,6 @@ AST *nct_cast_expr(AST *what, Type *to) {
ASTExprPrimitive *item = malloc(sizeof(*item)); ASTExprPrimitive *item = malloc(sizeof(*item));
item->nodeKind = AST_EXPR_PRIMITIVE; item->nodeKind = AST_EXPR_PRIMITIVE;
item->constantType = bajt == 0 ? EXPRESSION_CONSTANT_FALSY : EXPRESSION_CONSTANT_TRUTHY;
item->type = to->array.of; item->type = to->array.of;
item->val = bajt; item->val = bajt;
@ -130,7 +126,6 @@ AST *nct_cast_expr(AST *what, Type *to) {
ret->nodeKind = AST_EXPR_PRIMITIVE; ret->nodeKind = AST_EXPR_PRIMITIVE;
ret->type = primitive_parse("u32"); ret->type = primitive_parse("u32");
memcpy(&ret->val, what->exprStrLit.data, sizeof(ret->val)); memcpy(&ret->val, what->exprStrLit.data, sizeof(ret->val));
ret->constantType = what->expression.constantType;
return (AST*) ret; return (AST*) ret;
} else abort(); } else abort();
} }
@ -144,14 +139,12 @@ AST *nct_cast_expr(AST *what, Type *to) {
if(what->nodeKind == AST_EXPR_PRIMITIVE && to->type == TYPE_TYPE_PRIMITIVE) { if(what->nodeKind == AST_EXPR_PRIMITIVE && to->type == TYPE_TYPE_PRIMITIVE) {
ASTExprPrimitive *ret = malloc(sizeof(*ret)); ASTExprPrimitive *ret = malloc(sizeof(*ret));
ret->nodeKind = AST_EXPR_PRIMITIVE; ret->nodeKind = AST_EXPR_PRIMITIVE;
ret->constantType = what->exprPrim.val == 0 ? EXPRESSION_CONSTANT_FALSY : EXPRESSION_CONSTANT_FALSY;
ret->type = to; ret->type = to;
ret->val = what->exprPrim.val & (((int64_t) 1 << to->primitive.width) - 1); ret->val = what->exprPrim.val & (((int64_t) 1 << to->primitive.width) - 1);
return (AST*) ret; return (AST*) ret;
} else { } else {
ASTExprCast *ret = malloc(sizeof(*ret)); ASTExprCast *ret = malloc(sizeof(*ret));
ret->nodeKind = AST_EXPR_CAST; ret->nodeKind = AST_EXPR_CAST;
ret->constantType = EXPRESSION_CONSTANT_TRUTHY;
ret->type = to; ret->type = to;
ret->what = what; ret->what = what;
ret->to = to; ret->to = to;
@ -177,22 +170,12 @@ AST *nct_parse_expression(Parser *P, int lOP) {
ret->data = tok.content; ret->data = tok.content;
ret->length = tok.length; ret->length = tok.length;
ret->constantType = EXPRESSION_CONSTANT_FALSY;
for(size_t i = 0; i < tok.length; i++) {
if(tok.content[i]) {
ret->constantType = EXPRESSION_CONSTANT_TRUTHY;
break;
}
}
return (AST*) ret; return (AST*) ret;
} }
} else if(lOP == 4) { } else if(lOP == 4) {
if(maybe(P, TOKEN_STAR)) { if(maybe(P, TOKEN_STAR)) {
ASTExprUnaryOp *astop = malloc(sizeof(*astop)); ASTExprUnaryOp *astop = malloc(sizeof(*astop));
astop->nodeKind = AST_EXPR_UNARY_OP; astop->nodeKind = AST_EXPR_UNARY_OP;
astop->constantType = EXPRESSION_NOT_CONSTANT;
astop->operator = UNOP_DEREF; astop->operator = UNOP_DEREF;
astop->operand = nct_parse_expression(P, lOP); /* Not +1! */ astop->operand = nct_parse_expression(P, lOP); /* Not +1! */
astop->type = astop->operand->expression.type->pointer.of; astop->type = astop->operand->expression.type->pointer.of;
@ -201,7 +184,6 @@ AST *nct_parse_expression(Parser *P, int lOP) {
} else if(maybe(P, TOKEN_AMPERSAND)) { } else if(maybe(P, TOKEN_AMPERSAND)) {
ASTExprUnaryOp *astop = malloc(sizeof(*astop)); ASTExprUnaryOp *astop = malloc(sizeof(*astop));
astop->nodeKind = AST_EXPR_UNARY_OP; astop->nodeKind = AST_EXPR_UNARY_OP;
astop->constantType = EXPRESSION_NOT_CONSTANT;
astop->operator = UNOP_REF; astop->operator = UNOP_REF;
astop->operand = nct_parse_expression(P, lOP); astop->operand = nct_parse_expression(P, lOP);
astop->type = type_pointer_wrap(astop->operand->expression.type); astop->type = type_pointer_wrap(astop->operand->expression.type);
@ -216,7 +198,6 @@ AST *nct_parse_expression(Parser *P, int lOP) {
} else { } else {
ASTExprUnaryOp *astop = malloc(sizeof(*astop)); ASTExprUnaryOp *astop = malloc(sizeof(*astop));
astop->nodeKind = AST_EXPR_UNARY_OP; astop->nodeKind = AST_EXPR_UNARY_OP;
astop->constantType = EXPRESSION_NOT_CONSTANT;
astop->operator = UNOP_NEGATE; astop->operator = UNOP_NEGATE;
astop->operand = operand; astop->operand = operand;
astop->type = operand->expression.type; astop->type = operand->expression.type;
@ -233,7 +214,6 @@ AST *nct_parse_expression(Parser *P, int lOP) {
} else { } else {
ASTExprUnaryOp *astop = malloc(sizeof(*astop)); ASTExprUnaryOp *astop = malloc(sizeof(*astop));
astop->nodeKind = AST_EXPR_UNARY_OP; astop->nodeKind = AST_EXPR_UNARY_OP;
astop->constantType = EXPRESSION_NOT_CONSTANT;
astop->operator = UNOP_BITWISE_NOT; astop->operator = UNOP_BITWISE_NOT;
astop->operand = child; astop->operand = child;
astop->type = child->expression.type; astop->type = child->expression.type;
@ -252,7 +232,6 @@ AST *nct_parse_expression(Parser *P, int lOP) {
ASTExprCall *call = malloc(sizeof(*call)); ASTExprCall *call = malloc(sizeof(*call));
call->nodeKind = AST_EXPR_CALL; call->nodeKind = AST_EXPR_CALL;
call->constantType = EXPRESSION_NOT_CONSTANT;
call->type = ret->expression.type->function.ret; call->type = ret->expression.type->function.ret;
call->what = ret; call->what = ret;
call->args = NULL; call->args = NULL;
@ -277,14 +256,12 @@ AST *nct_parse_expression(Parser *P, int lOP) {
} else if(maybe(P, TOKEN_SQUAREN_L)) { } else if(maybe(P, TOKEN_SQUAREN_L)) {
ASTExprUnaryOp *ref = malloc(sizeof(*ref)); ASTExprUnaryOp *ref = malloc(sizeof(*ref));
ref->nodeKind = AST_EXPR_UNARY_OP; ref->nodeKind = AST_EXPR_UNARY_OP;
ref->constantType = EXPRESSION_NOT_CONSTANT;
ref->operator = UNOP_REF; ref->operator = UNOP_REF;
ref->operand = ret; ref->operand = ret;
ref->type = type_pointer_wrap(ret->expression.type->array.of); ref->type = type_pointer_wrap(ret->expression.type->array.of);
ASTExprBinaryOp *child = malloc(sizeof(*child)); ASTExprBinaryOp *child = malloc(sizeof(*child));
child->nodeKind = AST_EXPR_BINARY_OP; child->nodeKind = AST_EXPR_BINARY_OP;
child->constantType = EXPRESSION_NOT_CONSTANT;
child->operands[0] = (AST*) ref; child->operands[0] = (AST*) ref;
child->operands[1] = nct_parse_expression(P, 0); child->operands[1] = nct_parse_expression(P, 0);
child->operator = BINOP_ADD; child->operator = BINOP_ADD;
@ -308,7 +285,6 @@ AST *nct_parse_expression(Parser *P, int lOP) {
ASTExprUnaryOp *unop = malloc(sizeof(*unop)); ASTExprUnaryOp *unop = malloc(sizeof(*unop));
unop->nodeKind = AST_EXPR_UNARY_OP; unop->nodeKind = AST_EXPR_UNARY_OP;
unop->constantType = EXPRESSION_NOT_CONSTANT;
unop->type = ret->expression.type->array.of; unop->type = ret->expression.type->array.of;
unop->operator = UNOP_DEREF; unop->operator = UNOP_DEREF;
unop->operand = (AST*) child; unop->operand = (AST*) child;
@ -332,7 +308,6 @@ AST *nct_parse_expression(Parser *P, int lOP) {
ASTExprBinaryOp *astop = malloc(sizeof(*astop)); ASTExprBinaryOp *astop = malloc(sizeof(*astop));
astop->nodeKind = AST_EXPR_BINARY_OP; astop->nodeKind = AST_EXPR_BINARY_OP;
astop->constantType = EXPRESSION_NOT_CONSTANT;
astop->type = ret->expression.type; astop->type = ret->expression.type;
astop->operator = op; astop->operator = op;
astop->operands[0] = ret; astop->operands[0] = ret;
@ -361,7 +336,6 @@ AST *nct_parse_expression(Parser *P, int lOP) {
AST *operand2 = malloc(sizeof(ASTExprVar)); AST *operand2 = malloc(sizeof(ASTExprVar));
operand2->nodeKind = AST_EXPR_VAR; operand2->nodeKind = AST_EXPR_VAR;
operand2->expression.type = operand->expression.type; operand2->expression.type = operand->expression.type;
operand2->expression.constantType = EXPRESSION_NOT_CONSTANT;
operand2->exprVar.thing = thing; operand2->exprVar.thing = thing;
operand = operand2; operand = operand2;
} }
@ -402,7 +376,6 @@ AST *nct_parse_expression(Parser *P, int lOP) {
ASTExprBinaryOp *astop = malloc(sizeof(*astop)); ASTExprBinaryOp *astop = malloc(sizeof(*astop));
astop->nodeKind = AST_EXPR_BINARY_OP; astop->nodeKind = AST_EXPR_BINARY_OP;
astop->constantType = EXPRESSION_NOT_CONSTANT;
astop->type = ret->expression.type; astop->type = ret->expression.type;
astop->operator = op; astop->operator = op;
astop->operands[0] = ret; astop->operands[0] = ret;
@ -438,7 +411,6 @@ AST *nct_parse_expression(Parser *P, int lOP) {
ASTExprBinaryOp *astop = malloc(sizeof(*astop)); ASTExprBinaryOp *astop = malloc(sizeof(*astop));
astop->nodeKind = AST_EXPR_BINARY_OP; astop->nodeKind = AST_EXPR_BINARY_OP;
astop->constantType = EXPRESSION_NOT_CONSTANT;
astop->type = NULL; astop->type = NULL;
astop->operator = op; astop->operator = op;
astop->operands[0] = ret; astop->operands[0] = ret;
@ -613,9 +585,9 @@ static AST *parse_declaration(Parser *P) {
ret->expression = nct_cast_expr(nct_parse_expression(P, 0), type); ret->expression = nct_cast_expr(nct_parse_expression(P, 0), type);
if(ret->expression) { if(ret->expression) {
if(ret->expression->expression.constantType == EXPRESSION_NOT_CONSTANT) { //if(ret->expression->expression.constantType == EXPRESSION_NOT_CONSTANT) {
stahp(1, 4142, "Symbol declaration may contain constant expressions only."); // stahp(1, 4142, "Symbol declaration may contain constant expressions only.");
} //}
} }
} else if(isExternal) { } else if(isExternal) {
entry->kind = VARTABLEENTRY_SYMBOL; entry->kind = VARTABLEENTRY_SYMBOL;