forked from MrCrayfish/MrCrayfishDeviceMod
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathit.txt
121 lines (108 loc) · 4.99 KB
/
it.txt
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
ultranLang:
{
if (!tests.isEnabled("ultran_lang")) break ultranLang;
SpiKt.setShouldLogInternalErrors(false);
SpiKt.setShouldLogScope(false);
SpiKt.setShouldLogStack(false);
SpiKt.setShouldLogTokens(false);
String text = """
program Main;
function Alpha(a: integer; b: integer) {
function Beta(a: integer; b: integer) {
var x: integer;
x = a * 10 + b * 2;
};
var x: integer;
x = (a + b ) * 2;
Beta(5, 10); [ function call ]
};
Alpha(3 + 5, 7); [ function call ]
var x: integer;
x = 300;
var startX: integer;
var startY: integer;
startX = 10;
startY = 20;
function PrintHelloWorld() {
log("info", "Hello World from an UltranLang script.");
};
PrintHelloWorld();
log("info", "Hello World! Number: " + randInt(startX, startY));
""";
NativeCalls calls = new NativeCalls();
registerNativeFunctions(calls);
var lexer = new Lexer(text);
Program tree;
try {
var parser = new Parser(lexer);
tree = parser.parse();
} catch (LexerException | ParserException e) {
if (SpiKt.getShouldLogInternalErrors()) e.printStackTrace();
LOGGER.error("Error parsing file: {}", e.getMessage());
break ultranLang;
} catch (RuntimeException e) {
var cause = e.getCause();
while (cause instanceof InvocationTargetException || cause instanceof RuntimeException) {
cause = cause.getCause();
}
if (cause instanceof LexerException) {
if (SpiKt.getShouldLogInternalErrors()) cause.printStackTrace();
LOGGER.error("Error parsing file: {}", cause.getMessage());
} else if (cause instanceof ParserException) {
if (SpiKt.getShouldLogInternalErrors()) cause.printStackTrace();
LOGGER.error("Error parsing file: {}", cause.getMessage());
} else {
throw e;
}
break ultranLang;
}
var semanticAnalyzer = new SemanticAnalyzer(calls);
try {
semanticAnalyzer.visit(tree);
} catch (SemanticException e) {
if (SpiKt.getShouldLogInternalErrors()) e.printStackTrace();
LOGGER.error("Error analyzing file: {}", e.getMessage());
break ultranLang;
} catch (RuntimeException e) {
var cause = e.getCause();
while (cause instanceof InvocationTargetException || cause instanceof RuntimeException) {
cause = cause.getCause();
}
if (cause instanceof SemanticException) {
if (SpiKt.getShouldLogInternalErrors()) cause.printStackTrace();
ULTRAN_LANG_LOGGER.error("Error analyzing file: {}", cause.getMessage());
} else {
throw e;
}
break ultranLang;
}
try {
var interpreter = new Interpreter(tree);
interpreter.interpret();
} catch (Exception e) {
if (SpiKt.getShouldLogInternalErrors()) e.printStackTrace();
LOGGER.error("Error interpreting file: {}", e.getMessage());
}
}
}
private static void registerNativeFunctions(NativeCalls calls) {
calls.register("log", SpiKt.params()
.add("level", BuiltinTypeSymbol.STRING)
.add("message", BuiltinTypeSymbol.STRING), ar -> {
Object level = ar.get("level");
if (level instanceof String levelName) {
Object message = ar.get("message");
if (message == null) message = "null";
switch (levelName.toLowerCase(Locale.ROOT)) {
case "warn" -> ULTRAN_LANG_LOGGER.warn(message.toString());
case "error" -> ULTRAN_LANG_LOGGER.error(message.toString());
case "debug" -> ULTRAN_LANG_LOGGER.debug(message.toString());
case "trace" -> ULTRAN_LANG_LOGGER.trace(message.toString());
default -> ULTRAN_LANG_LOGGER.info(message.toString());
}
} else {
throw new IllegalArgumentException("Invalid level of type " + (level == null ? "null" : level.getClass().getName()));
}
return null;
});
}