From 8932fef6b9eea2feb7ccd376ac407c546398f811 Mon Sep 17 00:00:00 2001 From: Dustin Swan Date: Mon, 6 Apr 2026 20:52:05 -0600 Subject: [PATCH] Adding display, fixing show to include quotes in strings --- src/cg/06-tree.cg | 6 ++--- src/runtime-js.ts | 62 +++++++++++++++++++++++++++++------------------ 2 files changed, 41 insertions(+), 27 deletions(-) diff --git a/src/cg/06-tree.cg b/src/cg/06-tree.cg index f563800..d8e2dd8 100644 --- a/src/cg/06-tree.cg +++ b/src/cg/06-tree.cg @@ -22,7 +22,7 @@ treeNodeHeight = value path expanded \ | True \ lineH + (sum (mapWithIndex (item i \ (valueLabel item) | Some _ \ lineH - | None \ lineH + (treeNodeHeight item (path & [show i]) expanded) + | None \ lineH + (treeNodeHeight item (path & [display i]) expanded) ) items)) | False \ lineH) | _ \ lineH; @@ -40,7 +40,7 @@ visiblePaths = value path expanded \ value | ListValue items \ ( [{ path = path, leaf = False }, ...(contains path expanded) | True \ flatten (mapWithIndex (item i \ - itemPath = path & [show i]; + itemPath = path & [display i]; (valueLabel item) | Some _ \ [{ path = itemPath, leaf = True }] | None \ visiblePaths item itemPath expanded @@ -142,7 +142,7 @@ treeNode = config \ | True \ [ui.clickable { onClick = _ \ noOp, child = ui.column { gap = 0, children = mapWithIndex (item i \ - treeNode { value = item, depth = depth + 1, path = config.path & [show i], expanded = config.expanded, onToggle = config.onToggle, selectedPath = config.selectedPath, prefix = (show i) & ": ", editing = config.editing, onDoneEditing = config.onDoneEditing, onEditLeaf = config.onEditLeaf } + treeNode { value = item, depth = depth + 1, path = config.path & [display i], expanded = config.expanded, onToggle = config.onToggle, selectedPath = config.selectedPath, prefix = (display i) & ": ", editing = config.editing, onDoneEditing = config.onDoneEditing, onEditLeaf = config.onEditLeaf } ) items } }] | False \ []) diff --git a/src/runtime-js.ts b/src/runtime-js.ts index 6f0c505..b370b17 100644 --- a/src/runtime-js.ts +++ b/src/runtime-js.ts @@ -66,6 +66,20 @@ export const _rt = { int: (x: any) => typeof x === 'number' ? Math.floor(x) : parseInt(x, 10) || 0, show: (value: any): string => { if (value === null || value === undefined) return "None"; + if (typeof value === 'string') return JSON.stringify(value); + if (typeof value === 'number') return String(value); + if (typeof value === 'boolean') return value ? "True" : "False"; + if (value._tag) return value._0 !== undefined ? `(${value._tag} ${_rt.show(value._0)})` : value._tag; + if (Array.isArray(value)) return `[${value.map(_rt.show).join(", ")}]`; + if (typeof value === 'function') return ""; + if (typeof value === 'object') { + const entries = Object.entries(value).map(([k, v]) => `${k} = ${_rt.show(v)}`); + return `{ ${entries.join(", ")} }`; + } + return String(value); + }, + display: (value: any): string => { + if (value === null || value === undefined) return ""; if (typeof value === 'string') return value; if (typeof value === 'number') return String(value); if (typeof value === 'boolean') return value ? "True" : "False"; @@ -209,34 +223,34 @@ export const _rt = { return { _tag: 'Err', _0: e.message }; } }, - "saveModule!": (moduleName: string) => { - const moduleDefs = [...definitions.values()] - .filter(d => d.module === moduleName); - if (moduleDefs.length === 0) return { _tag: 'Err', _0: 'No module: ' + moduleName }; + //"saveModule!": (moduleName: string) => { + // const moduleDefs = [...definitions.values()] + // .filter(d => d.module === moduleName); + // if (moduleDefs.length === 0) return { _tag: 'Err', _0: 'No module: ' + moduleName }; - const content = '@' + moduleName + '\n\n' + - moduleDefs.map(d => prettyPrint(d)).join('\n\n') + '\n\n@\n'; + // const content = '@' + moduleName + '\n\n' + + // moduleDefs.map(d => prettyPrint(d)).join('\n\n') + '\n\n@\n'; - fetch('/api/save', { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ filename: moduleName, content }) - }); + // fetch('/api/save', { + // method: 'POST', + // headers: { 'Content-Type': 'application/json' }, + // body: JSON.stringify({ filename: moduleName, content }) + // }); - return { _tag: 'Defined', _0: moduleName }; - }, - "saveDef!": (name: string) => { - const def = definitions.get(name); - if (!def) return { _tag: 'Err', _0: 'Unknown: ' + name }; - const content = prettyPrint(def) + '\n'; - fetch('/api/save', { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ filename: name, content }) - }); + // return { _tag: 'Defined', _0: moduleName }; + //}, + //"saveDef!": (name: string) => { + // const def = definitions.get(name); + // if (!def) return { _tag: 'Err', _0: 'Unknown: ' + name }; + // const content = prettyPrint(def) + '\n'; + // fetch('/api/save', { + // method: 'POST', + // headers: { 'Content-Type': 'application/json' }, + // body: JSON.stringify({ filename: name, content }) + // }); - return { _tag: 'Defined', _0: name }; - }, + // return { _tag: 'Defined', _0: name }; + //}, rebind: (name: string, pathOrValue: any, maybeValue?: any) => { if (maybeValue === undefined) { store[name] = pathOrValue;