Skip to content

Abstract Syntax Tree Unnesting

Lancine Doumbia edited this page Jul 25, 2022 · 18 revisions

910pm 7/22/2022

{"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.

117am 7/24/2022 Sunday

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('');

6ish am

Designing algorithm.

123pm

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)

223pm Algorithm visualization

{"t":"u","b":[{"e":"f"},{"g":"h"}]} is the test ast
20220724_142001

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.

255pm Rules Defined.

  • \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
  • Skip through all other characters

352pm Algorithm built. But broken

//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

354pm

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.

426pm Visualize this algorithm again.

20220724_164544

Instructions:

  1. Add \n\t at index 0 plus 1
  2. Add \n\t at index 8 plus 1
  3. Add \n\t\t at index 13 plus 1
  4. Add \n\t\t\t at index 14 plus 1
  5. Add \n\t\t at index 22
  6. Add \n\t\t at index 23 plus 1
  7. Add \n\t\t\t at index 24 plus 1
  8. Add \n\t\t at index 32
  9. Add \n\t at index 33
  10. Add \n at index 34

1125pm

New solution:
Have the for loop iterate through stringified ast, while pushing everything including newlines and horizontal tabs into another array

1258am 7/25/2022 Monday - Algorithm is now built and fully operational.

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("")

120am 7/25/2022 Monday

The generated ast for the lexer results can now be readable.

1015pm 7/13/2022

Building a separate scanner and tokenizer to see what I've learned from Vaidehi Joshi's blog.

616pm 7/16/2022

Separate scanner and tokenizer completed since 4pm. Syntactic Analyzer operation now in progress.

Clone this wiki locally