2022-03-05 20:55:03 +01:00
|
|
|
import java.io.*;
|
2022-04-16 18:44:07 +02:00
|
|
|
import java.lang.reflect.*;
|
2022-03-06 19:34:29 +01:00
|
|
|
import java.net.BindException;
|
2022-03-06 14:26:18 +01:00
|
|
|
import java.net.ServerSocket;
|
|
|
|
import java.net.Socket;
|
2022-04-29 18:10:14 +02:00
|
|
|
import java.nio.file.Files;
|
2022-04-25 00:34:59 +02:00
|
|
|
import java.util.*;
|
2022-03-05 20:55:03 +01:00
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
2022-03-12 19:49:47 +01:00
|
|
|
import java.util.function.Supplier;
|
2022-04-30 09:45:34 +02:00
|
|
|
import sun.misc.Unsafe; // the Safe
|
2022-03-05 20:55:03 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author TudbuT
|
|
|
|
* @since 04 Mar 2022
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class ISBPL {
|
2022-04-12 16:43:51 +02:00
|
|
|
// TODO: fully implement JIO
|
|
|
|
// public static final boolean ENABLE_JINTEROP = true;
|
2022-04-16 18:44:07 +02:00
|
|
|
|
2022-04-30 09:45:34 +02:00
|
|
|
static Unsafe theSafe;
|
|
|
|
static {
|
|
|
|
try {
|
|
|
|
Field f = Unsafe.class.getDeclaredField("theUnsafe");
|
|
|
|
f.setAccessible(true);
|
|
|
|
theSafe = (Unsafe) f.get(null);
|
|
|
|
} catch (Throwable e) {
|
|
|
|
throw new Error(e);
|
|
|
|
}
|
|
|
|
}
|
2022-04-16 18:44:07 +02:00
|
|
|
|
|
|
|
static boolean debug = false, printCalls = false;
|
2022-03-06 14:26:18 +01:00
|
|
|
public ISBPLDebugger.IPC debuggerIPC = new ISBPLDebugger.IPC();
|
2022-03-05 20:55:03 +01:00
|
|
|
ArrayList<ISBPLType> types = new ArrayList<>();
|
2022-04-30 15:27:15 +02:00
|
|
|
ISBPLFrame level0 = new ISBPLFrame(this);
|
2022-03-16 00:45:22 +01:00
|
|
|
final ISBPLThreadLocal<Stack<File>> fileStack = ISBPLThreadLocal.withInitial(Stack::new);
|
2022-04-29 18:30:10 +02:00
|
|
|
final ISBPLThreadLocal<Stack<ISBPLFrame>> frameStack = ISBPLThreadLocal.withInitial(() -> {
|
|
|
|
Stack<ISBPLFrame> frames = new Stack<>();
|
|
|
|
frames.push(level0);
|
|
|
|
return frames;
|
|
|
|
});
|
2022-03-12 19:49:47 +01:00
|
|
|
final ISBPLThreadLocal<ArrayList<String>> lastWords = ISBPLThreadLocal.withInitial(() -> new ArrayList<>(16));
|
2022-03-05 20:55:03 +01:00
|
|
|
int exitCode;
|
2022-03-12 19:59:39 +01:00
|
|
|
private final ISBPLStreamer streamer = new ISBPLStreamer(this);
|
2022-03-06 18:46:04 +01:00
|
|
|
ArrayList<String> included = new ArrayList<>();
|
2022-03-14 17:17:58 +01:00
|
|
|
HashMap<String, ISBPLCallable> natives = new HashMap<>();
|
2022-03-05 20:55:03 +01:00
|
|
|
|
2022-04-29 18:10:14 +02:00
|
|
|
private final Object syncMakeThread = new Object();
|
2022-03-14 17:17:58 +01:00
|
|
|
private ISBPLKeyword getKeyword(String word) {
|
2022-03-05 20:55:03 +01:00
|
|
|
switch (word) {
|
|
|
|
case "native":
|
|
|
|
return (idx, words, file, stack) -> {
|
|
|
|
idx++;
|
2022-03-12 19:49:47 +01:00
|
|
|
addNative(words[idx]);
|
2022-03-05 20:55:03 +01:00
|
|
|
return idx;
|
|
|
|
};
|
|
|
|
case "func":
|
2022-03-16 00:45:22 +01:00
|
|
|
return (idx, words, file, stack) -> createFunction(idx, words, file);
|
2022-03-05 20:55:03 +01:00
|
|
|
case "def":
|
|
|
|
return (idx, words, file, stack) -> {
|
|
|
|
idx++;
|
2022-04-30 15:27:15 +02:00
|
|
|
frameStack.get().peek().define(words[idx], getNullObject());
|
2022-03-05 20:55:03 +01:00
|
|
|
return idx;
|
|
|
|
};
|
|
|
|
case "if":
|
|
|
|
return (idx, words, file, stack) -> {
|
|
|
|
idx++;
|
|
|
|
AtomicInteger i = new AtomicInteger(idx);
|
2022-05-13 12:18:22 +02:00
|
|
|
ISBPLCallable callable = readCallable(i, words, file);
|
2022-03-05 20:55:03 +01:00
|
|
|
if(stack.pop().isTruthy()) {
|
2022-03-16 00:45:22 +01:00
|
|
|
callable.call(stack);
|
2022-03-05 20:55:03 +01:00
|
|
|
}
|
|
|
|
return i.get();
|
|
|
|
};
|
|
|
|
case "while":
|
|
|
|
return (idx, words, file, stack) -> {
|
|
|
|
idx++;
|
|
|
|
AtomicInteger i = new AtomicInteger(idx);
|
2022-05-13 12:18:22 +02:00
|
|
|
ISBPLCallable cond = readCallable(i, words, file);
|
2022-03-05 20:55:03 +01:00
|
|
|
i.getAndIncrement();
|
2022-05-13 12:18:22 +02:00
|
|
|
ISBPLCallable block = readCallable(i, words, file);
|
2022-03-16 00:45:22 +01:00
|
|
|
cond.call(stack);
|
2022-03-05 20:55:03 +01:00
|
|
|
while (stack.pop().isTruthy()) {
|
2022-03-16 00:45:22 +01:00
|
|
|
block.call(stack);
|
|
|
|
cond.call(stack);
|
2022-03-05 20:55:03 +01:00
|
|
|
}
|
|
|
|
return i.get();
|
|
|
|
};
|
|
|
|
case "stop":
|
|
|
|
return (idx, words, file, stack) -> {
|
|
|
|
ISBPLObject o = stack.pop();
|
|
|
|
o.checkType(getType("int"));
|
|
|
|
throw new ISBPLStop((int) o.object);
|
|
|
|
};
|
|
|
|
case "try":
|
|
|
|
return (idx, words, file, stack) -> {
|
|
|
|
idx++;
|
|
|
|
ISBPLObject array = stack.pop();
|
|
|
|
array.checkTypeMulti(getType("array"), getType("string"));
|
|
|
|
String[] allowed;
|
|
|
|
if(array.type.name.equals("string")) {
|
|
|
|
allowed = new String[] { toJavaString(array) };
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ISBPLObject[] arr = ((ISBPLObject[]) array.object);
|
|
|
|
allowed = new String[arr.length];
|
|
|
|
for (int i = 0 ; i < arr.length ; i++) {
|
|
|
|
allowed[i] = toJavaString(arr[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
AtomicInteger i = new AtomicInteger(idx);
|
2022-05-13 12:18:22 +02:00
|
|
|
ISBPLCallable block = readCallable(i, words, file);
|
2022-03-05 20:55:03 +01:00
|
|
|
i.getAndIncrement();
|
2022-05-13 12:18:22 +02:00
|
|
|
ISBPLCallable catcher = readCallable(i, words, file);
|
2022-04-30 19:17:11 +02:00
|
|
|
int stackHeight = stack.size();
|
2022-03-05 20:55:03 +01:00
|
|
|
try {
|
2022-03-16 00:45:22 +01:00
|
|
|
block.call(stack);
|
2022-03-05 20:55:03 +01:00
|
|
|
} catch (ISBPLError error) {
|
2022-03-16 13:35:09 +01:00
|
|
|
if (Arrays.asList(allowed).contains(error.type) || allowed.length == 1 && allowed[0].equals("all")) {
|
2022-03-05 20:55:03 +01:00
|
|
|
stack.push(toISBPLString(error.message));
|
2022-03-06 15:37:19 +01:00
|
|
|
stack.push(toISBPLString(error.type));
|
2022-03-16 00:45:22 +01:00
|
|
|
catcher.call(stack);
|
2022-03-05 20:55:03 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw error;
|
|
|
|
}
|
2022-04-27 01:48:46 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
if (Arrays.asList(allowed).contains("Java") || allowed.length == 1 && allowed[0].equals("all")) {
|
|
|
|
stack.push(toISBPL(e));
|
|
|
|
stack.push(toISBPLString(e.getClass().getName()));
|
|
|
|
stack.push(toISBPLString("Java"));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw e;
|
|
|
|
}
|
2022-05-05 17:34:29 +02:00
|
|
|
} finally {
|
|
|
|
if(stack.size() > stackHeight) {
|
|
|
|
stack.setSize(stackHeight);
|
|
|
|
stack.trimToSize();
|
|
|
|
}
|
|
|
|
while(stack.size() < stackHeight) {
|
|
|
|
stack.push(getNullObject());
|
|
|
|
}
|
2022-03-05 20:55:03 +01:00
|
|
|
}
|
|
|
|
return i.get();
|
|
|
|
};
|
|
|
|
case "do":
|
|
|
|
return (idx, words, file, stack) -> {
|
|
|
|
idx++;
|
|
|
|
AtomicInteger i = new AtomicInteger(idx);
|
2022-05-13 12:18:22 +02:00
|
|
|
ISBPLCallable block = readCallable(i, words, file);
|
2022-03-05 20:55:03 +01:00
|
|
|
i.getAndIncrement();
|
2022-05-13 12:18:22 +02:00
|
|
|
ISBPLCallable catcher = readCallable(i, words, file);
|
2022-03-05 20:55:03 +01:00
|
|
|
try {
|
2022-03-16 00:45:22 +01:00
|
|
|
block.call(stack);
|
2022-03-05 20:55:03 +01:00
|
|
|
} finally {
|
2022-03-16 00:45:22 +01:00
|
|
|
catcher.call(stack);
|
2022-03-05 20:55:03 +01:00
|
|
|
}
|
|
|
|
return i.get();
|
|
|
|
};
|
2022-04-25 12:31:31 +02:00
|
|
|
// Returns closed function (func behaviour)
|
2022-04-16 18:44:07 +02:00
|
|
|
case "{":
|
2022-04-09 02:44:30 +02:00
|
|
|
return (idx, words, file, stack) -> {
|
|
|
|
AtomicInteger i = new AtomicInteger(idx);
|
2022-05-13 12:18:22 +02:00
|
|
|
ISBPLCallable block = readCallable(i, words, file);
|
2022-04-09 02:44:30 +02:00
|
|
|
stack.push(new ISBPLObject(getType("func"), block));
|
|
|
|
return i.get();
|
|
|
|
};
|
2022-03-12 19:49:47 +01:00
|
|
|
case "fork":
|
|
|
|
return (idx, words, file, stack) -> {
|
2022-04-27 01:48:46 +02:00
|
|
|
synchronized(syncMakeThread) {
|
|
|
|
idx++;
|
|
|
|
AtomicInteger i = new AtomicInteger(idx);
|
2022-04-29 18:10:14 +02:00
|
|
|
Stack<ISBPLObject> s = new ISBPLStack<>();
|
2022-04-27 01:48:46 +02:00
|
|
|
for(ISBPLObject obj : stack) {
|
|
|
|
s.push(obj);
|
2022-03-12 19:49:47 +01:00
|
|
|
}
|
2022-05-13 12:18:22 +02:00
|
|
|
ISBPLCallable block = readCallable(i, words, file);
|
2022-04-27 01:48:46 +02:00
|
|
|
long tid = Thread.currentThread().getId();
|
2022-04-30 15:27:15 +02:00
|
|
|
Stack<ISBPLFrame> fstack = (Stack<ISBPLFrame>) frameStack.get().clone();
|
2022-04-27 01:48:46 +02:00
|
|
|
new Thread(() -> {
|
|
|
|
debuggerIPC.run.put(Thread.currentThread().getId(), debuggerIPC.run.getOrDefault(tid, -1) == -1 ? -1 : 0);
|
|
|
|
debuggerIPC.stack.put(Thread.currentThread().getId(), s);
|
2022-04-30 15:27:15 +02:00
|
|
|
frameStack.set(fstack);
|
2022-04-27 01:48:46 +02:00
|
|
|
try {
|
|
|
|
block.call(s);
|
|
|
|
} catch (ISBPLStop stop) {
|
|
|
|
if(stop.amount == -1) {
|
|
|
|
System.exit(exitCode);
|
|
|
|
}
|
2022-04-30 12:15:00 +02:00
|
|
|
} catch (ISBPLError err) {
|
|
|
|
if(err.type.equals("Java"))
|
2022-04-30 15:27:15 +02:00
|
|
|
((Throwable)s.pop().object).printStackTrace();
|
|
|
|
else
|
|
|
|
throw err;
|
2022-04-27 01:48:46 +02:00
|
|
|
}
|
|
|
|
}).start();
|
|
|
|
return i.get();
|
|
|
|
}
|
2022-03-12 19:49:47 +01:00
|
|
|
};
|
2022-05-13 12:18:22 +02:00
|
|
|
case "construct":
|
|
|
|
return (idx, words, file, stack) -> {
|
|
|
|
idx++;
|
|
|
|
String typename = words[idx++];
|
|
|
|
// Chop string quote
|
|
|
|
if(typename.startsWith("\"")) typename = typename.substring(1);
|
|
|
|
|
|
|
|
// Create type
|
|
|
|
ISBPLType type = registerType(typename);
|
|
|
|
|
|
|
|
AtomicInteger i = new AtomicInteger(idx);
|
|
|
|
String[] words2 = readBlock(i, words, file);
|
|
|
|
boolean definingMethods = false;
|
|
|
|
for(int j = 0; j < words2.length; j++) {
|
|
|
|
String word2 = words2[j];
|
|
|
|
// Ignore empty
|
|
|
|
if(word2.equals("")) continue;
|
|
|
|
if(word2.equals("\"")) continue;
|
|
|
|
|
|
|
|
if(definingMethods) {
|
|
|
|
AtomicInteger idx2 = new AtomicInteger(++j);
|
|
|
|
addFunction(type, word2, readCallable(idx2, words2, file));
|
|
|
|
j = idx2.get();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(word2.equals(";")) {
|
|
|
|
definingMethods = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Object var = new Object();
|
|
|
|
addFunction(type, word2, (stack1) -> stack1.push(type.varget(stack1.pop()).getOrDefault(var, getNullObject())));
|
|
|
|
addFunction(type, "=" + word2, (stack1) -> type.varget(stack1.pop()).put(var, stack1.pop()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stack.push(new ISBPLObject(getType("int"), type.id));
|
|
|
|
return i.get();
|
|
|
|
};
|
2022-03-05 20:55:03 +01:00
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("RedundantCast")
|
2022-03-12 19:49:47 +01:00
|
|
|
private void addNative(String name) {
|
2022-04-16 18:44:07 +02:00
|
|
|
ISBPLCallable func;
|
2022-03-05 20:55:03 +01:00
|
|
|
switch (name) {
|
|
|
|
case "alen":
|
2022-03-16 00:45:22 +01:00
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
2022-03-05 20:55:03 +01:00
|
|
|
ISBPLObject o = stack.pop();
|
|
|
|
o.checkType(getType("array"));
|
|
|
|
stack.push(new ISBPLObject(getType("int"), ((ISBPLObject[]) o.object).length));
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "aget":
|
2022-03-16 00:45:22 +01:00
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
2022-03-05 20:55:03 +01:00
|
|
|
ISBPLObject i = stack.pop();
|
|
|
|
ISBPLObject o = stack.pop();
|
|
|
|
i.checkType(getType("int"));
|
|
|
|
o.checkType(getType("array"));
|
|
|
|
stack.push(((ISBPLObject[]) o.object)[((int) i.object)]);
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "aput":
|
2022-03-16 00:45:22 +01:00
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
2022-03-05 20:55:03 +01:00
|
|
|
ISBPLObject toPut = stack.pop();
|
|
|
|
ISBPLObject i = stack.pop();
|
|
|
|
ISBPLObject o = stack.pop();
|
|
|
|
i.checkType(getType("int"));
|
|
|
|
o.checkType(getType("array"));
|
|
|
|
((ISBPLObject[]) o.object)[((int) i.object)] = toPut;
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "anew":
|
2022-03-16 00:45:22 +01:00
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
2022-03-05 20:55:03 +01:00
|
|
|
ISBPLObject i = stack.pop();
|
|
|
|
i.checkType(getType("int"));
|
2022-04-17 16:10:49 +02:00
|
|
|
ISBPLObject[] arr = new ISBPLObject[((int) i.object)];
|
|
|
|
for (int j = 0 ; j < arr.length ; j++) {
|
2022-04-29 18:10:14 +02:00
|
|
|
arr[j] = getNullObject();
|
2022-04-17 16:10:49 +02:00
|
|
|
}
|
|
|
|
stack.push(new ISBPLObject(getType("array"), arr));
|
2022-03-05 20:55:03 +01:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "_array":
|
2022-03-16 00:45:22 +01:00
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
2022-03-05 20:55:03 +01:00
|
|
|
ISBPLObject a = stack.pop();
|
|
|
|
if(a.type.equals(getType("array")))
|
|
|
|
stack.push(a);
|
|
|
|
else if(a.object instanceof ISBPLObject[])
|
|
|
|
stack.push(new ISBPLObject(getType("array"), a.object));
|
|
|
|
else
|
|
|
|
typeError(a.type.name, "array");
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "_char":
|
2022-03-16 00:45:22 +01:00
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
2022-03-05 20:55:03 +01:00
|
|
|
ISBPLObject o = stack.pop();
|
|
|
|
o.checkTypeMulti(getType("int"), getType("byte"), getType("char"), getType("float"), getType("long"), getType("double"));
|
|
|
|
stack.push(new ISBPLObject(getType("char"), ((char) o.toLong())));
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "_byte":
|
2022-03-16 00:45:22 +01:00
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
2022-03-05 20:55:03 +01:00
|
|
|
ISBPLObject o = stack.pop();
|
|
|
|
o.checkTypeMulti(getType("int"), getType("byte"), getType("char"), getType("float"), getType("long"), getType("double"));
|
|
|
|
stack.push(new ISBPLObject(getType("byte"), ((byte) o.toLong())));
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "_int":
|
2022-03-16 00:45:22 +01:00
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
2022-03-05 20:55:03 +01:00
|
|
|
ISBPLObject o = stack.pop();
|
|
|
|
o.checkTypeMulti(getType("int"), getType("byte"), getType("char"), getType("float"), getType("long"), getType("double"));
|
|
|
|
stack.push(new ISBPLObject(getType("int"), ((int) o.toLong())));
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "_float":
|
2022-03-16 00:45:22 +01:00
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
2022-03-05 20:55:03 +01:00
|
|
|
ISBPLObject o = stack.pop();
|
|
|
|
o.checkTypeMulti(getType("int"), getType("byte"), getType("char"), getType("float"), getType("long"), getType("double"));
|
|
|
|
stack.push(new ISBPLObject(getType("float"), ((float) o.toDouble())));
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "_long":
|
2022-03-16 00:45:22 +01:00
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
2022-03-05 20:55:03 +01:00
|
|
|
ISBPLObject o = stack.pop();
|
|
|
|
o.checkTypeMulti(getType("int"), getType("byte"), getType("char"), getType("float"), getType("long"), getType("double"));
|
|
|
|
stack.push(new ISBPLObject(getType("long"), o.toLong()));
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "_double":
|
2022-03-16 00:45:22 +01:00
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
2022-03-05 20:55:03 +01:00
|
|
|
ISBPLObject o = stack.pop();
|
|
|
|
o.checkTypeMulti(getType("int"), getType("byte"), getType("char"), getType("float"), getType("long"), getType("double"));
|
|
|
|
stack.push(new ISBPLObject(getType("double"), o.toDouble()));
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "ischar":
|
2022-03-16 00:45:22 +01:00
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
2022-03-05 20:55:03 +01:00
|
|
|
ISBPLObject o = stack.pop();
|
|
|
|
stack.push(new ISBPLObject(getType("int"), o.type.equals(getType("char")) ? 1 : 0));
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "isbyte":
|
2022-03-16 00:45:22 +01:00
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
2022-03-05 20:55:03 +01:00
|
|
|
ISBPLObject o = stack.pop();
|
|
|
|
stack.push(new ISBPLObject(getType("int"), o.type.equals(getType("byte")) ? 1 : 0));
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "isint":
|
2022-03-16 00:45:22 +01:00
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
2022-03-05 20:55:03 +01:00
|
|
|
ISBPLObject o = stack.pop();
|
|
|
|
stack.push(new ISBPLObject(getType("int"), o.type.equals(getType("int")) ? 1 : 0));
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "isfloat":
|
2022-03-16 00:45:22 +01:00
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
2022-03-05 20:55:03 +01:00
|
|
|
ISBPLObject o = stack.pop();
|
|
|
|
stack.push(new ISBPLObject(getType("int"), o.type.equals(getType("float")) ? 1 : 0));
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "islong":
|
2022-03-16 00:45:22 +01:00
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
2022-03-05 20:55:03 +01:00
|
|
|
ISBPLObject o = stack.pop();
|
2022-04-29 18:10:14 +02:00
|
|
|
stack.push(new ISBPLObject(getType("int"), o.type.equals(getType("long")) ? 1 : 0));
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "isdouble":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
|
|
|
ISBPLObject o = stack.pop();
|
|
|
|
stack.push(new ISBPLObject(getType("int"), o.type.equals(getType("double")) ? 1 : 0));
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "isarray":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
|
|
|
ISBPLObject o = stack.pop();
|
|
|
|
stack.push(new ISBPLObject(getType("int"), o.type.equals(getType("array")) ? 1 : 0));
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "_layer_call":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
|
|
|
ISBPLObject i = stack.pop();
|
|
|
|
ISBPLObject s = stack.pop();
|
|
|
|
i.checkType(getType("int"));
|
2022-04-29 18:30:10 +02:00
|
|
|
frameStack.get().get(frameStack.get().size() - 1 - ((int) i.object)).resolve(toJavaString(s)).call(stack);
|
2022-04-29 18:10:14 +02:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "reload":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
|
|
|
String filepath = getFilePathForInclude((Stack<ISBPLObject>) stack, fileStack.get());
|
2022-03-08 10:22:26 +01:00
|
|
|
File f = new File(filepath).getAbsoluteFile();
|
|
|
|
try {
|
|
|
|
interpret(f, readFile(f), stack);
|
|
|
|
}
|
|
|
|
catch (IOException e) {
|
2022-04-29 18:10:14 +02:00
|
|
|
throw new ISBPLError("IO", "Couldn't find file " + filepath + " required by reload keyword.");
|
2022-03-05 20:55:03 +01:00
|
|
|
}
|
2022-04-29 18:10:14 +02:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "include":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
|
|
|
String filepath = getFilePathForInclude((Stack<ISBPLObject>) stack, fileStack.get());
|
|
|
|
if(!included.contains(filepath)) {
|
|
|
|
File f = new File(filepath).getAbsoluteFile();
|
|
|
|
try {
|
|
|
|
interpret(f, readFile(f), stack);
|
|
|
|
}
|
|
|
|
catch (IOException e) {
|
|
|
|
throw new ISBPLError("IO", "Couldn't find file " + filepath + " required by include keyword.");
|
|
|
|
}
|
|
|
|
included.add(filepath);
|
2022-03-05 20:55:03 +01:00
|
|
|
}
|
2022-04-29 18:10:14 +02:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "putchar":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
|
|
|
ISBPLObject c = stack.pop();
|
|
|
|
c.checkType(getType("char"));
|
|
|
|
System.out.print(((char) c.object));
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "eputchar":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
|
|
|
ISBPLObject c = stack.pop();
|
|
|
|
c.checkType(getType("char"));
|
|
|
|
System.err.print(((char) c.object));
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "_file":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
|
|
|
ISBPLObject s = stack.pop();
|
|
|
|
File f = new File(toJavaString(s));
|
|
|
|
stack.push(new ISBPLObject(getType("file"), f));
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "read":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
|
|
|
ISBPLObject end = stack.pop();
|
|
|
|
ISBPLObject begin = stack.pop();
|
|
|
|
ISBPLObject fileToRead = stack.pop();
|
|
|
|
end.checkType(getType("int"));
|
|
|
|
begin.checkType(getType("int"));
|
|
|
|
fileToRead.checkType(getType("file"));
|
|
|
|
try(FileInputStream f = new FileInputStream((File) fileToRead.object)) {
|
|
|
|
int b = ((int) begin.object);
|
|
|
|
int e = ((int) end.object);
|
|
|
|
byte[] bytes = new byte[e - b];
|
|
|
|
f.read(bytes, b, e);
|
|
|
|
ISBPLObject[] arr = new ISBPLObject[bytes.length];
|
|
|
|
for (int i = 0 ; i < arr.length ; i++) {
|
|
|
|
arr[i] = new ISBPLObject(getType("byte"), bytes[i]);
|
|
|
|
}
|
|
|
|
stack.push(new ISBPLObject(getType("array"), arr));
|
|
|
|
}
|
|
|
|
catch (FileNotFoundException e) {
|
|
|
|
throw new ISBPLError("FileNotFound", "File not found.");
|
|
|
|
}
|
|
|
|
catch (IOException e) {
|
|
|
|
throw new ISBPLError("IO", "File couldn't be read from" + (e.getMessage() != null ? ": " + e.getMessage() : "."));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "flength":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
|
|
|
ISBPLObject f = stack.pop();
|
|
|
|
f.checkType(getType("file"));
|
|
|
|
stack.push(new ISBPLObject(getType("int"), ((int) ((File) f.object).length())));
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "write":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
|
|
|
ISBPLObject content = stack.pop();
|
|
|
|
ISBPLObject fileToWrite = stack.pop();
|
|
|
|
content.checkType(getType("array"));
|
|
|
|
fileToWrite.checkType(getType("file"));
|
|
|
|
throw new ISBPLError("NotImplemented", "_file write is not implemented");
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "getos":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
2022-05-04 19:48:09 +02:00
|
|
|
stack.push(toISBPLString(System.getProperty("os.name")));
|
2022-04-29 18:10:14 +02:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "mktype":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
|
|
|
ISBPLObject s = stack.pop();
|
|
|
|
ISBPLType type = registerType(toJavaString(s));
|
|
|
|
stack.push(new ISBPLObject(getType("int"), type.id));
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "typename":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
|
|
|
ISBPLObject i = stack.pop();
|
|
|
|
i.checkType(getType("int"));
|
|
|
|
stack.push(toISBPLString(types.get(((int) i.object)).name));
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "gettype":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
|
|
|
ISBPLObject o = stack.pop();
|
|
|
|
stack.push(new ISBPLObject(getType("int"), o.type.id));
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "settype":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
|
|
|
ISBPLObject i = stack.pop();
|
|
|
|
ISBPLObject o = stack.pop();
|
|
|
|
i.checkType(getType("int"));
|
|
|
|
stack.push(new ISBPLObject(types.get(((int) i.object)), o.object));
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "throw":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
|
|
|
ISBPLObject message = stack.pop();
|
|
|
|
ISBPLObject type = stack.pop();
|
|
|
|
String msg = toJavaString(message);
|
|
|
|
String t = toJavaString(type);
|
|
|
|
throw new ISBPLError(t, msg);
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "exit":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
|
|
|
ISBPLObject code = stack.pop();
|
|
|
|
code.checkType(getType("int"));
|
|
|
|
exitCode = ((int) code.object);
|
|
|
|
throw new ISBPLStop(0);
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "eq":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
|
|
|
ISBPLObject o1 = stack.pop();
|
|
|
|
ISBPLObject o2 = stack.pop();
|
|
|
|
stack.push(new ISBPLObject(getType("int"), o1.equals(o2) ? 1 : 0));
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "gt":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
|
|
|
ISBPLObject o2 = stack.pop();
|
|
|
|
ISBPLObject o1 = stack.pop();
|
|
|
|
o1.checkTypeMulti(getType("int"), getType("byte"), getType("char"), getType("float"), getType("long"), getType("double"));
|
|
|
|
o2.checkTypeMulti(getType("int"), getType("byte"), getType("char"), getType("float"), getType("long"), getType("double"));
|
|
|
|
stack.push(new ISBPLObject(getType("int"), o1.toDouble() > o2.toDouble() ? 1 : 0));
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "lt":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
|
|
|
ISBPLObject o2 = stack.pop();
|
|
|
|
ISBPLObject o1 = stack.pop();
|
|
|
|
o1.checkTypeMulti(getType("int"), getType("byte"), getType("char"), getType("float"), getType("long"), getType("double"));
|
|
|
|
o2.checkTypeMulti(getType("int"), getType("byte"), getType("char"), getType("float"), getType("long"), getType("double"));
|
|
|
|
stack.push(new ISBPLObject(getType("int"), o1.toDouble() < o2.toDouble() ? 1 : 0));
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "not":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> stack.push(new ISBPLObject(getType("int"), stack.pop().isTruthy() ? 0 : 1));
|
|
|
|
break;
|
|
|
|
case "neg":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
|
|
|
ISBPLObject o = stack.pop();
|
|
|
|
o.checkTypeMulti(getType("int"), getType("byte"), getType("char"), getType("float"), getType("long"), getType("double"));
|
|
|
|
stack.push(new ISBPLObject(o.type, o.negative()));
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "or":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
|
|
|
ISBPLObject o2 = stack.pop();
|
|
|
|
ISBPLObject o1 = stack.pop();
|
|
|
|
if(o1.isTruthy())
|
|
|
|
stack.push(o1);
|
2022-03-05 20:55:03 +01:00
|
|
|
else
|
|
|
|
stack.push(o2);
|
2022-04-29 18:10:14 +02:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "and":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
|
|
|
ISBPLObject o2 = stack.pop();
|
|
|
|
ISBPLObject o1 = stack.pop();
|
|
|
|
// Pushes either 1 or the failed object
|
|
|
|
if (o1.isTruthy()) {
|
|
|
|
if (o2.isTruthy())
|
|
|
|
stack.push(new ISBPLObject(getType("int"), 1));
|
|
|
|
else
|
|
|
|
stack.push(o2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
stack.push(o1);
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "+":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
2022-03-05 20:55:03 +01:00
|
|
|
ISBPLObject o2 = stack.pop();
|
|
|
|
ISBPLObject o1 = stack.pop();
|
|
|
|
o1.checkTypeMulti(getType("int"), getType("byte"), getType("char"), getType("float"), getType("long"), getType("double"));
|
|
|
|
o2.checkTypeMulti(getType("int"), getType("byte"), getType("char"), getType("float"), getType("long"), getType("double"));
|
|
|
|
Object object1 = o1.object;
|
|
|
|
Object object2 = o2.object;
|
|
|
|
ISBPLObject r = null;
|
2022-04-29 18:10:14 +02:00
|
|
|
if(object1 instanceof Integer && object2 instanceof Integer) {
|
|
|
|
r = new ISBPLObject(getType("int"), (int) ((int) (Integer) object1 + (int) (Integer) object2));
|
2022-03-05 20:55:03 +01:00
|
|
|
}
|
2022-04-29 18:10:14 +02:00
|
|
|
if(object1 instanceof Long && object2 instanceof Long) {
|
|
|
|
r = new ISBPLObject(getType("long"), (long) ((long) (Long) object1 + (long) (Long) object2));
|
2022-03-05 20:55:03 +01:00
|
|
|
}
|
2022-04-29 18:10:14 +02:00
|
|
|
if(object1 instanceof Character && object2 instanceof Character) {
|
|
|
|
r = new ISBPLObject(getType("char"), (char) ((char) (Character) object1 + (char) (Character) object2));
|
2022-03-05 20:55:03 +01:00
|
|
|
}
|
2022-04-29 18:10:14 +02:00
|
|
|
if(object1 instanceof Byte && object2 instanceof Byte) {
|
|
|
|
r = new ISBPLObject(getType("byte"), (byte) (Byte.toUnsignedInt((Byte) object1) + Byte.toUnsignedInt((Byte) object2)));
|
2022-03-05 20:55:03 +01:00
|
|
|
}
|
2022-04-29 18:10:14 +02:00
|
|
|
if(object1 instanceof Float && object2 instanceof Float) {
|
|
|
|
r = new ISBPLObject(getType("float"), (float) ((float) (Float) object1 + (float) (Float) object2));
|
2022-03-05 20:55:03 +01:00
|
|
|
}
|
2022-04-29 18:10:14 +02:00
|
|
|
if(object1 instanceof Double && object2 instanceof Double) {
|
|
|
|
r = new ISBPLObject(getType("double"), (double) ((double) (Double) object1 + (double) (Double) object2));
|
2022-03-05 20:55:03 +01:00
|
|
|
}
|
2022-04-29 18:10:14 +02:00
|
|
|
if(r != null)
|
2022-03-05 20:55:03 +01:00
|
|
|
stack.push(r);
|
|
|
|
else
|
|
|
|
typeError(o1.type.name, o2.type.name);
|
2022-04-29 18:10:14 +02:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "-":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
2022-03-05 20:55:03 +01:00
|
|
|
ISBPLObject o2 = stack.pop();
|
|
|
|
ISBPLObject o1 = stack.pop();
|
|
|
|
o1.checkTypeMulti(getType("int"), getType("byte"), getType("char"), getType("float"), getType("long"), getType("double"));
|
|
|
|
o2.checkTypeMulti(getType("int"), getType("byte"), getType("char"), getType("float"), getType("long"), getType("double"));
|
|
|
|
Object object1 = o1.object;
|
|
|
|
Object object2 = o2.object;
|
|
|
|
ISBPLObject r = null;
|
2022-04-29 18:10:14 +02:00
|
|
|
if(object1 instanceof Integer && object2 instanceof Integer) {
|
|
|
|
r = new ISBPLObject(getType("int"), (int) ((int) (Integer) object1 - (int) (Integer) object2));
|
2022-03-05 20:55:03 +01:00
|
|
|
}
|
2022-04-29 18:10:14 +02:00
|
|
|
if(object1 instanceof Long && object2 instanceof Long) {
|
|
|
|
r = new ISBPLObject(getType("long"), (long) ((long) (Long) object1 - (long) (Long) object2));
|
2022-03-05 20:55:03 +01:00
|
|
|
}
|
2022-04-29 18:10:14 +02:00
|
|
|
if(object1 instanceof Character && object2 instanceof Character) {
|
|
|
|
r = new ISBPLObject(getType("char"), (char) ((char) (Character) object1 - (char) (Character) object2));
|
2022-03-05 20:55:03 +01:00
|
|
|
}
|
2022-04-29 18:10:14 +02:00
|
|
|
if(object1 instanceof Byte && object2 instanceof Byte) {
|
|
|
|
r = new ISBPLObject(getType("byte"), (byte) (Byte.toUnsignedInt((Byte) object1) - Byte.toUnsignedInt((Byte) object2)));
|
2022-03-05 20:55:03 +01:00
|
|
|
}
|
2022-04-29 18:10:14 +02:00
|
|
|
if(object1 instanceof Float && object2 instanceof Float) {
|
|
|
|
r = new ISBPLObject(getType("float"), (float) ((float) (Float) object1 - (float) (Float) object2));
|
2022-03-05 20:55:03 +01:00
|
|
|
}
|
2022-04-29 18:10:14 +02:00
|
|
|
if(object1 instanceof Double && object2 instanceof Double) {
|
|
|
|
r = new ISBPLObject(getType("double"), (double) ((double) (Double) object1 - (double) (Double) object2));
|
2022-03-05 20:55:03 +01:00
|
|
|
}
|
2022-04-29 18:10:14 +02:00
|
|
|
if(r != null)
|
2022-03-05 20:55:03 +01:00
|
|
|
stack.push(r);
|
|
|
|
else
|
|
|
|
typeError(o1.type.name, o2.type.name);
|
2022-04-29 18:10:14 +02:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "/":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
2022-03-06 17:39:49 +01:00
|
|
|
try {
|
2022-04-29 18:10:14 +02:00
|
|
|
ISBPLObject o2 = stack.pop();
|
|
|
|
ISBPLObject o1 = stack.pop();
|
|
|
|
o1.checkTypeMulti(getType("int"), getType("byte"), getType("char"), getType("float"), getType("long"), getType("double"));
|
|
|
|
o2.checkTypeMulti(getType("int"), getType("byte"), getType("char"), getType("float"), getType("long"), getType("double"));
|
|
|
|
Object object1 = o1.object;
|
|
|
|
Object object2 = o2.object;
|
|
|
|
ISBPLObject r = null;
|
|
|
|
if (object1 instanceof Integer && object2 instanceof Integer) {
|
|
|
|
r = new ISBPLObject(getType("int"), (int) ((int) (Integer) object1 / (int) (Integer) object2));
|
|
|
|
}
|
|
|
|
if (object1 instanceof Long && object2 instanceof Long) {
|
|
|
|
r = new ISBPLObject(getType("long"), (long) ((long) (Long) object1 / (long) (Long) object2));
|
|
|
|
}
|
|
|
|
if (object1 instanceof Character && object2 instanceof Character) {
|
|
|
|
r = new ISBPLObject(getType("char"), (char) ((char) (Character) object1 / (char) (Character) object2));
|
|
|
|
}
|
|
|
|
if (object1 instanceof Byte && object2 instanceof Byte) {
|
|
|
|
r = new ISBPLObject(getType("byte"), (byte) (Byte.toUnsignedInt((Byte) object1) / Byte.toUnsignedInt((Byte) object2)));
|
|
|
|
}
|
|
|
|
if (object1 instanceof Float && object2 instanceof Float) {
|
|
|
|
r = new ISBPLObject(getType("float"), (float) ((float) (Float) object1 / (float) (Float) object2));
|
|
|
|
}
|
|
|
|
if (object1 instanceof Double && object2 instanceof Double) {
|
|
|
|
r = new ISBPLObject(getType("double"), (double) ((double) (Double) object1 / (double) (Double) object2));
|
|
|
|
}
|
|
|
|
if (r != null)
|
|
|
|
stack.push(r);
|
|
|
|
else
|
|
|
|
typeError(o1.type.name, o2.type.name);
|
|
|
|
} catch (ArithmeticException ex) {
|
|
|
|
throw new ISBPLError("Arithmetic", "Division by 0");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "*":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
|
|
|
ISBPLObject o2 = stack.pop();
|
|
|
|
ISBPLObject o1 = stack.pop();
|
|
|
|
o1.checkTypeMulti(getType("int"), getType("byte"), getType("char"), getType("float"), getType("long"), getType("double"));
|
|
|
|
o2.checkTypeMulti(getType("int"), getType("byte"), getType("char"), getType("float"), getType("long"), getType("double"));
|
|
|
|
Object object1 = o1.object;
|
|
|
|
Object object2 = o2.object;
|
|
|
|
ISBPLObject r = null;
|
|
|
|
if(object1 instanceof Integer && object2 instanceof Integer) {
|
|
|
|
r = new ISBPLObject(getType("int"), (int) ((int) (Integer) object1 * (int) (Integer) object2));
|
|
|
|
}
|
|
|
|
if(object1 instanceof Long && object2 instanceof Long) {
|
|
|
|
r = new ISBPLObject(getType("long"), (long) ((long) (Long) object1 * (long) (Long) object2));
|
|
|
|
}
|
|
|
|
if(object1 instanceof Character && object2 instanceof Character) {
|
|
|
|
r = new ISBPLObject(getType("char"), (char) ((char) (Character) object1 * (char) (Character) object2));
|
|
|
|
}
|
|
|
|
if(object1 instanceof Byte && object2 instanceof Byte) {
|
|
|
|
r = new ISBPLObject(getType("byte"), (byte) (Byte.toUnsignedInt((Byte) object1) * Byte.toUnsignedInt((Byte) object2)));
|
|
|
|
}
|
|
|
|
if(object1 instanceof Float && object2 instanceof Float) {
|
|
|
|
r = new ISBPLObject(getType("float"), (float) ((float) (Float) object1 * (float) (Float) object2));
|
|
|
|
}
|
|
|
|
if(object1 instanceof Double && object2 instanceof Double) {
|
|
|
|
r = new ISBPLObject(getType("double"), (double) ((double) (Double) object1 * (double) (Double) object2));
|
|
|
|
}
|
|
|
|
if(r != null)
|
|
|
|
stack.push(r);
|
|
|
|
else
|
|
|
|
typeError(o1.type.name, o2.type.name);
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "**":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
|
|
|
ISBPLObject o2 = stack.pop();
|
|
|
|
ISBPLObject o1 = stack.pop();
|
|
|
|
o1.checkTypeMulti(getType("int"), getType("byte"), getType("char"), getType("float"), getType("long"), getType("double"));
|
|
|
|
o2.checkTypeMulti(getType("int"), getType("byte"), getType("char"), getType("float"), getType("long"), getType("double"));
|
|
|
|
Object object1 = o1.object;
|
|
|
|
Object object2 = o2.object;
|
|
|
|
ISBPLObject r = null;
|
|
|
|
if(object1 instanceof Integer && object2 instanceof Integer) {
|
|
|
|
r = new ISBPLObject(getType("int"), (int) Math.pow((int) (Integer) object1, (int) (Integer) object2));
|
|
|
|
}
|
|
|
|
if(object1 instanceof Long && object2 instanceof Long) {
|
|
|
|
r = new ISBPLObject(getType("long"), (long) Math.pow((long) (Long) object1, (long) (Long) object2));
|
|
|
|
}
|
|
|
|
if(object1 instanceof Character && object2 instanceof Character) {
|
|
|
|
r = new ISBPLObject(getType("char"), (char) Math.pow((char) (Character) object1, (char) (Character) object2));
|
|
|
|
}
|
|
|
|
if(object1 instanceof Byte && object2 instanceof Byte) {
|
|
|
|
r = new ISBPLObject(getType("byte"), (byte) Math.pow(Byte.toUnsignedInt((Byte) object1), Byte.toUnsignedInt((Byte) object2)));
|
|
|
|
}
|
|
|
|
if(object1 instanceof Float && object2 instanceof Float) {
|
|
|
|
r = new ISBPLObject(getType("float"), (float) Math.pow((float) (Float) object1, (float) (Float) object2));
|
|
|
|
}
|
|
|
|
if(object1 instanceof Double && object2 instanceof Double) {
|
|
|
|
r = new ISBPLObject(getType("double"), (double) Math.pow((double) (Double) object1, (double) (Double) object2));
|
|
|
|
}
|
|
|
|
if(r != null)
|
|
|
|
stack.push(r);
|
|
|
|
else
|
|
|
|
typeError(o1.type.name, o2.type.name);
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "%":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
|
|
|
try {
|
|
|
|
ISBPLObject o2 = stack.pop();
|
|
|
|
ISBPLObject o1 = stack.pop();
|
|
|
|
o1.checkTypeMulti(getType("int"), getType("byte"), getType("char"), getType("float"), getType("long"), getType("double"));
|
|
|
|
o2.checkTypeMulti(getType("int"), getType("byte"), getType("char"), getType("float"), getType("long"), getType("double"));
|
|
|
|
Object object1 = o1.object;
|
|
|
|
Object object2 = o2.object;
|
|
|
|
ISBPLObject r = null;
|
|
|
|
if (object1 instanceof Integer && object2 instanceof Integer) {
|
|
|
|
r = new ISBPLObject(getType("int"), (int) ((int) (Integer) object1 % (int) (Integer) object2));
|
|
|
|
}
|
|
|
|
if (object1 instanceof Long && object2 instanceof Long) {
|
|
|
|
r = new ISBPLObject(getType("long"), (long) ((long) (Long) object1 % (long) (Long) object2));
|
|
|
|
}
|
|
|
|
if (object1 instanceof Character && object2 instanceof Character) {
|
|
|
|
r = new ISBPLObject(getType("char"), (char) ((char) (Character) object1 % (char) (Character) object2));
|
|
|
|
}
|
|
|
|
if (object1 instanceof Byte && object2 instanceof Byte) {
|
|
|
|
r = new ISBPLObject(getType("byte"), (byte) (Byte.toUnsignedInt((Byte) object1) % Byte.toUnsignedInt((Byte) object2)));
|
|
|
|
}
|
|
|
|
if (object1 instanceof Float && object2 instanceof Float) {
|
|
|
|
r = new ISBPLObject(getType("float"), (float) ((float) (Float) object1 % (float) (Float) object2));
|
|
|
|
}
|
|
|
|
if (object1 instanceof Double && object2 instanceof Double) {
|
|
|
|
r = new ISBPLObject(getType("double"), (double) ((double) (Double) object1 % (double) (Double) object2));
|
|
|
|
}
|
|
|
|
if (r != null)
|
|
|
|
stack.push(r);
|
|
|
|
else
|
|
|
|
typeError(o1.type.name, o2.type.name);
|
|
|
|
} catch (ArithmeticException ex) {
|
|
|
|
throw new ISBPLError("Arithmetic", "Division by 0");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "^":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
|
|
|
ISBPLObject o2 = stack.pop();
|
|
|
|
ISBPLObject o1 = stack.pop();
|
|
|
|
o1.checkTypeMulti(getType("int"), getType("byte"), getType("char"), getType("long"));
|
|
|
|
o2.checkTypeMulti(getType("int"), getType("byte"), getType("char"), getType("long"));
|
|
|
|
Object object1 = o1.object;
|
|
|
|
Object object2 = o2.object;
|
|
|
|
ISBPLObject r = null;
|
|
|
|
if(object1 instanceof Integer && object2 instanceof Integer) {
|
|
|
|
r = new ISBPLObject(getType("int"), (int) ((int) (Integer) object1 ^ (int) (Integer) object2));
|
|
|
|
}
|
|
|
|
if(object1 instanceof Long && object2 instanceof Long) {
|
|
|
|
r = new ISBPLObject(getType("long"), (long) ((long) (Long) object1 ^ (long) (Long) object2));
|
|
|
|
}
|
|
|
|
if(object1 instanceof Character && object2 instanceof Character) {
|
|
|
|
r = new ISBPLObject(getType("char"), (char) ((char) (Character) object1 ^ (char) (Character) object2));
|
|
|
|
}
|
|
|
|
if(object1 instanceof Byte && object2 instanceof Byte) {
|
|
|
|
r = new ISBPLObject(getType("byte"), Byte.toUnsignedInt((Byte) object1) ^ Byte.toUnsignedInt((Byte) object2));
|
|
|
|
}
|
|
|
|
if(r != null)
|
|
|
|
stack.push(r);
|
|
|
|
else
|
|
|
|
typeError(o1.type.name, o2.type.name);
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "dup":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
|
|
|
ISBPLObject o = stack.pop();
|
2022-05-04 02:16:58 +02:00
|
|
|
stack.push(o);
|
|
|
|
stack.push(o);
|
2022-04-29 18:10:14 +02:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "pop":
|
|
|
|
func = Stack::pop;
|
|
|
|
break;
|
|
|
|
case "swap":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
|
|
|
ISBPLObject o1 = stack.pop();
|
|
|
|
ISBPLObject o2 = stack.pop();
|
|
|
|
stack.push(o1);
|
|
|
|
stack.push(o2);
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "_last_word":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
|
|
|
ISBPLObject i = stack.pop();
|
|
|
|
i.checkType(getType("int"));
|
|
|
|
int n = (int) i.object;
|
|
|
|
if(n >= lastWords.get().size())
|
|
|
|
throw new ISBPLError("IllegalArgument", "_last_words called with wrong argument");
|
|
|
|
stack.push(toISBPLString(lastWords.get().get(n)));
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "time":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
|
|
|
ISBPLObject i = stack.pop();
|
|
|
|
long n = (long) i.toLong();
|
|
|
|
try {
|
|
|
|
Thread.sleep(n);
|
2022-03-06 17:39:49 +01:00
|
|
|
}
|
|
|
|
catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2022-04-29 18:10:14 +02:00
|
|
|
stack.push(new ISBPLObject(getType("long"), System.currentTimeMillis()));
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "stream":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
|
|
|
ISBPLObject action = stack.pop();
|
|
|
|
action.checkType(getType("int"));
|
|
|
|
int n = ((int) action.object);
|
|
|
|
try {
|
|
|
|
streamer.action(stack, n);
|
|
|
|
}
|
|
|
|
catch (IOException e) {
|
|
|
|
throw new ISBPLError("IO", e.getMessage());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "_enable_debug":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
|
|
|
if(debuggerIPC.threadID == -1) {
|
|
|
|
ISBPLDebugger debugger = new ISBPLDebugger(this);
|
|
|
|
debugger.start();
|
|
|
|
try {
|
|
|
|
while(debuggerIPC.threadID == -1) Thread.sleep(1);
|
|
|
|
}
|
|
|
|
catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
2022-03-07 18:02:48 +01:00
|
|
|
}
|
2022-03-24 15:24:34 +01:00
|
|
|
}
|
2022-04-29 18:10:14 +02:00
|
|
|
stack.push(new ISBPLObject(getType("int"), debuggerIPC.port));
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "_getvars":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
2022-04-29 18:30:10 +02:00
|
|
|
ISBPLObject[] objects = new ISBPLObject[frameStack.get().size()];
|
2022-04-29 18:10:14 +02:00
|
|
|
int i = 0;
|
2022-04-29 18:30:10 +02:00
|
|
|
for (ISBPLFrame map : frameStack.get()) {
|
2022-04-29 18:10:14 +02:00
|
|
|
ArrayList<ISBPLObject> strings = new ArrayList<>();
|
2022-04-29 18:30:10 +02:00
|
|
|
for (String key : map.all().keySet()) {
|
2022-04-29 18:10:14 +02:00
|
|
|
if(key.startsWith("=")) {
|
|
|
|
strings.add(toISBPLString(key.substring(1)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
objects[i++] = new ISBPLObject(getType("array"), strings.toArray(new ISBPLObject[0]));
|
|
|
|
}
|
|
|
|
ISBPLObject array = new ISBPLObject(getType("array"), objects);
|
|
|
|
stack.push(array);
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "stacksize":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> stack.push(new ISBPLObject(getType("int"), stack.size()));
|
|
|
|
break;
|
|
|
|
case "fcall":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
|
|
|
ISBPLObject o = stack.pop();
|
|
|
|
o.checkType(getType("func"));
|
|
|
|
((ISBPLCallable) o.object).call(stack);
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "subprocess":
|
|
|
|
func = (Stack<ISBPLObject> stack) -> {
|
|
|
|
try {
|
|
|
|
Runtime.getRuntime().exec(toJavaString(stack.pop()));
|
|
|
|
} catch(IOException e) {
|
|
|
|
throw new ISBPLError("IO", "Couldn't start process");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "deffunc":
|
|
|
|
func = (stack) -> {
|
|
|
|
ISBPLObject str = stack.pop();
|
|
|
|
ISBPLObject callable = stack.pop();
|
|
|
|
String s = toJavaString(str);
|
|
|
|
callable.checkType(getType("func"));
|
2022-04-30 15:27:15 +02:00
|
|
|
frameStack.get().peek().add(s, (ISBPLCallable) callable.object);
|
2022-04-12 16:43:51 +02:00
|
|
|
};
|
|
|
|
break;
|
2022-04-16 00:10:51 +02:00
|
|
|
case "defmethod":
|
|
|
|
func = (stack) -> {
|
|
|
|
ISBPLObject type = stack.pop();
|
|
|
|
ISBPLObject callable = stack.pop();
|
|
|
|
ISBPLObject str = stack.pop();
|
|
|
|
type.checkType(getType("int"));
|
|
|
|
callable.checkType(getType("func"));
|
|
|
|
ISBPLType t = types.get((int) type.object);
|
|
|
|
String s = toJavaString(str);
|
2022-04-16 00:44:52 +02:00
|
|
|
addFunction(t, s, (ISBPLCallable) callable.object);
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "deffield":
|
|
|
|
func = (stack) -> {
|
|
|
|
ISBPLObject type = stack.pop();
|
|
|
|
ISBPLObject str = stack.pop();
|
|
|
|
type.checkType(getType("int"));
|
|
|
|
ISBPLType t = types.get((int) type.object);
|
|
|
|
String s = toJavaString(str);
|
|
|
|
Object var = new Object();
|
2022-04-18 21:36:53 +02:00
|
|
|
addFunction(t, s, (stack1) -> stack1.push(t.varget(stack1.pop()).getOrDefault(var, getNullObject())));
|
2022-04-16 00:44:52 +02:00
|
|
|
addFunction(t, "=" + s, (stack1) -> t.varget(stack1.pop()).put(var, stack1.pop()));
|
2022-04-16 00:10:51 +02:00
|
|
|
};
|
|
|
|
break;
|
2022-04-17 17:40:25 +02:00
|
|
|
case "callmethod":
|
|
|
|
func = (stack) -> {
|
|
|
|
ISBPLObject obj = stack.pop();
|
|
|
|
ISBPLObject str = stack.pop();
|
|
|
|
String s = toJavaString(str);
|
|
|
|
stack.push(obj);
|
|
|
|
obj.type.methods.get(s).call(stack);
|
|
|
|
};
|
|
|
|
break;
|
2022-04-16 18:44:07 +02:00
|
|
|
case "jio.class":
|
|
|
|
func = (stack) -> {
|
|
|
|
ISBPLObject str = stack.pop();
|
|
|
|
String s = toJavaString(str);
|
|
|
|
try {
|
|
|
|
stack.push(toISBPL(Class.forName(s)));
|
|
|
|
}
|
|
|
|
catch (ClassNotFoundException e) {
|
|
|
|
throw new ISBPLError("JIO", "Class not found");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
break;
|
2022-04-16 21:05:17 +02:00
|
|
|
case "jio.getclass":
|
|
|
|
func = (stack) -> {
|
|
|
|
ISBPLObject str = stack.pop();
|
|
|
|
String s = toJavaString(str);
|
|
|
|
try {
|
|
|
|
stack.push(toISBPL((Object) Class.forName(s)));
|
|
|
|
}
|
|
|
|
catch (ClassNotFoundException e) {
|
|
|
|
throw new ISBPLError("JIO", "Class not found");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
break;
|
2022-04-18 19:50:36 +02:00
|
|
|
case "jio.context":
|
2022-04-25 00:34:59 +02:00
|
|
|
func = (stack) -> stack.push(toISBPL(this));
|
2022-04-18 19:50:36 +02:00
|
|
|
break;
|
2022-04-17 16:10:49 +02:00
|
|
|
case "null":
|
2022-04-25 00:34:59 +02:00
|
|
|
func = (stack) -> stack.push(new ISBPLObject(getType("null"), 0));
|
2022-04-17 16:10:49 +02:00
|
|
|
break;
|
2022-04-18 21:36:53 +02:00
|
|
|
case "delete":
|
|
|
|
func = (stack) -> {
|
|
|
|
ISBPLObject o = stack.pop();
|
|
|
|
o.type.vars.remove(o);
|
|
|
|
};
|
|
|
|
break;
|
2022-05-04 02:16:58 +02:00
|
|
|
case "mkinstance":
|
|
|
|
func = (stack) -> {
|
|
|
|
ISBPLObject type = stack.pop();
|
|
|
|
stack.push(new ISBPLObject(types.get((int) type.toLong()), new Object()));
|
|
|
|
};
|
|
|
|
break;
|
2022-03-14 17:17:58 +01:00
|
|
|
default:
|
|
|
|
func = natives.get(name);
|
|
|
|
break;
|
2022-03-05 20:55:03 +01:00
|
|
|
}
|
2022-04-30 15:27:15 +02:00
|
|
|
frameStack.get().peek().add(name, func);
|
2022-03-16 00:45:22 +01:00
|
|
|
}
|
2022-04-25 00:34:59 +02:00
|
|
|
|
2022-04-18 21:36:53 +02:00
|
|
|
ISBPLObject nullObj = null;
|
|
|
|
public ISBPLObject getNullObject() {
|
2022-05-01 18:35:23 +02:00
|
|
|
if(nullObj == null || nullObj.type == null)
|
2022-04-18 21:36:53 +02:00
|
|
|
nullObj = new ISBPLObject(getType("null"), 0);
|
|
|
|
return nullObj;
|
|
|
|
}
|
2022-04-16 18:44:07 +02:00
|
|
|
|
2022-04-29 18:10:14 +02:00
|
|
|
private final Object syncJIOClass = new Object();
|
2022-04-16 18:44:07 +02:00
|
|
|
public ISBPLObject toISBPL(Class<?> clazz) {
|
2022-04-27 01:48:46 +02:00
|
|
|
synchronized(syncJIOClass) {
|
|
|
|
ISBPLType type = getType(clazz.getName());
|
|
|
|
if(type == null) {
|
|
|
|
type = registerType(clazz.getName());
|
|
|
|
Class<?> superClass = clazz.getSuperclass();
|
|
|
|
if (superClass != null)
|
|
|
|
type.superTypes.add(toISBPL(superClass).type);
|
|
|
|
Class<?>[] interfaces = clazz.getInterfaces();
|
|
|
|
for(Class<?> c : interfaces) {
|
|
|
|
type.superTypes.add(toISBPL(c).type);
|
|
|
|
}
|
|
|
|
if (clazz.isEnum()) {
|
|
|
|
for (Object o : clazz.getEnumConstants()) {
|
|
|
|
addFunction(type, o.toString(), stack -> {
|
|
|
|
if(debug)
|
|
|
|
System.err.println("Java GetEnum: " + o);
|
|
|
|
stack.push(toISBPL(o));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (Field field : clazz.getDeclaredFields()) {
|
|
|
|
addFunction(type, field.getName(), stack -> {
|
2022-04-30 09:45:34 +02:00
|
|
|
forceAccessible(field);
|
2022-04-27 01:48:46 +02:00
|
|
|
if(debug)
|
|
|
|
System.err.println("Java Get: " + field);
|
|
|
|
try {
|
|
|
|
stack.push(toISBPL(field.get(stack.pop().object)));
|
|
|
|
}
|
|
|
|
catch (IllegalAccessException ignored) {
|
|
|
|
}
|
|
|
|
});
|
|
|
|
addFunction(type, "=" + field.getName(), stack -> {
|
2022-04-30 09:45:34 +02:00
|
|
|
forceAccessible(field);
|
2022-04-16 21:31:42 +02:00
|
|
|
if(debug)
|
2022-04-27 01:48:46 +02:00
|
|
|
System.err.println("Java Set: " + field);
|
|
|
|
try {
|
|
|
|
field.set(stack.pop().object, fromISBPL(stack.pop(), field.getType()));
|
|
|
|
}
|
|
|
|
catch (IllegalAccessException ignored) {
|
|
|
|
}
|
2022-04-16 21:31:42 +02:00
|
|
|
});
|
|
|
|
}
|
2022-04-27 01:48:46 +02:00
|
|
|
HashMap<String, ArrayList<Method>> methods = new HashMap<>();
|
|
|
|
for (Method method : clazz.getDeclaredMethods()) {
|
|
|
|
ArrayList<Method> methodList = methods.get(method.getName() + method.getParameterCount());
|
|
|
|
if(methodList == null)
|
|
|
|
methodList = new ArrayList<>();
|
|
|
|
methodList.add(method);
|
|
|
|
methods.put(method.getName() + method.getParameterCount(), methodList);
|
|
|
|
}
|
|
|
|
for (Map.Entry<String, ArrayList<Method>> entry : methods.entrySet()) {
|
|
|
|
addFunction(type, entry.getKey(), stack -> {
|
|
|
|
Object o = stack.pop().object;
|
|
|
|
// Resolve
|
|
|
|
AtomicInteger mid = new AtomicInteger(0);
|
|
|
|
ArrayList<Method> ms = entry.getValue();
|
|
|
|
Class<?>[][] paramTypes = new Class<?>[ms.size()][ms.get(0).getParameterCount()];
|
|
|
|
for (int i = 0 ; i < ms.size() ; i++) {
|
|
|
|
paramTypes[i] = ms.get(i).getParameterTypes();
|
|
|
|
}
|
|
|
|
Object[] params = resolve(mid, stack, entry.getValue().get(0).getParameterCount(), paramTypes);
|
|
|
|
Method method = ms.get(mid.get());
|
|
|
|
|
2022-04-30 09:45:34 +02:00
|
|
|
forceAccessible(method);
|
2022-04-27 01:48:46 +02:00
|
|
|
if(debug)
|
|
|
|
System.err.println("Java Call: " + method + " - " + Arrays.toString(params));
|
|
|
|
try {
|
|
|
|
Object r = method.invoke(o, params);
|
|
|
|
if(method.getReturnType() != void.class)
|
|
|
|
stack.push(toISBPL(r));
|
|
|
|
}
|
|
|
|
catch (IllegalAccessException ignored) { }
|
|
|
|
catch (InvocationTargetException e) {
|
|
|
|
stack.push(toISBPL(e));
|
|
|
|
throw new ISBPLError("Java", "Java error");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
HashMap<Integer, ArrayList<Constructor<?>>> constructors = new HashMap<>();
|
|
|
|
for (Constructor<?> constructor : clazz.getDeclaredConstructors()) {
|
|
|
|
ArrayList<Constructor<?>> constructorList = constructors.get(constructor.getParameterCount());
|
|
|
|
if (constructorList == null)
|
|
|
|
constructorList = new ArrayList<>();
|
|
|
|
constructorList.add(constructor);
|
|
|
|
constructors.put(constructor.getParameterCount(), constructorList);
|
|
|
|
}
|
|
|
|
for (Map.Entry<Integer, ArrayList<Constructor<?>>> entry : constructors.entrySet()) {
|
|
|
|
addFunction(type, "new" + entry.getKey(), stack -> {
|
|
|
|
stack.pop();
|
2022-04-29 18:10:14 +02:00
|
|
|
|
2022-04-27 01:48:46 +02:00
|
|
|
AtomicInteger mid = new AtomicInteger(0);
|
|
|
|
ArrayList<Constructor<?>> ms = entry.getValue();
|
|
|
|
Class<?>[][] paramTypes = new Class<?>[ms.size()][ms.get(0).getParameterCount()];
|
|
|
|
for (int i = 0 ; i < ms.size() ; i++) {
|
|
|
|
paramTypes[i] = ms.get(i).getParameterTypes();
|
|
|
|
}
|
|
|
|
Object[] params = resolve(mid, stack, entry.getValue().get(0).getParameterCount(), paramTypes);
|
|
|
|
Constructor<?> constructor = ms.get(mid.get());
|
|
|
|
|
2022-04-30 09:45:34 +02:00
|
|
|
forceAccessible(constructor);
|
2022-04-27 01:48:46 +02:00
|
|
|
if(debug)
|
2022-04-30 15:27:15 +02:00
|
|
|
System.err.println("Java Call: " + constructor + " - " + Arrays.toString(params));
|
2022-04-27 01:48:46 +02:00
|
|
|
try {
|
|
|
|
Object r = constructor.newInstance(params);
|
2022-04-16 18:44:07 +02:00
|
|
|
stack.push(toISBPL(r));
|
2022-04-27 01:48:46 +02:00
|
|
|
}
|
|
|
|
catch (IllegalAccessException ignored) { }
|
|
|
|
catch (InvocationTargetException | InstantiationException e) {
|
|
|
|
stack.push(toISBPL(e));
|
|
|
|
throw new ISBPLError("Java", "Java error");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-04-17 16:10:49 +02:00
|
|
|
}
|
2022-04-27 01:48:46 +02:00
|
|
|
return new ISBPLObject(type, null);
|
2022-04-16 18:44:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-30 12:15:00 +02:00
|
|
|
// TODO: Resolve with subclass weight
|
2022-04-25 00:34:59 +02:00
|
|
|
public Object[] resolve(AtomicInteger mid, Stack<ISBPLObject> stack, int paramCount, Class<?>[][] paramTypes) {
|
|
|
|
ISBPLObject[] o = new ISBPLObject[paramCount];
|
|
|
|
Object[][] params = new Object[paramTypes.length][paramCount];
|
|
|
|
int[] methodIDs = new int[paramCount];
|
2022-04-30 15:27:15 +02:00
|
|
|
Arrays.fill(methodIDs, 0);
|
|
|
|
if(debug)
|
|
|
|
System.err.println("Consideration of " + paramTypes.length + " methods...");
|
|
|
|
for (int i = paramCount - 1 ; i >= 0 ; i--) {
|
2022-04-25 00:34:59 +02:00
|
|
|
o[i] = stack.pop();
|
|
|
|
for (int j = 0 ; j < paramTypes.length ; j++) {
|
|
|
|
Class<?>[] m = paramTypes[j];
|
|
|
|
try {
|
|
|
|
params[j][i] = fromISBPL(o[i], m[i]);
|
2022-04-30 15:27:15 +02:00
|
|
|
methodIDs[i] += 1 << j;
|
|
|
|
if(debug)
|
|
|
|
System.err.println("Consideration: Pass " + i + " " + Arrays.toString(paramTypes[j]));
|
|
|
|
}
|
|
|
|
catch (ISBPLError ignored) {
|
|
|
|
if(debug)
|
|
|
|
System.err.println("Consideration: Fail " + i + " " + Arrays.toString(paramTypes[j]));
|
2022-04-25 00:34:59 +02:00
|
|
|
}
|
|
|
|
}
|
2022-04-30 15:27:15 +02:00
|
|
|
if(debug)
|
|
|
|
System.err.println("Considerartion: " + i + " " + Integer.toBinaryString(methodIDs[i] + (1 << 31)).substring(32 - paramTypes.length));
|
2022-04-25 00:34:59 +02:00
|
|
|
}
|
|
|
|
int msize = paramTypes.length;
|
|
|
|
for (int i = 0 ; i < msize; i++) {
|
|
|
|
boolean works = true;
|
|
|
|
for (int j = 0 ; j < methodIDs.length ; j++) {
|
2022-04-30 15:27:15 +02:00
|
|
|
if ((methodIDs[j] & (1 << i)) == 0) {
|
2022-04-25 00:34:59 +02:00
|
|
|
works = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-04-30 15:27:15 +02:00
|
|
|
if(works) {
|
2022-04-25 00:34:59 +02:00
|
|
|
mid.set(i);
|
2022-04-30 15:27:15 +02:00
|
|
|
if(debug)
|
|
|
|
System.err.println("Considering: " + Arrays.toString(paramTypes[i]));
|
|
|
|
}
|
2022-04-25 00:34:59 +02:00
|
|
|
}
|
2022-04-30 15:27:15 +02:00
|
|
|
if(debug)
|
|
|
|
System.err.println("Considered " + paramTypes.length + " methods. Result: " + Arrays.toString(paramTypes[mid.get()]));
|
2022-04-25 00:34:59 +02:00
|
|
|
return params[mid.get()];
|
|
|
|
}
|
|
|
|
|
2022-04-25 12:29:52 +02:00
|
|
|
public Object primitiveDefault(Class<?> expectedType) {
|
|
|
|
if(expectedType == long.class)
|
|
|
|
return 0L;
|
|
|
|
if(expectedType == int.class)
|
|
|
|
return 0;
|
|
|
|
if(expectedType == double.class)
|
|
|
|
return 0.0d;
|
|
|
|
if(expectedType == float.class)
|
|
|
|
return 0.0f;
|
|
|
|
if(expectedType == short.class)
|
|
|
|
return (short) 0;
|
|
|
|
if(expectedType == boolean.class)
|
|
|
|
return false;
|
|
|
|
if(expectedType == char.class)
|
|
|
|
return (char) 0;
|
|
|
|
if(expectedType == byte.class)
|
|
|
|
return (byte) 0;
|
|
|
|
return null;
|
|
|
|
}
|
2022-04-25 00:34:59 +02:00
|
|
|
|
2022-04-25 12:29:52 +02:00
|
|
|
public Object fromISBPL(ISBPLObject o, Class<?> expectedType) {
|
2022-04-29 18:10:14 +02:00
|
|
|
ISBPLType type = o.type;
|
|
|
|
if (type.equals(getType("null"))) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (type.equals(getType("string"))) {
|
|
|
|
if(expectedType.isAssignableFrom(String.class))
|
|
|
|
return toJavaString(o);
|
|
|
|
else
|
|
|
|
typeError("string", expectedType.getName());
|
|
|
|
}
|
|
|
|
if (type.equals(getType("array"))) {
|
|
|
|
ISBPLObject[] isbplArray = ((ISBPLObject[]) o.object);
|
|
|
|
Object array = new Object[isbplArray.length];
|
|
|
|
if(expectedType.isArray())
|
|
|
|
array = Array.newInstance(expectedType.getComponentType(), isbplArray.length);
|
|
|
|
else
|
|
|
|
typeError("array", "matching-array");
|
|
|
|
for (int i = 0 ; i < isbplArray.length ; i++) {
|
|
|
|
Object obj = fromISBPL(isbplArray[i], expectedType.getComponentType());
|
|
|
|
Array.set(array, i, obj == null && expectedType.getComponentType().isPrimitive() ? primitiveDefault(expectedType.getComponentType()) : obj);
|
2022-04-27 01:48:46 +02:00
|
|
|
}
|
2022-04-29 18:10:14 +02:00
|
|
|
return array;
|
|
|
|
}
|
|
|
|
if(expectedType.isPrimitive()) {
|
|
|
|
if(expectedType == long.class)
|
|
|
|
return o.toLong();
|
|
|
|
if(expectedType == int.class)
|
|
|
|
return (int) o.toLong();
|
|
|
|
if(expectedType == double.class)
|
|
|
|
return o.toDouble();
|
|
|
|
if(expectedType == float.class)
|
|
|
|
return (float) o.toDouble();
|
|
|
|
if(expectedType == short.class)
|
|
|
|
return (short) o.toLong();
|
|
|
|
if(expectedType == boolean.class)
|
|
|
|
return o.isTruthy();
|
|
|
|
if(expectedType == char.class)
|
|
|
|
return (char) o.toLong();
|
|
|
|
if(expectedType == byte.class)
|
|
|
|
return (byte) o.toLong();
|
2022-04-25 04:42:30 +02:00
|
|
|
}
|
2022-04-29 18:10:14 +02:00
|
|
|
if(!expectedType.isAssignableFrom(o.object.getClass()))
|
|
|
|
typeError(o.type.name, expectedType.getName());
|
|
|
|
return o.object;
|
2022-04-16 18:44:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public ISBPLObject toISBPL(Object object) {
|
2022-04-29 18:10:14 +02:00
|
|
|
if(object == null) {
|
|
|
|
return getNullObject();
|
|
|
|
}
|
|
|
|
ISBPLObject o = toISBPL(object.getClass());
|
|
|
|
if (object instanceof String) {
|
|
|
|
return toISBPLString(((String) object));
|
|
|
|
}
|
|
|
|
if (object instanceof Integer)
|
|
|
|
return new ISBPLObject(getType("int"), object);
|
|
|
|
if (object instanceof Character)
|
|
|
|
return new ISBPLObject(getType("char"), object);
|
|
|
|
if (object instanceof Byte)
|
|
|
|
return new ISBPLObject(getType("byte"), object);
|
|
|
|
if (object instanceof Float)
|
|
|
|
return new ISBPLObject(getType("float"), object);
|
|
|
|
if (object instanceof Long)
|
|
|
|
return new ISBPLObject(getType("long"), object);
|
|
|
|
if (object instanceof Double)
|
|
|
|
return new ISBPLObject(getType("double"), object);
|
|
|
|
if (object.getClass().isArray()) {
|
|
|
|
ISBPLObject[] isbplArray = new ISBPLObject[Array.getLength(object)];
|
|
|
|
for (int i = 0 ; i < isbplArray.length ; i++) {
|
|
|
|
isbplArray[i] = toISBPL(Array.get(object, i));
|
2022-04-16 18:44:07 +02:00
|
|
|
}
|
2022-04-29 18:10:14 +02:00
|
|
|
o.type = getType("array");
|
|
|
|
object = isbplArray;
|
2022-04-16 18:44:07 +02:00
|
|
|
}
|
2022-04-29 18:10:14 +02:00
|
|
|
o.object = object;
|
|
|
|
return o;
|
2022-04-16 18:44:07 +02:00
|
|
|
}
|
|
|
|
|
2022-04-16 00:44:52 +02:00
|
|
|
public void addFunction(ISBPLType type, String name, ISBPLCallable callable) {
|
|
|
|
type.methods.put(name, callable);
|
|
|
|
type.methods.put("&" + name, stack -> stack.push(new ISBPLObject(getType("func"), callable)));
|
|
|
|
}
|
2022-03-16 00:45:22 +01:00
|
|
|
|
|
|
|
private String getFilePathForInclude(Stack<ISBPLObject> stack, Stack<File> file) {
|
2022-03-06 18:46:04 +01:00
|
|
|
ISBPLObject s = stack.pop();
|
|
|
|
String filepath = toJavaString(s);
|
2022-03-16 00:45:22 +01:00
|
|
|
for (File f : file) {
|
2022-03-16 02:07:53 +01:00
|
|
|
filepath = toJavaString(s);
|
2022-03-16 00:45:22 +01:00
|
|
|
processPath:
|
|
|
|
{
|
|
|
|
if (filepath.startsWith("/"))
|
|
|
|
break processPath;
|
|
|
|
if (filepath.startsWith("#")) {
|
|
|
|
filepath = System.getenv().getOrDefault("ISBPL_PATH", "/usr/lib/isbpl") + "/" + filepath.substring(1);
|
|
|
|
break processPath;
|
|
|
|
}
|
|
|
|
filepath = f.getParentFile().getAbsolutePath() + "/" + filepath;
|
2022-03-06 18:46:04 +01:00
|
|
|
}
|
2022-03-16 00:45:22 +01:00
|
|
|
if(new File(filepath).exists())
|
|
|
|
return filepath;
|
2022-03-06 18:46:04 +01:00
|
|
|
}
|
|
|
|
return filepath;
|
|
|
|
}
|
|
|
|
|
2022-03-16 00:45:22 +01:00
|
|
|
private int createFunction(int i, String[] words, File file) {
|
2022-03-05 20:55:03 +01:00
|
|
|
i++;
|
|
|
|
String name = words[i];
|
|
|
|
AtomicInteger integer = new AtomicInteger(++i);
|
2022-05-13 12:18:22 +02:00
|
|
|
ISBPLCallable callable = readCallable(integer, words, file);
|
2022-03-05 20:55:03 +01:00
|
|
|
i = integer.get();
|
2022-04-30 15:27:15 +02:00
|
|
|
frameStack.get().peek().add(name, callable);
|
2022-03-05 20:55:03 +01:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2022-05-13 12:18:22 +02:00
|
|
|
private String[] readBlock(AtomicInteger idx, String[] words, File file) {
|
2022-03-05 20:55:03 +01:00
|
|
|
ArrayList<String> newWords = new ArrayList<>();
|
|
|
|
int i = idx.get();
|
|
|
|
i++;
|
|
|
|
int lvl = 1;
|
|
|
|
for (; i < words.length && lvl > 0 ; i++) {
|
|
|
|
String word = words[i];
|
|
|
|
if(word.equals("{"))
|
|
|
|
lvl++;
|
|
|
|
if(word.equals("}")) {
|
|
|
|
if(--lvl == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
newWords.add(word);
|
|
|
|
}
|
|
|
|
idx.set(i);
|
2022-05-13 12:18:22 +02:00
|
|
|
return newWords.toArray(new String[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
private ISBPLCallable readCallable(AtomicInteger idx, String[] words, File file) {
|
|
|
|
String[] theWords = readBlock(idx, words, file);
|
2022-04-29 18:30:10 +02:00
|
|
|
ISBPLFrame frame = frameStack.get().peek();
|
2022-03-16 00:45:22 +01:00
|
|
|
return (stack) -> {
|
|
|
|
fileStack.get().push(file);
|
2022-04-30 15:27:15 +02:00
|
|
|
frameStack.get().push(new ISBPLFrame(this, frame));
|
2022-04-29 18:30:10 +02:00
|
|
|
try {
|
|
|
|
interpretRaw(theWords, stack);
|
|
|
|
} finally {
|
2022-04-30 15:27:15 +02:00
|
|
|
frameStack.get().pop();
|
2022-04-29 18:30:10 +02:00
|
|
|
fileStack.get().pop();
|
|
|
|
}
|
2022-03-16 00:45:22 +01:00
|
|
|
};
|
2022-03-05 20:55:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public String toJavaString(ISBPLObject string) {
|
|
|
|
string.checkType(getType("string"));
|
|
|
|
ISBPLObject[] array = ((ISBPLObject[]) string.object);
|
|
|
|
char[] chars = new char[array.length];
|
|
|
|
for (int i = 0 ; i < array.length ; i++) {
|
|
|
|
chars[i] = ((char) array[i].object);
|
|
|
|
}
|
|
|
|
return new String(chars);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ISBPLObject toISBPLString(String s) {
|
|
|
|
char[] chars = s.toCharArray();
|
|
|
|
ISBPLObject[] objects = new ISBPLObject[chars.length];
|
|
|
|
ISBPLType type = getType("char");
|
|
|
|
for (int i = 0 ; i < chars.length ; i++) {
|
|
|
|
objects[i] = new ISBPLObject(type, chars[i]);
|
|
|
|
}
|
|
|
|
return new ISBPLObject(getType("string"), objects);
|
|
|
|
}
|
|
|
|
|
2022-04-27 01:48:46 +02:00
|
|
|
public synchronized ISBPLType registerType(String name) {
|
2022-04-16 22:13:08 +02:00
|
|
|
ISBPLType type = new ISBPLType(name, types.size());
|
2022-03-05 20:55:03 +01:00
|
|
|
types.add(type);
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
// These will die as soon as std creates the real types and any types created before these are replaced become invalid.
|
2022-04-16 22:13:08 +02:00
|
|
|
static final ISBPLType defaultTypeInt = new ISBPLType("int", -2);
|
|
|
|
static final ISBPLType defaultTypeString = new ISBPLType("string", -1);
|
2022-03-05 20:55:03 +01:00
|
|
|
|
|
|
|
public ISBPLType getType(String name) {
|
|
|
|
for (int i = 0 ; i < types.size() ; i++) {
|
|
|
|
if(types.get(i).name.equals(name))
|
|
|
|
return types.get(i);
|
|
|
|
}
|
|
|
|
if(name.equals("int"))
|
|
|
|
return defaultTypeInt;
|
|
|
|
if(name.equals("string"))
|
|
|
|
return defaultTypeString;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
public void typeError(String got, String wanted) {
|
|
|
|
throw new ISBPLError("IncompatibleTypes", "Incompatible types: " + got + " - " + wanted);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void interpret(File file, String code, Stack<ISBPLObject> stack) {
|
2022-04-29 20:03:58 +02:00
|
|
|
if(code.startsWith("#!"))
|
|
|
|
code = code.substring(code.indexOf("\n") + 1);
|
2022-03-16 00:45:22 +01:00
|
|
|
fileStack.get().push(file);
|
2022-03-05 20:55:03 +01:00
|
|
|
code = cleanCode(code);
|
|
|
|
String[] words = splitWords(code);
|
2022-04-29 18:30:10 +02:00
|
|
|
interpretRaw(words, stack);
|
2022-03-16 00:45:22 +01:00
|
|
|
fileStack.get().pop();
|
2022-03-05 20:55:03 +01:00
|
|
|
}
|
|
|
|
|
2022-04-29 18:30:10 +02:00
|
|
|
private void interpretRaw(String[] words, Stack<ISBPLObject> stack) {
|
2022-03-05 20:55:03 +01:00
|
|
|
try {
|
2022-04-17 12:17:14 +02:00
|
|
|
nextWord: for (int i = 0 ; i < words.length ; i++) {
|
2022-03-05 20:55:03 +01:00
|
|
|
String word = words[i];
|
|
|
|
if (word.length() == 0)
|
|
|
|
continue;
|
2022-04-16 18:44:07 +02:00
|
|
|
if(printCalls) {
|
|
|
|
StringBuilder s = new StringBuilder();
|
2022-04-29 18:30:10 +02:00
|
|
|
for (int x = 0 ; x < frameStack.get().size() ; x++) {
|
2022-04-16 18:44:07 +02:00
|
|
|
s.append("\t");
|
2022-03-06 14:26:18 +01:00
|
|
|
}
|
2022-04-16 18:44:07 +02:00
|
|
|
System.err.println(s + word + "\t\t" + (debug ? stack : ""));
|
2022-03-06 14:26:18 +01:00
|
|
|
}
|
2022-04-16 21:31:42 +02:00
|
|
|
while (debuggerIPC.run.getOrDefault(Thread.currentThread().getId(), -1) == 0) Thread.sleep(1);
|
|
|
|
int rid = debuggerIPC.run.getOrDefault(Thread.currentThread().getId(), -1);
|
|
|
|
if(rid < 0) {
|
|
|
|
if(rid < -1) {
|
|
|
|
if (rid == -2) {
|
2022-03-06 14:26:18 +01:00
|
|
|
if (word.equals(debuggerIPC.until)) {
|
2022-03-12 19:49:47 +01:00
|
|
|
debuggerIPC.run.put(Thread.currentThread().getId(), 0);
|
2022-05-04 02:16:58 +02:00
|
|
|
while (debuggerIPC.run.get(Thread.currentThread().getId()) == 0) Thread.sleep(1);
|
2022-03-06 14:26:18 +01:00
|
|
|
}
|
|
|
|
}
|
2022-04-16 21:31:42 +02:00
|
|
|
if (rid == -3 && Thread.currentThread().getId() != debuggerIPC.threadID) {
|
2022-03-12 19:49:47 +01:00
|
|
|
while (debuggerIPC.run.get(Thread.currentThread().getId()) == -3) Thread.sleep(1);
|
2022-03-06 14:26:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
2022-03-12 19:49:47 +01:00
|
|
|
debuggerIPC.run.put(Thread.currentThread().getId(), debuggerIPC.run.get(Thread.currentThread().getId()) - 1);
|
|
|
|
lastWords.get().add(0, word);
|
|
|
|
while(lastWords.get().size() > 16)
|
|
|
|
lastWords.get().remove(lastWords.get().size() - 1);
|
2022-03-05 20:55:03 +01:00
|
|
|
ISBPLKeyword keyword = getKeyword(word);
|
|
|
|
if (keyword != null) {
|
2022-03-16 00:45:22 +01:00
|
|
|
i = keyword.call(i, words, fileStack.get().peek(), stack);
|
2022-03-05 20:55:03 +01:00
|
|
|
continue;
|
|
|
|
}
|
2022-04-16 00:10:51 +02:00
|
|
|
if(stack.size() > 0) {
|
2022-04-17 12:17:14 +02:00
|
|
|
// Doing this nonrecursively because it's much better despite the slight readability disadvantage.
|
2022-04-29 18:10:14 +02:00
|
|
|
if(stack.peek() == null) {
|
|
|
|
// We need to dump things immediately.
|
|
|
|
System.err.println("!!! ISBPL WORD PARSER ERROR !!!");
|
|
|
|
System.err.println("This is most likely due to a garbage collector malfunction.");
|
|
|
|
System.err.println("Stack: " + stack);
|
|
|
|
System.err.println("LastWords: " + lastWords);
|
|
|
|
System.err.println("FileStack: " + fileStack);
|
|
|
|
}
|
2022-04-16 00:10:51 +02:00
|
|
|
ISBPLType type = stack.peek().type;
|
2022-04-17 12:17:14 +02:00
|
|
|
Queue<ISBPLType> types = new LinkedList<>();
|
|
|
|
types.add(type);
|
|
|
|
while (!types.isEmpty()) {
|
|
|
|
type = types.poll();
|
|
|
|
types.addAll(type.superTypes);
|
|
|
|
if(type.methods.containsKey(word)) {
|
|
|
|
type.methods.get(word).call(stack);
|
|
|
|
continue nextWord;
|
|
|
|
}
|
2022-04-16 00:10:51 +02:00
|
|
|
}
|
|
|
|
}
|
2022-04-29 18:30:10 +02:00
|
|
|
ISBPLCallable func = frameStack.get().peek().resolve(word);
|
2022-03-05 20:55:03 +01:00
|
|
|
if(func != null) {
|
2022-03-16 00:45:22 +01:00
|
|
|
func.call(stack);
|
2022-03-05 20:55:03 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (word.startsWith("\"")) {
|
|
|
|
stack.push(toISBPLString(word.substring(1)));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
stack.push(new ISBPLObject(getType("int"), Integer.parseInt(word)));
|
|
|
|
continue;
|
|
|
|
} catch (Exception ignore) {}
|
|
|
|
try {
|
|
|
|
stack.push(new ISBPLObject(getType("long"), Long.parseLong(word)));
|
|
|
|
continue;
|
|
|
|
} catch (Exception ignore) {}
|
|
|
|
try {
|
|
|
|
stack.push(new ISBPLObject(getType("float"), Float.parseFloat(word)));
|
|
|
|
continue;
|
|
|
|
} catch (Exception ignore) {}
|
|
|
|
try {
|
|
|
|
stack.push(new ISBPLObject(getType("double"), Double.parseDouble(word)));
|
|
|
|
continue;
|
|
|
|
} catch (Exception ignore) {}
|
|
|
|
throw new ISBPLError("InvalidWord", word + " is not a function, object, or keyword.");
|
|
|
|
}
|
|
|
|
} catch (ISBPLStop stop) {
|
|
|
|
if(stop.amount == 0)
|
|
|
|
return;
|
|
|
|
throw new ISBPLStop(stop.amount);
|
2022-03-06 14:26:18 +01:00
|
|
|
}
|
|
|
|
catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2022-03-05 20:55:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Magic, please test before pushing changes!
|
|
|
|
private String[] splitWords(String code) {
|
|
|
|
ArrayList<String> words = new ArrayList<>();
|
|
|
|
char[] chars = code.toCharArray();
|
|
|
|
boolean isInString = false;
|
|
|
|
boolean escaping = false;
|
2022-04-16 18:44:07 +02:00
|
|
|
StringBuilder word = new StringBuilder();
|
2022-03-05 20:55:03 +01:00
|
|
|
for (int i = 0 ; i < chars.length ; i++) {
|
|
|
|
char c = chars[i];
|
|
|
|
if(isInString) {
|
|
|
|
if(c == '\\') {
|
|
|
|
escaping = !escaping;
|
|
|
|
if(escaping)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(c == 'n' && escaping) {
|
2022-04-16 18:44:07 +02:00
|
|
|
word.append('\n');
|
2022-03-05 20:55:03 +01:00
|
|
|
escaping = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(c == 'r' && escaping) {
|
|
|
|
escaping = false;
|
2022-04-16 18:44:07 +02:00
|
|
|
word.append('\r');
|
2022-03-05 20:55:03 +01:00
|
|
|
continue;
|
|
|
|
}
|
2022-05-04 02:16:58 +02:00
|
|
|
if(c == 't' && escaping) {
|
|
|
|
escaping = false;
|
|
|
|
word.append('\t');
|
|
|
|
continue;
|
|
|
|
}
|
2022-03-05 20:55:03 +01:00
|
|
|
if(c == '"') {
|
|
|
|
if (escaping) {
|
|
|
|
escaping = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
isInString = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2022-04-16 18:44:07 +02:00
|
|
|
word.append(c);
|
2022-03-05 20:55:03 +01:00
|
|
|
if(escaping)
|
|
|
|
throw new RuntimeException("Error parsing code: Invalid Escape.");
|
|
|
|
}
|
2022-05-13 14:00:46 +02:00
|
|
|
else if(c == '"') {
|
2022-04-16 18:44:07 +02:00
|
|
|
word.append('"');
|
2022-03-05 20:55:03 +01:00
|
|
|
isInString = true;
|
|
|
|
}
|
|
|
|
else if(c == ' ') {
|
2022-04-16 18:44:07 +02:00
|
|
|
words.add(word.toString());
|
|
|
|
word = new StringBuilder();
|
2022-03-05 20:55:03 +01:00
|
|
|
}
|
|
|
|
else {
|
2022-04-16 18:44:07 +02:00
|
|
|
word.append(c);
|
2022-03-05 20:55:03 +01:00
|
|
|
}
|
|
|
|
}
|
2022-04-16 18:44:07 +02:00
|
|
|
words.add(word.toString());
|
2022-03-05 20:55:03 +01:00
|
|
|
return words.toArray(new String[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
private String cleanCode(String code) {
|
|
|
|
return code
|
|
|
|
.replaceAll("\r", "\n")
|
|
|
|
.replaceAll("\n", " ")
|
|
|
|
;
|
|
|
|
}
|
2022-05-02 14:30:08 +02:00
|
|
|
|
|
|
|
public void dump(Stack<ISBPLObject> stack) {
|
|
|
|
try {
|
|
|
|
System.err.println("VAR DUMP\n----------------");
|
|
|
|
for (ISBPLFrame map : frameStack.get()) {
|
|
|
|
HashMap<String, ISBPLCallable> all = map.all();
|
|
|
|
for (String key : all.keySet()) {
|
|
|
|
if (key.startsWith("=")) {
|
|
|
|
all.get(key.substring(1)).call(stack);
|
|
|
|
System.err.println("\t" + key.substring(1) + ": \t" + stack.pop());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
System.err.println("----------------");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
System.err.println("!!! VARS CORRUPTED! CANNOT FIX AUTOMATICALLY.");
|
|
|
|
}
|
|
|
|
boolean fixed = false;
|
|
|
|
while (!fixed) {
|
|
|
|
try {
|
|
|
|
System.err.println("STACK DUMP");
|
|
|
|
for (ISBPLObject object : stack) {
|
|
|
|
System.err.println("\t" + object);
|
|
|
|
}
|
|
|
|
fixed = true;
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
System.err.println("!!! STACK CORRUPTED!");
|
|
|
|
stack.pop();
|
|
|
|
System.err.println("Popped. Trying again.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-05 20:55:03 +01:00
|
|
|
|
2022-03-12 19:59:39 +01:00
|
|
|
public static void main(String[] args) {
|
2022-04-29 18:10:14 +02:00
|
|
|
Stack<ISBPLObject> stack = new ISBPLStack<>();
|
2022-03-05 20:55:03 +01:00
|
|
|
ISBPL isbpl = new ISBPL();
|
2022-03-12 19:49:47 +01:00
|
|
|
isbpl.debuggerIPC.stack.put(Thread.currentThread().getId(), stack);
|
2022-03-06 14:26:18 +01:00
|
|
|
debug = !System.getenv().getOrDefault("DEBUG", "").equals("");
|
2022-03-12 19:49:47 +01:00
|
|
|
isbpl.debuggerIPC.run.put(Thread.currentThread().getId(), -1);
|
2022-03-06 14:26:18 +01:00
|
|
|
if(debug) {
|
|
|
|
new ISBPLDebugger(isbpl).start();
|
2022-03-12 19:49:47 +01:00
|
|
|
isbpl.debuggerIPC.run.put(Thread.currentThread().getId(), 0);
|
2022-03-06 14:26:18 +01:00
|
|
|
}
|
2022-03-05 20:55:03 +01:00
|
|
|
try {
|
|
|
|
File std = new File(System.getenv().getOrDefault("ISBPL_PATH", "/usr/lib/isbpl") + "/std.isbpl");
|
|
|
|
isbpl.interpret(std, readFile(std), stack);
|
2022-05-02 14:30:08 +02:00
|
|
|
if(args.length > 0) {
|
|
|
|
File file = new File(args[0]).getAbsoluteFile();
|
|
|
|
isbpl.interpret(file, readFile(file), stack);
|
|
|
|
stack.push(argarray(isbpl, args));
|
|
|
|
isbpl.interpret(file, "main exit", stack);
|
|
|
|
} else {
|
|
|
|
isbpl.level0.add("dump", isbpl::dump);
|
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
|
|
|
|
String line;
|
|
|
|
System.out.print("> ");
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
try {
|
|
|
|
isbpl.interpret(new File("_shell"), line, stack);
|
|
|
|
} catch(ISBPLError e) {
|
|
|
|
System.out.println("Error: " + e.type + ": " + e.message);
|
|
|
|
} catch(Throwable e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
System.out.print("\n> ");
|
|
|
|
}
|
|
|
|
}
|
2022-03-05 20:55:03 +01:00
|
|
|
} catch (ISBPLStop stop) {
|
|
|
|
System.exit(isbpl.exitCode);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
System.out.println(stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static ISBPLObject argarray(ISBPL isbpl, String[] args) {
|
|
|
|
ISBPLObject[] array = new ISBPLObject[args.length - 1];
|
|
|
|
for (int i = 1 ; i < args.length ; i++) {
|
2022-03-06 14:26:18 +01:00
|
|
|
array[i - 1] = isbpl.toISBPLString(args[i]);
|
2022-03-05 20:55:03 +01:00
|
|
|
}
|
|
|
|
return new ISBPLObject(isbpl.getType("array"), array);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String readFile(File f) throws IOException {
|
2022-04-29 18:10:14 +02:00
|
|
|
//noinspection resource
|
2022-03-05 20:55:03 +01:00
|
|
|
FileInputStream fis = new FileInputStream(f);
|
|
|
|
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
|
|
|
byte[] currentBytes = new byte[4096];
|
|
|
|
int len;
|
|
|
|
while ((len = fis.read(currentBytes)) > 0) {
|
|
|
|
bytes.write(currentBytes, 0, len);
|
|
|
|
}
|
|
|
|
return bytes.toString();
|
|
|
|
}
|
2022-04-16 18:44:07 +02:00
|
|
|
|
2022-04-30 12:15:00 +02:00
|
|
|
// Used for theSafe
|
|
|
|
private static class FakeAccessibleObject {
|
|
|
|
boolean override;
|
|
|
|
}
|
2022-04-30 09:45:34 +02:00
|
|
|
private static void forceAccessible(AccessibleObject thing) {
|
|
|
|
try {
|
2022-04-30 12:15:00 +02:00
|
|
|
theSafe.putBoolean(thing, theSafe.objectFieldOffset(FakeAccessibleObject.class.getDeclaredField("override")), true);
|
2022-04-30 09:45:34 +02:00
|
|
|
} catch(Exception e) { //we are doomed
|
|
|
|
e.printStackTrace();
|
|
|
|
System.err.println("Failed to set accessible property. We are doomed.");
|
|
|
|
System.exit(1);
|
|
|
|
}
|
|
|
|
}
|
2022-03-05 20:55:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
interface ISBPLKeyword {
|
|
|
|
int call(int idx, String[] words, File file, Stack<ISBPLObject> stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ISBPLCallable {
|
2022-03-16 00:45:22 +01:00
|
|
|
void call(Stack<ISBPLObject> stack);
|
2022-03-05 20:55:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class ISBPLType {
|
2022-04-16 22:13:08 +02:00
|
|
|
int id;
|
2022-03-05 20:55:03 +01:00
|
|
|
String name;
|
2022-04-16 00:10:51 +02:00
|
|
|
HashMap<String, ISBPLCallable> methods = new HashMap<>();
|
2022-04-16 00:44:52 +02:00
|
|
|
HashMap<ISBPLObject, HashMap<Object, ISBPLObject>> vars = new HashMap<>();
|
2022-04-17 12:17:14 +02:00
|
|
|
ArrayList<ISBPLType> superTypes = new ArrayList<>();
|
2022-03-05 20:55:03 +01:00
|
|
|
|
2022-04-16 22:13:08 +02:00
|
|
|
public ISBPLType(String name, int id) {
|
2022-03-05 20:55:03 +01:00
|
|
|
this.name = name;
|
2022-04-16 22:13:08 +02:00
|
|
|
this.id = id;
|
2022-03-05 20:55:03 +01:00
|
|
|
}
|
2022-04-16 18:44:07 +02:00
|
|
|
|
2022-04-16 00:44:52 +02:00
|
|
|
public HashMap<Object, ISBPLObject> varget(ISBPLObject o) {
|
|
|
|
if(!vars.containsKey(o)) {
|
|
|
|
vars.put(o, new HashMap<>());
|
|
|
|
}
|
|
|
|
return vars.get(o);
|
|
|
|
}
|
2022-03-05 20:55:03 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
if (this == o) return true;
|
|
|
|
if (!(o instanceof ISBPLType)) return false;
|
|
|
|
ISBPLType type = (ISBPLType) o;
|
|
|
|
return id == type.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return id;
|
|
|
|
}
|
2022-03-06 14:26:18 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return "ISBPLType{" +
|
|
|
|
"id=" + id +
|
|
|
|
", name='" + name + '\'' +
|
|
|
|
'}';
|
|
|
|
}
|
2022-03-05 20:55:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class ISBPLObject {
|
2022-04-17 17:02:50 +02:00
|
|
|
ISBPLType type;
|
2022-04-16 18:44:07 +02:00
|
|
|
Object object;
|
2022-03-05 20:55:03 +01:00
|
|
|
|
|
|
|
public ISBPLObject(ISBPLType type, Object object) {
|
|
|
|
this.type = type;
|
|
|
|
this.object = object;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isTruthy() {
|
2022-04-29 18:10:14 +02:00
|
|
|
return object != null && object != Integer.valueOf(0) && !object.equals(Boolean.FALSE);
|
2022-03-05 20:55:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// This has heavy optimizations, please do not change unless necessary
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
if(this == o) return true;
|
|
|
|
if(!(o instanceof ISBPLObject)) return false;
|
|
|
|
ISBPLObject object = (ISBPLObject) o;
|
2022-04-18 19:50:36 +02:00
|
|
|
if(!this.type.equals(object.type))
|
|
|
|
return false;
|
2022-03-05 20:55:03 +01:00
|
|
|
if(this.object == object.object)
|
|
|
|
return true;
|
2022-05-04 02:16:58 +02:00
|
|
|
// These can return false because the strict equality check has already been performed.
|
2022-03-05 20:55:03 +01:00
|
|
|
if(this.object == null)
|
|
|
|
return false;
|
|
|
|
if(object.object == null)
|
|
|
|
return false;
|
|
|
|
if(this.object.getClass().isArray() || object.object.getClass().isArray()) {
|
|
|
|
if(this.object.getClass().isArray() && object.object.getClass().isArray()) {
|
|
|
|
return Arrays.equals((Object[]) this.object, (Object[]) object.object);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this.object.equals(object.object);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void checkType(ISBPLType wanted) {
|
|
|
|
if(wanted.id != type.id) {
|
|
|
|
throw new ISBPLError("IncompatibleTypes", "Incompatible types: " + type.name + " - " + wanted.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-16 18:44:07 +02:00
|
|
|
public void checkTypeMulti(ISBPLType... wanted) {
|
2022-03-05 20:55:03 +01:00
|
|
|
int f = -1;
|
2022-04-16 18:44:07 +02:00
|
|
|
StringBuilder wantedNames = new StringBuilder();
|
2022-03-05 20:55:03 +01:00
|
|
|
for (int i = 0 ; i < wanted.length ; i++) {
|
2022-04-16 18:44:07 +02:00
|
|
|
wantedNames.append(" ").append(wanted[i].name);
|
2022-03-05 20:55:03 +01:00
|
|
|
if(wanted[i].id == type.id) {
|
|
|
|
f = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(f == -1) {
|
|
|
|
throw new ISBPLError("IncompatibleTypes", "Incompatible types: " + type.name + " - " + wantedNames.substring(1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-06 14:26:18 +01:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
2022-03-06 15:25:48 +01:00
|
|
|
if(type != null && object instanceof ISBPLObject[]) {
|
2022-03-06 14:26:18 +01:00
|
|
|
try {
|
|
|
|
return "ISBPLObject{" +
|
|
|
|
"type=" + type +
|
|
|
|
", object=" + Arrays.toString(((ISBPLObject[]) object)) +
|
|
|
|
'}';
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "ISBPLObject{" +
|
|
|
|
"type=" + type +
|
|
|
|
", object=" + object +
|
|
|
|
'}';
|
|
|
|
}
|
|
|
|
|
2022-03-05 20:55:03 +01:00
|
|
|
public double toDouble() {
|
|
|
|
if(object instanceof Integer) {
|
|
|
|
return (double) (int) (Integer) object;
|
|
|
|
}
|
|
|
|
if(object instanceof Long) {
|
|
|
|
return (double) (long) (Long) object;
|
|
|
|
}
|
|
|
|
if(object instanceof Character) {
|
|
|
|
return (double) (char) (Character) object;
|
|
|
|
}
|
|
|
|
if(object instanceof Byte) {
|
|
|
|
return Byte.toUnsignedInt((Byte) object);
|
|
|
|
}
|
|
|
|
if(object instanceof Float) {
|
|
|
|
return (double) (float) (Float) object;
|
|
|
|
}
|
|
|
|
if(object instanceof Double) {
|
|
|
|
return (double) (Double) object;
|
|
|
|
}
|
|
|
|
throw new ISBPLError("InvalidArgument", "The argument is not a number.");
|
|
|
|
}
|
|
|
|
|
|
|
|
public long toLong() {
|
|
|
|
if(object instanceof Integer) {
|
|
|
|
return (long) (int) (Integer) object;
|
|
|
|
}
|
|
|
|
if(object instanceof Long) {
|
|
|
|
return (long) (Long) object;
|
|
|
|
}
|
|
|
|
if(object instanceof Character) {
|
|
|
|
return (long) (char) (Character) object;
|
|
|
|
}
|
|
|
|
if(object instanceof Byte) {
|
|
|
|
return Byte.toUnsignedInt((Byte) object);
|
|
|
|
}
|
|
|
|
if(object instanceof Float) {
|
|
|
|
return (long) (float) (Float) object;
|
|
|
|
}
|
|
|
|
if(object instanceof Double) {
|
|
|
|
return (long) (double) (Double) object;
|
|
|
|
}
|
|
|
|
throw new ISBPLError("InvalidArgument", "The argument is not a number.");
|
|
|
|
}
|
|
|
|
|
|
|
|
public Object negative() {
|
|
|
|
if(object instanceof Integer) {
|
|
|
|
return -(int) (Integer) object;
|
|
|
|
}
|
|
|
|
if(object instanceof Long) {
|
|
|
|
return -(long) (Long) object;
|
|
|
|
}
|
|
|
|
if(object instanceof Float) {
|
|
|
|
return -(float) (Float) object;
|
|
|
|
}
|
|
|
|
if(object instanceof Double) {
|
|
|
|
return -(double) (Double) object;
|
|
|
|
}
|
|
|
|
throw new ISBPLError("InvalidArgument", "This type of number can't be negated!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ISBPLError extends RuntimeException {
|
|
|
|
final String type;
|
|
|
|
final String message;
|
|
|
|
|
|
|
|
public ISBPLError(String type, String message) {
|
|
|
|
this.type = type;
|
|
|
|
this.message = message;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getMessage() {
|
|
|
|
return type + ": " + message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ISBPLStop extends RuntimeException {
|
|
|
|
|
|
|
|
int amount;
|
2022-03-12 19:49:47 +01:00
|
|
|
|
2022-03-05 20:55:03 +01:00
|
|
|
public ISBPLStop(int amount) {
|
|
|
|
this.amount = amount - 1;
|
|
|
|
}
|
|
|
|
}
|
2022-03-06 14:26:18 +01:00
|
|
|
|
|
|
|
class ISBPLDebugger extends Thread {
|
2022-04-16 18:44:07 +02:00
|
|
|
private final ISBPL isbpl;
|
2022-03-07 13:17:25 +01:00
|
|
|
int port = -1;
|
2022-03-12 19:49:47 +01:00
|
|
|
long mainID = Thread.currentThread().getId();
|
2022-03-06 14:26:18 +01:00
|
|
|
|
|
|
|
public ISBPLDebugger(ISBPL isbpl) {
|
|
|
|
this.isbpl = isbpl;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
2022-03-06 19:34:29 +01:00
|
|
|
ServerSocket socket = null;
|
|
|
|
try {
|
2022-04-29 18:10:14 +02:00
|
|
|
//noinspection resource
|
2022-03-06 19:34:29 +01:00
|
|
|
socket = new ServerSocket(Integer.parseInt(System.getenv().getOrDefault("DEBUG", "9736")));
|
2022-03-07 13:17:25 +01:00
|
|
|
port = socket.getLocalPort();
|
2022-03-06 19:34:29 +01:00
|
|
|
System.err.println("Debugger listening on :" + socket.getLocalPort());
|
|
|
|
}
|
|
|
|
catch (BindException e) {
|
|
|
|
while (socket == null) {
|
|
|
|
try {
|
2022-04-29 18:10:14 +02:00
|
|
|
//noinspection resource
|
2022-03-06 19:34:29 +01:00
|
|
|
socket = new ServerSocket((int) (Math.random() * 5000 + 5000));
|
2022-03-07 15:17:34 +01:00
|
|
|
port = socket.getLocalPort();
|
2022-03-06 19:34:29 +01:00
|
|
|
System.err.println("Debugger listening on :" + socket.getLocalPort());
|
|
|
|
}
|
|
|
|
catch (BindException ignored) { }
|
|
|
|
}
|
|
|
|
}
|
2022-03-16 02:07:53 +01:00
|
|
|
isbpl.debuggerIPC.port = port;
|
2022-03-06 19:34:29 +01:00
|
|
|
isbpl.debuggerIPC.threadID = Thread.currentThread().getId();
|
2022-03-06 14:26:18 +01:00
|
|
|
while (true) {
|
|
|
|
Socket s = socket.accept();
|
2022-03-12 19:49:47 +01:00
|
|
|
new Thread(() -> {
|
2022-03-06 16:34:26 +01:00
|
|
|
try {
|
2022-03-12 19:49:47 +01:00
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream()));
|
|
|
|
String line;
|
|
|
|
long tid = mainID;
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
try {
|
|
|
|
switch (line.split(" ")[0]) {
|
|
|
|
case "continue":
|
|
|
|
case "cont":
|
|
|
|
case "c":
|
|
|
|
isbpl.debuggerIPC.run.put(tid, -1);
|
|
|
|
break;
|
|
|
|
case "stop":
|
|
|
|
case "s":
|
|
|
|
isbpl.debuggerIPC.run.put(tid, 0);
|
|
|
|
break;
|
|
|
|
case "next":
|
|
|
|
case "n":
|
|
|
|
isbpl.debuggerIPC.run.put(tid, 1);
|
|
|
|
break;
|
|
|
|
case "nextall":
|
|
|
|
case "na":
|
|
|
|
isbpl.debuggerIPC.run.replaceAll((i, v) -> 1);
|
|
|
|
break;
|
|
|
|
case "do":
|
|
|
|
case "d":
|
|
|
|
isbpl.debuggerIPC.run.put(tid, Integer.parseInt(line.split(" ")[1]));
|
|
|
|
break;
|
|
|
|
case "until":
|
|
|
|
case "u":
|
|
|
|
isbpl.debuggerIPC.until = line.split(" ")[1];
|
|
|
|
isbpl.debuggerIPC.run.put(tid, -2);
|
|
|
|
break;
|
|
|
|
case "eval":
|
|
|
|
isbpl.debuggerIPC.run.put(tid, -3);
|
|
|
|
isbpl.debuggerIPC.threadID = Thread.currentThread().getId();
|
2022-03-06 16:34:26 +01:00
|
|
|
try {
|
2022-03-12 19:49:47 +01:00
|
|
|
isbpl.interpret(new File("_debug").getAbsoluteFile(), line.substring(5), isbpl.debuggerIPC.stack.get(tid));
|
|
|
|
}
|
|
|
|
catch (ISBPLStop stop) {
|
|
|
|
System.exit(isbpl.exitCode);
|
2022-03-06 16:34:26 +01:00
|
|
|
}
|
2022-03-12 19:49:47 +01:00
|
|
|
catch (Throwable e) {
|
2022-03-06 16:34:26 +01:00
|
|
|
e.printStackTrace();
|
2022-03-12 19:49:47 +01:00
|
|
|
boolean fixed = false;
|
|
|
|
while (!fixed) {
|
|
|
|
try {
|
|
|
|
System.err.println("Stack recovered: " + isbpl.debuggerIPC.stack.get(tid));
|
|
|
|
fixed = true;
|
|
|
|
}
|
|
|
|
catch (Exception e1) {
|
|
|
|
e.printStackTrace();
|
|
|
|
System.err.println("!!! STACK CORRUPTED!");
|
|
|
|
isbpl.debuggerIPC.stack.get(tid).pop();
|
|
|
|
System.err.println("Popped. Trying again.");
|
|
|
|
}
|
|
|
|
}
|
2022-03-06 16:34:26 +01:00
|
|
|
}
|
2022-03-12 19:49:47 +01:00
|
|
|
break;
|
|
|
|
case "dump":
|
|
|
|
try {
|
|
|
|
System.err.println("VAR DUMP\n----------------");
|
2022-04-29 18:30:10 +02:00
|
|
|
for (ISBPLFrame map : isbpl.frameStack.map.get(tid)) {
|
|
|
|
HashMap<String, ISBPLCallable> all = map.all();
|
|
|
|
for (String key : all.keySet()) {
|
2022-03-12 19:49:47 +01:00
|
|
|
if (key.startsWith("=")) {
|
2022-04-29 18:30:10 +02:00
|
|
|
all.get(key.substring(1)).call(isbpl.debuggerIPC.stack.get(tid));
|
2022-03-12 19:49:47 +01:00
|
|
|
System.err.println("\t" + key.substring(1) + ": \t" + isbpl.debuggerIPC.stack.get(tid).pop());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
System.err.println("----------------");
|
2022-03-06 16:34:26 +01:00
|
|
|
}
|
|
|
|
}
|
2022-03-12 19:49:47 +01:00
|
|
|
catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
System.err.println("!!! VARS CORRUPTED! CANNOT FIX AUTOMATICALLY.");
|
|
|
|
}
|
|
|
|
case "stack":
|
|
|
|
boolean fixed = false;
|
|
|
|
while (!fixed) {
|
|
|
|
try {
|
|
|
|
System.err.println("STACK DUMP");
|
|
|
|
for (ISBPLObject object : isbpl.debuggerIPC.stack.get(tid)) {
|
|
|
|
System.err.println("\t" + object);
|
|
|
|
}
|
|
|
|
fixed = true;
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
System.err.println("!!! STACK CORRUPTED!");
|
|
|
|
isbpl.debuggerIPC.stack.get(tid).pop();
|
|
|
|
System.err.println("Popped. Trying again.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "son":
|
|
|
|
ISBPL.debug = true;
|
2022-04-16 18:44:07 +02:00
|
|
|
ISBPL.printCalls = true;
|
|
|
|
break;
|
|
|
|
case "sonf":
|
|
|
|
ISBPL.debug = false;
|
|
|
|
ISBPL.printCalls = true;
|
2022-03-12 19:49:47 +01:00
|
|
|
break;
|
|
|
|
case "soff":
|
|
|
|
ISBPL.debug = false;
|
2022-04-16 18:44:07 +02:00
|
|
|
ISBPL.printCalls = false;
|
2022-03-12 19:49:47 +01:00
|
|
|
break;
|
|
|
|
case "exit":
|
|
|
|
System.exit(255);
|
|
|
|
break;
|
|
|
|
case "threads":
|
|
|
|
System.err.println("THREAD DUMP");
|
|
|
|
for (Thread thread : Thread.getAllStackTraces().keySet()) {
|
|
|
|
if (isbpl.debuggerIPC.stack.containsKey(thread.getId()))
|
|
|
|
System.err.println(thread.getId() + "\t" + thread.getName());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "setthread":
|
|
|
|
case "st":
|
|
|
|
long l = Long.parseLong(line.split(" ")[1]);
|
|
|
|
if (isbpl.debuggerIPC.stack.containsKey(l)) {
|
|
|
|
tid = l;
|
|
|
|
System.err.println("Set TID=" + l);
|
2022-03-06 16:34:26 +01:00
|
|
|
}
|
2022-03-12 19:49:47 +01:00
|
|
|
else
|
|
|
|
System.err.println("Thread not valid");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2022-03-06 14:26:18 +01:00
|
|
|
}
|
2022-03-12 19:49:47 +01:00
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
try {
|
|
|
|
e.printStackTrace(new PrintStream(s.getOutputStream()));
|
|
|
|
}
|
|
|
|
catch (IOException ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
try {
|
|
|
|
s.close();
|
|
|
|
}
|
|
|
|
catch (IOException ex) {
|
|
|
|
ex.printStackTrace();
|
2022-03-06 16:34:26 +01:00
|
|
|
}
|
2022-03-06 14:26:18 +01:00
|
|
|
}
|
2022-03-12 19:49:47 +01:00
|
|
|
}).start();
|
2022-03-06 14:26:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static class IPC {
|
2022-03-06 19:34:29 +01:00
|
|
|
long threadID = -1;
|
2022-03-06 14:26:18 +01:00
|
|
|
String until = null;
|
2022-03-12 19:49:47 +01:00
|
|
|
HashMap<Long, Integer> run = new HashMap<>();
|
|
|
|
HashMap<Long, Stack<ISBPLObject>> stack = new HashMap<>();
|
2022-03-16 02:07:53 +01:00
|
|
|
int port = -1;
|
2022-03-06 14:26:18 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2022-03-06 18:13:25 +01:00
|
|
|
|
|
|
|
class ISBPLStreamer {
|
2022-03-06 18:46:04 +01:00
|
|
|
public static final int CREATE_FILE_IN = 0;
|
|
|
|
public static final int CREATE_FILE_OUT = 1;
|
|
|
|
public static final int CREATE_SOCKET = 2;
|
|
|
|
public static final int CLOSE = 3;
|
|
|
|
public static final int READ = 4;
|
|
|
|
public static final int WRITE = 5;
|
2022-04-16 18:44:07 +02:00
|
|
|
@SuppressWarnings("unused")
|
2022-03-06 18:46:04 +01:00
|
|
|
public static final int AREAD = 6;
|
2022-04-16 18:44:07 +02:00
|
|
|
@SuppressWarnings("unused")
|
2022-03-06 18:46:04 +01:00
|
|
|
public static final int AWRITE = 7;
|
2022-03-12 19:59:39 +01:00
|
|
|
public static final int CREATE_SERVER = 9;
|
2022-03-06 18:13:25 +01:00
|
|
|
|
|
|
|
static class ISBPLStream {
|
|
|
|
final InputStream in;
|
|
|
|
final OutputStream out;
|
|
|
|
static int gid = 0;
|
|
|
|
final int id = gid++;
|
2022-03-12 19:49:47 +01:00
|
|
|
|
2022-03-06 18:13:25 +01:00
|
|
|
public ISBPLStream(InputStream in, OutputStream out) {
|
|
|
|
this.in = in;
|
|
|
|
this.out = out;
|
|
|
|
}
|
2022-03-12 19:49:47 +01:00
|
|
|
|
2022-03-06 18:46:04 +01:00
|
|
|
public void close() throws IOException {
|
|
|
|
this.in.close();
|
|
|
|
this.out.close();
|
|
|
|
}
|
2022-03-06 18:13:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
final ISBPL isbpl;
|
|
|
|
|
|
|
|
public ISBPLStreamer(ISBPL isbpl) {
|
|
|
|
this.isbpl = isbpl;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ArrayList<ISBPLStream> streams = new ArrayList<>();
|
|
|
|
|
2022-03-12 19:59:39 +01:00
|
|
|
public synchronized void action(Stack<ISBPLObject> stack, int action) throws IOException {
|
2022-03-06 18:13:25 +01:00
|
|
|
ISBPLStream stream;
|
|
|
|
ISBPLObject s, i;
|
2022-03-06 18:46:04 +01:00
|
|
|
File f;
|
2022-03-06 18:13:25 +01:00
|
|
|
switch (action) {
|
2022-03-06 18:46:04 +01:00
|
|
|
case CREATE_FILE_IN:
|
|
|
|
s = stack.pop();
|
|
|
|
s.checkType(isbpl.getType("string"));
|
|
|
|
f = new File(isbpl.toJavaString(s));
|
2022-04-29 18:10:14 +02:00
|
|
|
stream = new ISBPLStream(Files.newInputStream(f.toPath()), new OutputStream() {
|
2022-03-06 18:46:04 +01:00
|
|
|
@Override
|
2022-04-16 18:44:07 +02:00
|
|
|
public void write(int b) {
|
2022-03-06 18:46:04 +01:00
|
|
|
throw new ISBPLError("IllegalArgument", "Can't write to a FILE_IN stream!");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
streams.add(stream);
|
|
|
|
stack.push(new ISBPLObject(isbpl.getType("int"), stream.id));
|
|
|
|
break;
|
|
|
|
case CREATE_FILE_OUT:
|
2022-03-06 18:13:25 +01:00
|
|
|
s = stack.pop();
|
|
|
|
s.checkType(isbpl.getType("string"));
|
2022-03-06 18:46:04 +01:00
|
|
|
f = new File(isbpl.toJavaString(s));
|
|
|
|
stream = new ISBPLStream(new InputStream() {
|
|
|
|
@Override
|
2022-04-16 18:44:07 +02:00
|
|
|
public int read() {
|
2022-03-06 18:46:04 +01:00
|
|
|
throw new ISBPLError("IllegalArgument", "Can't read a FILE_OUT stream!");
|
|
|
|
}
|
2022-04-29 18:10:14 +02:00
|
|
|
}, Files.newOutputStream(f.toPath()));
|
2022-03-06 18:13:25 +01:00
|
|
|
streams.add(stream);
|
|
|
|
stack.push(new ISBPLObject(isbpl.getType("int"), stream.id));
|
|
|
|
break;
|
|
|
|
case CREATE_SOCKET:
|
|
|
|
i = stack.pop();
|
|
|
|
s = stack.pop();
|
|
|
|
i.checkType(isbpl.getType("int"));
|
|
|
|
s.checkType(isbpl.getType("string"));
|
|
|
|
Socket socket = new Socket(isbpl.toJavaString(s), ((int) i.object));
|
|
|
|
stream = new ISBPLStream(socket.getInputStream(), socket.getOutputStream());
|
|
|
|
streams.add(stream);
|
|
|
|
stack.push(new ISBPLObject(isbpl.getType("int"), stream.id));
|
|
|
|
break;
|
2022-03-12 19:59:39 +01:00
|
|
|
case CREATE_SERVER:
|
|
|
|
i = stack.pop();
|
|
|
|
i.checkType(isbpl.getType("int"));
|
|
|
|
ServerSocket server = new ServerSocket(((int) i.object));
|
|
|
|
stream = new ISBPLStream(new InputStream() {
|
|
|
|
@Override
|
|
|
|
public int read() throws IOException {
|
|
|
|
Socket socket = server.accept();
|
|
|
|
ISBPLStream stream = new ISBPLStream(socket.getInputStream(), socket.getOutputStream());
|
|
|
|
return stream.id;
|
|
|
|
}
|
|
|
|
}, new OutputStream() {
|
|
|
|
@Override
|
2022-04-16 18:44:07 +02:00
|
|
|
public void write(int b) {
|
2022-03-12 19:59:39 +01:00
|
|
|
throw new ISBPLError("IllegalArgument", "Can't write to a SERVER stream!");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
streams.add(stream);
|
|
|
|
stack.push(new ISBPLObject(isbpl.getType("int"), stream.id));
|
|
|
|
break;
|
2022-03-06 18:13:25 +01:00
|
|
|
case READ:
|
|
|
|
i = stack.pop();
|
|
|
|
i.checkType(isbpl.getType("int"));
|
|
|
|
try {
|
|
|
|
stack.push(new ISBPLObject(isbpl.getType("int"), streams.get(((int) i.object)).in.read()));
|
|
|
|
} catch (IndexOutOfBoundsException e) {
|
|
|
|
throw new ISBPLError("IllegalArgument", "streamid STREAM_READ stream called with non-existing stream argument");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WRITE:
|
|
|
|
i = stack.pop();
|
|
|
|
i.checkType(isbpl.getType("int"));
|
|
|
|
ISBPLObject bte = stack.pop();
|
|
|
|
bte.checkTypeMulti(isbpl.getType("int"), isbpl.getType("char"), isbpl.getType("byte"));
|
|
|
|
try {
|
|
|
|
streams.get(((int) i.object)).out.write(((int) bte.toLong()));
|
|
|
|
} catch (IndexOutOfBoundsException e) {
|
2022-03-06 18:46:04 +01:00
|
|
|
throw new ISBPLError("IllegalArgument", "byte streamid STREAM_WRITE stream called with non-existing stream argument");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CLOSE:
|
|
|
|
i = stack.pop();
|
|
|
|
i.checkType(isbpl.getType("int"));
|
|
|
|
try {
|
|
|
|
ISBPLStream strm = streams.get(((int) i.object));
|
|
|
|
strm.close();
|
|
|
|
} catch (IndexOutOfBoundsException e) {
|
|
|
|
throw new ISBPLError("IllegalArgument", "streamid STREAM_CLOSE stream called with non-existing stream argument");
|
2022-03-06 18:13:25 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new ISBPLError("NotImplemented", "Not implemented");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-12 19:49:47 +01:00
|
|
|
|
|
|
|
class ISBPLThreadLocal<T> {
|
|
|
|
HashMap<Long, T> map = new HashMap<>();
|
|
|
|
Supplier<? extends T> supplier;
|
|
|
|
|
|
|
|
public ISBPLThreadLocal(Supplier<? extends T> supplier) {
|
|
|
|
this.supplier = supplier;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static <T> ISBPLThreadLocal<T> withInitial(Supplier<? extends T> supplier) {
|
|
|
|
return new ISBPLThreadLocal<>(supplier);
|
|
|
|
}
|
|
|
|
|
2022-04-27 02:47:44 +02:00
|
|
|
public synchronized T get() {
|
2022-03-12 19:49:47 +01:00
|
|
|
long tid = Thread.currentThread().getId();
|
|
|
|
if(!map.containsKey(tid))
|
|
|
|
map.put(tid, supplier.get());
|
|
|
|
return map.get(tid);
|
|
|
|
}
|
|
|
|
|
2022-04-27 02:47:44 +02:00
|
|
|
public synchronized void set(T t) {
|
2022-04-16 18:44:07 +02:00
|
|
|
map.put(Thread.currentThread().getId(), t);
|
2022-03-12 19:49:47 +01:00
|
|
|
}
|
2022-04-29 18:10:14 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return get().toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ISBPLStack<T> extends Stack<T> {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public T push(T t) {
|
2022-04-30 15:27:15 +02:00
|
|
|
if(t == null)
|
|
|
|
new IllegalArgumentException("item is null").printStackTrace();
|
2022-04-29 18:10:14 +02:00
|
|
|
return super.push(t);
|
|
|
|
}
|
2022-03-12 19:49:47 +01:00
|
|
|
}
|
2022-04-29 18:30:10 +02:00
|
|
|
|
|
|
|
class ISBPLFrame {
|
|
|
|
|
2022-04-30 15:27:15 +02:00
|
|
|
public ISBPL context;
|
2022-04-29 18:30:10 +02:00
|
|
|
public ISBPLFrame parent;
|
|
|
|
public HashMap<String, ISBPLCallable> map = new HashMap<>();
|
2022-04-30 15:27:15 +02:00
|
|
|
public HashMap<Object, ISBPLObject> variables = new HashMap<>();
|
2022-04-29 18:30:10 +02:00
|
|
|
|
2022-04-30 15:27:15 +02:00
|
|
|
public ISBPLFrame(ISBPL context) {
|
|
|
|
this.context = context;
|
|
|
|
}
|
|
|
|
public ISBPLFrame(ISBPL context, ISBPLFrame parentFrame) {
|
|
|
|
this.context = context;
|
2022-04-29 18:30:10 +02:00
|
|
|
parent = parentFrame;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ISBPLCallable resolve(String name) {
|
2022-04-30 15:27:15 +02:00
|
|
|
if(name.startsWith("&")) {
|
|
|
|
ISBPLCallable c = resolve(name.substring(1));
|
|
|
|
if(c != null) {
|
|
|
|
return (stack -> stack.push(new ISBPLObject(context.getType("func"), c)));
|
|
|
|
}
|
|
|
|
}
|
2022-04-29 18:30:10 +02:00
|
|
|
if(map.containsKey(name))
|
|
|
|
return map.get(name);
|
2022-05-02 10:38:29 +02:00
|
|
|
if(parent != null) {
|
|
|
|
return parent.resolve(name);
|
2022-04-30 15:27:15 +02:00
|
|
|
}
|
2022-04-29 18:30:10 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void add(String name, ISBPLCallable callable) {
|
|
|
|
map.put(name, callable);
|
|
|
|
}
|
2022-04-30 15:27:15 +02:00
|
|
|
|
|
|
|
public void define(String name, ISBPLObject value) {
|
|
|
|
Object var = new Object();
|
|
|
|
variables.put(var, value);
|
|
|
|
add(name, (stack) -> stack.push(variables.get(var)));
|
|
|
|
add("=" + name, (stack) -> variables.put(var, stack.pop()));
|
|
|
|
}
|
2022-04-29 18:30:10 +02:00
|
|
|
|
|
|
|
public HashMap<String, ISBPLCallable> all() {
|
|
|
|
HashMap<String, ISBPLCallable> r = parent == null ? new HashMap<>() : parent.all();
|
|
|
|
r.putAll(map);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|