Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifying of case comparison type #986

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/lparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3212,6 +3212,16 @@ static void lgoto (LexState *ls, TString *name, int line) {
}


static BinOpr testcondtype (LexState *ls, BinOpr fallback) {
if (testnext(ls, TK_EQ)) return OPR_EQ;
if (testnext(ls, '<')) return OPR_LT;
if (testnext(ls, TK_LE)) return OPR_LE;
if (testnext(ls, TK_NE)) return OPR_NE;
if (testnext(ls, '>')) return OPR_GT;
if (testnext(ls, TK_GE)) return OPR_GE;
return fallback;
}

static std::vector<int> casecond (LexState *ls, const expdesc& ctrl, int tk) {
std::vector<int> jumps{};
FuncState *fs = ls->fs;
Expand All @@ -3222,17 +3232,19 @@ static std::vector<int> casecond (LexState *ls, const expdesc& ctrl, int tk) {
expr_flags |= E_NO_CONSUME_COLON;
}

BinOpr op = testcondtype(ls, OPR_EQ);
expdesc e, cmpval;
e = ctrl;
luaK_infix(fs, OPR_EQ, &e);
luaK_infix(fs, op, &e);
expr(ls, &cmpval, nullptr, expr_flags);
luaK_posfix(fs, OPR_EQ, &e, &cmpval, case_line);
luaK_posfix(fs, op, &e, &cmpval, case_line);
jumps.emplace_back(e.u.pc);
while (testnext(ls, ',')) {
op = testcondtype(ls, op);
e = ctrl;
luaK_infix(fs, OPR_EQ, &e);
luaK_infix(fs, op, &e);
expr(ls, &cmpval, nullptr, expr_flags);
luaK_posfix(fs, OPR_EQ, &e, &cmpval, case_line);
luaK_posfix(fs, op, &e, &cmpval, case_line);
jumps.emplace_back(e.u.pc);
}
checknext(ls, tk);
Expand Down