Search in sources :

Example 1 with RuntimeCodePage

use of priv.bajdcc.LALR1.grammar.runtime.RuntimeCodePage in project jMiniLang by bajdcc.

the class RuntimeMachine method opCallExtern.

@Override
public void opCallExtern(boolean invoke) throws Exception {
    int idx = loadInt();
    String name = "";
    if (invoke) {
        RuntimeStack itStack = stack;
        RuntimeObject obj = null;
        while (obj == null && itStack != null) {
            obj = itStack.findVariable(pageName, idx);
            itStack = itStack.prev;
        }
        if (obj == null) {
            err(RuntimeError.WRONG_LOAD_EXTERN, String.valueOf(idx));
        }
        if (obj.getType() == RuntimeObjectType.kFunc) {
            RuntimeFuncObject func = (RuntimeFuncObject) obj.getObj();
            Map<Integer, RuntimeObject> env = func.getEnv();
            if (env != null) {
                for (Entry<Integer, RuntimeObject> entry : env.entrySet()) {
                    int id = entry.getKey();
                    RuntimeObject o = entry.getValue();
                    if (o != null) {
                        if (o.getSymbol() == null)
                            o.setSymbol(currentPage.getData().get(id));
                        o.setReadonly(false);
                    }
                    stack.storeClosure(id, o);
                }
                stack.pushData(obj);
            }
            int address = func.getAddr();
            stack.opCall(address, func.getPage(), stack.reg.execId, pageName, pageMap.get(func.getPage()).getInfo().getFuncNameByAddress(address));
            stack.reg.execId = address;
            stack.reg.pageId = func.getPage();
            switchPage();
            pop();
            return;
        } else if (obj.getType() == RuntimeObjectType.kString) {
            name = obj.getObj().toString();
        } else {
            err(RuntimeError.WRONG_LOAD_EXTERN, obj.toString());
        }
    } else {
        RuntimeObject obj = fetchFromGlobalData(idx);
        name = obj.getObj().toString();
    }
    List<RuntimeCodePage> refers = pageRefer.get(pageName);
    for (RuntimeCodePage page : refers) {
        int address = page.getInfo().getAddressOfExportFunc(name);
        if (address != -1) {
            String jmpPage = page.getInfo().getDataMap().get("name").toString();
            stack.opCall(address, jmpPage, stack.reg.execId, stack.reg.pageId, name);
            stack.reg.execId = address;
            stack.reg.pageId = jmpPage;
            switchPage();
            return;
        }
    }
    for (RuntimeCodePage page : refers) {
        IRuntimeDebugExec exec = page.getInfo().getExecCallByName(name);
        if (exec != null) {
            int argsCount = stack.getFuncArgsCount();
            RuntimeObjectType[] types = exec.getArgsType();
            if ((types == null && argsCount != 0) || (types != null && types.length != argsCount)) {
                err(RuntimeError.WRONG_ARGCOUNT, name + " " + String.valueOf(argsCount));
            }
            List<RuntimeObject> args = new ArrayList<>();
            for (int i = 0; i < argsCount; i++) {
                RuntimeObjectType type = types[i];
                RuntimeObject objParam = stack.loadFuncArgs(i);
                if (type != RuntimeObjectType.kObject) {
                    RuntimeObjectType objType = objParam.getType();
                    if (objType != type) {
                        Token token = Token.createFromObject(objParam.getObj());
                        TokenType objTokenType = RuntimeObject.toTokenType(type);
                        if (objTokenType == TokenType.ERROR) {
                            err(RuntimeError.WRONG_ARGTYPE, name + " " + objTokenType.getName());
                        }
                        if (!TokenTools.promote(objTokenType, token)) {
                            err(RuntimeError.UNDEFINED_CONVERT, name + " " + token.toString() + " " + objTokenType.getName());
                        } else {
                            objParam.setObj(token.object);
                        }
                    }
                }
                args.add(objParam);
            }
            stack.opCall(stack.reg.execId, stack.reg.pageId, stack.reg.execId, stack.reg.pageId, name);
            RuntimeObject retVal = exec.ExternalProcCall(args, this);
            if (retVal == null) {
                store(new RuntimeObject(null));
            } else {
                store(retVal);
            }
            opReturn();
            return;
        }
    }
    err(RuntimeError.WRONG_LOAD_EXTERN, name);
}
Also used : Token(priv.bajdcc.util.lexer.token.Token) BigInteger(java.math.BigInteger) TokenType(priv.bajdcc.util.lexer.token.TokenType) RuntimeFuncObject(priv.bajdcc.LALR1.grammar.runtime.data.RuntimeFuncObject)

Example 2 with RuntimeCodePage

use of priv.bajdcc.LALR1.grammar.runtime.RuntimeCodePage in project jMiniLang by bajdcc.

the class RuntimeProcess method getPage.

public RuntimeCodePage getPage(String name) throws Exception {
    String vfs = null;
    if (arrCodes.containsKey(name) || (vfs = service.getFileService().getVfs(name)) != null) {
        String code = vfs == null ? arrCodes.get(name) : vfs;
        if (arrPages.containsKey(code)) {
            return arrPages.get(code);
        } else {
            logger.debug("Loading page: " + name);
            try {
                Grammar grammar = new Grammar(code);
                RuntimeCodePage page = grammar.getCodePage();
                service.getFileService().addVfs(name, code);
                arrPages.put(name, page);
                return page;
            } catch (SyntaxException e) {
                String filename = pageFileMap.get(name);
                e.setFileName(filename == null ? "Unknown Source" : (filename + ".txt"));
                e.setPageName(name);
                System.err.println("#PAGE ERROR# --> " + name);
                throw e;
            }
        }
    }
    return null;
}
Also used : SyntaxException(priv.bajdcc.LALR1.syntax.handler.SyntaxException) Grammar(priv.bajdcc.LALR1.grammar.Grammar)

Example 3 with RuntimeCodePage

use of priv.bajdcc.LALR1.grammar.runtime.RuntimeCodePage in project jMiniLang by bajdcc.

the class TestGrammar2 method main.

public static void main(String[] args) {
    try {
        String[] exprs = new String[] { "var a = 1;", "var a = 4 + 4;", "var a = !(!true * (++4 / !4 + 4 ? 5 & 6 && true ^ !\"ddd\" | 'r' : 8.0+9*9));", "var a = 3.0 + 4.0 / 7 * 8.9;", "var a = 3000.0 << ++2.0;", "var a = true << 9 ? 1 + 3 : 2;", "var a = 5;let a = a + 4 + a ? 3 : 5;var t = func main1(){};", "var d= func [\"d\",\"t\"] ~()->a;", "var d = func ~()->4;", "var a = call (func ~(a) {call (func b(c){var b = '5'+ 66; call b(666);})();}) (6);", "var a = call (func ~(a) {\n" + "call (func b(c){\n" + "var c = '5'+ 1;\n" + "var d = c;\n" + "//call b(666);\n" + "let c = 7;\n" + "})(7);\n" + "}) (44);", "var a = call\n" + "(func ~(x, y) -> x + y)\n" + " (1, 2);\n", "var d = 4;" + "var c = d || 6;" };
        /*
			 * BufferedReader br = new BufferedReader(new
			 * FileReader("E:/http.c")); String line = ""; StringBuffer sb = new
			 * StringBuffer(); while ((line = br.readLine()) != null) {
			 * sb.append(line + System.lineSeparator()); }
			 * br.close();
			 */
        Grammar grammar = new Grammar(exprs[exprs.length - 1]);
        System.out.println(grammar.toString());
        RuntimeCodePage page = grammar.getCodePage();
        System.out.println(page.toString());
        RuntimeMachine machine = new RuntimeMachine();
        machine.run("test1", page);
    // FileWriter fw = new FileWriter("E:/testgrammar.txt");
    // fw.append(grammar.toString());
    // fw.close();
    } catch (RegexException e) {
        System.err.println(e.getPosition() + "," + e.getMessage());
        e.printStackTrace();
    } catch (SyntaxException e) {
        System.err.println(e.getPosition() + "," + e.getMessage() + " " + e.getInfo());
        e.printStackTrace();
    // } catch (IOException e) {
    // System.err.println(e.getMessage());
    // e.printStackTrace();
    } catch (RuntimeException e) {
        System.err.println(e.getPosition() + ": " + e.getInfo());
        e.printStackTrace();
    // } catch (IOException e) {
    // System.err.println(e.getMessage());
    // e.printStackTrace();
    } catch (Exception e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
    // } catch (IOException e) {
    // System.err.println(e.getMessage());
    // e.printStackTrace();
    }
}
Also used : RuntimeException(priv.bajdcc.LALR1.grammar.runtime.RuntimeException) SyntaxException(priv.bajdcc.LALR1.syntax.handler.SyntaxException) RegexException(priv.bajdcc.util.lexer.error.RegexException) RuntimeMachine(priv.bajdcc.LALR1.grammar.runtime.RuntimeMachine) Grammar(priv.bajdcc.LALR1.grammar.Grammar) RuntimeCodePage(priv.bajdcc.LALR1.grammar.runtime.RuntimeCodePage) SyntaxException(priv.bajdcc.LALR1.syntax.handler.SyntaxException) RuntimeException(priv.bajdcc.LALR1.grammar.runtime.RuntimeException) RegexException(priv.bajdcc.util.lexer.error.RegexException)

Example 4 with RuntimeCodePage

use of priv.bajdcc.LALR1.grammar.runtime.RuntimeCodePage in project jMiniLang by bajdcc.

the class TestGrammar3 method main.

public static void main(String[] args) {
    try {
        String a = "\n" + "var g_a = func ~(x, y) { return x + y;};\n" + "export \"g_a\";";
        String b = "import \"test1\";\n" + "var d = call g_a(1,2);\n" + "var c = g_gk;\n" + "call g_print(c);\n" + "var t = call g_print(c);";
        /*
			 * BufferedReader br = new BufferedReader(new
			 * FileReader("E:/http.c")); String line = ""; StringBuffer sb = new
			 * StringBuffer(); while ((line = br.readLine()) != null) {
			 * sb.append(line + System.lineSeparator()); }
			 * br.close();
			 */
        Grammar grammar = new Grammar(a);
        System.out.println(grammar.toString());
        RuntimeCodePage page = grammar.getCodePage();
        System.out.println(page.toString());
        RuntimeMachine machine = new RuntimeMachine();
        machine.run("test1", page);
        Grammar grammar2 = new Grammar(b);
        System.out.println(grammar2.toString());
        RuntimeCodePage page2 = grammar2.getCodePage();
        page2.getInfo().addExternalValue("g_gk", () -> new RuntimeObject("abc"));
        page2.getInfo().addExternalFunc("g_print", new IRuntimeDebugExec() {

            @Override
            public String getDoc() {
                return "Print";
            }

            @Override
            public RuntimeObjectType[] getArgsType() {
                return new RuntimeObjectType[] { RuntimeObjectType.kObject };
            }

            @Override
            public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
                System.out.println(args.get(0).getObj());
                return null;
            }
        });
        System.out.println(page2.toString());
        machine.run("test2", page2);
    // FileWriter fw = new FileWriter("E:/testgrammar.txt");
    // fw.append(grammar.toString());
    // fw.close();
    } catch (RegexException e) {
        System.err.println(e.getPosition() + "," + e.getMessage());
        e.printStackTrace();
    } catch (SyntaxException e) {
        System.err.println(e.getPosition() + "," + e.getMessage() + " " + e.getInfo());
        e.printStackTrace();
    // } catch (IOException e) {
    // System.err.println(e.getMessage());
    // e.printStackTrace();
    } catch (RuntimeException e) {
        System.err.println(e.getPosition() + ": " + e.getInfo());
        e.printStackTrace();
    // } catch (IOException e) {
    // System.err.println(e.getMessage());
    // e.printStackTrace();
    } catch (Exception e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
    // } catch (IOException e) {
    // System.err.println(e.getMessage());
    // e.printStackTrace();
    }
}
Also used : RuntimeMachine(priv.bajdcc.LALR1.grammar.runtime.RuntimeMachine) Grammar(priv.bajdcc.LALR1.grammar.Grammar) RuntimeCodePage(priv.bajdcc.LALR1.grammar.runtime.RuntimeCodePage) IRuntimeStatus(priv.bajdcc.LALR1.grammar.runtime.IRuntimeStatus) SyntaxException(priv.bajdcc.LALR1.syntax.handler.SyntaxException) RuntimeException(priv.bajdcc.LALR1.grammar.runtime.RuntimeException) RegexException(priv.bajdcc.util.lexer.error.RegexException) RuntimeException(priv.bajdcc.LALR1.grammar.runtime.RuntimeException) RuntimeObject(priv.bajdcc.LALR1.grammar.runtime.RuntimeObject) SyntaxException(priv.bajdcc.LALR1.syntax.handler.SyntaxException) RegexException(priv.bajdcc.util.lexer.error.RegexException) IRuntimeDebugExec(priv.bajdcc.LALR1.grammar.runtime.IRuntimeDebugExec)

Example 5 with RuntimeCodePage

use of priv.bajdcc.LALR1.grammar.runtime.RuntimeCodePage in project jMiniLang by bajdcc.

the class ModuleClass method getCodePage.

@Override
public RuntimeCodePage getCodePage() throws Exception {
    if (runtimeCodePage != null)
        return runtimeCodePage;
    String base = ResourceLoader.load(getClass());
    Grammar grammar = new Grammar(base);
    RuntimeCodePage page = grammar.getCodePage();
    IRuntimeDebugInfo info = page.getInfo();
    return runtimeCodePage = page;
}
Also used : Grammar(priv.bajdcc.LALR1.grammar.Grammar) RuntimeCodePage(priv.bajdcc.LALR1.grammar.runtime.RuntimeCodePage) IRuntimeDebugInfo(priv.bajdcc.LALR1.grammar.runtime.IRuntimeDebugInfo)

Aggregations

Grammar (priv.bajdcc.LALR1.grammar.Grammar)32 RuntimeCodePage (priv.bajdcc.LALR1.grammar.runtime.RuntimeCodePage)21 RuntimeException (priv.bajdcc.LALR1.grammar.runtime.RuntimeException)17 SyntaxException (priv.bajdcc.LALR1.syntax.handler.SyntaxException)16 RegexException (priv.bajdcc.util.lexer.error.RegexException)16 ByteArrayInputStream (java.io.ByteArrayInputStream)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)14 Interpreter (priv.bajdcc.LALR1.interpret.Interpreter)14 IRuntimeDebugInfo (priv.bajdcc.LALR1.grammar.runtime.IRuntimeDebugInfo)5 BigInteger (java.math.BigInteger)2 RuntimeMachine (priv.bajdcc.LALR1.grammar.runtime.RuntimeMachine)2 RuntimeObject (priv.bajdcc.LALR1.grammar.runtime.RuntimeObject)2 RuntimeFuncObject (priv.bajdcc.LALR1.grammar.runtime.data.RuntimeFuncObject)2 BufferedReader (java.io.BufferedReader)1 FileReader (java.io.FileReader)1 Scanner (java.util.Scanner)1 IRuntimeDebugExec (priv.bajdcc.LALR1.grammar.runtime.IRuntimeDebugExec)1 IRuntimeStatus (priv.bajdcc.LALR1.grammar.runtime.IRuntimeStatus)1 RuntimeArray (priv.bajdcc.LALR1.grammar.runtime.data.RuntimeArray)1 RuntimeMap (priv.bajdcc.LALR1.grammar.runtime.data.RuntimeMap)1