aboutsummaryrefslogtreecommitdiff
path: root/src/check_expr.cpp
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2022-08-11 13:01:54 +0100
committergingerBill <bill@gingerbill.org>2022-08-11 13:01:54 +0100
commit38102f14c19f83ef1e0c13a824448bab4e80877e (patch)
tree15aa1dcfeb2a8e427bf6859f4b10848be2d19bff /src/check_expr.cpp
parent0997df4fcf735830ee6a03867712ce89aa4255e1 (diff)
Add `#load(path) or_else default` in favour of `#load_or(path, default)`
Diffstat (limited to 'src/check_expr.cpp')
-rw-r--r--src/check_expr.cpp70
1 files changed, 68 insertions, 2 deletions
diff --git a/src/check_expr.cpp b/src/check_expr.cpp
index f6c94466b..ae459574c 100644
--- a/src/check_expr.cpp
+++ b/src/check_expr.cpp
@@ -121,6 +121,28 @@ void check_or_return_split_types(CheckerContext *c, Operand *x, String const &na
bool is_diverging_expr(Ast *expr);
+
+enum LoadDirectiveResult {
+ LoadDirective_Success = 0,
+ LoadDirective_Error = 1,
+ LoadDirective_NotFound = 2,
+};
+
+bool is_load_directive_call(Ast *call) {
+ call = unparen_expr(call);
+ if (call->kind != Ast_CallExpr) {
+ return false;
+ }
+ ast_node(ce, CallExpr, call);
+ if (ce->proc->kind != Ast_BasicDirective) {
+ return false;
+ }
+ ast_node(bd, BasicDirective, ce->proc);
+ String name = bd->name.string;
+ return name == "load";
+}
+LoadDirectiveResult check_load_directive(CheckerContext *c, Operand *operand, Ast *call, Type *type_hint, bool err_on_not_found);
+
void check_did_you_mean_print(DidYouMeanAnswers *d, char const *prefix = "") {
auto results = did_you_mean_results(d);
if (results.count != 0) {
@@ -7407,9 +7429,54 @@ ExprKind check_or_else_expr(CheckerContext *c, Operand *o, Ast *node, Type *type
String name = oe->token.string;
Ast *arg = oe->x;
Ast *default_value = oe->y;
-
Operand x = {};
Operand y = {};
+
+ // NOTE(bill, 2022-08-11): edge case to handle #load(path) or_else default
+ if (is_load_directive_call(arg)) {
+ LoadDirectiveResult res = check_load_directive(c, &x, arg, type_hint, false);
+ if (res == LoadDirective_Success) {
+ *o = x;
+ return Expr_Expr;
+ }
+
+ bool y_is_diverging = false;
+ check_expr_base(c, &y, default_value, x.type);
+ switch (y.mode) {
+ case Addressing_NoValue:
+ if (is_diverging_expr(y.expr)) {
+ // Allow
+ y.mode = Addressing_Value;
+ y_is_diverging = true;
+ } else {
+ error_operand_no_value(&y);
+ y.mode = Addressing_Invalid;
+ }
+ break;
+ case Addressing_Type:
+ error_operand_not_expression(&y);
+ y.mode = Addressing_Invalid;
+ break;
+ }
+
+ if (y.mode == Addressing_Invalid) {
+ o->mode = Addressing_Value;
+ o->type = t_invalid;
+ o->expr = node;
+ return Expr_Expr;
+ }
+
+ if (!y_is_diverging) {
+ check_assignment(c, &y, x.type, name);
+ }
+
+ o->mode = y.mode;
+ o->type = y.type;
+ o->expr = node;
+
+ return Expr_Expr;
+ }
+
check_multi_expr_with_type_hint(c, &x, arg, type_hint);
if (x.mode == Addressing_Invalid) {
o->mode = Addressing_Value;
@@ -7417,7 +7484,6 @@ ExprKind check_or_else_expr(CheckerContext *c, Operand *o, Ast *node, Type *type
o->expr = node;
return Expr_Expr;
}
-
bool y_is_diverging = false;
check_expr_base(c, &y, default_value, x.type);
switch (y.mode) {