Skip to content

Commit 5e68a7b

Browse files
committed
Implement list
1 parent ea7e604 commit 5e68a7b

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

src/virtual-machines/source-2.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,8 @@ const primitives = list(
334334
list("MIN ", 81, math_min , "math_min" , list("var"), "num"),
335335
list("HYPOT ", 82, math_hypot , "math_hypot" , list("var"), "num"),
336336
list("RUNTIME", 83, runtime , "runtime" , null, "num"),
337-
list("STRINGI", 84, stringify , "stringify" , list("num"), "str")
337+
list("STRINGI", 84, stringify , "stringify" , list("num"), "str"),
338+
list("LIST ", 85, list , "list" , list("var"), "pair")
338339
// list("PROMPT ", 84, prompt , "prompt" , null, "string")
339340
);
340341

@@ -1778,6 +1779,9 @@ M[DONE] = () => { RUNNING = false;
17781779

17791780
// ============================== INJECTED PRIMITIVE FUNCTIONS ========================
17801781
// utilize underlying source functions
1782+
// special cases handled are:
1783+
// is_pair, is_null, pair access and list
1784+
// as they cannot simply utilize the underlying source functions in the VM
17811785

17821786
function insert_primitive(p) {
17831787
const OP = injected_prim_func_opcode(p);
@@ -1800,6 +1804,23 @@ function insert_primitive(p) {
18001804
} else if (injected_prim_func_string(p) === "tail") {
18011805
POP_OS();
18021806
RES = HEAP[RES + TAIL_VALUE_SLOT];
1807+
} else if (injected_prim_func_string(p) === "list") {
1808+
// list is a variadic function
1809+
const num_of_args = HEAP[OS + SIZE_SLOT] - 4;
1810+
NEW_NULL(); // null at the end of list
1811+
B = RES;
1812+
// we look into OS and load from the start to end instead of popping it
1813+
for (E = 0; E < num_of_args; E = E + 1) {
1814+
A = HEAP[OS + HEAP[OS + FIRST_CHILD_SLOT] + E];
1815+
NEW_PAIR();
1816+
B = RES;
1817+
}
1818+
G = B;
1819+
// clean up OS
1820+
for (E = 0; E < num_of_args; E = E + 1) {
1821+
POP_OS();
1822+
}
1823+
RES = G;
18031824
} else {
18041825
if (is_variadic_function(injected_prim_func_string(p))) {
18051826
const num_of_args = HEAP[OS + SIZE_SLOT] - 4;
@@ -1910,7 +1931,7 @@ run();
19101931
// run();
19111932

19121933
P = parse_and_compile("\
1913-
stringify(1000000000000000000000000000000000000000000000000000000);\
1934+
reverse(list(1, 2, 3));\
19141935
\
19151936
");
19161937
print_program(P);

0 commit comments

Comments
 (0)