-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockTracer.java
More file actions
132 lines (112 loc) · 5.31 KB
/
BlockTracer.java
File metadata and controls
132 lines (112 loc) · 5.31 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* Represents a tracer class that keeps track of all the blocks using stack.
*/
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.Stack;
import java.time.Duration;
import java.time.Instant;
public class BlockTracer {
public static void main(String[] args) {
Instant start = Instant.now();
Scanner scanner = new Scanner(System.in);
// Comment out from here
//System.out.print("Enter C program filename: ");
//String fileName = scanner.nextLine();
//System.out.println();
// To here
Stack<Block> stackTrace = new Stack<>();
FileInputStream fis;
String data_old;
try {
fis = new FileInputStream("sample4.c"); // Change filename to say sample4.c
InputStreamReader inStream = new InputStreamReader(fis);
BufferedReader stdin = new BufferedReader(inStream);
while ((data_old = stdin.readLine()) != null){
String[] new_data = stringCleaner(data_old);
for (String data: new_data){
if (data.isEmpty()) continue;
if (data.charAt(0) == '{'){
stackTrace.push(new Block());
} else if (data.startsWith("int")){
String[] dataNew = data.split(" ");
for (String token : dataNew){
if (token.equals("int") || token.isEmpty()) continue;
String[] sub_token = token.split("=");
int num;
if (sub_token.length == 2) num = Integer.parseInt(sub_token[1]);
else num = 0;
Variable var = new Variable(sub_token[0],num);
stackTrace.peek().addVar(var);
}
} else if (data.contains("/*$print")){
String intName = data.substring(9, data.length()-2);
if (intName.equals("LOCAL")){
String LOCAL = stackTrace.peek().toString();
if (!LOCAL.isEmpty()){
System.out.println("Variable Name Initial Value");
System.out.println(LOCAL + "\n");
} else {
System.out.println("No local variables to print.\n");
}
continue;
}
Stack<Block> tempStack = new Stack<>();
boolean valF = false;
Variable var = new Variable();
while (stackTrace.size() > 0){
var = stackTrace.peek().findVal(intName);
if (var == null){
tempStack.push(stackTrace.pop());
continue;
}
valF = true;
break;
}
while (tempStack.size() > 0){
stackTrace.push(tempStack.pop());
}
if (valF){
System.out.println("Variable Name Initial Value");
System.out.println(var.toString() + "\n");
} else {
System.out.println("Variable not found: " + intName + "\n");
}
} else if (data.charAt(0) == '}') {
stackTrace.pop();
}
}
}
stdin.close();
} catch (FileNotFoundException e) {
System.out.println("The file you entered was not found. Try again.");
} catch (IOException e) {
System.out.println("Internal IOException Error Occured. Try again.");
}
scanner.close();
// Print execution time
Instant end = Instant.now();
Duration duration = Duration.between(start, end);
System.out.println("Execution time: " + duration.toMillis() + " milliseconds");
}
// A universal function that is used to reformat the input line of C code into neatly formatted array, each containing one logical component of the line.
public static String[] stringCleaner(String b){
String[] parts = b.split("(?<=[;\\{\\}]|(?<=\\*\\/))|(?=[\\{\\}])");
for (int i = 0; i < parts.length; i++){
parts[i] = parts[i].trim().strip().replace(";", "");
if (parts[i].startsWith("int")){
parts[i] = parts[i].replace(" = ", "=").replace(",", "");
}
String var = parts[i];
Boolean isTrue = var.startsWith("int") || var.startsWith("/*$print ") || var.startsWith("{") || var.startsWith("}");
if (!isTrue){
parts[i] = "";
}
}
return parts;
}
}