Skip to content
Merged
Show file tree
Hide file tree
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
11 changes: 10 additions & 1 deletion src/interpreter.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,20 @@ export function parse(tokens) {
const name = expect("id").v;
expect("punc", "(");
const args = [];
let expectArg = true;
while (peek() && !(peek().t === "punc" && peek().v === ")")) {
const a = next();
if (a.t === "punc" && a.v === ",") continue;
if (a.t === "punc" && a.v === ",") {
if (expectArg) throw new Error("moshscript: expected argument before comma");
expectArg = true;
continue;
}
if (!expectArg) throw new Error("moshscript: expected comma between arguments");
if (a.t === "punc") throw new Error(`moshscript: unexpected ${JSON.stringify(a.v)}`);
args.push(a.v);
expectArg = false;
}
if (expectArg && args.length) throw new Error("moshscript: expected argument after comma");
expect("punc", ")");
if (peek() && peek().t === "punc" && peek().v === ";") next(); // optional ;
return { type: "call", name, args };
Expand Down
12 changes: 12 additions & 0 deletions test/interpreter.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,15 @@ test("compile preserves valid moshscript behavior", () => {
args: ["hi"],
});
});

test("compile requires commas between call arguments", () => {
assert.throws(() => compile("say(\"one\" \"two\");"), /expected comma/);
assert.throws(() => compile("say(\"one\",);"), /expected argument after comma/);
assert.throws(() => compile("say(,\"one\");"), /expected argument before comma/);

assert.deepEqual(compile("say(\"one\", \"two\");").body[0], {
type: "call",
name: "say",
args: ["one", "two"],
});
});
Loading