Skip to content

Commit

Permalink
ADD: Add remote
Browse files Browse the repository at this point in the history
- Add module: ModuleRemote
- Add task: TKUI
- Add window: Remote window
  • Loading branch information
bajdcc committed Apr 20, 2017
1 parent c052b8e commit 8861c52
Show file tree
Hide file tree
Showing 10 changed files with 296 additions and 13 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 12 additions & 11 deletions src/priv/bajdcc/LALR1/grammar/runtime/RuntimeMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public RuntimeMachine() throws Exception {
ModuleProc.getInstance(),
ModuleUI.getInstance(),
ModuleTask.getInstance(),
ModuleRemote.getInstance(),
};
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}

Expand Down
123 changes: 123 additions & 0 deletions src/priv/bajdcc/LALR1/interpret/module/ModuleRemote.java
Original file line number Diff line number Diff line change
@@ -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<Character> queue = new LinkedBlockingDeque<>(1024);
private Queue<Character> 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<RuntimeObject> 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<RuntimeObject> args,
IRuntimeStatus status) throws Exception {
graphics.drawText((char) args.get(0).getObj());
return null;
}
});
}
}
6 changes: 6 additions & 0 deletions src/priv/bajdcc/LALR1/interpret/os/kern/OSIrq.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ 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" +
"foreach (var i : call g_range(0, interrupt_num - 1)) {\n" +
" 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" +
Expand Down Expand Up @@ -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" +
"";
}
}
1 change: 1 addition & 0 deletions src/priv/bajdcc/LALR1/interpret/os/kern/OSTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -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" +
Expand Down
69 changes: 69 additions & 0 deletions src/priv/bajdcc/LALR1/interpret/os/task/TKUI.java
Original file line number Diff line number Diff line change
@@ -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";
}
}
4 changes: 3 additions & 1 deletion src/priv/bajdcc/LALR1/ui/UIMainFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -70,6 +71,7 @@ private static void startOS(UIGraphics g) {
// TASK
new TKSystem(),
new TKUtil(),
new TKUI(),
// USER
new UserMain(),
// USER ROUTINE
Expand Down
1 change: 0 additions & 1 deletion src/priv/bajdcc/LALR1/ui/UIPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
public class UIPanel extends JPanel {

private UIGraphics graphics;
private JTextField input;
private ModuleUI moduleUI;

public UIPanel() {
Expand Down
29 changes: 29 additions & 0 deletions src/priv/bajdcc/LALR1/ui/UIRemotePanel.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
47 changes: 47 additions & 0 deletions src/priv/bajdcc/LALR1/ui/UIRemoteWindow.java
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 8861c52

Please sign in to comment.