Search in sources :

Example 6 with ConstExpr

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

the class AstBuilder method createInitializedArray.

public ExprNode createInitializedArray(Type type, ExprNode[] exprs) {
    NewArrayExpr ae = new NewArrayExpr(type, new ConstExpr(exprs.length));
    if (exprs.length > 0) {
        Statement[] initStmts = new Statement[exprs.length + 2];
        // TODO create a method for temp var creation
        // TODO localVarNode should add a type parameter
        LocalVarNode local = this.declareTempLocalVar(ae.getType());
        initStmts[0] = new VarDeclStmt(local);
        VarExpr arrVar = new VarExpr(local);
        initStmts[1] = new ExprStmt(new AssignExpr(arrVar, ae));
        for (int i = 0; i < exprs.length; i++) {
            initStmts[i + 2] = new ExprStmt(new AssignExpr(new ElementExpr(arrVar, new ConstExpr(i)), exprs[i]));
        }
        return new MultiStmtExpr(Arrays.asList(initStmts), arrVar);
    } else {
        return ae;
    }
}
Also used : MultiStmtExpr(kalang.ast.MultiStmtExpr) ConstExpr(kalang.ast.ConstExpr) ExprStmt(kalang.ast.ExprStmt) NewArrayExpr(kalang.ast.NewArrayExpr) Statement(kalang.ast.Statement) VarDeclStmt(kalang.ast.VarDeclStmt) VarExpr(kalang.ast.VarExpr) LocalVarNode(kalang.ast.LocalVarNode) AssignExpr(kalang.ast.AssignExpr) ElementExpr(kalang.ast.ElementExpr)

Example 7 with ConstExpr

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

the class Ast2Class method getConstX.

private ConstExpr getConstX(Type type, int i) {
    Object obj;
    int t = getT(type);
    switch(t) {
        case T_I:
            obj = new Integer(i);
            break;
        case T_L:
            obj = new Long(i);
            break;
        case T_F:
            obj = new Float(i);
            break;
        case T_D:
            obj = new Double(i);
            break;
        default:
            throw new UnsupportedOperationException("unsupported type:" + type);
    }
    return new ConstExpr(obj);
}
Also used : ConstExpr(kalang.ast.ConstExpr) VarObject(kalang.ast.VarObject)

Example 8 with ConstExpr

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

the class AstBuilder method visitForEachStat.

@Override
public Object visitForEachStat(KalangParser.ForEachStatContext ctx) {
    BlockStmt block = newBlock();
    ExprNode expr = this.visitExpression(ctx.expression());
    Type exprType = expr.getType();
    List<TerminalNode> idsCtx = ctx.Identifier();
    VarExpr indexVarExpr = null;
    TerminalNode varId;
    if (idsCtx.size() == 1) {
        varId = idsCtx.get(0);
    } else {
        TerminalNode indexId = idsCtx.get(0);
        LocalVarNode indexVar = this.declareLocalVar(indexId.getText(), Types.INT_TYPE, Modifier.FINAL, ctx);
        if (indexVar == null)
            return null;
        block.statements.add(new VarDeclStmt(indexVar));
        indexVarExpr = new VarExpr(indexVar);
        varId = idsCtx.get(1);
    }
    LoopStmt loopStmt;
    if (exprType instanceof ArrayType) {
        LocalVarNode localVarNode = this.declareLocalVar(varId.getText(), ((ArrayType) exprType).getComponentType(), Modifier.FINAL, ctx);
        if (localVarNode == null)
            return null;
        VarExpr localVariable = new VarExpr(localVarNode);
        block.statements.add(new VarDeclStmt(localVarNode));
        LocalVarNode lenVar = this.declareTempLocalVar(Types.INT_TYPE);
        LocalVarNode counterVar = this.declareTempLocalVar(Types.INT_TYPE);
        // var len
        block.statements.add(new VarDeclStmt(lenVar));
        // var i
        block.statements.add(new VarDeclStmt(counterVar));
        VarExpr counterVarExpr = new VarExpr(counterVar);
        VarExpr lenVarExpr = new VarExpr(lenVar);
        block.statements.add(new ExprStmt(new AssignExpr(lenVarExpr, new ArrayLengthExpr(expr))));
        // l = array.length
        block.statements.add(new ExprStmt(new AssignExpr(counterVarExpr, new ConstExpr(0))));
        // i=0
        ExprNode cnd = new CompareExpr(counterVarExpr, lenVarExpr, CompareExpr.OP_LT);
        BlockStmt loopBody = this.newBlock();
        loopBody.statements.add(new ExprStmt(new AssignExpr(localVariable, new ElementExpr(expr, counterVarExpr))));
        if (indexVarExpr != null) {
            loopBody.statements.add(new ExprStmt(new AssignExpr(indexVarExpr, counterVarExpr)));
        }
        loopBody.statements.add(visitStat(ctx.stat()));
        popBlock();
        BlockStmt updateBs = newBlock();
        // increment counter
        updateBs.statements.add(new ExprStmt(new AssignExpr(counterVarExpr, new MathExpr(counterVarExpr, new ConstExpr(1), MathExpr.OP_ADD))));
        popBlock();
        loopStmt = new LoopStmt(cnd, null, loopBody, updateBs);
    } else {
        ObjectType iterType = Types.getIterableClassType();
        if (iterType.isAssignableFrom(exprType)) {
            ObjectInvokeExpr getIterableExpr;
            try {
                getIterableExpr = ObjectInvokeExpr.create(expr, "iterator", null);
            } catch (MethodNotFoundException | AmbiguousMethodException ex) {
                throw Exceptions.unexceptedException(ex);
            }
            LocalVarNode iterableVarNode = this.declareTempLocalVar(getIterableExpr.getType());
            block.statements.add(new VarDeclStmt(iterableVarNode));
            VarExpr iterableVarExpr = new VarExpr(iterableVarNode);
            block.statements.add(new ExprStmt(new AssignExpr(iterableVarExpr, getIterableExpr)));
            // set index = 0
            if (indexVarExpr != null) {
                block.statements.add(new ExprStmt(new AssignExpr(indexVarExpr, new ConstExpr(0))));
            }
            ObjectInvokeExpr cnd;
            try {
                cnd = ObjectInvokeExpr.create(iterableVarExpr, "hasNext", null);
            } catch (MethodNotFoundException | AmbiguousMethodException ex) {
                throw Exceptions.unexceptedException(ex);
            }
            BlockStmt loopBody = this.newBlock();
            ObjectInvokeExpr nextInvokeExpr;
            try {
                nextInvokeExpr = ObjectInvokeExpr.create(iterableVarExpr, "next", null);
            } catch (MethodNotFoundException | AmbiguousMethodException ex) {
                throw Exceptions.unexceptedException(ex);
            }
            LocalVarNode localVarNode = this.declareLocalVar(varId.getText(), nextInvokeExpr.getType(), Modifier.FINAL, ctx);
            if (localVarNode == null)
                return null;
            VarExpr localVariable = new VarExpr(localVarNode);
            loopBody.statements.add(new VarDeclStmt(localVarNode));
            loopBody.statements.add(new ExprStmt(new AssignExpr(localVariable, new CastExpr(localVariable.getType(), nextInvokeExpr))));
            loopBody.statements.add(visitStat(ctx.stat()));
            popBlock();
            BlockStmt updateBs = newBlock();
            if (indexVarExpr != null) {
                // do index++
                updateBs.statements.add(new ExprStmt(new AssignExpr(indexVarExpr, new MathExpr(indexVarExpr, new ConstExpr(1), BinaryExpr.OP_ADD))));
            }
            popBlock();
            loopStmt = new LoopStmt(cnd, null, loopBody, updateBs);
        } else {
            this.handleSyntaxError("require array type or iterable type", ctx.expression());
            loopStmt = null;
        }
    }
    popBlock();
    if (loopStmt != null)
        block.statements.add(loopStmt);
    return block;
}
Also used : ConstExpr(kalang.ast.ConstExpr) LoopStmt(kalang.ast.LoopStmt) BlockStmt(kalang.ast.BlockStmt) ArrayLengthExpr(kalang.ast.ArrayLengthExpr) AssignExpr(kalang.ast.AssignExpr) 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) ExprStmt(kalang.ast.ExprStmt) CompareExpr(kalang.ast.CompareExpr) VarDeclStmt(kalang.ast.VarDeclStmt) ObjectInvokeExpr(kalang.ast.ObjectInvokeExpr) PrimitiveCastExpr(kalang.ast.PrimitiveCastExpr) CastExpr(kalang.ast.CastExpr) VarExpr(kalang.ast.VarExpr) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) LocalVarNode(kalang.ast.LocalVarNode) MethodNotFoundException(kalang.MethodNotFoundException) MathExpr(kalang.ast.MathExpr) ElementExpr(kalang.ast.ElementExpr) AmbiguousMethodException(kalang.AmbiguousMethodException)

Example 9 with ConstExpr

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

the class AstBuilder method visitAnnotation.

@Override
@Nullable
public AnnotationNode visitAnnotation(KalangParser.AnnotationContext ctx) {
    ClassNode anType = requireAst(ctx.annotationType);
    if (anType == null)
        return null;
    List<Token> vk = ctx.annotationValueKey;
    LiteralContext dv = ctx.annotationDefaultValue;
    AnnotationNode anNode = new AnnotationNode(anType);
    if (vk != null && vk.size() > 0) {
        List<LiteralContext> anValues = ctx.annotationValue;
        int ksize = vk.size();
        for (int i = 0; i < ksize; i++) {
            String kname = vk.get(i).getText();
            ConstExpr value = visitLiteral(anValues.get(i));
            anNode.values.put(kname, value);
        }
    } else if (dv != null) {
        ConstExpr defaultValue = visitLiteral(dv);
        anNode.values.put("value", defaultValue);
    }
    if (!semanticAnalyzer.validateAnnotation(anNode))
        return null;
    // TODO validate annotation's values
    return anNode;
}
Also used : LiteralContext(kalang.antlr.KalangParser.LiteralContext) ClassNode(kalang.ast.ClassNode) ConstExpr(kalang.ast.ConstExpr) AnnotationNode(kalang.ast.AnnotationNode) Token(org.antlr.v4.runtime.Token) Nullable(javax.annotation.Nullable)

Aggregations

ConstExpr (kalang.ast.ConstExpr)9 AssignExpr (kalang.ast.AssignExpr)5 ExprNode (kalang.ast.ExprNode)4 ExprStmt (kalang.ast.ExprStmt)4 LocalVarNode (kalang.ast.LocalVarNode)4 VarDeclStmt (kalang.ast.VarDeclStmt)4 VarExpr (kalang.ast.VarExpr)4 ArrayType (kalang.core.ArrayType)4 ClassType (kalang.core.ClassType)4 GenericType (kalang.core.GenericType)4 ObjectType (kalang.core.ObjectType)4 PrimitiveType (kalang.core.PrimitiveType)4 Type (kalang.core.Type)4 WildcardType (kalang.core.WildcardType)4 ElementExpr (kalang.ast.ElementExpr)3 MathExpr (kalang.ast.MathExpr)3 MultiStmtExpr (kalang.ast.MultiStmtExpr)3 Statement (kalang.ast.Statement)3 VarObject (kalang.ast.VarObject)3 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)3