From f9487a2c24738cbf4f4fcb47a06de8da63ae456c Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Mon, 13 Jun 2022 15:38:54 +0200 Subject: [PATCH] New cast '->' --- ast.cpp | 17 +---------------- ccodegen.cpp | 19 +++++++++---------- lambdas.kl | 2 +- new_types.kl | 4 ++-- parsing.cpp | 10 ++-------- typechecking.cpp | 16 +++++++--------- 6 files changed, 22 insertions(+), 46 deletions(-) diff --git a/ast.cpp b/ast.cpp index 2470ec3..b25a290 100644 --- a/ast.cpp +++ b/ast.cpp @@ -111,13 +111,6 @@ struct Ast_Load: Ast{ String string; }; -struct Ast_Cast: Ast_Expr{ - Ast_Expr *expr; - Ast_Expr *typespec; - Ast_Type *before_type; - Ast_Type *after_type; -}; - struct Ast_Index: Ast_Expr{ Ast_Expr *expr; Ast_Expr *index; @@ -130,6 +123,7 @@ struct Ast_Binary: Ast_Expr{ Ast_Expr *right; Ast_Type *type; + Ast_Type *before_type; }; // Problem: We are parsing out of order, in the middle of parsing a function @@ -314,15 +308,6 @@ ast_call_item(Token *pos, Ast_Atom *name, Ast_Expr *index, Ast_Expr *item){ return result; } -function Ast_Expr * -ast_expr_cast(Token *pos, Ast_Expr *expr, Ast_Expr *typespec){ - AST_NEW(Cast, CAST, pos, AST_EXPR); - result->flags = AST_EXPR; - result->expr = expr; - result->typespec = typespec; - return result; -} - function Ast_Expr * ast_expr_unary(Token *pos, Token_Kind op, Ast_Expr *expr){ AST_NEW(Unary, UNARY, pos, AST_EXPR); diff --git a/ccodegen.cpp b/ccodegen.cpp index f8bc6ff..334a333 100644 --- a/ccodegen.cpp +++ b/ccodegen.cpp @@ -197,6 +197,15 @@ gen_expr(Ast_Expr *ast){ gen_expr(node->right); return; } + else if(node->op == TK_Arrow){ + gen("("); + gen("("); + gen_simple_decl(node->type); + gen(")"); + gen_expr(node->left); + gen(")"); + return; + } if(!token_is_assign(node->op)) gen("("); gen_expr(node->left); @@ -216,16 +225,6 @@ gen_expr(Ast_Expr *ast){ BREAK(); } - CASE(CAST, Cast){ - gen("("); - gen("("); - gen_simple_decl(node->after_type); - gen(")"); - gen_expr(node->expr); - gen(")"); - BREAK(); - } - CASE(VAR, Decl){ gen_ast(node); BREAK(); diff --git a/lambdas.kl b/lambdas.kl index f9dbcfa..9aea6d4 100644 --- a/lambdas.kl +++ b/lambdas.kl @@ -6,7 +6,7 @@ test: Test member := test.len enum_val: Memory.Allocator_Kind = Memory.Allocator_Kind.Heap -other_enum_val: S64 = cast(enum_val: S64) +other_enum_val: S64 = enum_val->S64 a_type :: S64 pointer_type :: *S64 diff --git a/new_types.kl b/new_types.kl index 15b94c0..04170de 100644 --- a/new_types.kl +++ b/new_types.kl @@ -12,7 +12,7 @@ unary_test :: () notb := !true neg64: S64 = ~int_val neg32: S32 = ~int_val - big_neg32: U32 = ~cast(41512512: U32) + big_neg32: U32 = ~41512512->U32 var1: S64 var2: S64 = 20 @@ -49,7 +49,7 @@ binary_test :: (thing: S32 = 442) boolean_equals :: true == false boolean_var: Bool = boolean_equals - cast_value :: 4242 + cast(32: S32) + cast(42: S32) + cast_value :: 4242 + 32->S32 + 42->S32 value: S32 = cast_value bvar2 := int_val > 1 diff --git a/parsing.cpp b/parsing.cpp index 4c8637d..2f6b90b 100644 --- a/parsing.cpp +++ b/parsing.cpp @@ -403,6 +403,8 @@ binding_power(Binding binding, Token_Kind kind){ return {17,18}; case TK_Dot: return {24,23}; + case TK_Arrow: + return {31,30}; default: return {}; } Postfix: switch(kind){ @@ -457,14 +459,6 @@ parse_expr(S64 min_bp){ case TK_Keyword: { if(token->intern_val == keyword_true) left = ast_bool(token, 1); else if(token->intern_val == keyword_false) left = ast_bool(token, 0); - else if(token->intern_val == keyword_cast){ - token_expect(TK_OpenParen); - Ast_Expr *expr = parse_expr(0); - token_expect(TK_Colon); - Ast_Expr *typespec = parse_expr(0); - token_expect(TK_CloseParen); - left = ast_expr_cast(token, expr, typespec); - } else compiler_error(token, "Unexpected keyword: [%s], expected keyword [cast]", token->intern_val.str); }break; diff --git a/typechecking.cpp b/typechecking.cpp index 1f25275..3baf66f 100644 --- a/typechecking.cpp +++ b/typechecking.cpp @@ -511,9 +511,9 @@ try_resolving_lambda_scope(Operand *op, Ast_Lambda *lambda, Ast_Type *lambda_typ } function Operand -resolve_cast(Ast_Cast *node){ - Operand expr = resolve_expr(node->expr, AST_CANT_BE_NULL); - Ast_Type *type = resolve_typespec(node->typespec, AST_CANT_BE_NULL); +resolve_cast(Ast_Binary *node){ + Operand expr = resolve_expr(node->left, AST_CANT_BE_NULL); + Ast_Type *type = resolve_typespec(node->right, AST_CANT_BE_NULL); Ast_Type *original_type = expr.type; node->before_type = expr.type; @@ -555,7 +555,7 @@ resolve_cast(Ast_Cast *node){ assert(original_type != type ? expr.type == type : 1); if(expr.is_const) check_value_bounds(node->pos, &expr.value); - node->after_type = expr.type; + node->type = expr.type; return expr; } @@ -743,6 +743,9 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context){ node->type = right.type; return {}; } + else if(node->op == TK_Arrow){ + return resolve_cast(node); + } else if(node->op == TK_Dot){ Operand op = resolve_field_access(node, 0); if(op.is_const){ @@ -800,11 +803,6 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context){ BREAK(); } - CASE(CAST, Cast){ - return resolve_cast(node); - BREAK(); - } - CASE(COMPOUND, Call){ Operand op = resolve_expr(node->typespec, AST_CAN_BE_NULL);