-
Notifications
You must be signed in to change notification settings - Fork 0
Abstract Syntax Tree Unnesting
{"type":"Program","body":[{"type":"CallExpression","name":"subtract","args":[{"type":"NumLiteral","value":"2"},{"type":"NumLiteral","value":"2"}]}]}
Better readability:
{ "type":"Program",
"body":[
{ "type":"CallExpression",
"name":"subtract",
"args":[
{ "type":"NumLiteral",
"value":"2"},
{ "type":"NumLiteral",
"value":"2"}
]
}
]
}
About 5 hours ago, I found a solution to the terminal not displaying objects / arrays inside objects / arrays. But the string tree is unreadable since everything is on 1 line. I'll need to write an algorithm to un-flatten the ast to make it more readable.
indenter alg solution here
Stack overflow
var arr = ["Jani","Hege","Stale","Kai Jim","Borge"];
console.log(arr); // Jani, Hege, Stale, Kai Jim, Borge
//the rest of the elements will move to the right to make room for the new element at this index
arr.splice(2, 0, "Lene");
console.log(arr); // Jani, Hege, Lene, Stale, Kai Jim, Borge
// split every letter in string into an item in our array let newArray = string.split('');
Designing algorithm.
This needs more work.
let arranger = boriates.split('')
console.log(arranger.length) //28 0..27
arranger.splice(arranger.indexOf("{") + 1, 0,"\t")
arranger.splice(arranger.indexOf("{") + 1 ,0,"\n") // { indent
arranger.splice(arranger.indexOf(",") + 1, 0,"\t")
arranger.splice(arranger.indexOf(",") + 1, 0,"\n") // , newline
arranger.splice(arranger.indexOf("[") + 1, 0,"\t")
arranger.splice(arranger.indexOf("[") + 1, 0,"\n") // [ indent
arranger.splice( arranger.indexOf("]") ,0,"\n") // ] newline
arranger.splice( arranger.indexOf("]") ,0,"\t") //] indent
arranger.splice( arranger.indexOf("}") ,0,"\n") // } newline
var shuffled = arranger.join("")
console.log(shuffled)
{"t":"u","b":[{"e":"f"},{"g":"h"}]}
is the test ast
Essentially this: {\n\t"t":"u",\n\t"b":[\n\t\t{\n\t\t\t"e":"f"\n\t\t},\n\t\t{\n\t\t\t"g":"h"\n\t\t}\n\t]\n}
This is a string problem
Define Alg rules here
In case you don't know what an algorithm is, click here.
-
\n
is constant. always present for every shift. use\n
when either{
,[
,}
,]
or,
are found -
\t
is not constant- Add 1
\t
when{
or[
is found - Subtract 1
\t
when}
or]
is found - Hold all
\t
s when,
is found
- Add 1
- Skip through all other characters
//ALGORITHM START
let astmover = boriates
console.log(`Start: ${astmover}`)
//be careful
let tree = astmover.split('')
var tabs = ''
for (let cdot = 0; cdot < tree.length; cdot++) {
let plant = tree[cdot];
console.log('B99')
switch (plant) {
case "{":
case "[":
tabs += '\t'
tree.splice(tree.indexOf(plant) + 1 ,0,`\n${tabs}`)
break;
case "}":
case "]":
tabs -= '\t'
tree.splice(tree.indexOf(plant) ,0,`\n${tabs}`)
break;
case ",":
tree.splice(tree.indexOf(plant) + 1 ,0,`\n${tabs}`)
break;
/*default:
plant = tree[++cdot]*/
}
}
const nicetree = tree.join("")
console.log(`End: ${nicetree}`)
//ALGORITHM END
Unintentionally caused the for loop to go infinite. The array tree that I'm using to add new elements in the middle of existing elements is also being used as an iterator. Not great.
Possible solution: Make this for loop a while loop.
Instructions:
- Add
\n\t
at index 0 plus 1 - Add
\n\t
at index 8 plus 1 - Add
\n\t\t
at index 13 plus 1 - Add
\n\t\t\t
at index 14 plus 1 - Add
\n\t\t
at index 22 - Add
\n\t\t
at index 23 plus 1 - Add
\n\t\t\t
at index 24 plus 1 - Add
\n\t\t
at index 32 - Add
\n\t
at index 33 - Add
\n
at index 34
New solution:
Have the for loop iterate through stringified ast, while pushing everything including newlines and horizontal tabs into another array
var boriates = JSON.stringify(obj)
let template = []
var tabs = 0
for (let cdot = 0; cdot < boriates.length; cdot++) {
let plant = boriates[cdot];
switch (plant) {
case "{":
case "[":
tabs++
template.push(plant)
template.push("\n")
for (let i = 0; i < tabs; i++) {
template.push("\t")
}
break;
case "}":
case "]":
tabs--
template.push("\n")
for (let i = 0; i < tabs; i++) {
template.push("\t")
}
template.push(plant)
break;
case ",":
template.push(plant)
template.push("\n")
for (let i = 0; i < tabs; i++) {
template.push("\t")
}
break;
default:
template.push(plant)
}
}
const nicetree = template.join("")
The generated ast for the lexer results can now be readable.