-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOSnakeParser.java
More file actions
93 lines (75 loc) · 3.22 KB
/
OSnakeParser.java
File metadata and controls
93 lines (75 loc) · 3.22 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
import java.io.*;
import java.util.*;
import java.util.regex.*;
public class OSnakeParser {
private static boolean verboseMode = false;
public static void main(String[] args) {
System.out.println("oSNAKE Java Runtime Initialized...");
if (args.length == 0) {
System.out.println("Error: No script file specified.");
System.out.println("Usage: java OSnakeParser [-v] <script-file>");
return;
}
boolean verboseMode = false;
String scriptFilePath;
if (args.length > 1 && args[0].equals("-v")) {
System.out.println("Verbose mode enabled.");
verboseMode = true;
scriptFilePath = args[1];
} else {
scriptFilePath = args[0];
}
File scriptFile = new File(scriptFilePath);
if (!scriptFile.exists()) {
System.out.println("Error: Script file not found.");
return;
}
try (BufferedReader reader = new BufferedReader(new FileReader(scriptFile))) {
StringBuilder script = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
script.append(line).append("\n");
}
System.out.println("Executing script: " + scriptFilePath);
OSnakeParser parser = new OSnakeParser();
parser.execute(script.toString(), verboseMode);
} catch (IOException e) {
System.out.println("Error reading script file: " + e.getMessage());
}
}
public void execute(String script, boolean verboseMode) {
String[] lines = script.split("\n");
for (String line : lines) {
processLine(line.trim(), verboseMode);
}
}
private void processLine(String line, boolean verboseMode) {
if (verboseMode) System.out.println("Processing: " + line);
Pattern funcPattern = Pattern.compile("func\\{(\\w+)\\}");
Matcher funcMatcher = funcPattern.matcher(line);
if (funcMatcher.find()) {
String funcName = funcMatcher.group(1);
if (verboseMode) System.out.println("Defined function: " + funcName);
return;
}
Pattern dataPattern = Pattern.compile("DATA\\[x=(\\d+)\\]");
Matcher dataMatcher = dataPattern.matcher(line);
if (dataMatcher.find()) {
int value = Integer.parseInt(dataMatcher.group(1));
if (verboseMode) System.out.println("Stored: DATA[x=" + value + "]");
return;
}
Pattern tokenPrintPattern = Pattern.compile("print TOKEN\\[(\\w+)\\]");
Matcher tokenPrintMatcher = tokenPrintPattern.matcher(line);
if (tokenPrintMatcher.find()) {
String tokenKey = tokenPrintMatcher.group(1);
System.out.println(tokenKey);
return;
}
Pattern printPattern = Pattern.compile("print\\s*\\((.*?)\\)");
Matcher printMatcher = printPattern.matcher(line);
if (printMatcher.find()) {
System.out.println(printMatcher.group(1).trim());
}
}
}