Skip to content
This repository was archived by the owner on Jan 1, 2026. It is now read-only.

Commit fd9b208

Browse files
committed
Bump @forthic/interp to 0.2.0
- FIX: Parallel map over record - Change behavior of `.s` `.s` now halts execution and console.logs the top of stack
1 parent 4ea5148 commit fd9b208

5 files changed

Lines changed: 72 additions & 7 deletions

File tree

forthic-ts/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@forthic/interp",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"description": "Forthic interpreter",
55
"main": "dist/cjs/index.js",
66
"module": "dist/esm/index.js",

forthic-ts/src/forthic/global_module.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2638,7 +2638,13 @@ export class GlobalModule extends Module {
26382638
// ( -- )
26392639
word_dot_s(interp: Interpreter) {
26402640
const stack = (interp as any).stack;
2641-
console.log(stack);
2641+
if (stack.length > 0) {
2642+
console.log(stack[stack.length - 1]);
2643+
}
2644+
else {
2645+
console.log("<STACK EMPTY>");
2646+
}
2647+
interp.halt()
26422648
}
26432649

26442650
// ( a b -- a - b )

forthic-ts/src/forthic/global_module/map_word.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class MapWord {
1919
push_error?: boolean;
2020
with_key?: boolean;
2121
cur_index: number;
22-
result: any[];
22+
result: any[] | { [key: string]: any };
2323
errors: any[];
2424
is_debugging: boolean;
2525
processing_item: boolean;
@@ -64,7 +64,10 @@ export class MapWord {
6464
this.result = [];
6565
this.errors = [];
6666
if (this.num_interps > 1) {
67-
const group_size = Math.ceil(items.length / this.num_interps);
67+
interp.stack_push(items)
68+
await interp.run("LENGTH");
69+
const num_items = interp.stack_pop();
70+
const group_size = Math.ceil(num_items / this.num_interps);
6871
interp.stack_push(items);
6972
interp.stack_push(group_size);
7073
await interp.run("GROUPS-OF");
@@ -82,13 +85,21 @@ export class MapWord {
8285
const run_results = await Promise.all(interp_runs);
8386

8487
// Gather results
85-
let result = [];
88+
const is_array = items instanceof Array;
89+
let array_result = []
90+
let object_result = {}
8691
let errors = [];
8792
for (const res of run_results) {
88-
result = [...result, ...res[0]];
93+
if (is_array) {
94+
array_result = [...array_result, ...res[0]]
95+
}
96+
else {
97+
object_result = { ...object_result, ...res[0] };
98+
}
99+
89100
errors = [...errors, ...res[1]];
90101
}
91-
this.result = result;
102+
this.result = is_array ? array_result : object_result;
92103
this.errors = errors;
93104
} else {
94105
await this.map(interp, items);

forthic-ts/src/forthic/interpreter.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export class Interpreter {
7373
private module_stack: Module[];
7474
private registered_modules: { [key: string]: Module };
7575
private is_compiling: boolean;
76+
private should_stop: boolean;
7677
private is_memo_definition: boolean;
7778
private cur_definition: any; // Assuming this can be of any type, update if you have a specific type
7879
private screens: { [key: string]: any }; // Assuming screens can be of any type, update if you have a specific type
@@ -96,6 +97,7 @@ export class Interpreter {
9697
this.module_stack = [this.app_module];
9798
this.registered_modules = {};
9899
this.is_compiling = false;
100+
this.should_stop = false;
99101
this.is_memo_definition = false;
100102
this.cur_definition = null;
101103
this.screens = {};
@@ -114,6 +116,10 @@ export class Interpreter {
114116
this.timestamps = [];
115117
}
116118

119+
halt() {
120+
this.should_stop = true;
121+
}
122+
117123
get_app_module(): Module {
118124
return this.app_module;
119125
}
@@ -196,6 +202,10 @@ export class Interpreter {
196202
if (token.type === TokenType.EOS) {
197203
break;
198204
}
205+
if (this.should_stop) {
206+
this.should_stop = false;
207+
break;
208+
}
199209

200210
// Regardless of debug mode, we don't want to stop when compiling definitions or going through comments
201211
if (

forthic-ts/src/forthic/tests/global_module.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,26 @@ test("Groups of", async () => {
395395
expect(groups[2]).toEqual([7, 8]);
396396
});
397397

398+
test("Groups of record", async () => {
399+
await interp.run(`
400+
[
401+
['a' 1]
402+
['b' 2]
403+
['c' 3]
404+
['d' 4]
405+
['e' 5]
406+
['f' 6]
407+
['g' 7]
408+
['h' 8]
409+
] REC 3 GROUPS-OF
410+
`);
411+
const groups = interp.stack_pop();
412+
expect(groups[0]).toEqual({ a: 1, b: 2, c: 3 });
413+
expect(groups[1]).toEqual({ d: 4, e: 5, f: 6 });
414+
expect(groups[2]).toEqual({ g: 7, h: 8 });
415+
});
416+
417+
398418
test("Groups of using record", async () => {
399419
// Test grouping a record
400420
interp = new Interpreter();
@@ -1666,6 +1686,18 @@ test("Parallel map", async () => {
16661686
expect(interp.stack_pop()).toEqual([1, 4, 9, 16, 25]);
16671687
});
16681688

1689+
test("Parallel map over record", async () => {
1690+
await interp.run(`
1691+
[
1692+
['a' 1]
1693+
['b' 2]
1694+
['c' 3]
1695+
['d' 4]
1696+
] REC "3 *" 2 !INTERPS MAP
1697+
`);
1698+
expect(interp.stack_pop()).toEqual({ a: 3, b: 6, c: 9, d: 12 });
1699+
});
1700+
16691701
test("|REC@|", async () => {
16701702
interp.stack_push([{ a: 1 }, { a: 2 }, { a: 3 }]);
16711703
await interp.run(`'a' |REC@`);
@@ -1828,3 +1860,9 @@ test("ADD-DAYS", async () => {
18281860
const res1 = interp.stack_pop();
18291861
console.log("ADD-DAYS", res1);
18301862
});
1863+
1864+
test(".s", async () => {
1865+
await interp.run("1 2 .s 3 4");
1866+
const res1 = interp.stack_pop();
1867+
expect(res1).toEqual(2);
1868+
})

0 commit comments

Comments
 (0)