From 0ca48c98e897c6edd800c0aefabd20cc4e7a5409 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 11 Sep 2024 01:51:16 -0400 Subject: Support conditionals for tables/arrays/sets --- compile.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'compile.c') diff --git a/compile.c b/compile.c index bfdaa091..af1a32ac 100644 --- a/compile.c +++ b/compile.c @@ -1206,16 +1206,17 @@ CORD compile_statement(env_t *env, ast_t *ast) } type_t *cond_t = get_type(env, condition); - if (cond_t->tag == PointerType) { + if (cond_t->tag == PointerType) code_err(condition, "This pointer will always be non-null, so it should not be used in a conditional."); - } else if (cond_t->tag != BoolType && cond_t->tag != TextType && cond_t->tag != OptionalType) { - code_err(condition, "Only boolean values, optional pointers, and text can be used in conditionals (this is a %T)", cond_t); - } env_t *truthy_scope = env; CORD condition_code; if (cond_t->tag == TextType) { condition_code = CORD_all("(", compile(env, condition), ").length"); + } else if (cond_t->tag == ArrayType) { + condition_code = CORD_all("(", compile(env, condition), ").length"); + } else if (cond_t->tag == TableType || cond_t->tag == SetType) { + condition_code = CORD_all("(", compile(env, condition), ").entries.length"); } else if (cond_t->tag == OptionalType) { if (condition->tag == Var) { truthy_scope = fresh_scope(env); @@ -1228,8 +1229,10 @@ CORD compile_statement(env_t *env, ast_t *ast) set_binding(truthy_scope, varname, nonnull_b); } condition_code = compile_optional_check(env, condition); - } else { + } else if (cond_t->tag == BoolType) { condition_code = compile(env, condition); + } else { + code_err(condition, "%T values cannot be used for conditionals", cond_t); } code = CORD_all(code, "if (", condition_code, ")", compile_statement(truthy_scope, if_->body)); -- cgit v1.2.3