Skip to content

Commit 73a2c62

Browse files
committed
add string capability for case statement
1 parent 86b6375 commit 73a2c62

2 files changed

Lines changed: 26 additions & 5 deletions

File tree

compiler.cpp

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -685,24 +685,47 @@ void CaseStatement::generate(Emitter &emitter) const
685685

686686
void CaseStatement::ComparisonWhenCondition::generate(Emitter &emitter) const
687687
{
688-
static const unsigned char op[] = {EQN, NEN, LTN, GTN, LEN, GEN};
688+
static const unsigned char opn[] = {EQN, NEN, LTN, GTN, LEN, GEN};
689+
static const unsigned char ops[] = {EQS, NES, LTS, GTS, LES, GES};
690+
const unsigned char *op;
691+
if (expr->type == TYPE_NUMBER) {
692+
op = opn;
693+
} else if (expr->type == TYPE_STRING) {
694+
op = ops;
695+
} else {
696+
fprintf(stderr, "compiler internal error: when condition not number or string");
697+
abort();
698+
}
699+
689700
emitter.emit(DUP);
690701
expr->generate(emitter);
691702
emitter.emit(op[comp]);
692703
}
693704

694705
void CaseStatement::RangeWhenCondition::generate(Emitter &emitter) const
695706
{
707+
static const unsigned char opn[] = {EQN, NEN, LTN, GTN, LEN, GEN};
708+
static const unsigned char ops[] = {EQS, NES, LTS, GTS, LES, GES};
709+
const unsigned char *op;
710+
if (low_expr->type == TYPE_NUMBER) {
711+
op = opn;
712+
} else if (low_expr->type == TYPE_STRING) {
713+
op = ops;
714+
} else {
715+
fprintf(stderr, "compiler internal error: when condition not number or string");
716+
abort();
717+
}
718+
696719
emitter.emit(DUP);
697720
auto result_label = emitter.create_label();
698721
low_expr->generate(emitter);
699-
emitter.emit(GEN);
722+
emitter.emit(op[ComparisonExpression::GE]);
700723
emitter.emit(DUP);
701724
emitter.emit_jump(JF, result_label);
702725
emitter.emit(DROP);
703726
emitter.emit(DUP);
704727
high_expr->generate(emitter);
705-
emitter.emit(LEN);
728+
emitter.emit(op[ComparisonExpression::LE]);
706729
emitter.jump_target(result_label);
707730
}
708731

t/case3.simple

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
% TODO
2-
31
VAR a: String
42

53
a := "hello"

0 commit comments

Comments
 (0)