From eb13d3d98dabfd179920472165448cf34c4f2723 Mon Sep 17 00:00:00 2001 From: aiirvizionz Date: Fri, 10 Jul 2026 22:56:45 -0600 Subject: [PATCH] Require commas between moshscript arguments --- src/interpreter.mjs | 11 ++++++++++- test/interpreter.test.mjs | 12 ++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/interpreter.mjs b/src/interpreter.mjs index dd1c647..fb5bcdd 100644 --- a/src/interpreter.mjs +++ b/src/interpreter.mjs @@ -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 }; diff --git a/test/interpreter.test.mjs b/test/interpreter.test.mjs index 399cf04..41f71d0 100644 --- a/test/interpreter.test.mjs +++ b/test/interpreter.test.mjs @@ -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"], + }); +});