Skip to content

Commit

Permalink
feat(position): Added positions to AST nodes for formatting preparation
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasBoda committed Dec 12, 2023
1 parent d581e65 commit 70e6cbb
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 210 deletions.
4 changes: 4 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# TODO

- source code formatter (input: raw source code, output: formatted source code)
- node positions in AST (parser)
247 changes: 80 additions & 167 deletions ast.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,196 +11,109 @@
"variableType": "Const",
"identifier": "x",
"value": {
"type": "CallExpression",
"caller": {
"type": "Identifier",
"identifier": "random"
"type": "BinaryExpression",
"left": {
"type": "NumericLiteral",
"value": 20,
"position": {
"line": 3,
"character": 15
}
},
"args": [
{
"type": "NumericLiteral",
"value": 0
},
{
"type": "CallExpression",
"caller": {
"type": "Identifier",
"identifier": "width"
},
"args": []
"right": {
"type": "NumericLiteral",
"value": 5,
"position": {
"line": 3,
"character": 20
}
]
},
"operator": "+",
"position": {
"line": 3,
"character": 18
}
},
"position": {
"line": 3,
"character": 5
}
},
{
"type": "VariableDeclaration",
"variableType": "Const",
"identifier": "y",
"value": {
"type": "CallExpression",
"caller": {
"type": "Identifier",
"identifier": "random"
},
"args": [
{
"type": "BinaryExpression",
"left": {
"type": "BinaryExpression",
"left": {
"type": "NumericLiteral",
"value": 0
"value": 30,
"position": {
"line": 4,
"character": 15
}
},
{
"type": "CallExpression",
"caller": {
"type": "Identifier",
"identifier": "height"
},
"args": []
}
]
}
},
{
"type": "VariableDeclaration",
"variableType": "Property",
"identifier": "close",
"value": {
"type": "CallExpression",
"caller": {
"type": "Identifier",
"identifier": "filter"
},
"args": [
{
"type": "LambdaExpression",
"base": {
"type": "CallExpression",
"caller": {
"type": "Identifier",
"identifier": "agents"
},
"args": [
{
"type": "Identifier",
"identifier": "person"
}
]
},
"param": "p",
"value": {
"type": "LogicalExpression",
"left": {
"type": "BinaryExpression",
"left": {
"type": "MemberExpression",
"caller": {
"type": "Identifier",
"identifier": "p"
},
"value": {
"type": "Identifier",
"identifier": "x"
}
},
"right": {
"type": "NumericLiteral",
"value": 0
},
"operator": ">="
},
"right": {
"type": "BinaryExpression",
"left": {
"type": "MemberExpression",
"caller": {
"type": "Identifier",
"identifier": "p"
},
"value": {
"type": "Identifier",
"identifier": "x"
}
},
"right": {
"type": "CallExpression",
"caller": {
"type": "Identifier",
"identifier": "width"
},
"args": []
},
"operator": "<="
},
"operator": "and"
"right": {
"type": "NumericLiteral",
"value": 2,
"position": {
"line": 4,
"character": 20
}
},
"operator": "*",
"position": {
"line": 4,
"character": 18
}
]
}
},
{
"type": "VariableDeclaration",
"variableType": "Property",
"identifier": "closest",
"value": {
"type": "CallExpression",
"caller": {
"type": "Identifier",
"identifier": "min"
},
"args": [
{
"type": "LambdaExpression",
"base": {
"type": "Identifier",
"identifier": "close"
},
"param": "c",
"value": {
"type": "MemberExpression",
"caller": {
"type": "Identifier",
"identifier": "c"
},
"value": {
"type": "Identifier",
"identifier": "x"
}
}
"right": {
"type": "NumericLiteral",
"value": 3,
"position": {
"line": 4,
"character": 24
}
]
}
},
{
"type": "VariableDeclaration",
"variableType": "Property",
"identifier": "color",
"value": {
"type": "BooleanLiteral",
"value": true
},
"operator": "-",
"position": {
"line": 4,
"character": 22
}
},
"position": {
"line": 4,
"character": 5
}
},
{
"type": "VariableDeclaration",
"variableType": "Property",
"variableType": "Const",
"identifier": "coloured",
"value": {
"type": "OtherwiseExpression",
"left": {
"type": "MemberExpression",
"caller": {
"type": "Identifier",
"identifier": "closest"
},
"value": {
"type": "Identifier",
"identifier": "color"
}
},
"right": {
"type": "BooleanLiteral",
"value": false
"type": "BooleanLiteral",
"value": false,
"position": {
"line": 6,
"character": 22
}
},
"position": {
"line": 6,
"character": 5
}
}
]
],
"position": {
"line": 1,
"character": 1
}
}
]
],
"position": {
"line": 0,
"character": 0
}
}
8 changes: 6 additions & 2 deletions code.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
agent snowflake 200 {
const boolean = prob(0.5);
agent person 10 {

const x = 20 + 5;
const y = 30 * 2 - 3;

const coloured = false;
}
3 changes: 3 additions & 0 deletions src/interpreter/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { createGlobalFunction } from "../utils/functions";
import { Symbolizer } from "../symbolizer/symbolizer";
import { Symbol } from "../symbolizer/symbolizer.types";
import { ErrorLexer, ErrorModel, ErrorParser, ErrorRuntime } from "../utils/errors";
import { writeFileSync } from "fs";

export class Interpreter {

Expand Down Expand Up @@ -52,6 +53,8 @@ export class Interpreter {
return of(this.getRuntimeError(error as ErrorParser));
}

writeFileSync("ast.json", JSON.stringify(program));

// save abstract syntax tree
this.program = program as Program;

Expand Down
Loading

0 comments on commit 70e6cbb

Please sign in to comment.