From d41b414f2a1a1712a15e17811a47f9a781815a74 Mon Sep 17 00:00:00 2001 From: Ryan Fleury Date: Tue, 29 Apr 2025 14:29:42 -0700 Subject: [PATCH] eliminate very-poorly-thought-through idea of 'chained casts' - we need to recognize cast operators in the parser somehow, otherwise we are using the expr chain for way too many things in many cases... --- src/eval/eval_ir.c | 73 ++++------------------------------------ src/eval/eval_parse.c | 78 ++++++++++++------------------------------- 2 files changed, 29 insertions(+), 122 deletions(-) diff --git a/src/eval/eval_ir.c b/src/eval/eval_ir.c index 5989eff7..fc9ee2fc 100644 --- a/src/eval/eval_ir.c +++ b/src/eval/eval_ir.c @@ -2069,82 +2069,23 @@ e_push_irtree_and_type_from_expr(Arena *arena, E_IRTreeAndType *root_parent, B32 }break; } - //- rjf: if our currently-computed result is just a type, and we have - // a chained expression, then treat the first expression as a cast on - // the second. - if(!disallow_chained_casts && result.mode == E_Mode_Null && expr->next != &e_expr_nil) - { - // rjf: unpack - E_IRTreeAndType cast_type_tree = result; - E_IRTreeAndType casted_tree = e_push_irtree_and_type_from_expr(arena, parent, disallow_autohooks, 1, disallow_chained_casts, expr->next); - e_msg_list_concat_in_place(&result.msgs, &casted_tree.msgs); - E_TypeKey cast_type = cast_type_tree.type_key; - E_TypeKind cast_type_kind = e_type_kind_from_key(cast_type); - U64 cast_type_byte_size = e_type_byte_size_from_key(cast_type); - E_TypeKey casted_type = casted_tree.type_key; - E_TypeKind casted_type_kind = e_type_kind_from_key(casted_type); - U64 casted_type_byte_size = e_type_byte_size_from_key(casted_type); - U8 in_group = e_type_group_from_kind(casted_type_kind); - U8 out_group = e_type_group_from_kind(cast_type_kind); - RDI_EvalConversionKind conversion_rule = rdi_eval_conversion_kind_from_typegroups(in_group, out_group); - - // rjf: bad conditions? -> error if applicable, exit - if(casted_tree.root->op == 0) - { - goto end_cast_gen; - } - else if(cast_type_kind == E_TypeKind_Null) - { - goto end_cast_gen; - } - else if(conversion_rule != RDI_EvalConversionKind_Noop && - conversion_rule != RDI_EvalConversionKind_Legal) - { - String8 text = str8_lit("Unknown cast conversion rule."); - if(conversion_rule < RDI_EvalConversionKind_COUNT) - { - text.str = rdi_explanation_string_from_eval_conversion_kind(conversion_rule, &text.size); - } - e_msg(arena, &result.msgs, E_MsgKind_MalformedInput, expr->location, text); - goto end_cast_gen; - } - - // rjf: generate casted result - { - E_IRNode *in_tree = e_irtree_resolve_to_value(arena, casted_tree.mode, casted_tree.root, casted_type); - E_IRNode *new_tree = in_tree; - if(conversion_rule == RDI_EvalConversionKind_Legal) - { - new_tree = e_irtree_convert_lo(arena, in_tree, out_group, in_group); - } - if(cast_type_byte_size < casted_type_byte_size && e_type_kind_is_integer(cast_type_kind)) - { - new_tree = e_irtree_trunc(arena, in_tree, cast_type); - } - result.root = new_tree; - result.type_key = cast_type; - result.mode = E_Mode_Value; - } - - end_cast_gen:; - } - //- rjf: check chained expressions for simple wrappers if(!disallow_chained_fastpaths) { - struct + local_persist struct { String8 shorthand; String8 full_name; } shorthand_lens_pair_table[] = { - {str8_lit("x"), str8_lit("hex")}, - {str8_lit("b"), str8_lit("bin")}, - {str8_lit("o"), str8_lit("oct")}, - {str8_lit("d"), str8_lit("dec")}, + {str8_lit_comp("x"), str8_lit_comp("hex")}, + {str8_lit_comp("b"), str8_lit_comp("bin")}, + {str8_lit_comp("o"), str8_lit_comp("oct")}, + {str8_lit_comp("d"), str8_lit_comp("dec")}, }; - for(E_Expr *chained_expr = expr->next; + E_Expr *first_chained = expr->next; + for(E_Expr *chained_expr = first_chained; chained_expr != &e_expr_nil; chained_expr = chained_expr->next) { diff --git a/src/eval/eval_parse.c b/src/eval/eval_parse.c index 00ffd23e..61b5c139 100644 --- a/src/eval/eval_parse.c +++ b/src/eval/eval_parse.c @@ -786,6 +786,7 @@ e_push_parse_from_string_tokens__prec(Arena *arena, String8 text, E_TokenArray t ////////////////////////////// //- rjf: parse chain of expressions // + E_Expr *possible_cast = &e_expr_nil; for(U64 chain_count = 0; it < it_opl && chain_count < max_chain_count;) { //////////////////////////// @@ -831,7 +832,6 @@ e_push_parse_from_string_tokens__prec(Arena *arena, String8 text, E_TokenArray t { PrefixUnaryTask *next; E_ExprKind kind; - E_Expr *cast_expr; void *location; }; PrefixUnaryTask *first_prefix_unary = 0; @@ -844,7 +844,6 @@ e_push_parse_from_string_tokens__prec(Arena *arena, String8 text, E_TokenArray t String8 token_string = str8_substr(text, token.range); S64 prefix_unary_precedence = 0; E_ExprKind prefix_unary_kind = 0; - E_Expr *cast_expr = &e_expr_nil; void *location = 0; // rjf: try op table @@ -873,50 +872,6 @@ e_push_parse_from_string_tokens__prec(Arena *arena, String8 text, E_TokenArray t it += 1; } - // rjf: try casting expression -#if 0 - if(prefix_unary_precedence == 0 && str8_match(token_string, str8_lit("("), 0)) - { - E_Token some_type_identifier_maybe = e_token_at_it(it+1, &tokens); - String8 some_type_identifier_maybe_string = str8_substr(text, some_type_identifier_maybe.range); - if(some_type_identifier_maybe.kind == E_TokenKind_Identifier) - { - E_TypeKey type_key = e_leaf_type_from_name(some_type_identifier_maybe_string); - if(!e_type_key_match(type_key, e_type_key_zero()) || str8_match(some_type_identifier_maybe_string, str8_lit("unsigned"), 0)) - { - // rjf: move past open paren - it += 1; - - // rjf: parse type expr - E_Parse type_parse = e_push_type_parse_from_text_tokens(arena, text, e_token_array_make_first_opl(it, it_opl)); - E_Expr *type = type_parse.expr; - e_msg_list_concat_in_place(&result.msgs, &type_parse.msgs); - it = type_parse.last_token; - location = token_string.str; - - // rjf: expect ) - E_Token close_paren_maybe = e_token_at_it(it, &tokens); - String8 close_paren_maybe_string = str8_substr(text, close_paren_maybe.range); - if(close_paren_maybe.kind != E_TokenKind_Symbol || !str8_match(close_paren_maybe_string, str8_lit(")"), 0)) - { - e_msgf(arena, &result.msgs, E_MsgKind_MalformedInput, token_string.str, "Missing `)`."); - } - - // rjf: consume ) - else - { - it += 1; - } - - // rjf: fill - prefix_unary_precedence = 2; - prefix_unary_kind = E_ExprKind_Cast; - cast_expr = type; - } - } - } -#endif - // rjf: break if we got no operators if(prefix_unary_precedence == 0) { @@ -933,7 +888,6 @@ e_push_parse_from_string_tokens__prec(Arena *arena, String8 text, E_TokenArray t { PrefixUnaryTask *op_n = push_array(scratch.arena, PrefixUnaryTask, 1); op_n->kind = prefix_unary_kind; - op_n->cast_expr = cast_expr; op_n->location = location; SLLQueuePushFront(first_prefix_unary, last_prefix_unary, op_n); } @@ -981,6 +935,9 @@ e_push_parse_from_string_tokens__prec(Arena *arena, String8 text, E_TokenArray t atom = nested_parse.expr; it = nested_parse.last_token; + // rjf: this may have been a cast + possible_cast = atom; + // rjf: expect ) E_Token close_paren_maybe = e_token_at_it(it, &tokens); String8 close_paren_maybe_string = str8_substr(text, close_paren_maybe.range); @@ -1269,6 +1226,13 @@ e_push_parse_from_string_tokens__prec(Arena *arena, String8 text, E_TokenArray t } } + // rjf: if this is a postfix unary, then we know the previous expression + // could not have been a cast. + if(is_postfix_unary) + { + possible_cast = &e_expr_nil; + } + // rjf: quit if this doesn't look like any patterns of postfix unary we know if(!is_postfix_unary) { @@ -1277,12 +1241,16 @@ e_push_parse_from_string_tokens__prec(Arena *arena, String8 text, E_TokenArray t } //////////////////////////// - //- rjf: no `atom`, but we have a cast expression? -> just evaluate the type as the atom - // - if(atom == &e_expr_nil && first_prefix_unary != 0 && first_prefix_unary->cast_expr != &e_expr_nil) + //- rjf: if we have an atom, and we have a potential cast expression, then remove + // the cast from the chain, and build a cast tree + if(atom != &e_expr_nil && possible_cast != &e_expr_nil && atom != possible_cast) { - atom = first_prefix_unary->cast_expr; - first_prefix_unary = first_prefix_unary->next; + E_Expr *casted = atom; + DLLRemove_NPZ(&e_expr_nil, result.expr, result.last_expr, possible_cast, next, prev); + atom = e_push_expr(arena, E_ExprKind_Cast, atom->location); + e_expr_push_child(atom, possible_cast); + e_expr_push_child(atom, casted); + possible_cast = &e_expr_nil; } //////////////////////////// @@ -1296,10 +1264,6 @@ e_push_parse_from_string_tokens__prec(Arena *arena, String8 text, E_TokenArray t { E_Expr *rhs = atom; atom = e_push_expr(arena, prefix_unary->kind, prefix_unary->location); - if(prefix_unary->cast_expr != &e_expr_nil) - { - e_expr_push_child(atom, prefix_unary->cast_expr); - } e_expr_push_child(atom, rhs); } } @@ -1338,6 +1302,7 @@ e_push_parse_from_string_tokens__prec(Arena *arena, String8 text, E_TokenArray t // precedence if(binary_precedence != 0 && binary_precedence <= max_precedence) { + possible_cast = &e_expr_nil; E_Parse rhs_expr_parse = e_push_parse_from_string_tokens__prec(arena, text, e_token_array_make_first_opl(it+1, it_opl), binary_precedence-1, 1); e_msg_list_concat_in_place(&result.msgs, &rhs_expr_parse.msgs); E_Expr *rhs = rhs_expr_parse.expr; @@ -1370,6 +1335,7 @@ e_push_parse_from_string_tokens__prec(Arena *arena, String8 text, E_TokenArray t if(token.kind == E_TokenKind_Symbol && str8_match(token_string, str8_lit("?"), 0) && 13 <= max_precedence) { it += 1; + possible_cast = &e_expr_nil; // rjf: parse middle expression E_Parse middle_expr_parse = e_push_parse_from_string_tokens__prec(arena, text, e_token_array_make_first_opl(it, it_opl), e_max_precedence, 1);