Skip to content

Instantly share code, notes, and snippets.

@guyjacks
Last active August 29, 2015 14:05
Show Gist options
  • Save guyjacks/176bd4e4b96ba96dcc37 to your computer and use it in GitHub Desktop.
Save guyjacks/176bd4e4b96ba96dcc37 to your computer and use it in GitHub Desktop.
HOW DO I REPRESENT THE FOLLOWING JEAP TEMPLATE IN AN ABSTRACT SYNTAX TREE?
#### jeap template
people =
{% for person in people %}
name = {{ person.name }}
{% end %}
#### end jeap template
#### python code
people = [{name: "Guy"},{name: "Troy"}]
jeap.render(people=people) # assume it works similar to jinja2 or any other template engine
#### end python code
#### jeap output
{
"people": [
{"name": "Guy"},
{"name": "Troy"}
]
}
#### end jeap output
#### jeap template without for-loop
people =
name = guy
name = troy
#### end jeap template without for-loop
Here is how I would build the syntax tree.
1. 1st pair "people" is encountered. A pair must belong to an object so the object is implied.
RootNode -> children [ObjectNode -> children [PairNode (people)]]
RootNode
- ObjectNode
- PairNode
- key = people
- value = Null
2. 2nd pair "name=guy" is encountered. The parent is a pair. A pair cannot directly be the value of another pair and a pair must belong to an object. The implied object is created and set as the value of the 1st pair. The second pair is added as teh first child of the new object.
RootNode
- ObjectNode
- PairNode
- key = people
- value
- ObjectNode
- PairNode
- key = name
- value
- LiteralNode(guy)
3. 3rd pair "name=troy" is encountered. Since the parent is already an object, JEAP checks to see if the key is a duplicate. It is so JEAP implies that it must belong to a new object. JEAP creates the new object and adds the pair to its children. JEAP then tries to add the object to the node tree. An object can not exist side-by-side with another object unless they are members of an array... {...}, {...} is illegal. [{...}, {...}] is legal. JEAP creates the array and adds both objects as its children. It then removes then replaces the value of the 1st pair with the array (it was the object which now is a member of the array).
RootNode
- ObjectNode
- PairNode
- key = people
- value
- ArrayNode
- ObjectNode
- PairNode
- key = name
- value
- LiteralNode(guy)
- ObjectNode
- PairNode
- key = name
- value
- LiteralNode(troy)
This logic is relatively easy to implement without control-logic nodes. I don't know how to implement these rules when the pairs exist as children of loops.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment