-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Commands: echo, dup, pipe, grep. GUI Features: - input - output - cls
- Loading branch information
Showing
9 changed files
with
499 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
Oops, something went wrong.