From e9022405c9c85c033dcbbb397405d23e00f6ef86 Mon Sep 17 00:00:00 2001 From: TudbuT Date: Sun, 6 Mar 2022 18:13:25 +0100 Subject: [PATCH] add streams --- bootstrap/ISBPL.java | 89 ++++++++++++++++++++++++++++++++++++++++++++ stream.isbpl | 11 ++++++ 2 files changed, 100 insertions(+) create mode 100644 stream.isbpl diff --git a/bootstrap/ISBPL.java b/bootstrap/ISBPL.java index 713b6a4..219e9fb 100644 --- a/bootstrap/ISBPL.java +++ b/bootstrap/ISBPL.java @@ -20,6 +20,7 @@ public class ISBPL { HashMap vars = new HashMap<>(); ArrayList lastWords = new ArrayList<>(16); int exitCode; + private ISBPLStreamer streamer = new ISBPLStreamer(this); public ISBPL() { functionStack.push(new HashMap<>()); @@ -741,6 +742,18 @@ public class ISBPL { stack.push(new ISBPLObject(getType("long"), System.currentTimeMillis())); }; break; + case "stream": + func = (File file) -> { + 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()); + } + }; } functionStack.peek().put(name, func); } @@ -1342,3 +1355,79 @@ class ISBPLDebugger extends Thread { } } + +class ISBPLStreamer { + public static final int CREATE_FILE = 1; + public static final int CREATE_SOCKET = 2; + public static final int READ = 3; + public static final int WRITE = 4; + public static final int AREAD = 5; + public static final int AWRITE = 6; + + static class ISBPLStream { + final InputStream in; + final OutputStream out; + static int gid = 0; + final int id = gid++; + + public ISBPLStream(InputStream in, OutputStream out) { + this.in = in; + this.out = out; + } + } + + final ISBPL isbpl; + + public ISBPLStreamer(ISBPL isbpl) { + this.isbpl = isbpl; + } + + public ArrayList streams = new ArrayList<>(); + + public void action(Stack stack, int action) throws IOException { + ISBPLStream stream; + ISBPLObject s, i; + switch (action) { + case CREATE_FILE: + s = stack.pop(); + s.checkType(isbpl.getType("string")); + File f = new File(isbpl.toJavaString(s)); + stream = new ISBPLStream(new FileInputStream(f), new FileOutputStream(f)); + 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; + 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) { + throw new ISBPLError("IllegalArgument", "streamid STREAM_READ stream called with non-existing stream argument"); + } + break; + default: + throw new ISBPLError("NotImplemented", "Not implemented"); + } + } +} diff --git a/stream.isbpl b/stream.isbpl new file mode 100644 index 0000000..744e8c1 --- /dev/null +++ b/stream.isbpl @@ -0,0 +1,11 @@ +native stream + + +def STREAM_CREATE_FILE 1 =STREAM_CREATE_FILE +def STREAM_CREATE_SOCKET 2 =STREAM_CREATE_SOCKET +def STREAM_READ 3 =STREAM_READ +def STREAM_WRITE 4 =STREAM_WRITE + +"not implemented:" # + "def STREAM_AREAD 5 =STREAM_AREAD" # + "def STREAM_AWRITE 6 =STREAM_AWRITE" #