Skip to content

Commit 86b6375

Browse files
committed
check type and constness for when clauses
1 parent 8f55aba commit 86b6375

2 files changed

Lines changed: 28 additions & 9 deletions

File tree

parser.cpp

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,9 @@ static const Statement *parseCaseStatement(Scope *scope, const std::vector<Token
845845
{
846846
++i;
847847
const Expression *expr = parseExpression(scope, tokens, i);
848+
if (expr->type != TYPE_NUMBER && expr->type != TYPE_STRING) {
849+
error(tokens[i], "CASE expression must be Number or String");
850+
}
848851
std::vector<std::pair<std::vector<const CaseStatement::WhenCondition *>, std::vector<const Statement *>>> clauses;
849852
while (tokens[i].type == WHEN) {
850853
std::vector<const CaseStatement::WhenCondition *> conditions;
@@ -859,20 +862,38 @@ static const Statement *parseCaseStatement(Scope *scope, const std::vector<Token
859862
case GREATEREQ: {
860863
auto op = tokens[i];
861864
++i;
862-
const Expression *expr = parseExpression(scope, tokens, i);
863-
const CaseStatement::WhenCondition *cond = new CaseStatement::ComparisonWhenCondition(comparisonFromToken(op), expr);
865+
const Expression *when = parseExpression(scope, tokens, i);
866+
if (when->type != expr->type) {
867+
error(tokens[i], "type mismatch");
868+
}
869+
if (not when->is_constant) {
870+
error(tokens[i], "WHEN condition must be constant");
871+
}
872+
const CaseStatement::WhenCondition *cond = new CaseStatement::ComparisonWhenCondition(comparisonFromToken(op), when);
864873
conditions.push_back(cond);
865874
break;
866875
}
867876
default: {
868-
const Expression *expr = parseExpression(scope, tokens, i);
877+
const Expression *when = parseExpression(scope, tokens, i);
878+
if (when->type != expr->type) {
879+
error(tokens[i], "type mismatch");
880+
}
881+
if (not when->is_constant) {
882+
error(tokens[i], "WHEN condition must be constant");
883+
}
869884
if (tokens[i].type == DOTDOT) {
870885
++i;
871-
const Expression *expr2 = parseExpression(scope, tokens, i);
872-
const CaseStatement::WhenCondition *cond = new CaseStatement::RangeWhenCondition(expr, expr2);
886+
const Expression *when2 = parseExpression(scope, tokens, i);
887+
if (when2->type != expr->type) {
888+
error(tokens[i], "type mismatch");
889+
}
890+
if (not when2->is_constant) {
891+
error(tokens[i], "WHEN condition must be constant");
892+
}
893+
const CaseStatement::WhenCondition *cond = new CaseStatement::RangeWhenCondition(when, when2);
873894
conditions.push_back(cond);
874895
} else {
875-
const CaseStatement::WhenCondition *cond = new CaseStatement::ComparisonWhenCondition(ComparisonExpression::EQ, expr);
896+
const CaseStatement::WhenCondition *cond = new CaseStatement::ComparisonWhenCondition(ComparisonExpression::EQ, when);
876897
conditions.push_back(cond);
877898
}
878899
break;

t/case2.simple

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
% TODO
2-
31
VAR a, b: Number
42

53
CASE a
64
WHEN b DO
75
END
86

9-
%! when condition must be constant
7+
%! WHEN condition must be constant

0 commit comments

Comments
 (0)