Search in sources :

Example 1 with ExpValue

use of priv.bajdcc.LALR1.grammar.tree.ExpValue 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 2 with ExpValue

use of priv.bajdcc.LALR1.grammar.tree.ExpValue 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)

Example 3 with ExpValue

use of priv.bajdcc.LALR1.grammar.tree.ExpValue 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 4 with ExpValue

use of priv.bajdcc.LALR1.grammar.tree.ExpValue 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)

Aggregations

ExpValue (priv.bajdcc.LALR1.grammar.tree.ExpValue)3 OperatorType (priv.bajdcc.util.lexer.token.OperatorType)3 Token (priv.bajdcc.util.lexer.token.Token)3 RuntimeInst (priv.bajdcc.LALR1.grammar.runtime.RuntimeInst)1 RuntimeInstUnary (priv.bajdcc.LALR1.grammar.runtime.RuntimeInstUnary)1