-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature (cfa): if, while, do-while, for, switch
- Loading branch information
Andreas
committed
Mar 19, 2022
1 parent
60509ee
commit 0479202
Showing
6 changed files
with
279 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
./pylintrc.txt | ||
examples/ | ||
/graphs | ||
|
||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
/graphs | ||
|
||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import unittest | ||
from os import linesep | ||
from typing import List, Tuple | ||
|
||
from src.cfa import CFA | ||
|
||
from src.ts import ( | ||
LanguageLibrary, | ||
Parser, | ||
Query, | ||
Tree, | ||
TreeCFAVisitor, | ||
) | ||
|
||
|
||
class TestGraphsGraphics(unittest.TestCase): | ||
def setUp(self) -> None: | ||
LanguageLibrary.build() | ||
self._language = LanguageLibrary.c() | ||
self._parser = Parser.create_with_language(self._language) | ||
|
||
self._compound_assignment_query: Query = self._language.query(self._language.syntax.query_compound_assignment) | ||
self._assignment_query: Query = self._language.query(self._language.syntax.query_assignment) | ||
self._binary_expression_query: Query = self._language.query(self._language.syntax.query_binary_expression) | ||
return super().setUp() | ||
|
||
def test_create_graphs_graphics(self) -> None: | ||
programs: List[Tuple[str, str]] = [ | ||
("if_1", "a=1; if(a==2) { }"), | ||
("if_2", "a=1; if(a==2) { } else { }"), | ||
("if_3", "a=1; if(a==2) { } else { } a=2;"), | ||
("if_4", "a=1; if(a==2) { } else if(a==3) { } else { }"), | ||
("if_5", "a=1; if(a==2) { } else if(a==3) { } a=2;"), | ||
("if_6", "a=1; if(a==1) { a=2; }"), | ||
("if_7", "a=1; if(a==1) { a=2; } a=3;"), | ||
("if_8", "a=1; if(a==1) { a=2; } else { a=3; }"), | ||
("if_9", "a=1; if(a==1) { a=2; } else { a=3; } a=4;"), | ||
("if_10", "a=1; if(a==1) { a=2; } else if(a==2) { a=3; } a=4;"), | ||
("if_11", "a=1; if(a==1) { a=2; } else if(a==2) { a=3; } a=4;"), | ||
("if_12", "a=1; if(a==1) { a=2; } else if(a==2) { a=3; } else { a=4; } a=5; a=6;"), | ||
("if_13", "a=1; if(a==1) { a=2; } else if(a==2) { a=3; } else if(a==3) { a=4; } a=5;"), | ||
("if_14", "a=1; if(a==1) { a=2; } else if(a==2) { } else if(a==3) { a=4; } else if(a==4) { a=5; } else { a=6; } a=7;"), | ||
("if_15", "a=1; if(a==1) { a=2; } else if(a==2) { a=3; } else if(a==3) { a=4; } else if(a==4) { a=5; } else { a=6; } a=7;"), | ||
("if_16", "a=1; if(a==1) { a=2; } a=3; if(a==2) { a=2; } a=3; if(a==3) { a=2; } a=3;"), | ||
("while_1", "while(a==1) { }"), | ||
("while_2", "while(a==1) { } a=3;"), | ||
("while_3", "while(a==1) { a=2; } a=3;"), | ||
("while_4", "while(a==1) { a=1; a=2; a=3; } a=4;"), | ||
("while_4", "while(a==1) { a=1; a=2; a=3; } a=4;"), | ||
("while_5", "while(a==1) { if(a==1) { a=1; } a=2; a=3; a=4; a=5; } a=6;"), | ||
("while_6", "while(a==1) { if(a==1) { a=1; } else { a=2; } a=3; } a=4;"), | ||
("while_7", "while(a==1) { if(a==1) { a=1; } else if(a==2) { a=2; } else { a=3; } a=4; } a=5;"), | ||
("while_8", "while(a==1) { if(a==1) { a=1; } a=2; if(a==2) { a=3; } a=4; a=5; a=6; } a=7;"), | ||
("do_while_1", "do { a=1; } while(a==1); a=2;"), | ||
("do_while_2", "do { if(a==1) { a=1; } a=2; } while(a==1); a=2;"), | ||
("do_while_3", "do { a=0; if(a==1) { a=1; } a=2; } while(a==1); a=2;"), | ||
("for_1", "for (int i=0; i<5; ++i) { a=2; } a=3;"), | ||
("switch_1", """ | ||
switch (a) | ||
{ | ||
case 1: a=1; | ||
} | ||
"""), | ||
("switch_2", """ | ||
switch (a) | ||
{ | ||
case 3: { a=2; } | ||
} | ||
"""), | ||
("switch_3", """ | ||
switch (a) | ||
{ | ||
case 1: a=1; | ||
case 2: a=1; | ||
case 3: { a=2; } | ||
default: a=3; | ||
} | ||
"""), | ||
("switch_4", """ | ||
switch (a) | ||
{ | ||
case 1: | ||
case 2: a=1; | ||
case 3: { a=2; } | ||
default: a=3; | ||
} | ||
"""), | ||
("switch_5", """ | ||
switch (a) | ||
{ | ||
case 1: | ||
case 2: a=1; | ||
case 3: { a=2; } | ||
default: a=3; | ||
} | ||
a=10; | ||
""") | ||
] | ||
for program in programs: | ||
name: str = program[0] | ||
prog: str = program[1] | ||
tree: Tree = self._parser.parse(prog) | ||
visitor: TreeCFAVisitor = TreeCFAVisitor() | ||
cfa: CFA = visitor.create(tree.root_node) | ||
dot = cfa.draw(tree, name) | ||
dot.save(directory="graphs") | ||
|
||
self.assertEqual(' '.join(visitor._order), None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.