Search in sources :

Example 1 with LocalVarDeclContext

use of kalang.antlr.KalangParser.LocalVarDeclContext 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)

Aggregations

ExpressionContext (kalang.antlr.KalangParser.ExpressionContext)1 LiteralExprContext (kalang.antlr.KalangParser.LiteralExprContext)1 LocalVarDeclContext (kalang.antlr.KalangParser.LocalVarDeclContext)1 TypeContext (kalang.antlr.KalangParser.TypeContext)1 VarDeclContext (kalang.antlr.KalangParser.VarDeclContext)1 AssignExpr (kalang.ast.AssignExpr)1 ExprNode (kalang.ast.ExprNode)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 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