I’m making my own programming language for fun and I hava added the goto command.
This is the code of the function that executes the code:
class Var {
char type;
String name;
Object univ;
Var(Object i, String n, char t) {
univ = i;
name = n;
type = t;
}
}
int compile(String[] rows) {
int kurwar = 0;
if (rows[0] == "" && rows.length == 1) {
println("Code is empty");
return 1;
}
for (int r = 0; r < rows.length; r++) {
String row = rows[r];
if (row.contains("goto")) {
r = int(row.substring(5, row.length()));
row = rows[r];
}
if (row == "nop") continue;
if (row == "end") return 0;
if (row.contains("print")) {
print(row.substring(6, row.length()).replace("\\n", "\n"));
continue;
}
/*if (row.contains("println")) {
print(row.substring(8, row.length()).replace("\\n", "\n") + "\n");
continue;
}*/
if (row.contains("let")) {
String[] params = split(row.substring(4, row.length()).replace(" ", ""), ",");
Object val;
String v = params[2];
char type;
switch(params[0]) {
case "int":
val = int(v);
type = 'i';
break;
case "float":
val = float(v);
type = 'f';
break;
case "boolean":
case "bool":
val = boolean(v);
type = 'b';
break;
case "string":
case "String":
val = v;
type = 's';
break;
default:
continue;
}
variables[kurwar] = new Var(val, params[1], type);
kurwar++;
continue;
}
if (row.contains("++")) {
String params = row.substring(3, row.length()).replace(" ", "");
int varInd = searchVar(params);
switch(variables[varInd].type) {
case 'i':
case 'f':
variables[varInd].univ = ((int) variables[varInd].univ) + 1;
break;
case 'b':
variables[varInd].univ = !((boolean) variables[varInd].univ);
break;
}
continue;
}
if (row.contains("--")) {
String params = row.substring(3, row.length()).replace(" ", "");
int varInd = searchVar(params);
switch(variables[varInd].type) {
case 'i':
case 'f':
variables[varInd].univ = ((int) variables[varInd].univ) - 1;
break;
case 'b':
variables[varInd].univ = !((boolean) variables[varInd].univ);
break;
}
continue;
}
if (row.contains("=")) {
String[] params = split(row.substring(2, row.length()).replace(" ", ""), ",");
int varInd = searchVar(params[0]);
String val = params[1];
switch(variables[varInd].type) {
case 'i':
variables[varInd].univ = int(val);
break;
case 'f':
variables[varInd].univ = float(val);
break;
case 'b':
variables[varInd].univ = boolean(val);
break;
}
continue;
}
if (row.contains("wait")) {
delay(int(row.substring(5, row.length())));
continue;
}
}
return 0;
}
int searchVar(String name) {
for (int i = 0; i < variables.length; i++) {
if (variables[i] == null) break;
if (variables[i].name == name) return i;
}
return -1;
}
When I close the main window, the program execution doesn`t stop, the program still endlessly prints hello world with this program code:
print Hello World!
wait 1000
goto 0
I think the solution would be to detect if the main window closes and then end the program. How?