Search in sources :

Example 6 with OperatorType

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

the class OperatorTokenizer method getRegexString.

public static String getRegexString() {
    MetaType[] metaTypes = new MetaType[] { MetaType.LPARAN, MetaType.RPARAN, MetaType.STAR, MetaType.PLUS, MetaType.LSQUARE, MetaType.RSQUARE, MetaType.LBRACE, MetaType.RBRACE, MetaType.DOT, MetaType.BAR, MetaType.QUERY };
    StringBuilder sb = new StringBuilder();
    for (OperatorType type : OperatorType.values()) {
        String op = type.getName();
        for (MetaType meta : metaTypes) {
            op = op.replace(meta.getChar() + "", "\\" + meta.getChar());
        }
        if (type == OperatorType.ESCAPE)
            op += op;
        sb.append(op).append("|");
    }
    if (sb.length() > 0) {
        sb.deleteCharAt(sb.length() - 1);
    }
    return sb.toString();
}
Also used : OperatorType(priv.bajdcc.util.lexer.token.OperatorType) MetaType(priv.bajdcc.util.lexer.token.MetaType)

Example 7 with OperatorType

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

the class TokenTools method binop.

/**
 * 双目运算
 *
 * @param recorder 错误记录
 * @param exp      表达式
 * @return 运算是否合法
 */
public static boolean binop(ISemanticRecorder recorder, ExpBinop exp) {
    ExpValue leftValue = (ExpValue) exp.getLeftOperand();
    ExpValue rightValue = (ExpValue) exp.getRightOperand();
    Token token = exp.getToken();
    Token leftToken = leftValue.getToken();
    Token rightToken = rightValue.getToken();
    OperatorType type = (OperatorType) token.object;
    if (bin(type, leftToken, rightToken)) {
        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 8 with OperatorType

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

the class RuntimeMachine method runByStep.

private boolean runByStep() throws Exception {
    RuntimeInst inst = RuntimeInst.values()[currentInst()];
    if (inst == RuntimeInst.ihalt) {
        return false;
    }
    if (debug) {
        System.err.println();
        System.err.print(stack.reg.execId + ": " + inst.toString());
    }
    OperatorType op = TokenTools.ins2op(inst);
    nextInst();
    if (op != null) {
        if (!RuntimeTools.calcOp(stack.reg, inst, this)) {
            err(RuntimeError.UNDEFINED_CONVERT, op.getName());
        }
    } else {
        if (!RuntimeTools.calcData(stack.reg, inst, this)) {
            if (!RuntimeTools.calcJump(stack.reg, inst, this)) {
                err(RuntimeError.WRONG_INST, inst.toString());
            }
        }
    }
    if (debug) {
        System.err.println();
        System.err.print(stack.toString());
        // System.err.print("协程栈:");
        // System.err.print(stkYieldData.toString());
        System.err.println();
    }
    return true;
}
Also used : OperatorType(priv.bajdcc.util.lexer.token.OperatorType)

Example 9 with OperatorType

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

the class ExpBinop method genCode.

@Override
public void genCode(ICodegen codegen) {
    if (token.kToken == TokenType.OPERATOR && token.object == OperatorType.DOT) {
        codegen.genCode(RuntimeInst.iopena);
        leftOperand.genCode(codegen);
        codegen.genCode(RuntimeInst.ipusha);
        rightOperand.genCode(codegen);
        codegen.genCode(RuntimeInst.ipusha);
        codegen.genCode(RuntimeInst.ipush, codegen.genDataRef("g_get_property"));
        codegen.genCode(RuntimeInst.icallx);
        return;
    }
    if (token.kToken == TokenType.OPERATOR) {
        OperatorType op = (OperatorType) token.object;
        if (TokenTools.isAssignment(op)) {
            RuntimeInst ins = TokenTools.op2ins(token);
            ExpValue left = (ExpValue) leftOperand;
            if (ins == RuntimeInst.ice) {
                rightOperand.genCode(codegen);
                codegen.genCode(RuntimeInst.ipush, codegen.genDataRef(left.getToken().object));
                codegen.genCode(RuntimeInst.istore);
                return;
            }
            leftOperand.genCode(codegen);
            rightOperand.genCode(codegen);
            codegen.genCode(ins);
            codegen.genCode(RuntimeInst.ipush, codegen.genDataRef(left.getToken().object));
            codegen.genCode(RuntimeInst.istore);
            return;
        } else if (op == OperatorType.COLON) {
            rightOperand.genCode(codegen);
            leftOperand.genCode(codegen);
            return;
        }
    }
    RuntimeInst inst = TokenTools.op2ins(token);
    leftOperand.genCode(codegen);
    RuntimeInstUnary jmp = null;
    switch(inst) {
        case iandl:
            jmp = codegen.genCode(RuntimeInst.ijfx, -1);
            break;
        case iorl:
            jmp = codegen.genCode(RuntimeInst.ijtx, -1);
            break;
        default:
            break;
    }
    rightOperand.genCode(codegen);
    codegen.genCode(inst);
    if (jmp != null) {
        jmp.op1 = codegen.getCodeIndex();
    }
}
Also used : RuntimeInstUnary(priv.bajdcc.LALR1.grammar.runtime.RuntimeInstUnary) RuntimeInst(priv.bajdcc.LALR1.grammar.runtime.RuntimeInst) OperatorType(priv.bajdcc.util.lexer.token.OperatorType)

Example 10 with OperatorType

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

the class ExpBinop method analysis.

@Override
public void analysis(ISemanticRecorder recorder) {
    if (token.kToken == TokenType.OPERATOR) {
        OperatorType op = (OperatorType) token.object;
        if (TokenTools.isAssignment(op)) {
            if (!(leftOperand instanceof ExpValue)) {
                recorder.add(SemanticException.SemanticError.INVALID_ASSIGNMENT, token);
            }
        }
    }
    leftOperand.analysis(recorder);
    rightOperand.analysis(recorder);
}
Also used : OperatorType(priv.bajdcc.util.lexer.token.OperatorType)

Aggregations

OperatorType (priv.bajdcc.util.lexer.token.OperatorType)12 Token (priv.bajdcc.util.lexer.token.Token)6 RegexException (priv.bajdcc.util.lexer.error.RegexException)4 GrammarException (priv.bajdcc.OP.grammar.error.GrammarException)3 IPatternHandler (priv.bajdcc.OP.grammar.handler.IPatternHandler)3 SyntaxException (priv.bajdcc.OP.syntax.handler.SyntaxException)3 ExpValue (priv.bajdcc.LALR1.grammar.tree.ExpValue)2 Grammar (priv.bajdcc.OP.grammar.Grammar)2 Grammar (priv.bajdcc.LALR1.grammar.Grammar)1 RuntimeInst (priv.bajdcc.LALR1.grammar.runtime.RuntimeInst)1 RuntimeInstUnary (priv.bajdcc.LALR1.grammar.runtime.RuntimeInstUnary)1 Semantic (priv.bajdcc.LALR1.semantic.Semantic)1 ISemanticAnalyzer (priv.bajdcc.LALR1.semantic.token.ISemanticAnalyzer)1 SyntaxException (priv.bajdcc.LALR1.syntax.handler.SyntaxException)1 MetaType (priv.bajdcc.util.lexer.token.MetaType)1 TokenType (priv.bajdcc.util.lexer.token.TokenType)1