Search in sources :

Example 1 with Token

use of priv.bajdcc.util.lexer.token.Token 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 Token

use of priv.bajdcc.util.lexer.token.Token in project jMiniLang by bajdcc.

the class ManageScopeSymbol method getEntryToken.

@Override
public Token getEntryToken() {
    Token token = new Token();
    token.kToken = TokenType.ID;
    token.object = getEntryName();
    token.position = new Position();
    return token;
}
Also used : Position(priv.bajdcc.util.Position) Token(priv.bajdcc.util.lexer.token.Token)

Example 3 with Token

use of priv.bajdcc.util.lexer.token.Token in project jMiniLang by bajdcc.

the class SemanticHandler method initializeAction.

/**
 * 初始化动作
 */
private void initializeAction() {
    /* 进入块 */
    mapSemanticAction.put("do_enter_scope", (indexed, manage, access, recorder) -> manage.getManageScopeService().enterScope());
    /* 离开块 */
    mapSemanticAction.put("do_leave_scope", (indexed, manage, access, recorder) -> manage.getManageScopeService().leaveScope());
    /* 声明过程名 */
    mapSemanticAction.put("predeclear_funcname", (indexed, manage, access, recorder) -> {
        Token token = access.relativeGet(0);
        String funcName = token.toRealString();
        if (token.kToken == TokenType.ID) {
            if (manage.getQueryScopeService().getEntryName().equals(funcName)) {
                recorder.add(SemanticError.DUP_ENTRY, token);
            } else if (manage.getQueryScopeService().isRegisteredFunc(funcName)) {
                recorder.add(SemanticError.DUP_FUNCNAME, token);
            }
        }
        Function func = new Function();
        func.setName(token);
        manage.getManageScopeService().registerFunc(func);
        if (token.kToken != TokenType.ID) {
            token.object = func.getRealName();
            token.kToken = TokenType.ID;
        }
    });
    /* 声明变量名 */
    mapSemanticAction.put("declear_variable", (indexed, manage, access, recorder) -> {
        KeywordType spec = (KeywordType) access.relativeGet(-1).object;
        Token token = access.relativeGet(0);
        String name = token.toRealString();
        if (spec == KeywordType.VARIABLE) {
            if (!manage.getQueryScopeService().findDeclaredSymbol(name)) {
                if (!manage.getQueryScopeService().isRegisteredFunc(name)) {
                    manage.getManageScopeService().registerSymbol(name);
                } else {
                    recorder.add(SemanticError.VAR_FUN_CONFLICT, token);
                }
            } else if (!TokenTools.isExternalName(name) && manage.getQueryScopeService().findDeclaredSymbolDirect(name)) {
                recorder.add(SemanticError.VARIABLE_REDECLARAED, token);
            }
        } else if (spec == KeywordType.LET) {
            if (!manage.getQueryScopeService().findDeclaredSymbol(name)) {
                recorder.add(SemanticError.VARIABLE_NOT_DECLARAED, token);
            }
        }
    });
    /* 声明参数 */
    mapSemanticAction.put("declear_param", (indexed, manage, access, recorder) -> {
        Token token = access.relativeGet(0);
        if (!manage.getManageScopeService().registerFutureSymbol(token.toRealString())) {
            recorder.add(SemanticError.DUP_PARAM, token);
        }
    });
    /* 清除参数 */
    mapSemanticAction.put("func_clearargs", (indexed, manage, access, recorder) -> {
        manage.getManageScopeService().clearFutureArgs();
        Token token = access.relativeGet(0);
        KeywordType type = (KeywordType) token.object;
        if (type == KeywordType.YIELD) {
            manage.getQueryBlockService().enterBlock(BlockType.kYield);
        }
    });
    /* CATCH 清除参数 */
    mapSemanticAction.put("clear_catch", (indexed, manage, access, recorder) -> {
        manage.getManageScopeService().clearFutureArgs();
    });
    /* 循环体 */
    mapSemanticAction.put("do_enter_cycle", (indexed, manage, access, recorder) -> manage.getQueryBlockService().enterBlock(BlockType.kCycle));
    /* 匿名函数处理 */
    mapSemanticAction.put("lambda", (indexed, manage, access, recorder) -> {
        manage.getManageScopeService().clearFutureArgs();
        Token token = access.relativeGet(0);
        Function func = new Function();
        func.setName(token);
        manage.getManageScopeService().registerLambda(func);
        token.object = func.getRealName();
    });
}
Also used : KeywordType(priv.bajdcc.util.lexer.token.KeywordType) Token(priv.bajdcc.util.lexer.token.Token)

Example 4 with Token

use of priv.bajdcc.util.lexer.token.Token in project jMiniLang by bajdcc.

the class TokenTools method sinop.

/**
 * 单目运算
 *
 * @param recorder 错误记录
 * @param exp      表达式
 * @return 运算是否合法
 */
public static boolean sinop(ISemanticRecorder recorder, ExpSinop exp) {
    ExpValue value = (ExpValue) exp.getOperand();
    Token token = value.getToken();
    OperatorType type = (OperatorType) exp.getToken().object;
    if (sin(type, token)) {
        return true;
    }
    recorder.add(SemanticError.INVALID_OPERATOR, token);
    return false;
}
Also used : ExpValue(priv.bajdcc.LALR1.grammar.tree.ExpValue) Token(priv.bajdcc.util.lexer.token.Token) OperatorType(priv.bajdcc.util.lexer.token.OperatorType)

Example 5 with Token

use of priv.bajdcc.util.lexer.token.Token in project jMiniLang by bajdcc.

the class TokenTools method triop.

/**
 * 三目运算(当前只有一种形式)
 *
 * @param recorder 错误记录
 * @param exp      表达式
 * @return 运算是否合法
 */
public static int triop(ISemanticRecorder recorder, ExpTriop exp) {
    ExpValue firstValue = (ExpValue) exp.getFirstOperand();
    Token firstToken = exp.getFirstToken();
    Token secondToken = exp.getSecondToken();
    int branch = tri(firstToken, secondToken, firstValue.getToken());
    if (branch != 0) {
        return branch;
    }
    recorder.add(SemanticError.INVALID_OPERATOR, firstToken);
    return 0;
}
Also used : ExpValue(priv.bajdcc.LALR1.grammar.tree.ExpValue) Token(priv.bajdcc.util.lexer.token.Token)

Aggregations

Token (priv.bajdcc.util.lexer.token.Token)22 OperatorType (priv.bajdcc.util.lexer.token.OperatorType)6 RegexException (priv.bajdcc.util.lexer.error.RegexException)5 IPatternHandler (priv.bajdcc.OP.grammar.handler.IPatternHandler)4 ExpValue (priv.bajdcc.LALR1.grammar.tree.ExpValue)3 GrammarException (priv.bajdcc.OP.grammar.error.GrammarException)3 SyntaxException (priv.bajdcc.OP.syntax.handler.SyntaxException)3 SyntaxException (priv.bajdcc.LALR1.syntax.handler.SyntaxException)2 Grammar (priv.bajdcc.OP.grammar.Grammar)2 Lexer (priv.bajdcc.util.lexer.Lexer)2 TokenType (priv.bajdcc.util.lexer.token.TokenType)2 BufferedReader (java.io.BufferedReader)1 FileOutputStream (java.io.FileOutputStream)1 FileReader (java.io.FileReader)1 PrintStream (java.io.PrintStream)1 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 Scanner (java.util.Scanner)1 Grammar (priv.bajdcc.LALR1.grammar.Grammar)1 RuntimeFuncObject (priv.bajdcc.LALR1.grammar.runtime.data.RuntimeFuncObject)1