Search in sources :

Example 1 with TypeContext

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

the class AstBuilder method visitLocalVarDecl.

@Override
public Statement visitLocalVarDecl(LocalVarDeclContext ctx) {
    MultiStmt ms = new MultiStmt();
    for (VarDeclContext v : ctx.varDecl()) {
        TypeContext varType = v.varType;
        Type exceptedType = varType == null ? null : parseType(varType);
        ExprNode initExpr = null;
        ExpressionContext initExprContext = v.expression();
        if (initExprContext != null) {
            if (initExprContext instanceof LiteralExprContext) {
                initExpr = this.parseLiteral(((LiteralExprContext) initExprContext).literal(), exceptedType);
            } else {
                initExpr = visitExpression(initExprContext);
            }
        }
        VarInfo varInfo = varDecl(v, initExpr == null ? Types.getRootType() : initExpr.getType());
        LocalVarNode localVar = this.declareLocalVar(varInfo.name, varInfo.type, varInfo.modifier, ctx);
        if (localVar == null)
            return null;
        VarDeclStmt vds = new VarDeclStmt(localVar);
        ms.statements.add(vds);
        if (initExpr != null) {
            AssignExpr assignExpr = new AssignExpr(new VarExpr(localVar), initExpr);
            mapAst(assignExpr, v);
            ms.statements.add(new ExprStmt(assignExpr));
        }
        mapAst(localVar, ctx);
    }
    return ms;
}
Also used : MultiStmt(kalang.ast.MultiStmt) TypeContext(kalang.antlr.KalangParser.TypeContext) AssignExpr(kalang.ast.AssignExpr) 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) ExprStmt(kalang.ast.ExprStmt) ExpressionContext(kalang.antlr.KalangParser.ExpressionContext) VarDeclStmt(kalang.ast.VarDeclStmt) VarExpr(kalang.ast.VarExpr) LiteralExprContext(kalang.antlr.KalangParser.LiteralExprContext) LocalVarNode(kalang.ast.LocalVarNode) VarDeclContext(kalang.antlr.KalangParser.VarDeclContext) LocalVarDeclContext(kalang.antlr.KalangParser.LocalVarDeclContext)

Example 2 with TypeContext

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

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

the class AstBuilder method varDecl.

protected VarInfo varDecl(VarDeclContext ctx, Type inferedType) {
    VarInfo vds = new VarInfo();
    String name = ctx.name.getText();
    TypeContext type = null;
    if (ctx.varType != null) {
        type = ctx.varType;
    } else if (ctx.type() != null) {
        type = ctx.type();
    }
    Type declType = type != null ? parseType(type) : inferedType;
    if (isDefindedId(name)) {
        diagnosisReporter.report(Diagnosis.Kind.ERROR, "the name is definded:" + name, ctx);
    }
    vds.name = name;
    vds.type = declType;
    vds.modifier = ctx.valToken != null ? Modifier.FINAL : 0;
    // }
    if (vds.type == null) {
        vds.type = Types.getRootType();
    }
    return vds;
}
Also used : 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) TypeContext(kalang.antlr.KalangParser.TypeContext)

Aggregations

TypeContext (kalang.antlr.KalangParser.TypeContext)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 ExpressionContext (kalang.antlr.KalangParser.ExpressionContext)2 ExprNode (kalang.ast.ExprNode)2 LiteralExprContext (kalang.antlr.KalangParser.LiteralExprContext)1 LocalVarDeclContext (kalang.antlr.KalangParser.LocalVarDeclContext)1 VarDeclContext (kalang.antlr.KalangParser.VarDeclContext)1 AssignExpr (kalang.ast.AssignExpr)1 ExprStmt (kalang.ast.ExprStmt)1 LocalVarNode (kalang.ast.LocalVarNode)1 MultiStmt (kalang.ast.MultiStmt)1 VarDeclStmt (kalang.ast.VarDeclStmt)1 VarExpr (kalang.ast.VarExpr)1