From 8861c525486c41a4bbaa2cbdcc4ec206e0627299 Mon Sep 17 00:00:00 2001 From: bajdcc Date: Thu, 20 Apr 2017 19:48:49 +0800 Subject: [PATCH] ADD: Add remote - Add module: ModuleRemote - Add task: TKUI - Add window: Remote window --- README.md | 6 + .../LALR1/grammar/runtime/RuntimeMachine.java | 23 ++-- .../LALR1/interpret/module/ModuleRemote.java | 123 ++++++++++++++++++ .../bajdcc/LALR1/interpret/os/kern/OSIrq.java | 6 + .../LALR1/interpret/os/kern/OSTask.java | 1 + .../bajdcc/LALR1/interpret/os/task/TKUI.java | 69 ++++++++++ src/priv/bajdcc/LALR1/ui/UIMainFrame.java | 4 +- src/priv/bajdcc/LALR1/ui/UIPanel.java | 1 - src/priv/bajdcc/LALR1/ui/UIRemotePanel.java | 29 +++++ src/priv/bajdcc/LALR1/ui/UIRemoteWindow.java | 47 +++++++ 10 files changed, 296 insertions(+), 13 deletions(-) create mode 100644 src/priv/bajdcc/LALR1/interpret/module/ModuleRemote.java create mode 100644 src/priv/bajdcc/LALR1/interpret/os/task/TKUI.java create mode 100644 src/priv/bajdcc/LALR1/ui/UIRemotePanel.java create mode 100644 src/priv/bajdcc/LALR1/ui/UIRemoteWindow.java diff --git a/README.md b/README.md index 391d10d..2138a17 100755 --- a/README.md +++ b/README.md @@ -49,9 +49,15 @@ Now has commands: - time - count +Tasks: +- System +- Utility +- Remote + Implemented IPC, usage: - `task system now` -> Get system time - `task util calc 1+2*3` -> Val = 7 +- `task ui print hello world` -> Remote window #### Manual diff --git a/src/priv/bajdcc/LALR1/grammar/runtime/RuntimeMachine.java b/src/priv/bajdcc/LALR1/grammar/runtime/RuntimeMachine.java index 9b1c5e3..b0fb05b 100755 --- a/src/priv/bajdcc/LALR1/grammar/runtime/RuntimeMachine.java +++ b/src/priv/bajdcc/LALR1/grammar/runtime/RuntimeMachine.java @@ -51,6 +51,7 @@ public RuntimeMachine() throws Exception { ModuleProc.getInstance(), ModuleUI.getInstance(), ModuleTask.getInstance(), + ModuleRemote.getInstance(), }; } @@ -186,10 +187,10 @@ private boolean runByStep() throws Exception { if (inst == RuntimeInst.ihalt) { return false; } - if (debug) { - System.err.println(); - System.err.print(stack.reg.execId + ": " + inst.toString()); - } +// if (debug) { +// System.err.println(); +// System.err.print(stack.reg.execId + ": " + inst.toString()); +// } OperatorType op = TokenTools.ins2op(inst); nextInst(); if (op != null) { @@ -203,13 +204,13 @@ private boolean runByStep() throws Exception { } } } - if (debug) { - System.err.println(); - System.err.print(stack.toString()); - System.err.print("协程栈:"); - System.err.print(stkYieldData.toString()); - System.err.println(); - } +// if (debug) { +// System.err.println(); +// System.err.print(stack.toString()); +// System.err.print("协程栈:"); +// System.err.print(stkYieldData.toString()); +// System.err.println(); +// } return true; } diff --git a/src/priv/bajdcc/LALR1/interpret/module/ModuleRemote.java b/src/priv/bajdcc/LALR1/interpret/module/ModuleRemote.java new file mode 100644 index 0000000..d42edf8 --- /dev/null +++ b/src/priv/bajdcc/LALR1/interpret/module/ModuleRemote.java @@ -0,0 +1,123 @@ +package priv.bajdcc.LALR1.interpret.module; + +import priv.bajdcc.LALR1.grammar.Grammar; +import priv.bajdcc.LALR1.grammar.runtime.*; +import priv.bajdcc.LALR1.ui.UIRemoteWindow; +import priv.bajdcc.LALR1.ui.drawing.UIGraphics; + +import java.util.ArrayDeque; +import java.util.List; +import java.util.Queue; +import java.util.concurrent.LinkedBlockingDeque; + +/** + * 【模块】远程用户界面 + * + * @author bajdcc + */ +public class ModuleRemote implements IInterpreterModule { + + private static ModuleRemote instance = new ModuleRemote(); + private UIRemoteWindow remote; + private UIGraphics graphics; + private Queue queue = new LinkedBlockingDeque<>(1024); + private Queue queueDisplay = new ArrayDeque<>(); + private StringBuilder sb = new StringBuilder(); + + public void setGraphics() { + if (remote == null) { + remote = new UIRemoteWindow(); + graphics = remote.getUIGraphics(); + } + } + + public void addInputChar(char c) { + queue.add(c); + } + + public void addDisplayChar(char c) { + queueDisplay.add(c); + } + + public static ModuleRemote getInstance() { + return instance; + } + + @Override + public String getModuleName() { + return "sys.remote"; + } + + @Override + public RuntimeCodePage getCodePage() throws Exception { + String base = "import \"sys.base\";\n" + + "import \"sys.list\";\n" + + "import \"sys.proc\";\n" + + "import \"sys.string\";\n" + + "var g_remote_print = func ~(str) {\n" + + " var remote_int = call g_create_pipe(\"int#0\");\n" + + " foreach (var c : call g_range_string(str)) {\n" + + " call g_write_pipe(remote_int, c);\n" + + " }\n" + + "};\n" + + "export \"g_remote_print\";\n" + + "var g_remote_printn = func ~(str) {\n" + + " call g_remote_print(str);\n" + + " call g_remote_println();\n" + + "};\n" + + "export \"g_remote_printn\";\n" + + "var g_remote_println = func ~() {\n" + + " call g_remote_print(g_endl);\n" + + "};\n" + + "export \"g_remote_println\";\n" + + "call g_remote_init();\n" + + "\n"; + + Grammar grammar = new Grammar(base); + RuntimeCodePage page = grammar.getCodePage(); + IRuntimeDebugInfo info = page.getInfo(); + buildRemoteMethods(info); + + return page; + } + + private void buildRemoteMethods(IRuntimeDebugInfo info) { + info.addExternalFunc("g_remote_init", new IRuntimeDebugExec() { + @Override + public String getDoc() { + return "显示初始化"; + } + + @Override + public RuntimeObjectType[] getArgsType() { + return null; + } + + @Override + public RuntimeObject ExternalProcCall(List args, + IRuntimeStatus status) throws Exception { + if (graphics == null) + setGraphics(); + return null; + } + }); + info.addExternalFunc("g_remote_print_internal", new IRuntimeDebugExec() { + @Override + public String getDoc() { + return "显示输出"; + } + + @Override + public RuntimeObjectType[] getArgsType() { + return new RuntimeObjectType[]{RuntimeObjectType.kChar}; + } + + @Override + public RuntimeObject ExternalProcCall(List args, + IRuntimeStatus status) throws Exception { + graphics.drawText((char) args.get(0).getObj()); + return null; + } + }); + } +} diff --git a/src/priv/bajdcc/LALR1/interpret/os/kern/OSIrq.java b/src/priv/bajdcc/LALR1/interpret/os/kern/OSIrq.java index 21ae755..e510cf7 100644 --- a/src/priv/bajdcc/LALR1/interpret/os/kern/OSIrq.java +++ b/src/priv/bajdcc/LALR1/interpret/os/kern/OSIrq.java @@ -24,6 +24,7 @@ public String getCode() { "import \"sys.proc\";\n" + "import \"sys.task\";\n" + "import \"sys.ui\";\n" + + "import \"sys.remote\";\n" + "var interrupt_num = " + INT_NUM + ";\n" + "var int_table = [];\n" + "var desc_table = [];\n" + @@ -31,6 +32,7 @@ public String getCode() { " call g_array_add(int_table, g_null);\n" + " call g_array_add(desc_table, \"unused irq#\" + i);\n" + "}\n" + + "call g_array_set(desc_table, 0, \"remote task\");\n" + "call g_array_set(desc_table, 1, \"service task\");\n" + "call g_array_set(desc_table, 2, \"print task\");\n" + "call g_array_set(desc_table, 3, \"signal task\");\n" + @@ -103,6 +105,10 @@ public String getCode() { "};\n" + "call g_task_init();\n" + "call add_int_proc(1, task_handler);\n" + + "var remote_handler = func ~(ch) {\n" + + " call g_remote_print_internal(ch);\n" + + "};\n" + + "call add_int_proc(0, remote_handler);\n" + ""; } } diff --git a/src/priv/bajdcc/LALR1/interpret/os/kern/OSTask.java b/src/priv/bajdcc/LALR1/interpret/os/kern/OSTask.java index 1ec380d..02789da 100644 --- a/src/priv/bajdcc/LALR1/interpret/os/kern/OSTask.java +++ b/src/priv/bajdcc/LALR1/interpret/os/kern/OSTask.java @@ -32,6 +32,7 @@ public String getCode() { "\n" + "call g_array_set(task_name_table, 1, \"system\");\n" + "call g_array_set(task_name_table, 2, \"util\");\n" + + "call g_array_set(task_name_table, 3, \"ui\");\n" + "\n" + "foreach (var j : call g_range(0, task_num - 1)) {\n" + " var t = call g_array_get(task_name_table, j);\n" + diff --git a/src/priv/bajdcc/LALR1/interpret/os/task/TKUI.java b/src/priv/bajdcc/LALR1/interpret/os/task/TKUI.java new file mode 100644 index 0000000..1dc6140 --- /dev/null +++ b/src/priv/bajdcc/LALR1/interpret/os/task/TKUI.java @@ -0,0 +1,69 @@ +package priv.bajdcc.LALR1.interpret.os.task; + +import priv.bajdcc.LALR1.interpret.os.IOSCodePage; + +/** + * 【服务】用户界面 + * + * @author bajdcc + */ +public class TKUI implements IOSCodePage { + @Override + public String getName() { + return "/task/ui"; + } + + @Override + public String getCode() { + return "import \"sys.base\";\n" + + "import \"sys.list\";\n" + + "import \"sys.proc\";\n" + + "import \"sys.task\";\n" + + "import \"sys.remote\";\n" + + "\n" + + "call g_set_process_desc(\"ui service\");\n" + + "call g_set_process_priority(74);\n" + + "\n" + + "var tid = 3;\n" + + "var handle = call g_create_pipe(\"TASKSEND#\" + tid);\n" + + "\n" + + "var time = func ~(msg, caller) {\n" + + " var id = call g_map_get(msg, \"id\");\n" + + " if (call g_is_null(id)) {\n" + + " call g_map_put(msg, \"error\", 1);\n" + + " call g_map_put(msg, \"val\", \"invalid task argument - id\");\n" + + " return;\n" + + " }\n" + + " if (id == \"print\") {\n" + + " var arg = call g_map_get(msg, \"arg\");\n" + + " var str = \"\";\n" + + " var len = call g_array_size(arg);\n" + + " foreach (var i : call g_range(2, len - 1)) {\n" + + " let str = str + call g_array_get(arg, i);\n" + + " }\n" + + " call g_remote_print(str);\n" + + " call g_map_put(msg, \"val\", str);\n" + + " }" + + "};\n" + + "\n" + + "var handler = func ~(ch) {\n" + + " if (ch == 'E') {\n" + + " call g_destroy_pipe(handle);\n" + + " return;\n" + + " }\n" + + " var msg = call g_query_share(\"TASKDATA#\" + tid);\n" + + " var caller = call g_query_share(\"TASKCALLER#\" + tid);\n" + + " call time(msg, caller);\n" + + " var handle = call g_create_pipe(\"TASKRECV#\" + tid);\n" + + " var f = func ~(ch) {\n" + + " if (ch == 'E') { call g_destroy_pipe(handle); }" + + " };\n" + + " call g_read_pipe(handle, f);\n" + + "};\n" + + "\n" + + "var data = {};\n" + + "call g_task_add_proc(3, data);\n" + + "\n" + + "call g_read_pipe(handle, handler);\n"; + } +} diff --git a/src/priv/bajdcc/LALR1/ui/UIMainFrame.java b/src/priv/bajdcc/LALR1/ui/UIMainFrame.java index 68cdfe7..efd965a 100644 --- a/src/priv/bajdcc/LALR1/ui/UIMainFrame.java +++ b/src/priv/bajdcc/LALR1/ui/UIMainFrame.java @@ -10,6 +10,7 @@ import priv.bajdcc.LALR1.interpret.os.kern.OSTask; import priv.bajdcc.LALR1.interpret.os.proc.OSSchd; import priv.bajdcc.LALR1.interpret.os.task.TKSystem; +import priv.bajdcc.LALR1.interpret.os.task.TKUI; import priv.bajdcc.LALR1.interpret.os.task.TKUtil; import priv.bajdcc.LALR1.interpret.os.user.UserMain; import priv.bajdcc.LALR1.interpret.os.user.routine.*; @@ -37,7 +38,7 @@ public UIPanel getPanel() { public UIMainFrame() { panel = new UIPanel(); - this.setTitle("jMiniLang OS Window"); + this.setTitle("jMiniLang Command Window"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setPreferredSize(new Dimension(800, 600)); this.setContentPane(panel); @@ -70,6 +71,7 @@ private static void startOS(UIGraphics g) { // TASK new TKSystem(), new TKUtil(), + new TKUI(), // USER new UserMain(), // USER ROUTINE diff --git a/src/priv/bajdcc/LALR1/ui/UIPanel.java b/src/priv/bajdcc/LALR1/ui/UIPanel.java index a43c7af..ef08da0 100644 --- a/src/priv/bajdcc/LALR1/ui/UIPanel.java +++ b/src/priv/bajdcc/LALR1/ui/UIPanel.java @@ -16,7 +16,6 @@ public class UIPanel extends JPanel { private UIGraphics graphics; - private JTextField input; private ModuleUI moduleUI; public UIPanel() { diff --git a/src/priv/bajdcc/LALR1/ui/UIRemotePanel.java b/src/priv/bajdcc/LALR1/ui/UIRemotePanel.java new file mode 100644 index 0000000..7fe4ce5 --- /dev/null +++ b/src/priv/bajdcc/LALR1/ui/UIRemotePanel.java @@ -0,0 +1,29 @@ +package priv.bajdcc.LALR1.ui; + +import priv.bajdcc.LALR1.ui.drawing.UIGraphics; + +import javax.swing.*; +import java.awt.*; + +/** + * 【界面】远程渲染界面 + * + * @author bajdcc + */ +public class UIRemotePanel extends JPanel { + + private UIGraphics graphics; + + public UIRemotePanel() { + this.graphics = new UIGraphics(800, 600, 70, 23, 11, 25, 1); + this.setFocusable(true); + } + + public UIGraphics getUIGraphics() { + return graphics; + } + + public void paint(Graphics g) { + graphics.paint((Graphics2D) g); + } +} diff --git a/src/priv/bajdcc/LALR1/ui/UIRemoteWindow.java b/src/priv/bajdcc/LALR1/ui/UIRemoteWindow.java new file mode 100644 index 0000000..91fc704 --- /dev/null +++ b/src/priv/bajdcc/LALR1/ui/UIRemoteWindow.java @@ -0,0 +1,47 @@ +package priv.bajdcc.LALR1.ui; + +import priv.bajdcc.LALR1.ui.drawing.UIGraphics; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.WindowEvent; + +/** + * 【界面】远程用户界面 + * + * @author bajdcc + */ +public class UIRemoteWindow extends JFrame { + private UIRemotePanel panel; + + public UIRemotePanel getPanel() { + return panel; + } + + public UIRemoteWindow() { + panel = new UIRemotePanel(); + this.setTitle("jMiniOS Remote Window"); + this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); + this.setPreferredSize(new Dimension(800, 600)); + this.setContentPane(panel); + this.pack(); + this.setLocationRelativeTo(null); + this.setResizable(false); + this.setVisible(true); + this.setTimer(); + } + + private void close() { + this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING)); + } + + public UIGraphics getUIGraphics() { + return this.panel.getUIGraphics(); + } + + private void setTimer() { + new Timer(33, e -> { + panel.repaint(); + }).start(); + } +}