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
2 changes: 1 addition & 1 deletion src/interpreter.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// flag is truthy, bounded by ctx.maxIterations. Commands run top-to-bottom.

export function tokenize(src) {
const re = /("(?:[^"\\]|\\.)*")|(\/\/[^\n]*)|(\d+(?:\.\d+)?)|([A-Za-z_]\w*)|([(){};,])|(\s+)/g;
const re = /("(?:[^"\\]|\\.)*")|(\/\/[^\n]*)|(-?\d+(?:\.\d+)?)|([A-Za-z_]\w*)|([(){};,])|(\s+)/g;
const tokens = [];
let m;
let lastIndex = 0;
Expand Down
15 changes: 14 additions & 1 deletion test/interpreter.test.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import assert from "node:assert/strict";
import test from "node:test";
import { compile, tokenize } from "../src/interpreter.mjs";
import { defaultCommands } from "../src/commands.mjs";
import { compile, run, tokenize } from "../src/interpreter.mjs";

test("tokenize rejects unexpected characters between valid tokens", () => {
assert.throws(() => tokenize("say(@);"), /unexpected character/);
Expand All @@ -15,6 +16,18 @@ test("compile preserves valid moshscript behavior", () => {
});
});

test("negative numeric arguments reach command validation", async () => {
const ast = compile("sleep(-1);");
const ctx = {
vars: { alive: true },
iter: 0,
maxIterations: 1,
commands: defaultCommands(),
};

await assert.rejects(() => run(ast, ctx), /finite non-negative number/);
});

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