@@ -54,6 +54,7 @@ type Name = Bend.Name;
5454type File = {
5555 book : Bend . Book ;
5656 js : boolean ;
57+ to_lists : Set < Name > | null ;
5758 bangs : Set < Name > ;
5859 sites : Map < Name , number > ;
5960 hot : Set < Name > ;
@@ -245,6 +246,7 @@ const OPERATIONS: Record<string, Intr> = Object.setPrototypeOf({
245246 string_append : { JS : "($0 + $1)" } ,
246247 string_contains : { JS : "string_contains($0, $1)" } ,
247248 string_length : { JS : "[...str_out($0)].length" } ,
249+ array_to_list : { JS : "array_to_list($0)" } ,
248250 ...Object . fromEntries ( Object . entries ( {
249251 new : "array_new($0, $1)" , set : "($0[$1 % $0.length] = $2, $0)" ,
250252 get : "{$: \"Tuple\", fst: $0, snd: $0[$1 % $0.length]}" ,
@@ -842,8 +844,15 @@ function dom_live([q]: Dom): boolean {
842844
843845function intr_of ( k : Name , js = false ) : Intr | undefined {
844846 const tld = FL . book . tlds [ k ] ;
845- const it = tld ?. $ === "Def" && tld . i === undefined && tld . b
846- ? OPERATIONS [ op_name ( k ) ] : undefined ;
847+ if ( tld ?. $ !== "Def" || tld . i !== undefined ) {
848+ return undefined ;
849+ }
850+ // Specializations of a Base template do not carry its Base flag.
851+ const list = FL . to_lists !== null && FL . to_lists . has ( k ) ;
852+ if ( ! tld . b && ! list ) {
853+ return undefined ;
854+ }
855+ const it = OPERATIONS [ list ? "array_to_list" : op_name ( k ) ] ;
847856 return it && ( js || it . C !== undefined ) && ( ! FL . js || it . JS )
848857 ? it : undefined ;
849858}
@@ -1416,9 +1425,13 @@ export function io_run(book: Bend.Book, args: string[]): number {
14161425
14171426function file_new ( book : Bend . Book , js : boolean ) : File {
14181427 PROBES . length = 1 ;
1428+ const lists = js && book . tlds [ "Array.to_list" ] ?. b === true
1429+ ? book . tmps [ "Array.to_list" ] : undefined ;
14191430 return {
14201431 book,
14211432 js,
1433+ to_lists : lists === undefined || lists . size === 0
1434+ ? null : new Set ( lists . values ( ) ) ,
14221435 bangs : new Set ( ) ,
14231436 sites : new Map ( ) ,
14241437 hot : new Set ( ) ,
@@ -6057,6 +6070,14 @@ function array_new(d, v) {
60576070 return Array(2 ** d).fill(v);
60586071}
60596072
6073+ function array_to_list(a) {
6074+ let xs = {$: "Nil"};
6075+ for (let i = a.length - 1; i >= 0; i -= 1) {
6076+ xs = {$: "Con", head: a[i], tail: xs};
6077+ }
6078+ return xs;
6079+ }
6080+
60606081function array_node(a, b) {
60616082 if (a.length !== b.length) {
60626083 throw "bend: ${ ERRS [ 2 ] } ";
0 commit comments