Skip to content

Commit 345b8df

Browse files
authored
Parse negative moshscript numbers (#15)
1 parent c639a65 commit 345b8df

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

src/interpreter.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// flag is truthy, bounded by ctx.maxIterations. Commands run top-to-bottom.
1313

1414
export function tokenize(src) {
15-
const re = /("(?:[^"\\]|\\.)*")|(\/\/[^\n]*)|(\d+(?:\.\d+)?)|([A-Za-z_]\w*)|([(){};,])|(\s+)/g;
15+
const re = /("(?:[^"\\]|\\.)*")|(\/\/[^\n]*)|(-?\d+(?:\.\d+)?)|([A-Za-z_]\w*)|([(){};,])|(\s+)/g;
1616
const tokens = [];
1717
let m;
1818
let lastIndex = 0;

test/interpreter.test.mjs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import assert from "node:assert/strict";
22
import test from "node:test";
3-
import { compile, tokenize } from "../src/interpreter.mjs";
3+
import { defaultCommands } from "../src/commands.mjs";
4+
import { compile, run, tokenize } from "../src/interpreter.mjs";
45

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

19+
test("negative numeric arguments reach command validation", async () => {
20+
const ast = compile("sleep(-1);");
21+
const ctx = {
22+
vars: { alive: true },
23+
iter: 0,
24+
maxIterations: 1,
25+
commands: defaultCommands(),
26+
};
27+
28+
await assert.rejects(() => run(ast, ctx), /finite non-negative number/);
29+
});
30+
1831
test("compile requires commas between call arguments", () => {
1932
assert.throws(() => compile("say(\"one\" \"two\");"), /expected comma/);
2033
assert.throws(() => compile("say(\"one\",);"), /expected argument after comma/);

0 commit comments

Comments
 (0)