-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSTPrinter.cs
More file actions
111 lines (100 loc) · 4.4 KB
/
Copy pathSTPrinter.cs
File metadata and controls
111 lines (100 loc) · 4.4 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Antlr4.Runtime.Tree;
namespace TeachingMaterial {
public class STPrinter :SimpleCalcParserBaseVisitor<int> {
StreamWriter m_STSpecFile = new StreamWriter("test.dot");
Stack<string> m_parentsLabel = new Stack<string>();
private static int ms_serialCounter = 0;
public override int VisitExprAssignment(SimpleCalcParser.ExprAssignmentContext context) {
string label = "Assignment" + "_" + ms_serialCounter++;
m_STSpecFile.WriteLine("\"{0}\"->\"{1}\";", m_parentsLabel.Peek(), label);
m_parentsLabel.Push(label);
base.VisitExprAssignment(context);
m_parentsLabel.Pop();
return 0;
}
public override int VisitExprParenthesis(SimpleCalcParser.ExprParenthesisContext context) {
return base.VisitExprParenthesis(context);
}
public override int VisitExprMulDiv(SimpleCalcParser.ExprMulDivContext context) {
string label = "";
switch (context.op.Type) {
case SimpleCalcLexer.MULT:
label = "Multiplication" + "_" + ms_serialCounter++;
break;
case SimpleCalcLexer.DIV:
label = "Division" + "_" + ms_serialCounter++;
break;
}
m_STSpecFile.WriteLine("\"{0}\"->\"{1}\";", m_parentsLabel.Peek(), label);
m_parentsLabel.Push(label);
base.VisitExprMulDiv(context);
m_parentsLabel.Pop();
return 0;
}
public override int VisitExprAddSub(SimpleCalcParser.ExprAddSubContext context) {
string label = "";
switch (context.op.Type) {
case SimpleCalcLexer.PLUS:
label = "Addition" + "_" + ms_serialCounter++;
break;
case SimpleCalcLexer.MINUS:
label = "Subtraction" + "_" + ms_serialCounter++;
break;
}
m_STSpecFile.WriteLine("\"{0}\"->\"{1}\";", m_parentsLabel.Peek(), label);
m_parentsLabel.Push(label);
base.VisitExprAddSub(context);
m_parentsLabel.Pop();
return 0;
}
public override int VisitCompileUnit(SimpleCalcParser.CompileUnitContext context) {
string label = "CompileUnit" + "_" + ms_serialCounter++;
m_STSpecFile.WriteLine("digraph G{");
m_parentsLabel.Push(label);
base.VisitCompileUnit(context);
m_parentsLabel.Pop();
m_STSpecFile.WriteLine("}");
m_STSpecFile.Close();
// Prepare the process dot to run
ProcessStartInfo start = new ProcessStartInfo();
// Enter in the command line arguments, everything you would enter after the executable name itself
start.Arguments = "-Tgif " +
Path.GetFileName("test.dot") + " -o " +
Path.GetFileNameWithoutExtension("test") + ".gif";
// Enter the executable to run, including the complete path
start.FileName = "dot";
// Do you want to show a console window?
start.WindowStyle = ProcessWindowStyle.Hidden;
start.CreateNoWindow = true;
int exitCode;
// Run the external process & wait for it to finish
using (Process proc = Process.Start(start)) {
proc.WaitForExit();
// Retrieve the app's exit code
exitCode = proc.ExitCode;
}
return 0;
}
public override int VisitTerminal(ITerminalNode node) {
string label;
switch (node.Symbol.Type) {
case SimpleCalcLexer.NUMBER:
label = "NUMBER" + "_" +node.Symbol.Text +"_" + ms_serialCounter++;
m_STSpecFile.WriteLine("\"{0}\"->\"{1}\";", m_parentsLabel.Peek(), label);
break;
case SimpleCalcLexer.VARIABLE:
label = "VARIABLE" + "_" + node.Symbol.Text + "_" + ms_serialCounter++;
m_STSpecFile.WriteLine("\"{0}\"->\"{1}\";", m_parentsLabel.Peek(), label);
break;
}
return base.VisitTerminal(node);
}
}
}