-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodeDeveloper.java
129 lines (105 loc) · 4.02 KB
/
CodeDeveloper.java
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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CodeDeveloper {
private static Map<String, Object> variableMap;
private static Object getVariableValue(String variable) {
// return variableMap.getOrDefault(variable, 0);
Object value = variableMap.get(variable);
if (value instanceof Integer) {
return (int) value;
}
return value;
}
private static void variableDeclarator(String name, String Value) {
// check if Value is int else String
// String regexIntMatcher = "-?\\d+"; // not needed
try {
// Value.matches(regexIntMatcher);
int numValue = Integer.parseInt(Value);
// Treat as Int
// System.out.println("Int: " + numValue + " is " + Value.getClass());
variableMap.put(name, numValue);
} catch (NumberFormatException E) {
// Treat as string
// System.out.println("String: " + Value + " is " + Value.getClass());
variableMap.put(name, Value);
}
}
private static void codeMatcher(String line) {
// String pseudoCode = "LET variable = hello";
// Define the regular expression pattern for the basic-style pseudo-code
Pattern pattern = Pattern.compile("(?i)\\b(LET)\\s+([a-zA-Z0-9_]*)\\s*=\\s*([a-zA-Z0-9_]*)\\b|"
+ "(IF\\s+([a-zA-Z][a-zA-Z0-9_]*)\\s+THEN\\s+([a-zA-Z][a-zA-Z0-9_]*))|"
+ "(WHILE\\s+([a-zA-Z][a-zA-Z0-9_]*)\\s+DO\\s+([a-zA-Z][a-zA-Z0-9_]*))|"
+ "(ENDIF)|"
+ "(LOOP)");
// (?i)(Let\\s+([a-zA-Z0-9_]*)+\\s+=\\s+([a-zA-Z0-9_]*))|
Matcher matcher = pattern.matcher(line);
// Check if the pseudo-code matches the pattern
if (matcher.matches()) {
// Check which pattern is matched and extract variables accordingly
String letVariable = matcher.group(2);
String letValue = matcher.group(3);
String ifCondition = matcher.group(5);
String ifAction = matcher.group(6);
String whileCondition = matcher.group(8);
String whileAction = matcher.group(9);
String endif = matcher.group(10);
String loop = matcher.group(11);
if (letVariable != null) {
System.out.println("LET Statement");
System.out.println("Variable: " + letVariable);
System.out.println("Value: " + letValue);
// initialize variable
variableDeclarator(letVariable, letValue);
} else if (ifCondition != null) {
System.out.println("IF Statement");
System.out.println("Condition: " + ifCondition);
System.out.println("Action: " + ifAction);
} else if (whileCondition != null) {
System.out.println("WHILE Statement");
System.out.println("Condition: " + whileCondition);
System.out.println("Action: " + whileAction);
} else if (endif != null) {
System.out.println("ENDIF Statement");
} else if (loop != null) {
System.out.println("LOOP Statement");
}
} else {
System.out.println("Invalid pseudo-code.");
}
}
// Debug method to print out all variables in system
private static void printMemory() {
System.out.println("\n ___PRINTING ALL MEMORY___");
for (Map.Entry<String, Object> entry : variableMap.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (value instanceof Integer) {
System.out.println("int " + key + " : " + value);
} else {
System.out.println("String " + key + " : " + value);
}
}
System.out.println("__________________________\n");
}
public static void main(String[] args) {
variableMap = new HashMap<>(); // hashmap to store variables
String filePath = "code.txt";
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
codeMatcher(line);
}
} catch (IOException e) {
e.printStackTrace();
}
printMemory();
}
}