Skip to content

Commit bf7484a

Browse files
authored
Merge pull request #27 from geokoko/26-expressions-in-if-conditions
Fix erroneous rejection of boolean conditions
2 parents 7b247ba + 0ff6a36 commit bf7484a

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

src/frontend/semantic/semantic_pass.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -816,8 +816,14 @@ void SemanticPass::visit(ExprCond& n) {
816816
if (auto* expr = n.expression()) {
817817
expr->accept(*this);
818818
}
819-
// Dana does not allow bare expressions as conditions;
819+
// Dana does not allow bare expressions as conditions - except boolean literals;
820820
// conditions must be relational (=, <>, <, >, <=, >=) or logical (and, or, not)
821+
// for variables of type byte.
822+
if (dynamic_cast<TrueConst*>(n.expression()) ||
823+
dynamic_cast<FalseConst*>(n.expression())) {
824+
n.setType(makeByteType());
825+
return;
826+
}
821827
context_.diags().report(Diagnostics::Severity::Error, Diagnostics::Phase::Semantic,
822828
n.loc, "bare expression cannot be used as condition");
823829
n.setType(makeByteType());

tests/programs/conds.dana

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
def cond
2+
var a is byte
3+
var x y sum is int
4+
x := 10
5+
y := 0
6+
a := true
7+
sum := 0
8+
9+
loop:
10+
if a = false:
11+
break
12+
if a = true and x >= y:
13+
sum := sum + x * y
14+
x := x-1
15+
elif true:
16+
if y <= 10:
17+
x := 10
18+
elif not false:
19+
a := false
20+
y := y + 1
21+
if sum = 1705:
22+
writeString: "42\n"
23+
else:
24+
writeString: "wrong!!!\n"

tests/test_compiler.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import subprocess
44
import glob
55
import difflib
6+
import warnings
67

78
# Paths
89
TESTS_ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
@@ -118,7 +119,10 @@ def test_dana_program(compiler, dana_file):
118119
diff_text = "\n".join(diff)
119120
pytest.fail(f"Output mismatch for {base_name}:\n{diff_text}")
120121
else:
121-
pytest.warns(None, match=f"No .result file for {base_name} — skipping output verification")
122+
warnings.warn(
123+
f"No .result file for {base_name} — skipping output verification",
124+
UserWarning,
125+
)
122126

123127
finally:
124128
# Cleanup a.out

0 commit comments

Comments
 (0)