-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserializeLisp.js
More file actions
26 lines (22 loc) · 813 Bytes
/
serializeLisp.js
File metadata and controls
26 lines (22 loc) · 813 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* Serializes a structured Lisp expression back into a string,
* preserving all whitespace from the original format
* @param {Object} node The structured Lisp expression node
* @returns {string} The formatted Lisp S-expression string
*/
function serializeLisp(node) {
if (!node || node.type === 'empty') {
return '';
}
switch (node.type) {
case 'atom':
return (node.beforeWs || '') + node.value + (node.afterWs || '');
case 'list': {
const items = node.items.map(item => serializeLisp(item)).join('');
return (node.beforeWs || '') + '(' + (node.insideWs || '') + items + ')' + (node.afterWs || '');
}
default:
throw new Error(`Unknown node type: ${node.type}`);
}
}
module.exports = serializeLisp;