-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleInterpreter.java
More file actions
285 lines (255 loc) · 9.65 KB
/
Copy pathsimpleInterpreter.java
File metadata and controls
285 lines (255 loc) · 9.65 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* Copyright (c) 2025 - 2026 PCazzaniga (github.com)
*
* simpleInterpreter.java is part of SIMPLE.
*
* SIMPLE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SIMPLE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SIMPLE. If not, see <http://www.gnu.org/licenses/>.
*/
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class simpleInterpreter {
public static void main(String[] args){
if (args.length < 1) {
System.out.println("Invalid command line parameters");
System.exit(exitCodes.INVALID_COMMAND_ARGS);
}
List<String> argsList = new ArrayList<>(List.of(args));
String firstArg = argsList.get(0);
if(firstArg.equals("-h") || firstArg.equals("--help")) argsInterpreter.printHelp();
if(firstArg.equals("-s") || firstArg.equals("--simple")) argsInterpreter.printLogo();
if(!firstArg.endsWith(".simple")) {
System.out.println(firstArg + " is not a valid .simple file.");
System.exit(exitCodes.WRONG_FILE_TYPE);
}
CharStream input = null;
try{
input = CharStreams.fromFileName(firstArg);
} catch (IOException e) {
System.out.println("Failed to read input file.");
System.exit(exitCodes.CANNOT_READ_FILE);
}
argsList.remove(0);
argsInterpreter argsIn = new argsInterpreter(argsList);
if (!argsIn.validOpts) {
System.out.println("Invalid command line parameters");
System.exit(exitCodes.INVALID_COMMAND_ARGS);
}
simpleSpellCheckLexer spellLexer = new simpleSpellCheckLexer(input, argsIn.dialect);
CommonTokenStream spellTokens = new CommonTokenStream(spellLexer);
simpleSpellCheckParser spellParser = new simpleSpellCheckParser(spellTokens, argsIn.dialect);
if(!argsIn.minimalOpt){
spellParser.removeErrorListeners();
spellParser.addErrorListener(new simpleErrorListener(spellParser));
}
simpleSpellCheckParser.FileContext spellFileTree = spellParser.file();
if (spellParser.getNumberOfSyntaxErrors() > 0) System.exit(exitCodes.FAILED_SPELLING_PARSE);
ParseTreeWalker walker = new ParseTreeWalker();
spellCheckListener speller = new spellCheckListener(spellParser);
walker.walk(speller, spellFileTree);
if(speller.foundErrors()) System.exit(exitCodes.FAILED_SPELLING_CHECK);
try{
input = CharStreams.fromFileName(firstArg);
} catch (IOException e) {
System.out.println("Failed to read again input file.");
System.exit(exitCodes.CANNOT_READ_FILE);
}
simpleLexer lexer = new simpleLexer(input, argsIn.dialect);
CommonTokenStream tokens = new CommonTokenStream(lexer);
simpleParser parser = new simpleParser(tokens, argsIn.dialect);
if(!argsIn.minimalOpt){
parser.removeErrorListeners();
parser.addErrorListener(new simpleErrorListener(parser));
simpleErrorStrategy handler = new simpleErrorStrategy();
parser.setErrorHandler(handler);
}
simpleParser.FileContext fileTree = parser.file();
if (parser.getNumberOfSyntaxErrors() > 0) System.exit(exitCodes.FAILED_PROGRAM_PARSE);
validateListener validator;
if(!argsIn.minimalOpt){
nodeCounter nC = new nodeCounter();
walker.walk(nC, fileTree);
validator = new validateListenerWithBar(parser, nC.getCount());
}else{
validator = new validateListener(parser);
}
walker.walk(validator, fileTree);
if (!validator.isValidationOk()) System.exit(exitCodes.FAILED_PROGRAM_VALIDATION);
if(!(argsIn.minimalOpt && argsIn.execOpt)) System.out.println("Validation successful");
if (argsIn.execOpt) {
List<typeVisitor.dataType> pArgs = argsIn.programArgs.stream().map(inputHandler::typeOfLiteral).toList();
if (!validator.validateProgramArgs(pArgs)){
System.out.println("Invalid program arguments");
System.exit(exitCodes.INVALID_PROGRAM_ARGS);
}
if(!argsIn.minimalOpt) {
simpleErrorListener el = (simpleErrorListener) parser.getErrorListeners().get(0);
el.showCounter(false);
}
executeVisitor.Builder execB = new executeVisitor.Builder(parser);
execB.setExpectedInputs(validator.getRuntimeInputs());
execB.setProgramArgs(argsIn.programArgs.stream().map(inputHandler::valOfLiteral).toList());
if(argsIn.loopLimit > 0) execB.setMaxLoop(argsIn.loopLimit);
if (argsIn.recLimit > 0) execB.setMaxRecursion(argsIn.recLimit);
executeVisitor executor = execB.build();
executor.visitFile(fileTree);
}
}
private static class argsInterpreter{
boolean execOpt = false;
boolean validOpts = true;
boolean minimalOpt = false;
int loopLimit = 0;
int recLimit = 0;
simpleLanguages.Lang dialect = simpleLanguages.Lang.ENG;
final List<simpleRTLitParser.LiteralContext> programArgs = new ArrayList<>();
public argsInterpreter(List<String> args) {
List<String> argsList = new ArrayList<>(args);
boolean ignoreRest = false;
for (Iterator<String> itr = argsList.iterator(); itr.hasNext(); ) {
String arg = itr.next();
switch (arg) {
case "-a": case "--args":
itr.remove();
ignoreRest = true;
break;
case "-d": case "--dialect":
itr.remove();
if (itr.hasNext()) {
try{
dialect = simpleLanguages.Lang.valueOf(itr.next());
} catch (IllegalArgumentException e) {
validOpts = false;
ignoreRest = true;
}
itr.remove();
} else {
validOpts = false;
ignoreRest = true;
}
break;
case "-e": case "--execute":
itr.remove();
execOpt = true;
break;
case "-h": case "--help":
printHelp();
break;
case "-l": case "--loop":
itr.remove();
if (itr.hasNext()) {
try{
loopLimit = Integer.parseInt(itr.next());
if(loopLimit < 1){
validOpts = false;
ignoreRest = true;
}
} catch (NumberFormatException e) {
validOpts = false;
ignoreRest = true;
}
itr.remove();
} else {
validOpts = false;
ignoreRest = true;
}
break;
case "-m": case "--minimal":
itr.remove();
minimalOpt = true;
break;
case "-r": case "--recursion":
itr.remove();
if (itr.hasNext()) {
try{
recLimit = Integer.parseInt(itr.next());
if(recLimit < 1){
validOpts = false;
ignoreRest = true;
}
} catch (NumberFormatException e) {
validOpts = false;
ignoreRest = true;
}
itr.remove();
} else {
validOpts = false;
ignoreRest = true;
}
break;
case "-s": case "--simple":
printLogo();
break;
default:
System.out.println("Unknown option " + arg + ". Use --help to view all options.");
System.exit(exitCodes.INVALID_COMMAND_ARGS);
}
if (ignoreRest) break;
}
if (validOpts && !argsList.isEmpty()) {
simpleRTLitLexer argsLxr = new simpleRTLitLexer(null);
simpleRTLitParser argsPsr = new simpleRTLitParser(null);
for (String arg : argsList) {
argsLxr.setInputStream(CharStreams.fromString(arg));
argsPsr.setInputStream(new CommonTokenStream(argsLxr));
programArgs.add(argsPsr.literal());
}
}
}
private static void printLogo() {
String logo =
"""
\t _____
\t / ____|_ _
\t| (___ (_)_ __ ___ _ __ | | ___
\t \\___ \\| | '_ ` _ \\| '_ \\| |/ _ \\
\t ____) | | | | | | | |_) | | __/
\t|_____/|_|_| |_| |_| .__/|_|\\___|
\t |_|
+-------------------------------------------------+
| Simple Is My Programming Language for Education |
+-------------------------------------------------+
""";
System.out.println(logo);
System.exit(exitCodes.TERMINATION);
}
private static void printHelp(){
String helpMsg =
"""
SIMPLE v1.6.1 Copyright (C) 2025 - 2026 PCazzaniga (github.com)
This program is distributed under the GNU General Public License Version 3
Interpreter for the S.I.M.P.L.E. programming language, validates and optionally executes a .simple file.
Usage: simplexe filename [options]
Example: simplexe myProgram.simple -e -l 20 --args Hello 2 5
Options:
\t-h, --help\t\tPrint this help message instead of running the interpreter\t*
\t-a, --args\t\tUse anything after this as program arguments
\t-d, --dialect\t\tSpecify the code dialect as ENG (default), ESP, FRA, ITA or ALT
\t-e, --execute\t\tExecute file after (successful) validation
\t-l, --loop\t\tSet custom iteration limit for conditional loops during execution
\t-m, --minimal\t\tReduce interpreter output during parsing and validation
\t-r, --recursion\t\tSet custom recursion limit for functions during execution
\t-s, --simple\t\tPrint a cool ASCII logo instead of running the interpreter :)\t*
Options with * can be used without a filename.
""";
System.out.println(helpMsg);
System.exit(exitCodes.TERMINATION);
}
}
}