Skip to content

Commit

Permalink
ADD: Support GUI
Browse files Browse the repository at this point in the history
Commands: echo, dup, pipe, grep.

GUI Features:
- input
- output
- cls
  • Loading branch information
bajdcc committed Dec 21, 2016
1 parent 28a6ad2 commit 51717ed
Show file tree
Hide file tree
Showing 9 changed files with 499 additions and 25 deletions.
17 changes: 7 additions & 10 deletions src/priv/bajdcc/LALR1/grammar/runtime/RuntimeMachine.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
package priv.bajdcc.LALR1.grammar.runtime;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Stack;

import priv.bajdcc.LALR1.grammar.Grammar;
import priv.bajdcc.LALR1.grammar.runtime.RuntimeException.RuntimeError;
import priv.bajdcc.LALR1.grammar.runtime.data.RuntimeArray;
Expand All @@ -23,6 +13,12 @@
import priv.bajdcc.util.lexer.token.Token;
import priv.bajdcc.util.lexer.token.TokenType;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;
import java.util.*;
import java.util.Map.Entry;

/**
* 【虚拟机】运行时自动机
*
Expand Down Expand Up @@ -51,6 +47,7 @@ public RuntimeMachine() throws Exception {
ModuleList.getInstance(),
ModuleString.getInstance(),
ModuleProc.getInstance(),
ModuleUI.getInstance(),
};
}

Expand Down
155 changes: 155 additions & 0 deletions src/priv/bajdcc/LALR1/interpret/module/ModuleUI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package priv.bajdcc.LALR1.interpret.module;

import priv.bajdcc.LALR1.grammar.Grammar;
import priv.bajdcc.LALR1.grammar.runtime.*;
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 ModuleUI implements IInterpreterModule {

private static final int INPUT_TIME = 10;
private static ModuleUI instance = new ModuleUI();
private UIGraphics graphics;
private Queue<Character> queue = new LinkedBlockingDeque<>(1024);
private Queue<Character> queueDisplay = new ArrayDeque<>();
private StringBuilder sb = new StringBuilder();

public void setGraphics(UIGraphics graphics) {
this.graphics = graphics;
}

public void addInputChar(char c) {
queue.add(c);
}

public static ModuleUI getInstance() {
return instance;
}

@Override
public String getModuleName() {
return "sys.ui";
}

@Override
public RuntimeCodePage getCodePage() throws Exception {
String base = "import \"sys.base\";\n" +
"import \"sys.proc\";\n" +
"import \"sys.string\";\n" +
"var g_ui_print = func ~(str) {\n" +
" var ui_int = call g_create_pipe(\"int#12\");\n" +
" foreach (var c : call g_range_string(str)) {\n" +
" call g_write_pipe(ui_int, c);\n" +
" }\n" +
"};\n" +
"export \"g_ui_print\";\n" +
"var g_ui_printn = func ~(str) {\n" +
" call g_ui_print(str);\n" +
" call g_ui_println();\n" +
"};\n" +
"export \"g_ui_printn\";\n" +
"var g_ui_println = func ~() {\n" +
" call g_ui_print(g_endl);\n" +
"};\n" +
"export \"g_ui_println\";\n" +
"var g_ui_input = func ~() {\n" +
" for (;;) {\n" +
" var s = call g_ui_input_internal();\n" +
" if (!call g_is_null(s)) {\n" +
" call g_ui_println();\n" +
" return s;\n" +
" }\n" +
" var c = call g_ui_print_input();\n" +
" if (!call g_is_null(c)) {\n" +
" call g_ui_print(c);\n" +
" }\n" +
" }\n" +
"};\n" +
"export \"g_ui_input\";\n";

Grammar grammar = new Grammar(base);
RuntimeCodePage page = grammar.getCodePage();
IRuntimeDebugInfo info = page.getInfo();
buildUIMethods(info);

return page;
}

private void buildUIMethods(IRuntimeDebugInfo info) {
info.addExternalFunc("g_ui_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;
}
});
info.addExternalFunc("g_ui_input_internal", new IRuntimeDebugExec() {
@Override
public String getDoc() {
return "显示输入";
}

@Override
public RuntimeObjectType[] getArgsType() {
return null;
}

@Override
public RuntimeObject ExternalProcCall(List<RuntimeObject> args,
IRuntimeStatus status) throws Exception {
status.getService().getProcessService().sleep(status.getPid(), INPUT_TIME);
Character c = queue.poll();
if (c == null) {
return null;
}
if (c.equals('\n')) {
String str = sb.toString();
sb = new StringBuilder();
queueDisplay.clear();
return new RuntimeObject(str);
} else {
queueDisplay.add(c);
sb.append(c);
}
return null;
}
});
info.addExternalFunc("g_ui_print_input", new IRuntimeDebugExec() {
@Override
public String getDoc() {
return "实时显示输入";
}

@Override
public RuntimeObjectType[] getArgsType() {
return null;
}

@Override
public RuntimeObject ExternalProcCall(List<RuntimeObject> args,
IRuntimeStatus status) throws Exception {
return new RuntimeObject(queueDisplay.poll());
}
});
}
}
5 changes: 5 additions & 0 deletions src/priv/bajdcc/LALR1/interpret/os/kern/OSIrq.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public String getCode() {
return "import \"sys.base\";\n" +
"import \"sys.list\";\n" +
"import \"sys.proc\";\n" +
"import \"sys.ui\";\n" +
"var interrupt_num = " + INT_NUM + ";\n" +
"var int_table = [];\n" +
"foreach (var i : call g_range(0, interrupt_num - 1)) {\n" +
Expand Down Expand Up @@ -74,6 +75,10 @@ public String getCode() {
" }\n" +
"};\n" +
"call add_int_proc(10, schd_handler);\n" +
"var print_handler = func ~(ch) {\n" +
" call g_ui_print_internal(ch);\n" +
"};\n" +
"call add_int_proc(12, print_handler);\n" +
"";
}
}
31 changes: 16 additions & 15 deletions src/priv/bajdcc/LALR1/interpret/os/user/routine/URShell.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@ public String getCode() {
"import \"sys.list\";\n" +
"import \"sys.string\";\n" +
"import \"sys.proc\";\n" +
"import \"sys.ui\";\n" +
"\n" +
"// PRINT WELCOME\n" +
"var welcome = func [\"WELCOME\"] ~() {\n" +
" call g_println();\n" +
" call g_printn(\" ________ ________ ___ ________ ________ ________ \");\n" +
" call g_printn(\"|\\\\ __ \\\\|\\\\ __ \\\\ |\\\\ \\\\|\\\\ ___ \\\\|\\\\ ____\\\\|\\\\ ____\\\\ \");\n" +
" call g_printn(\"\\\\ \\\\ \\\\|\\\\ /\\\\ \\\\ \\\\|\\\\ \\\\ \\\\ \\\\ \\\\ \\\\ \\\\_|\\\\ \\\\ \\\\ \\\\___|\\\\ \\\\ \\\\___| \");\n" +
" call g_printn(\" \\\\ \\\\ __ \\\\ \\\\ __ \\\\ __ \\\\ \\\\ \\\\ \\\\ \\\\ \\\\\\\\ \\\\ \\\\ \\\\ \\\\ \\\\ \\\\ \");\n" +
" call g_printn(\" \\\\ \\\\ \\\\|\\\\ \\\\ \\\\ \\\\ \\\\ \\\\|\\\\ \\\\\\\\_\\\\ \\\\ \\\\ \\\\_\\\\\\\\ \\\\ \\\\ \\\\____\\\\ \\\\ \\\\____ \");\n" +
" call g_printn(\" \\\\ \\\\_______\\\\ \\\\__\\\\ \\\\__\\\\ \\\\________\\\\ \\\\_______\\\\ \\\\_______\\\\ \\\\_______\\\\\");\n" +
" call g_printn(\" \\\\|_______|\\\\|__|\\\\|__|\\\\|________|\\\\|_______|\\\\|_______|\\\\|_______|\");\n" +
" call g_println();\n" +
" call g_ui_println();\n" +
" call g_ui_printn(\" ________ ________ ___ ________ ________ ________ \");\n" +
" call g_ui_printn(\"|\\\\ __ \\\\|\\\\ __ \\\\ |\\\\ \\\\|\\\\ ___ \\\\|\\\\ ____\\\\|\\\\ ____\\\\ \");\n" +
" call g_ui_printn(\"\\\\ \\\\ \\\\|\\\\ /\\\\ \\\\ \\\\|\\\\ \\\\ \\\\ \\\\ \\\\ \\\\ \\\\_|\\\\ \\\\ \\\\ \\\\___|\\\\ \\\\ \\\\___| \");\n" +
" call g_ui_printn(\" \\\\ \\\\ __ \\\\ \\\\ __ \\\\ __ \\\\ \\\\ \\\\ \\\\ \\\\ \\\\\\\\ \\\\ \\\\ \\\\ \\\\ \\\\ \\\\ \");\n" +
" call g_ui_printn(\" \\\\ \\\\ \\\\|\\\\ \\\\ \\\\ \\\\ \\\\ \\\\|\\\\ \\\\\\\\_\\\\ \\\\ \\\\ \\\\_\\\\\\\\ \\\\ \\\\ \\\\____\\\\ \\\\ \\\\____ \");\n" +
" call g_ui_printn(\" \\\\ \\\\_______\\\\ \\\\__\\\\ \\\\__\\\\ \\\\________\\\\ \\\\_______\\\\ \\\\_______\\\\ \\\\_______\\\\\");\n" +
" call g_ui_printn(\" \\\\|_______|\\\\|__|\\\\|__|\\\\|________|\\\\|_______|\\\\|_______|\\\\|_______|\");\n" +
" call g_ui_println();\n" +
"};\n" +
"call g_join_process(call g_create_user_process(welcome));\n" +
"\n" +
Expand All @@ -50,7 +51,7 @@ public String getCode() {
" var child = call g_load_user_x(path);\n" +
" call g_start_share(\"PID#\" + child, share);\n" +
" if (child+1 == 0) {\n" +
" call g_printn(\"Cannot execute '\"+path+\"'.\");\n" +
" call g_ui_printn(\"Cannot execute '\"+path+\"'.\");\n" +
" var p = call g_wait_pipe(\"PIPEIN#\" + parent);\n" +
" call g_sleep(50);\n" +
" call g_destroy_pipe(p);\n" +
Expand Down Expand Up @@ -85,7 +86,7 @@ public String getCode() {
" var path = \"/usr/p/\" + exec;\n" +
" var child = call g_load_user_x(path);\n" +
" if (child+1 == 0) {\n" +
" call g_printn(\"Cannot execute '\"+path+\"'.\");\n" +
" call g_ui_printn(\"Cannot execute '\"+path+\"'.\");\n" +
" return;\n" +
" }\n" +
" call g_start_share(\"PID#\" + child, share);\n" +
Expand All @@ -96,7 +97,7 @@ public String getCode() {
" call g_map_put(_args_, \"parent\", child);\n" +
" call g_create_user_process_args(parse, _args_);\n" +
" }\n" +
" var f = func ~(ch) -> call g_print(ch);\n" +
" var f = func ~(ch) -> call g_ui_print(ch);\n" +
" var out = call g_create_pipe(\"PIPEOUT#\" + child);\n" +
" call g_read_pipe(out, f);\n" +
"};\n" +
Expand All @@ -105,11 +106,11 @@ public String getCode() {
"var get_input = func [\"INPUT\"] ~(arg) {\n" +
" var this = call g_array_get(arg, 0);\n" +
" var parse = call g_array_get(arg, 1);\n" +
" call g_print(\"$ \");\n" +
" var cmd = call g_stdin_read_line();\n" +
" call g_ui_print(\"$ \");\n" +
" var cmd = call g_ui_input();\n" +
" let cmd = call g_string_trim(cmd);\n" +
" if (call g_string_length(cmd) == 0) {\n" +
" call g_printn(\"Error: no cmd\");\n" +
" call g_ui_printn(\"Error: no cmd\");\n" +
" return;\n" +
" }\n" +
" if (cmd == \"exit\") {\n" +
Expand Down
115 changes: 115 additions & 0 deletions src/priv/bajdcc/LALR1/ui/UIMainFrame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package priv.bajdcc.LALR1.ui;

import priv.bajdcc.LALR1.grammar.Grammar;
import priv.bajdcc.LALR1.grammar.runtime.RuntimeCodePage;
import priv.bajdcc.LALR1.grammar.runtime.RuntimeException;
import priv.bajdcc.LALR1.interpret.Interpreter;
import priv.bajdcc.LALR1.interpret.os.IOSCodePage;
import priv.bajdcc.LALR1.interpret.os.kern.OSEntry;
import priv.bajdcc.LALR1.interpret.os.kern.OSIrq;
import priv.bajdcc.LALR1.interpret.os.proc.OSSchd;
import priv.bajdcc.LALR1.interpret.os.user.UserMain;
import priv.bajdcc.LALR1.interpret.os.user.routine.*;
import priv.bajdcc.LALR1.syntax.handler.SyntaxException;
import priv.bajdcc.LALR1.ui.drawing.UIGraphics;
import priv.bajdcc.util.lexer.error.RegexException;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

/**
* 【界面】窗口
*
* @author bajdcc
*/
public class UIMainFrame extends JFrame {
private UIPanel panel;

public UIPanel getPanel() {
return panel;
}

public UIMainFrame() {
panel = new UIPanel();
this.setTitle("jMiniLang OS Window");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setPreferredSize(new Dimension(800, 600));
this.setContentPane(panel);
this.pack();
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setVisible(true);
}

public static void main(String[] args) {
UIMainFrame frame = new UIMainFrame();
frame.setTimer();
startOS(frame.getPanel().getUIGraphics());
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
}

private void setTimer() {
new Timer(33, e -> {
panel.repaint();
}).start();
}

private static void startOS(UIGraphics g) {
IOSCodePage pages[] = new IOSCodePage[]{
// OS
new OSEntry(),
new OSIrq(),
new OSSchd(),
// USER
new UserMain(),
// USER ROUTINE
new URShell(),
new UREcho(),
new URPipe(),
new URDup(),
new URGrep(),
};

try {
String code = "import \"sys.base\";\n" +
"import \"sys.proc\";\n" +
"call g_load_sync_x(\"/kern/entry\");\n";

Interpreter interpreter = new Interpreter();

for (IOSCodePage page : pages) {
interpreter.load(page.getName(), page.getCode());
}

Grammar grammar = new Grammar(code);
//System.out.println(grammar.toString());
RuntimeCodePage page = grammar.getCodePage();
//System.out.println(page.toString());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
RuntimeCodePage.exportFromStream(page, baos);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
interpreter.run("@main", bais);

} catch (RegexException e) {
System.err.println();
System.err.println(e.getPosition() + "," + e.getMessage());
e.printStackTrace();
} catch (SyntaxException e) {
System.err.println();
System.err.println(e.getPosition() + "," + e.getMessage() + " "
+ e.getInfo());
e.printStackTrace();
} catch (RuntimeException e) {
System.err.println();
System.err.println(e.getPosition() + ": " + e.getInfo());
e.printStackTrace();
} catch (Exception e) {
System.err.println();
System.err.println(e.getMessage());
e.printStackTrace();
}
}
}
Loading

0 comments on commit 51717ed

Please sign in to comment.