Search in sources :

Example 6 with ExpressionContext

use of kalang.antlr.KalangParser.ExpressionContext 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 7 with ExpressionContext

use of kalang.antlr.KalangParser.ExpressionContext in project kalang by kasonyang.

the class AstBuilder method visitExpressions.

@Override
public List<Statement> visitExpressions(ExpressionsContext ctx) {
    List<Statement> list = new LinkedList();
    for (ExpressionContext e : ctx.expression()) {
        ExprNode expr = visitExpression(e);
        list.add(new ExprStmt(expr));
    }
    return list;
}
Also used : ExprNode(kalang.ast.ExprNode) ExprStmt(kalang.ast.ExprStmt) ExpressionContext(kalang.antlr.KalangParser.ExpressionContext) Statement(kalang.ast.Statement) LinkedList(java.util.LinkedList)

Aggregations

ExpressionContext (kalang.antlr.KalangParser.ExpressionContext)7 ExprNode (kalang.ast.ExprNode)7 AssignExpr (kalang.ast.AssignExpr)4 ExprStmt (kalang.ast.ExprStmt)3 ArrayType (kalang.core.ArrayType)3 ClassType (kalang.core.ClassType)3 GenericType (kalang.core.GenericType)3 ObjectType (kalang.core.ObjectType)3 PrimitiveType (kalang.core.PrimitiveType)3 Type (kalang.core.Type)3 WildcardType (kalang.core.WildcardType)3 LinkedList (java.util.LinkedList)2 TypeContext (kalang.antlr.KalangParser.TypeContext)2 AssignableExpr (kalang.ast.AssignableExpr)2 ConstExpr (kalang.ast.ConstExpr)2 LocalVarNode (kalang.ast.LocalVarNode)2 Statement (kalang.ast.Statement)2 VarDeclStmt (kalang.ast.VarDeclStmt)2 VarExpr (kalang.ast.VarExpr)2 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)2