Search in sources :

Example 21 with ExprNode

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

the class AstBuilder method createFieldExpr.

protected ExprNode createFieldExpr(GetFieldExprContext to, @Nullable ExpressionContext fromCtx, OffsetRange offsetRange) {
    // TODO support iterating syntax
    String refKey = to.refKey.getText();
    ExpressionContext exp = to.expression();
    String fname = to.Identifier().getText();
    AssignableExpr toExpr;
    Object expr = visit(exp);
    if (refKey.equals(".")) {
        ExprNode fieldExpr;
        if (expr instanceof ExprNode) {
            ExprNode exprNode = (ExprNode) expr;
            fieldExpr = getObjectFieldLikeExpr(exprNode, fname, to);
        } else if (expr instanceof ClassReference) {
            fieldExpr = getStaticFieldExpr((ClassReference) expr, fname, to);
        } else {
            throw new UnknownError("unknown node:" + expr);
        }
        if (fromCtx == null) {
            return fieldExpr;
        } else {
            if (fieldExpr instanceof AssignableExpr) {
                toExpr = (AssignableExpr) fieldExpr;
            } else {
                AstBuilder.this.handleSyntaxError("unsupported", to);
                return null;
            }
            ExprNode fromExpr = visitExpression(fromCtx);
            if (!this.semanticAnalyzer.validateAssign(toExpr, fromExpr, offsetRange)) {
                return null;
            }
            return new AssignExpr(toExpr, fromExpr);
        }
    } else if (refKey.equals("->")) {
        ExprNode[] params;
        String methodName;
        if (fromCtx == null) {
            params = new ExprNode[0];
            methodName = "get" + NameUtil.firstCharToUpperCase(fname);
        } else {
            params = new ExprNode[1];
            methodName = "set" + NameUtil.firstCharToUpperCase(fname);
        }
        if (expr instanceof ExprNode) {
            if (fromCtx != null)
                params[0] = visitExpression(fromCtx);
            return getObjectInvokeExpr((ExprNode) expr, methodName, params, to);
        } else {
            // don't support static property
            handleSyntaxError("object expression required.", to);
            return null;
        }
    } else {
        throw Exceptions.unknownValue(refKey);
    }
}
Also used : ExprNode(kalang.ast.ExprNode) ExpressionContext(kalang.antlr.KalangParser.ExpressionContext) AssignableExpr(kalang.ast.AssignableExpr) VarObject(kalang.ast.VarObject) ClassReference(kalang.ast.ClassReference) AssignExpr(kalang.ast.AssignExpr)

Example 22 with ExprNode

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

the class AstBuilder method visitArrayExpr.

@Override
public Object visitArrayExpr(KalangParser.ArrayExprContext ctx) {
    ExprNode[] initExprs;
    List<ExpressionContext> exprCtx = ctx.expression();
    if (exprCtx != null) {
        initExprs = new ExprNode[exprCtx.size()];
        for (int i = 0; i < initExprs.length; i++) {
            initExprs[i] = visitExpression(exprCtx.get(i));
        }
    } else {
        initExprs = new ExprNode[0];
    }
    TypeContext typeCtx = ctx.type();
    Type type;
    if (typeCtx != null) {
        type = parseType(typeCtx);
    } else {
        type = TypeUtil.getCommonType(AstUtil.getExprTypes(initExprs));
    }
    for (int i = 0; i < initExprs.length; i++) {
        if (exprCtx == null)
            throw Exceptions.unexceptedValue(exprCtx);
        initExprs[i] = requireCastable(initExprs[i], initExprs[i].getType(), type, exprCtx.get(i).getStart());
        if (initExprs[i] == null)
            return null;
    }
    ExprNode arrExpr = createInitializedArray(type, initExprs);
    mapAst(arrExpr, ctx);
    return arrExpr;
}
Also used : ExprNode(kalang.ast.ExprNode) 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) ExpressionContext(kalang.antlr.KalangParser.ExpressionContext) TypeContext(kalang.antlr.KalangParser.TypeContext)

Example 23 with ExprNode

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

the class AstBuilder method getIncrementExpr.

public IncrementExpr getIncrementExpr(ExpressionContext expressionContext, String op, boolean isPrefix) {
    ExprNode expr = visitExpression(expressionContext);
    if (!(expr instanceof AssignableExpr)) {
        AstBuilder.this.handleSyntaxError("require assignable expression", expressionContext);
        return null;
    }
    boolean isDesc = op.equals("--");
    return new IncrementExpr((AssignableExpr) expr, isDesc, isPrefix);
}
Also used : ExprNode(kalang.ast.ExprNode) IncrementExpr(kalang.ast.IncrementExpr) AssignableExpr(kalang.ast.AssignableExpr)

Example 24 with ExprNode

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

the class AstBuilder method getObjectFieldExpr.

@Nullable
protected ExprNode getObjectFieldExpr(ExprNode expr, String fieldName, @Nullable ParserRuleContext rule) {
    ExprNode ret;
    Type type = expr.getType();
    if (!(type instanceof ObjectType)) {
        // AstBuilder.this.handleSyntaxError("unsupported type", rule==null ? ParserRuleContext.EMPTY : rule);
        return null;
    }
    ObjectType exprType = (ObjectType) type;
    if ((exprType instanceof ArrayType)) {
        return null;
    } else {
        try {
            ret = ObjectFieldExpr.create(expr, fieldName, exprType.getClassNode());
        } catch (FieldNotFoundException ex) {
            return null;
        }
    }
    if (rule != null)
        mapAst(ret, rule);
    return ret;
}
Also used : ExprNode(kalang.ast.ExprNode) ArrayType(kalang.core.ArrayType) 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) FieldNotFoundException(kalang.FieldNotFoundException) Nullable(javax.annotation.Nullable)

Example 25 with ExprNode

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

the class AstBuilder method visitNewArrayExpr.

@Override
public ExprNode visitNewArrayExpr(KalangParser.NewArrayExprContext ctx) {
    Type type = parseType(ctx.type());
    ExprNode ret;
    if (ctx.size != null) {
        ExprNode size = visitExpression(ctx.size);
        ret = new NewArrayExpr(type, size);
    } else {
        ExprNode[] initExprs = new ExprNode[ctx.initExpr.size()];
        for (int i = 0; i < initExprs.length; i++) {
            initExprs[i] = visitExpression(ctx.initExpr.get(i));
        }
        ret = createInitializedArray(type, initExprs);
    }
    mapAst(ret, ctx);
    return ret;
}
Also used : ExprNode(kalang.ast.ExprNode) 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) NewArrayExpr(kalang.ast.NewArrayExpr)

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