Search in sources :

Example 11 with ExprNode

use of kalang.ast.ExprNode 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 12 with ExprNode

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

the class AstBuilder method visitDoWhileStat.

@Override
public AstNode visitDoWhileStat(DoWhileStatContext ctx) {
    BlockStmt loopBody = null;
    if (ctx.blockStmt() != null) {
        loopBody = requireBlock(ctx.blockStmt());
    }
    ExprNode postConditionExpr = visitExpression(ctx.expression());
    LoopStmt ls = new LoopStmt(null, postConditionExpr, loopBody, null);
    mapAst(ls, ctx);
    return ls;
}
Also used : ExprNode(kalang.ast.ExprNode) LoopStmt(kalang.ast.LoopStmt) BlockStmt(kalang.ast.BlockStmt)

Example 13 with ExprNode

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

the class AstBuilder method visitAssignExpr.

@Override
public ExprNode visitAssignExpr(AssignExprContext ctx) {
    ExprNode expr;
    String assignOp = ctx.getChild(1).getText();
    ExpressionContext toCtx = ctx.expression(0);
    ExpressionContext fromCtx = ctx.expression(1);
    if (toCtx instanceof GetFieldExprContext) {
        // TODO check readonly
        expr = createFieldExpr((GetFieldExprContext) toCtx, fromCtx, OffsetRangeHelper.getOffsetRange(ctx));
    } else {
        ExprNode to = visitExpression(toCtx);
        ExprNode from = visitExpression(fromCtx);
        if (assignOp.length() > 1) {
            String op = assignOp.substring(0, assignOp.length() - 1);
            from = createBinaryExpr(to, from, op);
        }
        AssignableExpr toExpr;
        if (to instanceof AssignableExpr) {
            toExpr = (AssignableExpr) to;
            if (!this.semanticAnalyzer.validateAssign(toExpr, from, OffsetRangeHelper.getOffsetRange(ctx))) {
                return null;
            }
            AssignExpr aexpr = new AssignExpr(toExpr, from);
            mapAst(aexpr, ctx);
            // TODO remove override information before assign
            onAssign(toExpr, from);
            expr = aexpr;
        } else {
            AstBuilder.this.handleSyntaxError("unsupported assign statement", ctx);
            return null;
        }
    }
    return expr;
}
Also used : ExprNode(kalang.ast.ExprNode) ExpressionContext(kalang.antlr.KalangParser.ExpressionContext) AssignableExpr(kalang.ast.AssignableExpr) GetFieldExprContext(kalang.antlr.KalangParser.GetFieldExprContext) AssignExpr(kalang.ast.AssignExpr)

Example 14 with ExprNode

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

the class AstBuilder method visitMapExpr.

@Override
public MultiStmtExpr visitMapExpr(KalangParser.MapExprContext ctx) {
    Type keyType = ctx.keyType != null ? requireClassType(ctx.keyType) : Types.getRootType();
    Type valueType = ctx.valueType != null ? requireClassType(ctx.valueType) : Types.getRootType();
    if (keyType == null || valueType == null)
        return null;
    LocalVarNode vo = declareTempLocalVar(Types.getClassType(Types.getMapImplClassType().getClassNode(), new Type[] { keyType, valueType }));
    VarDeclStmt vds = new VarDeclStmt(vo);
    NewObjectExpr newExpr;
    try {
        newExpr = new NewObjectExpr(Types.getMapImplClassType());
    } catch (MethodNotFoundException | AmbiguousMethodException ex) {
        throw Exceptions.unexceptedException(ex);
    }
    List<Statement> stmts = new LinkedList<>();
    stmts.add(vds);
    stmts.add(new ExprStmt(new AssignExpr(new VarExpr(vo), newExpr)));
    VarExpr ve = new VarExpr(vo);
    List<TerminalNode> ids = ctx.Identifier();
    for (int i = 0; i < ids.size(); i++) {
        ExpressionContext e = ctx.expression(i);
        ExprNode v = (ExprNode) visit(e);
        ConstExpr k = new ConstExpr(ctx.Identifier(i).getText());
        ExprNode[] args = new ExprNode[] { k, v };
        InvocationExpr iv;
        try {
            iv = ObjectInvokeExpr.create(ve, "put", args);
        } catch (MethodNotFoundException | AmbiguousMethodException ex) {
            throw Exceptions.unexceptedException(ex);
        }
        ExprStmt es = new ExprStmt(iv);
        stmts.add(es);
    }
    MultiStmtExpr mse = new MultiStmtExpr(stmts, ve);
    mapAst(mse, ctx);
    return mse;
}
Also used : ConstExpr(kalang.ast.ConstExpr) Statement(kalang.ast.Statement) NewObjectExpr(kalang.ast.NewObjectExpr) LinkedList(java.util.LinkedList) AssignExpr(kalang.ast.AssignExpr) ExprNode(kalang.ast.ExprNode) MultiStmtExpr(kalang.ast.MultiStmtExpr) WildcardType(kalang.core.WildcardType) ArrayType(kalang.core.ArrayType) ClassType(kalang.core.ClassType) PrimitiveType(kalang.core.PrimitiveType) Type(kalang.core.Type) GenericType(kalang.core.GenericType) ObjectType(kalang.core.ObjectType) ExprStmt(kalang.ast.ExprStmt) ExpressionContext(kalang.antlr.KalangParser.ExpressionContext) VarDeclStmt(kalang.ast.VarDeclStmt) VarExpr(kalang.ast.VarExpr) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) LocalVarNode(kalang.ast.LocalVarNode) MethodNotFoundException(kalang.MethodNotFoundException) InvocationExpr(kalang.ast.InvocationExpr) UnknownInvocationExpr(kalang.ast.UnknownInvocationExpr) AmbiguousMethodException(kalang.AmbiguousMethodException)

Example 15 with ExprNode

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

the class AstBuilder method getObjectInvokeExpr.

@Nullable
private ExprNode getObjectInvokeExpr(ExprNode target, String methodName, ExprNode[] args, ParserRuleContext ctx) {
    if ("<init>".equals(methodName)) {
        throw Exceptions.unexceptedException("Don't get constructor by this method.");
    }
    Type targetType = target.getType();
    if (!(targetType instanceof ObjectType)) {
        handleSyntaxError("class type required.", ctx);
        return null;
    }
    ObjectType targetClassType = (ObjectType) targetType;
    if (targetClassType.getNullable() == NullableKind.NULLABLE) {
        handleSyntaxError("expression may be null", ctx);
        return null;
    }
    ExprNode expr;
    try {
        ObjectInvokeExpr invoke = ObjectInvokeExpr.create(target, methodName, args, thisClazz);
        if (invoke.getMethod().getMethodNode().getType() instanceof GenericType) {
            Type invokeType = invoke.getType();
            if (invokeType instanceof ObjectType) {
                expr = new CastExpr(invokeType, invoke);
            } else {
                expr = invoke;
            }
        } else {
            expr = invoke;
        }
    } catch (MethodNotFoundException ex) {
        methodNotFound(ctx.start, className, methodName, args);
        expr = new UnknownInvocationExpr(target, methodName, args);
    } catch (AmbiguousMethodException ex) {
        methodIsAmbiguous(ctx.start, ex);
        return null;
    }
    mapAst(expr, ctx);
    return expr;
}
Also used : ExprNode(kalang.ast.ExprNode) ObjectType(kalang.core.ObjectType) WildcardType(kalang.core.WildcardType) ArrayType(kalang.core.ArrayType) ClassType(kalang.core.ClassType) PrimitiveType(kalang.core.PrimitiveType) Type(kalang.core.Type) GenericType(kalang.core.GenericType) ObjectType(kalang.core.ObjectType) GenericType(kalang.core.GenericType) ObjectInvokeExpr(kalang.ast.ObjectInvokeExpr) PrimitiveCastExpr(kalang.ast.PrimitiveCastExpr) CastExpr(kalang.ast.CastExpr) MethodNotFoundException(kalang.MethodNotFoundException) UnknownInvocationExpr(kalang.ast.UnknownInvocationExpr) AmbiguousMethodException(kalang.AmbiguousMethodException) Nullable(javax.annotation.Nullable)

Aggregations

ExprNode (kalang.ast.ExprNode)47 ObjectType (kalang.core.ObjectType)23 ArrayType (kalang.core.ArrayType)17 GenericType (kalang.core.GenericType)17 Type (kalang.core.Type)17 ClassType (kalang.core.ClassType)16 PrimitiveType (kalang.core.PrimitiveType)15 WildcardType (kalang.core.WildcardType)14 ExprStmt (kalang.ast.ExprStmt)13 AssignExpr (kalang.ast.AssignExpr)12 BlockStmt (kalang.ast.BlockStmt)10 AmbiguousMethodException (kalang.AmbiguousMethodException)9 MethodNotFoundException (kalang.MethodNotFoundException)9 ClassReference (kalang.ast.ClassReference)9 ThisExpr (kalang.ast.ThisExpr)8 ExpressionContext (kalang.antlr.KalangParser.ExpressionContext)7 Statement (kalang.ast.Statement)7 LocalVarNode (kalang.ast.LocalVarNode)6 ObjectInvokeExpr (kalang.ast.ObjectInvokeExpr)6 VarExpr (kalang.ast.VarExpr)6