Skip to content

Commit

Permalink
Added support for object methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ronynudelman committed Jan 7, 2023
1 parent 388171b commit b704b50
Show file tree
Hide file tree
Showing 13 changed files with 221 additions and 163 deletions.
45 changes: 40 additions & 5 deletions sources/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ class Analyzer {
}

let methodStartNodeId: NodeId = this.graph.addVertex(VertexType.Start, {name: methodName});
// This symbol is not used, but adding it
// to the symbol table does no harm
this.symbolTable.addSymbol(methodName, methodStartNodeId, true);
let prevControlVertex: NodeId = this.controlVertex;
this.controlVertex = methodStartNodeId;
Expand Down Expand Up @@ -345,9 +347,8 @@ class Analyzer {
let preMergeControlVertex: NodeId = this.controlVertex;
let whileNodeId: NodeId = this.graph.addVertex(VertexType.While)
let mergeNodeId: NodeId = this.graph.addVertex(VertexType.Merge, {branchOriginId: whileNodeId});
this.nextControl(mergeNodeId);
this.nextControl(whileNodeId);
this.whileStack.unshift(whileNodeId);

this.whileStack.unshift(mergeNodeId);
this.breakStack.unshift(new Array<NodeId>()); // the list is popped right after backpatching it inside nextControl()

let symbolTableCopy: Map<string, NodeId> = this.symbolTable.getCopy();
Expand All @@ -356,6 +357,8 @@ class Analyzer {
let expNodeId: NodeId = this.processExpression(whileStatement.expression);
this.graph.addEdge(expNodeId, whileNodeId, "condition");
this.currentBranchType = true;
this.nextControl(mergeNodeId);
this.nextControl(whileNodeId);
this.processBranchBlockWrapper(whileStatement.statement);

let lastTrueBranchControlVertex: NodeId = this.getLastBranchControlVertex(whileNodeId);
Expand All @@ -374,10 +377,11 @@ class Analyzer {
}

private processIfStatement(ifStatement: ts.IfStatement): void {
let expNodeId: NodeId = this.processExpression(ifStatement.expression);

let ifNodeId: NodeId = this.graph.addVertex(VertexType.If);
this.nextControl(ifNodeId);

let expNodeId: NodeId = this.processExpression(ifStatement.expression);

this.graph.addEdge(expNodeId, ifNodeId, "condition");

let symbolTableCopy: Map<string, NodeId> = this.symbolTable.getCopy();
Expand Down Expand Up @@ -553,6 +557,9 @@ class Analyzer {
case ts.SyntaxKind.ObjectLiteralExpression:
expNodeId = this.processObjectLiteralExpression(expression as ts.ObjectLiteralExpression);
break;
case ts.SyntaxKind.FunctionExpression:
expNodeId = this.processFunctionExpression(expression as ts.FunctionExpression);
break;
default:
throw new Error(`not implemented`);
}
Expand Down Expand Up @@ -586,6 +593,34 @@ class Analyzer {
return newNodeId;
}

private processFunctionExpression(funcExp: ts.FunctionExpression): NodeId {
let prevControlVertex: NodeId = this.controlVertex;

let funcStartNodeId: NodeId = this.graph.addVertex(VertexType.Start, {name: "__anonymousFunction__"});
this.controlVertex = funcStartNodeId;

this.symbolTable.addNewScope();
this.functionsStack.unshift(funcStartNodeId);

let thisNodeId: NodeId = this.graph.addVertex(VertexType.Parameter, {pos: 0, funcId: funcStartNodeId});
this.symbolTable.addSymbol('this', thisNodeId, false, true);
funcExp.parameters.forEach((parameter: ts.ParameterDeclaration, position: number) => {
let parameterName: string = (parameter.name as any).escapedText;
let parameterNodeId: NodeId = this.graph.addVertex(VertexType.Parameter,
{pos: position + 1, funcId: funcStartNodeId});
this.symbolTable.addSymbol(parameterName, parameterNodeId, false, true);
});

this.processBlockStatements((funcExp.body as ts.Block).statements);

this.functionsStack.shift();
this.symbolTable.removeCurrentScope();

this.controlVertex = prevControlVertex;

return funcStartNodeId;
}

private processThisExpression(thisExpression: ts.ThisExpression): NodeId {
return this.symbolTable.getIdByName('this');
}
Expand Down
16 changes: 8 additions & 8 deletions tests/goldens/graph_10.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ digraph G {
0 [ label="0 | start (__entryPoint__)" shape="rectangle" ];
1 [ label="1 | 0" shape="rectangle" ];
2 [ label="2 | 1" shape="rectangle" ];
3 [ label="3 | if" shape="rectangle" ];
4 [ label="4 | true" shape="rectangle" ];
5 [ label="5 | merge (3)" shape="rectangle" ];
3 [ label="3 | true" shape="rectangle" ];
4 [ label="4 | if" shape="rectangle" ];
5 [ label="5 | merge (4)" shape="rectangle" ];
6 [ label="6 | if" shape="rectangle" ];
7 [ label="7 | merge (6)" shape="rectangle" ];
8 [ label="8 | 2" shape="rectangle" ];
Expand All @@ -17,10 +17,10 @@ digraph G {
15 [ label="15 | dummy" shape="rectangle" ];
16 [ label="16 | phi (5)" shape="rectangle" ];
17 [ label="17 | phi (5)" shape="rectangle" ];
0 -> 3 [ label="control" ];
4 -> 3 [ label="condition" ];
3 -> 6 [ label="true-control" ];
4 -> 6 [ label="condition" ];
0 -> 4 [ label="control" ];
3 -> 4 [ label="condition" ];
4 -> 6 [ label="true-control" ];
3 -> 6 [ label="condition" ];
6 -> 9 [ label="true-control" ];
9 -> 7 [ label="control" ];
6 -> 11 [ label="false-control" ];
Expand All @@ -30,7 +30,7 @@ digraph G {
8 -> 13 [ label="from 9" ];
1 -> 13 [ label="from 11" ];
7 -> 5 [ label="control" ];
3 -> 15 [ label="false-control" ];
4 -> 15 [ label="false-control" ];
15 -> 5 [ label="control" ];
14 -> 16 [ label="from 7" ];
2 -> 16 [ label="from 15" ];
Expand Down
14 changes: 7 additions & 7 deletions tests/goldens/graph_11.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ digraph G {
1 [ label="1 | start (add)" shape="rectangle" ];
2 [ label="2 | start (sub)" shape="rectangle" ];
3 [ label="3 | 5" shape="rectangle" ];
4 [ label="4 | if" shape="rectangle" ];
5 [ label="5 | true" shape="rectangle" ];
6 [ label="6 | merge (4)" shape="rectangle" ];
4 [ label="4 | true" shape="rectangle" ];
5 [ label="5 | if" shape="rectangle" ];
6 [ label="6 | merge (5)" shape="rectangle" ];
7 [ label="7 | 6" shape="rectangle" ];
8 [ label="8 | call" shape="rectangle" ];
9 [ label="9 | dummy" shape="rectangle" ];
Expand All @@ -17,13 +17,13 @@ digraph G {
15 [ label="15 | param (1): (2)" shape="rectangle" ];
16 [ label="16 | return (2)" shape="rectangle" ];
17 [ label="17 | -" shape="rectangle" ];
0 -> 4 [ label="control" ];
5 -> 4 [ label="condition" ];
0 -> 5 [ label="control" ];
4 -> 5 [ label="condition" ];
7 -> 8 [ label="pos: 1" ];
1 -> 8 [ label="callable" ];
4 -> 8 [ label="true-control" ];
5 -> 8 [ label="true-control" ];
8 -> 6 [ label="control" ];
4 -> 9 [ label="false-control" ];
5 -> 9 [ label="false-control" ];
9 -> 6 [ label="control" ];
3 -> 10 [ label="pos: 1" ];
1 -> 10 [ label="callable" ];
Expand Down
26 changes: 13 additions & 13 deletions tests/goldens/graph_12.txt
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
digraph G {
0 [ label="0 | start (__entryPoint__)" shape="rectangle" ];
1 [ label="1 | 0" shape="rectangle" ];
2 [ label="2 | if" shape="rectangle" ];
3 [ label="3 | true" shape="rectangle" ];
4 [ label="4 | merge (2)" shape="rectangle" ];
2 [ label="2 | true" shape="rectangle" ];
3 [ label="3 | if" shape="rectangle" ];
4 [ label="4 | merge (3)" shape="rectangle" ];
5 [ label="5 | 1" shape="rectangle" ];
6 [ label="6 | dummy" shape="rectangle" ];
7 [ label="7 | if" shape="rectangle" ];
8 [ label="8 | false" shape="rectangle" ];
9 [ label="9 | merge (7)" shape="rectangle" ];
7 [ label="7 | false" shape="rectangle" ];
8 [ label="8 | if" shape="rectangle" ];
9 [ label="9 | merge (8)" shape="rectangle" ];
10 [ label="10 | 2" shape="rectangle" ];
11 [ label="11 | dummy" shape="rectangle" ];
12 [ label="12 | 3" shape="rectangle" ];
13 [ label="13 | dummy" shape="rectangle" ];
14 [ label="14 | phi (9)" shape="rectangle" ];
15 [ label="15 | phi (4)" shape="rectangle" ];
0 -> 2 [ label="control" ];
3 -> 2 [ label="condition" ];
2 -> 6 [ label="true-control" ];
0 -> 3 [ label="control" ];
2 -> 3 [ label="condition" ];
3 -> 6 [ label="true-control" ];
6 -> 4 [ label="control" ];
2 -> 7 [ label="false-control" ];
8 -> 7 [ label="condition" ];
7 -> 11 [ label="true-control" ];
3 -> 8 [ label="false-control" ];
7 -> 8 [ label="condition" ];
8 -> 11 [ label="true-control" ];
11 -> 9 [ label="control" ];
7 -> 13 [ label="false-control" ];
8 -> 13 [ label="false-control" ];
13 -> 9 [ label="control" ];
10 -> 14 [ label="from 11" ];
12 -> 14 [ label="from 13" ];
Expand Down
24 changes: 12 additions & 12 deletions tests/goldens/graph_13.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ digraph G {
0 [ label="0 | start (__entryPoint__)" shape="rectangle" ];
1 [ label="1 | 1" shape="rectangle" ];
2 [ label="2 | 2" shape="rectangle" ];
3 [ label="3 | if" shape="rectangle" ];
4 [ label="4 | true" shape="rectangle" ];
5 [ label="5 | merge (3)" shape="rectangle" ];
3 [ label="3 | true" shape="rectangle" ];
4 [ label="4 | if" shape="rectangle" ];
5 [ label="5 | merge (4)" shape="rectangle" ];
6 [ label="6 | if" shape="rectangle" ];
7 [ label="7 | merge (6)" shape="rectangle" ];
8 [ label="8 | 3" shape="rectangle" ];
Expand Down Expand Up @@ -38,14 +38,14 @@ digraph G {
36 [ label="36 | phi (19)" shape="rectangle" ];
37 [ label="37 | phi (5)" shape="rectangle" ];
38 [ label="38 | phi (5)" shape="rectangle" ];
0 -> 3 [ label="control" ];
4 -> 3 [ label="condition" ];
3 -> 6 [ label="true-control" ];
4 -> 6 [ label="condition" ];
0 -> 4 [ label="control" ];
3 -> 4 [ label="condition" ];
4 -> 6 [ label="true-control" ];
3 -> 6 [ label="condition" ];
6 -> 9 [ label="true-control" ];
9 -> 7 [ label="control" ];
6 -> 10 [ label="false-control" ];
4 -> 10 [ label="condition" ];
3 -> 10 [ label="condition" ];
10 -> 13 [ label="true-control" ];
13 -> 11 [ label="control" ];
10 -> 14 [ label="false-control" ];
Expand All @@ -58,12 +58,12 @@ digraph G {
8 -> 17 [ label="from 9" ];
1 -> 17 [ label="from 11" ];
7 -> 5 [ label="control" ];
3 -> 18 [ label="false-control" ];
4 -> 18 [ label="condition" ];
4 -> 18 [ label="false-control" ];
3 -> 18 [ label="condition" ];
18 -> 20 [ label="true-control" ];
4 -> 20 [ label="condition" ];
3 -> 20 [ label="condition" ];
20 -> 22 [ label="true-control" ];
4 -> 22 [ label="condition" ];
3 -> 22 [ label="condition" ];
22 -> 25 [ label="true-control" ];
25 -> 23 [ label="control" ];
22 -> 27 [ label="false-control" ];
Expand Down
4 changes: 2 additions & 2 deletions tests/goldens/graph_14.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ digraph G {
34 [ label="34 | DONE!" shape="rectangle" ];
4 -> 5 [ label="prefix" ];
6 -> 7 [ label="prefix" ];
0 -> 10 [ label="control" ];
10 -> 9 [ label="control" ];
11 -> 12 [ label="right" ];
22 -> 12 [ label="left" ];
12 -> 9 [ label="condition" ];
0 -> 10 [ label="control" ];
10 -> 9 [ label="control" ];
22 -> 13 [ label="pos: 1" ];
1 -> 13 [ label="callable" ];
9 -> 13 [ label="true-control" ];
Expand Down
48 changes: 24 additions & 24 deletions tests/goldens/graph_15.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,39 @@ digraph G {
4 [ label="4 | while" shape="rectangle" ];
5 [ label="5 | merge (4)" shape="rectangle" ];
6 [ label="6 | true" shape="rectangle" ];
7 [ label="7 | if" shape="rectangle" ];
8 [ label="8 | 0" shape="rectangle" ];
9 [ label="9 | >" shape="rectangle" ];
10 [ label="10 | merge (7)" shape="rectangle" ];
7 [ label="7 | 0" shape="rectangle" ];
8 [ label="8 | >" shape="rectangle" ];
9 [ label="9 | if" shape="rectangle" ];
10 [ label="10 | merge (9)" shape="rectangle" ];
11 [ label="11 | continue" shape="rectangle" ];
12 [ label="12 | dummy" shape="rectangle" ];
13 [ label="13 | if" shape="rectangle" ];
14 [ label="14 | 7" shape="rectangle" ];
15 [ label="15 | <" shape="rectangle" ];
16 [ label="16 | merge (13)" shape="rectangle" ];
13 [ label="13 | 7" shape="rectangle" ];
14 [ label="14 | <" shape="rectangle" ];
15 [ label="15 | if" shape="rectangle" ];
16 [ label="16 | merge (15)" shape="rectangle" ];
17 [ label="17 | break" shape="rectangle" ];
18 [ label="18 | dummy" shape="rectangle" ];
19 [ label="19 | call" shape="rectangle" ];
20 [ label="20 | call" shape="rectangle" ];
21 [ label="21 | return (1)" shape="rectangle" ];
22 [ label="22 | return (2)" shape="rectangle" ];
6 -> 4 [ label="condition" ];
0 -> 5 [ label="control" ];
5 -> 4 [ label="control" ];
6 -> 4 [ label="condition" ];
4 -> 7 [ label="true-control" ];
8 -> 9 [ label="right" ];
3 -> 9 [ label="left" ];
9 -> 7 [ label="condition" ];
7 -> 11 [ label="true-control" ];
11 -> 4 [ label="control" ];
7 -> 12 [ label="false-control" ];
7 -> 8 [ label="right" ];
3 -> 8 [ label="left" ];
4 -> 9 [ label="true-control" ];
8 -> 9 [ label="condition" ];
9 -> 11 [ label="true-control" ];
11 -> 5 [ label="control" ];
9 -> 12 [ label="false-control" ];
12 -> 10 [ label="control" ];
10 -> 13 [ label="control" ];
14 -> 15 [ label="right" ];
3 -> 15 [ label="left" ];
15 -> 13 [ label="condition" ];
13 -> 17 [ label="true-control" ];
13 -> 18 [ label="false-control" ];
13 -> 14 [ label="right" ];
3 -> 14 [ label="left" ];
10 -> 15 [ label="control" ];
14 -> 15 [ label="condition" ];
15 -> 17 [ label="true-control" ];
15 -> 18 [ label="false-control" ];
18 -> 16 [ label="control" ];
1 -> 19 [ label="callable" ];
16 -> 19 [ label="control" ];
Expand All @@ -47,7 +47,7 @@ digraph G {
4 -> 20 [ label="false-control" ];
17 -> 20 [ label="control" ];
1 -> 21 [ label="control" ];
8 -> 21 [ label="value" ];
7 -> 21 [ label="value" ];
2 -> 22 [ label="control" ];
8 -> 22 [ label="value" ];
7 -> 22 [ label="value" ];
}
Loading

0 comments on commit b704b50

Please sign in to comment.