Search in sources :

Example 6 with ExprNode

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

the class AstBuilder method visitInstanceofExpr.

@Override
public Object visitInstanceofExpr(KalangParser.InstanceofExprContext ctx) {
    ExprNode expr = visitExpression(ctx.expression());
    Token ts = ctx.Identifier().getSymbol();
    AstNode tnode = getNodeById(ts.getText(), ts);
    if (tnode instanceof ClassReference) {
        InstanceOfExpr ie = new InstanceOfExpr(expr, (ClassReference) tnode);
        mapAst(ie, ctx);
        return ie;
    } else {
        AstBuilder.this.handleSyntaxError("unsupported type", ts);
        return null;
    }
}
Also used : ExprNode(kalang.ast.ExprNode) Token(org.antlr.v4.runtime.Token) ClassReference(kalang.ast.ClassReference) InstanceOfExpr(kalang.ast.InstanceOfExpr) AstNode(kalang.ast.AstNode)

Example 7 with ExprNode

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

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

the class AstBuilder method visitInterpolationExpr.

@Override
public Object visitInterpolationExpr(KalangParser.InterpolationExprContext ctx) {
    List<ParseTree> children = ctx.children;
    ExprNode[] exprs = new ExprNode[children.size()];
    Token[] exprTokens = new Token[children.size()];
    for (int i = 0; i < exprs.length; i++) {
        ParseTree c = children.get(i);
        if (c instanceof TerminalNode) {
            Token token = ((TerminalNode) c).getSymbol();
            int t = token.getType();
            String rawText = c.getText();
            String text;
            switch(t) {
                case KalangLexer.InterpolationPreffixString:
                    text = rawText.substring(1, rawText.length() - 2);
                    break;
                case KalangLexer.INTERPOLATION_STRING:
                    text = rawText;
                    break;
                case KalangLexer.RBRACE:
                case KalangLexer.INTERPOLATION_END:
                case KalangLexer.INTERPOLATION_INTERUPT:
                    // TODO optimize empty string
                    text = "";
                    break;
                default:
                    throw Exceptions.unexceptedValue(t);
            }
            exprs[i] = new ConstExpr(StringLiteralUtil.parse(text));
            exprTokens[i] = token;
        } else if (c instanceof ExpressionContext) {
            ExprNode expr = this.visitExpression((ExpressionContext) c);
            if (expr == null)
                return null;
            exprs[i] = expr;
            exprTokens[i] = ((ExpressionContext) c).getStart();
        } else {
            throw Exceptions.unexceptedValue(c);
        }
    }
    return this.concatExpressionsToStringExpr(exprs, exprTokens);
}
Also used : ExprNode(kalang.ast.ExprNode) ConstExpr(kalang.ast.ConstExpr) ExpressionContext(kalang.antlr.KalangParser.ExpressionContext) Token(org.antlr.v4.runtime.Token) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 9 with ExprNode

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

the class AstBuilder method visitWhileStat.

@Override
public AstNode visitWhileStat(WhileStatContext ctx) {
    ExprNode preConditionExpr = visitExpression(ctx.expression());
    BlockStmt loopBody = null;
    if (ctx.stat() != null) {
        loopBody = requireBlock(ctx.stat());
    }
    LoopStmt ws = new LoopStmt(preConditionExpr, null, loopBody, null);
    mapAst(ws, ctx);
    return ws;
}
Also used : ExprNode(kalang.ast.ExprNode) LoopStmt(kalang.ast.LoopStmt) BlockStmt(kalang.ast.BlockStmt)

Example 10 with ExprNode

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

the class AstBuilder method getNodeById.

@Nullable
private AstNode getNodeById(@Nonnull String name, @Nullable Token token) {
    // find local var
    LocalVarNode var = this.getNamedLocalVar(name);
    if (var != null) {
        VarExpr ve = new VarExpr(var, getVarObjectType(var));
        if (token != null)
            mapAst(ve, token);
        return ve;
    }
    // find parameters
    ParameterNode paramNode = this.getNamedParameter(name);
    if (paramNode != null) {
        ParameterExpr ve = new ParameterExpr(paramNode, this.getVarObjectType(paramNode));
        if (token != null)
            mapAst(ve, token);
        return ve;
    }
    // find field
    ExprNode fieldExpr = this.getObjectFieldExpr(new ThisExpr(this.getThisType()), name, ParserRuleContext.EMPTY);
    if (fieldExpr == null)
        fieldExpr = this.getStaticFieldExpr(new ClassReference(thisClazz), name, ParserRuleContext.EMPTY);
    if (fieldExpr != null)
        return fieldExpr;
    ExprNode outerClassInstanceExpr = this.getOuterClassInstanceExpr(new ThisExpr(this.getThisType()));
    while (outerClassInstanceExpr != null) {
        ExprNode fe = this.getObjectFieldExpr(outerClassInstanceExpr, name, ParserRuleContext.EMPTY);
        if (fe == null)
            fe = this.getStaticFieldExpr(new ClassReference(thisClazz), name, ParserRuleContext.EMPTY);
        if (fe != null)
            return fe;
        outerClassInstanceExpr = this.getOuterClassInstanceExpr(outerClassInstanceExpr);
    }
    String resolvedTypeName = this.typeNameResolver.resolve(name, topClass, thisClazz);
    if (resolvedTypeName != null) {
        ClassReference clsRef = new ClassReference(requireAst(resolvedTypeName, token));
        if (token != null)
            mapAst(clsRef, token);
        return clsRef;
    }
    return null;
}
Also used : ExprNode(kalang.ast.ExprNode) ParameterNode(kalang.ast.ParameterNode) ParameterExpr(kalang.ast.ParameterExpr) VarExpr(kalang.ast.VarExpr) ClassReference(kalang.ast.ClassReference) LocalVarNode(kalang.ast.LocalVarNode) ThisExpr(kalang.ast.ThisExpr) 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