Search in sources :

Example 1 with BinaryExpr

use of kalang.ast.BinaryExpr in project kalang by kasonyang.

the class Ast2Class method visitIncrementExpr.

@Override
public Object visitIncrementExpr(IncrementExpr node) {
    if (!node.isIsPrefix()) {
        visit(node.getExpr());
    }
    Type exprType = node.getExpr().getType();
    ConstExpr ce = getConstX(exprType, node.isIsDesc() ? -1 : 1);
    BinaryExpr be = new MathExpr(node.getExpr(), ce, "+");
    AssignExpr addOne = new AssignExpr(node.getExpr(), be);
    visit(addOne);
    pop(exprType);
    if (node.isIsPrefix()) {
        visit(node.getExpr());
    }
    return null;
}
Also used : Type(kalang.core.Type) GenericType(kalang.core.GenericType) WildcardType(kalang.core.WildcardType) ArrayType(kalang.core.ArrayType) ObjectType(kalang.core.ObjectType) ClassType(kalang.core.ClassType) PrimitiveType(kalang.core.PrimitiveType) ConstExpr(kalang.ast.ConstExpr) BinaryExpr(kalang.ast.BinaryExpr) MathExpr(kalang.ast.MathExpr) AssignExpr(kalang.ast.AssignExpr)

Example 2 with BinaryExpr

use of kalang.ast.BinaryExpr in project kalang by kasonyang.

the class AstBuilder method visitPostIfStmt.

@Override
public AstNode visitPostIfStmt(KalangParser.PostIfStmtContext ctx) {
    ExprNode leftExpr = visitExpression(ctx.expression(0));
    if (!(leftExpr instanceof AssignExpr)) {
        diagnosisReporter.report(Diagnosis.Kind.ERROR, "AssignExpr required", ctx);
    }
    AssignExpr assignExpr = (AssignExpr) leftExpr;
    AssignableExpr to = assignExpr.getTo();
    ExprNode from = assignExpr.getFrom();
    ExprNode cond = visitExpression(ctx.expression(1));
    Token op = ctx.op;
    if (op != null) {
        String opStr = op.getText();
        BinaryExpr be = createBinaryExpr(to, cond, opStr);
        cond = be;
    }
    AssignExpr as = new AssignExpr(to, from);
    IfStmt is = new IfStmt(cond);
    is.getTrueBody().statements.add(new ExprStmt(as));
    mapAst(is, ctx);
    return is;
}
Also used : ExprNode(kalang.ast.ExprNode) IfStmt(kalang.ast.IfStmt) ExprStmt(kalang.ast.ExprStmt) AssignableExpr(kalang.ast.AssignableExpr) BinaryExpr(kalang.ast.BinaryExpr) Token(org.antlr.v4.runtime.Token) AssignExpr(kalang.ast.AssignExpr)

Example 3 with BinaryExpr

use of kalang.ast.BinaryExpr in project kalang by kasonyang.

the class AstBuilder method createBinaryExpr.

private BinaryExpr createBinaryExpr(ExprNode expr1, ExprNode expr2, String op) {
    BinaryExpr binExpr;
    switch(op) {
        case "==":
        case "!=":
        case ">":
        case ">=":
        case "<":
        case "<=":
            binExpr = new CompareExpr(expr1, expr2, op);
            break;
        case "&&":
        case "||":
            binExpr = new LogicExpr(expr1, expr2, op);
            break;
        default:
            binExpr = new MathExpr(expr1, expr2, op);
            break;
    }
    semanticAnalyzer.validateBinaryExpr(binExpr);
    return binExpr;
}
Also used : CompareExpr(kalang.ast.CompareExpr) LogicExpr(kalang.ast.LogicExpr) BinaryExpr(kalang.ast.BinaryExpr) MathExpr(kalang.ast.MathExpr)

Aggregations

BinaryExpr (kalang.ast.BinaryExpr)3 AssignExpr (kalang.ast.AssignExpr)2 MathExpr (kalang.ast.MathExpr)2 AssignableExpr (kalang.ast.AssignableExpr)1 CompareExpr (kalang.ast.CompareExpr)1 ConstExpr (kalang.ast.ConstExpr)1 ExprNode (kalang.ast.ExprNode)1 ExprStmt (kalang.ast.ExprStmt)1 IfStmt (kalang.ast.IfStmt)1 LogicExpr (kalang.ast.LogicExpr)1 ArrayType (kalang.core.ArrayType)1 ClassType (kalang.core.ClassType)1 GenericType (kalang.core.GenericType)1 ObjectType (kalang.core.ObjectType)1 PrimitiveType (kalang.core.PrimitiveType)1 Type (kalang.core.Type)1 WildcardType (kalang.core.WildcardType)1 Token (org.antlr.v4.runtime.Token)1