Support conditionals for tables/arrays/sets
This commit is contained in:
parent
989dc3f442
commit
0ca48c98e8
13
compile.c
13
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));
|
||||
|
Loading…
Reference in New Issue
Block a user