New cast '->'

This commit is contained in:
Krzosa Karol
2022-06-13 15:38:54 +02:00
parent 2503a6d680
commit f9487a2c24
6 changed files with 22 additions and 46 deletions

17
ast.cpp
View File

@@ -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);

View File

@@ -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();

View File

@@ -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

View File

@@ -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

View File

@@ -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;

View File

@@ -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);