Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved to AST to facilitate code generation.
Signed-off-by: Tim Henderson <tim.tadh@gmail.com>
- Loading branch information
Showing
18 changed files
with
4,344 additions
and
3,827 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
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
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,104 @@ | ||
@TOKEN(r'>>=') | ||
def t_RIGHT_ASSIGN(self, token): | ||
self.token = token | ||
return token | ||
|
||
@TOKEN(r'<<=') | ||
def t_LEFT_ASSIGN(self, token): | ||
self.token = token | ||
return token | ||
|
||
@TOKEN(r'\+=') | ||
def t_ADD_ASSIGN(self, token): | ||
self.token = token | ||
return token | ||
|
||
@TOKEN(r'\-=') | ||
def t_SUB_ASSIGN(self, token): | ||
self.token = token | ||
return token | ||
|
||
@TOKEN(r'\*=') | ||
def t_MUL_ASSIGN(self, token): | ||
self.token = token | ||
return token | ||
|
||
@TOKEN(r'\/=') | ||
def t_DIV_ASSIGN(self, token): | ||
self.token = token | ||
return token | ||
|
||
@TOKEN(r'\%=') | ||
def t_MOD_ASSIGN(self, token): | ||
self.token = token | ||
return token | ||
|
||
@TOKEN(r'\&=') | ||
def t_AND_ASSIGN(self, token): | ||
self.token = token | ||
return token | ||
|
||
@TOKEN(r'\^=') | ||
def t_XOR_ASSIGN(self, token): | ||
self.token = token | ||
return token | ||
|
||
@TOKEN(r'\|=') | ||
def t_OR_ASSIGN(self, token): | ||
self.token = token | ||
return token | ||
|
||
@TOKEN(r'>>') | ||
def t_RIGHT_OP(self, token): | ||
self.token = token | ||
return token | ||
|
||
@TOKEN(r'<<') | ||
def t_LEFT_OP(self, token): | ||
self.token = token | ||
return token | ||
|
||
@TOKEN(r'\+\+') | ||
def t_INC_OP(self, token): | ||
self.token = token | ||
return token | ||
|
||
@TOKEN(r'\-\-') | ||
def t_DEC_OP(self, token): | ||
self.token = token | ||
return token | ||
|
||
@TOKEN(r'\->') | ||
def t_PTR_OP(self, token): | ||
self.token = token | ||
return token | ||
|
||
@TOKEN(r'\&\&') | ||
def t_AND_OP(self, token): | ||
self.token = token | ||
return token | ||
|
||
@TOKEN(r'\|\|') | ||
def t_OR_OP(self, token): | ||
self.token = token | ||
return token | ||
|
||
@TOKEN(r'\<=') | ||
def t_LE_OP(self, token): | ||
self.token = token | ||
return token | ||
|
||
@TOKEN(r'\>=') | ||
def t_GE_OP(self, token): | ||
self.token = token | ||
return token | ||
|
||
@TOKEN(r'==') | ||
def t_EQ_OP(self, token): | ||
self.token = token | ||
return token | ||
|
||
@TOKEN(r'\!=') | ||
def t_NE_OP(self, token): | ||
self.token = token | ||
return token |
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,19 +1,21 @@ | ||
int hello; | ||
|
||
//HELLO; | ||
hello = 010; | ||
hello = 123; | ||
hello = 0xff; | ||
hello = 5e0123; | ||
hello = .1234e3; | ||
hello /= hello; | ||
char c; | ||
c = 'x'; | ||
char *string; | ||
string = "this is string\0"; | ||
/*this is a comment | ||
int hello = 010; | ||
int hello = 123; | ||
int hello = 0xff; | ||
int hello = 5e0123; | ||
int hello = .1234e3; | ||
char c = 'x'; | ||
char *string = "this is string"; | ||
/*/*this is a comment | ||
it goes on for multiple lines | ||
it has "string" inside it | ||
hurray*/ | ||
hurray | ||
int x = 5; | ||
/*int x = 6;*/ | ||
int x = 7; | ||
/*int x = 6; | ||
int x = 7; | ||
*/ | ||
int main() | ||
{ | ||
hello %= hello; | ||
} |
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,21 @@ | ||
|
||
//HELLO; | ||
int hello = 010; | ||
int hello = 123; | ||
int hello = 0xff; | ||
int hello = 5e0123; | ||
int hello = .1234e3; | ||
char c = 'x'; | ||
char *string = "this is string"; | ||
/*this is a comment | ||
it goes on for multiple lines | ||
it has "string" inside it | ||
hurray*/ | ||
int x = 5; | ||
/*int x = 6;*/ | ||
int x = 7; | ||
|
||
int main() | ||
{ | ||
hello %= hello; | ||
} |
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,4 +1,4 @@ | ||
int main() | ||
{ | ||
int flags; | ||
return 0; | ||
} |
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,4 +1,4 @@ | ||
int main() | ||
{ | ||
goto flags; | ||
int flags; | ||
} |
Binary file not shown.
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,69 @@ | ||
import gram | ||
import subprocess | ||
import sys | ||
|
||
header_string = "digraph %s {\n node [shape=box];\n" | ||
subgraph_prefix = " {\n rank=same; \n" | ||
node_string_nocolor = " %s [label=%s];\n" | ||
node_string_color = " %s [label=%s, style=filled, fillcolor=\"%s\"];\n" | ||
subgraph_postfix = " }\n" | ||
edge_string = " %s -> %s;\n" | ||
footer_string = "}\n" | ||
|
||
class Node(object): | ||
"""Example node with the proper attributes.""" | ||
def __init__(self, string, children=[]): | ||
super(Node, self).__init__() | ||
self.graph_id = "" | ||
self.string = string | ||
self.children = children | ||
|
||
def __str__(self): | ||
return self.string | ||
|
||
node_count = 0 | ||
|
||
def ast_walk_tree(node, rank, subgraph_list=[]): | ||
global node_count | ||
node_count += 1 | ||
node.graph_id = str(node_count) | ||
|
||
if len(subgraph_list)-1 < rank: | ||
subgraph_list.append([]) | ||
|
||
subgraph_list[rank].append(node) | ||
|
||
for child in node.children: | ||
ast_walk_tree(child, rank+1, subgraph_list) | ||
|
||
return subgraph_list | ||
|
||
def ast_dot(root, f, name="AST"): | ||
f.write(header_string % name) | ||
|
||
global node_count | ||
node_count = 0 | ||
|
||
subgraph_list = ast_walk_tree(root, 0, []) | ||
for subgraph in subgraph_list: | ||
f.write(subgraph_prefix) | ||
for node in subgraph: | ||
if hasattr(node,"graph_color"): | ||
f.write(node_string_color % (node.graph_id, str(node), node.graph_color)) | ||
else: | ||
f.write(node_string_nocolor % (node.graph_id, str(node))) | ||
f.write(subgraph_postfix) | ||
|
||
for subgraph in subgraph_list: | ||
for node in subgraph: | ||
for child in node.children: | ||
f.write(edge_string % (node.graph_id, child.graph_id)) | ||
f.write(footer_string) | ||
|
||
if __name__ == "__main__": | ||
c_parser = gram.C_Parser() | ||
c_parser.build() | ||
top = c_parser.parse(sys.stdin.read()) | ||
ast_dot(top, sys.stdout) | ||
print "Running dot..." | ||
popen_obj = subprocess.Popen(["dot", "-Tpdf", "AST.dot", "-o", "AST.pdf"]) |