ocamlbuild -r -pkg llvm treelang.native
- AST Parsing:
./treelang.native -a example.tl
- SAST Parsing:
./treelang.native -s example.tl
- IR generation (not finished yet): uncomment the corresponding IR gen lines in
treelang.ml
, build again, and./treelang.native -l example.tl
- The language consists of nodes and connections between them.
- There are three types of nodes: Root, Directory, and File.
- Each node has two integer attributes.
- The Root node is unique and serves as the starting point of the structure.
- Nodes are connected using the "connect" keyword followed by the source node type and the target node type.
- Nodes can be instantiated using their type followed by a name.
- File nodes can be assigned an integer value using the "=" operator.
- Connections between nodes are established using the "->" operator, specifying the source node name and the target node name.
- Each node type has two associated operations: forward_operation and backward_operation.
- The forward_operation is performed during the forward pass, which traverses the structure from child nodes to parent nodes.
- The backward_operation is performed during the backward pass, which traverses the structure from parent nodes to child nodes.
- The available operations are:
- "sum": Adds the values of the child nodes and assigns the result to the parent node.
- "percentage": Calculates the percentage contribution of each child node to the parent node's value and assigns the percentage to the child node.
- The script should output the forward and backward values of each node.
Example:
Root root int int
node Directory int int
node File int int
connect Directory -> Directory
connect Directory -> File
root -> Directory dir1
root -> File file1 = 200
dir1 -> Directory dir2
dir1 -> File file2 = 100
dir2 -> File file3 = 50
Directory.forward_operation = sum
File.forward_operation = sum
Directory.backward_operation = percentage
File.backward_operation = percentage
Output:
Forward Pass:
root: 350
dir1: 150
dir2: 50
file1: 200
file2: 100
file3: 50
Backward Pass:
root: 100%
dir1: 42.86%
dir2: 14.29%
file1: 57.14%
file2: 28.57%
file3: 14.29%
To implement the compiler for this language, you would need to:
- Parse the input script and build an abstract syntax tree (AST) representing the node structure and connections.
- Perform semantic analysis to validate the correctness of the script, ensuring that node types and operations are used correctly.
- Generate an intermediate representation (IR) or directly generate target code (e.g., assembly or bytecode) based on the AST.
- Implement the forward pass by traversing the AST from child nodes to parent nodes, applying the specified forward_operation at each node.
- Implement the backward pass by traversing the AST from parent nodes to child nodes, applying the specified backward_operation at each node.
- Execute the generated code or interpret the IR to obtain the forward and backward values of each node.
- Output the results as shown in the example.
This language description provides a high-level overview of the language's syntax, semantics, and expected behavior, allowing a person to implement a compiler for it.