-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodePrinter.cs
54 lines (47 loc) · 1.84 KB
/
CodePrinter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.Collections.Generic;
using System.IO;
namespace MiniC {
class CodePrinter : CodeBaseVisitor<int> {
private StreamWriter STSpecFile = new StreamWriter("code.dot");
private static int clusterCount = 0;
private ASTElement currParent = null;
private void ExtractSubgraphs(ASTElement node) {
for (int context = 0; context < node.ContextNumber(); ++context) {
if (node.ChildrenNumber(context) == 0) continue;
STSpecFile.WriteLine("subgraph cluster" + clusterCount++ + "{");
STSpecFile.WriteLine("\tnode [style=filled,color=white];");
STSpecFile.WriteLine("\tstyle=filled;");
STSpecFile.WriteLine("\tcolor=lightgrey;");
STSpecFile.Write("\t");
for (var i = 0; i < node.ChildrenNumber(context); ++i) {
STSpecFile.Write(node.GetChild(context, i).Name + ";");
}
STSpecFile.WriteLine("\n\tlabel=" + node.ContextNames[context] + ";");
//STSpecFile.WriteLine("\n\t\tlabel=" + context + ";");
STSpecFile.WriteLine("}");
}
}
public override int VisitFile(GFile node) {
currParent = node;
STSpecFile.WriteLine("digraph G{");
ExtractSubgraphs(node);
base.VisitChildren(node);
STSpecFile.WriteLine("}");
STSpecFile.Close();
System.Diagnostics.Process.Start("dot.exe", "-Tgif -ocode.gif code.dot");
return 0;
}
public override int VisitChildren(ASTVisitableElement node) {
currParent = node;
ExtractSubgraphs(node);
base.VisitChildren(node);
STSpecFile.WriteLine("\"{0}\"->\"{1}\";", node.Parents[0].Name, node.Name);
return 0;
}
public override int VisitCodeRepo(GCodeRepo node) {
STSpecFile.WriteLine("\"{0}\"->\"{1}\";", currParent.Name, node.Name);
return 0;
}
}
}