diff --git a/self_hosted/evaluate.jou b/self_hosted/evaluate.jou index e2b0c0e8..863bc152 100644 --- a/self_hosted/evaluate.jou +++ b/self_hosted/evaluate.jou @@ -22,6 +22,14 @@ def evaluate_condition(expr: AstExpression*) -> bool: return True if v == 0: return False + + if expr->kind == AstExpressionKind::And: + return evaluate_condition(&expr->operands[0]) and evaluate_condition(&expr->operands[1]) + if expr->kind == AstExpressionKind::Or: + return evaluate_condition(&expr->operands[0]) or evaluate_condition(&expr->operands[1]) + if expr->kind == AstExpressionKind::Not: + return not evaluate_condition(&expr->operands[0]) + fail(expr->location, "cannot evaluate condition at compile time") diff --git a/src/evaluate.c b/src/evaluate.c index dccf373a..8af47a3f 100644 --- a/src/evaluate.c +++ b/src/evaluate.c @@ -36,6 +36,13 @@ static bool evaluate_condition(const AstExpression *expr) return (bool)v; } + if (expr->kind == AST_EXPR_AND) + return evaluate_condition(&expr->data.operands[0]) && evaluate_condition(&expr->data.operands[1]); + if (expr->kind == AST_EXPR_OR) + return evaluate_condition(&expr->data.operands[0]) || evaluate_condition(&expr->data.operands[1]); + if (expr->kind == AST_EXPR_NOT) + return !evaluate_condition(&expr->data.operands[0]); + fail(expr->location, "cannot evaluate condition at compile time"); } diff --git a/tests/should_succeed/compile_time_if.jou b/tests/should_succeed/compile_time_if.jou index 1374e312..9aad81fd 100644 --- a/tests/should_succeed/compile_time_if.jou +++ b/tests/should_succeed/compile_time_if.jou @@ -12,6 +12,21 @@ else: # python uses 777 as default perms, see help(os.mkdir) mkdir("tmp/tests/foo", 0o777) + +# Test and,or,not operators +if WINDOWS or MACOS: + def bar1() -> None: + printf("One") + def bar2() -> None: + printf("Two\n") + +if not WINDOWS and not MACOS: + def bar1() -> None: + printf("OneTwo\n") + def bar2() -> None: + pass + + def main() -> int: make_foo() f = fopen("tmp/tests/foo/bar", "w") @@ -19,4 +34,8 @@ def main() -> int: puts("ok") # Output: ok fclose(f) + # Output: OneTwo + bar1() + bar2() + return 0