Search in sources :

Example 16 with BlockStmt

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

the class AstBuilder method visitTryStat.

@Override
public AstNode visitTryStat(TryStatContext ctx) {
    // TODO handle multi-branched assign
    BlockStmt tryExecStmt = requireBlock(ctx.exec);
    boolean tryReturned = this.returned;
    List<CatchBlock> tryCatchBlocks = new LinkedList<>();
    if (ctx.catchTypes != null) {
        for (int i = 0; i < ctx.catchTypes.size(); i++) {
            this.newFrame();
            this.returned = false;
            String vName = ctx.catchVarNames.get(i).getText();
            String vType = ctx.catchTypes.get(i).getText();
            LocalVarNode vo = this.declareLocalVar(vName, requireClassType(vType, ctx.catchTypes.get(i).start), Modifier.FINAL, ctx);
            if (vo == null)
                return null;
            BlockStmt catchExecStmt = requireBlock(ctx.catchExec.get(i));
            CatchBlock catchStmt = new CatchBlock(vo, catchExecStmt);
            tryCatchBlocks.add(catchStmt);
            this.returned = this.returned && tryReturned;
            this.popFrame();
        }
    }
    BlockStmt tryFinallyStmt = null;
    if (ctx.finallyExec != null) {
        tryFinallyStmt = requireBlock(ctx.finallyExec);
    }
    TryStmt tryStmt = new TryStmt(tryExecStmt, tryCatchBlocks, tryFinallyStmt);
    mapAst(tryStmt, ctx);
    return tryStmt;
}
Also used : CatchBlock(kalang.ast.CatchBlock) BlockStmt(kalang.ast.BlockStmt) TryStmt(kalang.ast.TryStmt) LocalVarNode(kalang.ast.LocalVarNode) LinkedList(java.util.LinkedList)

Example 17 with BlockStmt

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

the class AstBuilder method requireBlock.

private BlockStmt requireBlock(ParserRuleContext stmt) {
    if (stmt instanceof BlockStmtContext) {
        return (BlockStmt) visit(stmt);
    } else {
        BlockStmt bs = newBlock();
        bs.statements.add((Statement) visit(stmt));
        popBlock();
        return bs;
    }
}
Also used : BlockStmtContext(kalang.antlr.KalangParser.BlockStmtContext) BlockStmt(kalang.ast.BlockStmt)

Example 18 with BlockStmt

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

the class AstBuilder method visitAssertStmt.

@Override
public Object visitAssertStmt(KalangParser.AssertStmtContext ctx) {
    ExprNode failExpr = visitExpression(ctx.testCondition);
    if (failExpr == null)
        return null;
    failExpr = BoxUtil.assign(failExpr, failExpr.getType(), Types.BOOLEAN_TYPE);
    if (failExpr == null) {
        this.diagnosisReporter.report(Diagnosis.Kind.ERROR, "boolean type expected", ctx.testCondition);
        return null;
    }
    failExpr = new UnaryExpr(failExpr, UnaryExpr.OPERATION_LOGIC_NOT);
    ExprNode failMsgExpr = null;
    if (ctx.failMessage != null) {
        failMsgExpr = visitExpression(ctx.failMessage);
        if (failMsgExpr == null)
            return null;
        if (Types.VOID_TYPE.equals(failMsgExpr.getType())) {
            this.diagnosisReporter.report(Diagnosis.Kind.ERROR, "non-void type expected", ctx.failMessage);
            return null;
        }
    }
    BlockStmt body = this.newBlock();
    NewObjectExpr newErrorExpr;
    try {
        newErrorExpr = new NewObjectExpr(Types.requireAssertionErrorClassType(), failMsgExpr != null ? new ExprNode[] { failMsgExpr } : new ExprNode[0]);
    } catch (MethodNotFoundException | AmbiguousMethodException ex) {
        throw Exceptions.unexceptedException(ex);
    }
    body.statements.add(new ThrowStmt(newErrorExpr));
    popBlock();
    return new IfStmt(failExpr, body, null);
}
Also used : ExprNode(kalang.ast.ExprNode) IfStmt(kalang.ast.IfStmt) BlockStmt(kalang.ast.BlockStmt) NewObjectExpr(kalang.ast.NewObjectExpr) UnaryExpr(kalang.ast.UnaryExpr) MethodNotFoundException(kalang.MethodNotFoundException) ThrowStmt(kalang.ast.ThrowStmt) AmbiguousMethodException(kalang.AmbiguousMethodException)

Example 19 with BlockStmt

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

the class AstUtil method createGetter.

public static void createGetter(ClassNode clazz, FieldDescriptor field, int accessModifier) {
    String fn = field.getName();
    String getterName = "get" + NameUtil.firstCharToUpperCase(fn);
    boolean isStatic = isStatic(field.getModifier());
    if (isStatic) {
        accessModifier |= Modifier.STATIC;
    }
    MethodNode getter = clazz.createMethodNode(field.getType(), getterName, accessModifier);
    // getter.offset = field.offset;
    BlockStmt body = getter.getBody();
    FieldExpr fe;
    ClassReference cr = new ClassReference(clazz);
    if (isStatic) {
        fe = new StaticFieldExpr(cr, field);
    } else {
        fe = new ObjectFieldExpr(new ThisExpr(Types.getClassType(clazz)), field);
    }
    body.statements.add(new ReturnStmt(fe));
}
Also used : StaticFieldExpr(kalang.ast.StaticFieldExpr) ObjectFieldExpr(kalang.ast.ObjectFieldExpr) MethodNode(kalang.ast.MethodNode) BlockStmt(kalang.ast.BlockStmt) ClassReference(kalang.ast.ClassReference) StaticFieldExpr(kalang.ast.StaticFieldExpr) FieldExpr(kalang.ast.FieldExpr) ObjectFieldExpr(kalang.ast.ObjectFieldExpr) ReturnStmt(kalang.ast.ReturnStmt) ThisExpr(kalang.ast.ThisExpr)

Example 20 with BlockStmt

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

the class AstUtil method createScriptMainMethodIfNotExists.

public static void createScriptMainMethodIfNotExists(ClassNode clazz) {
    ClassType clazzType = Types.getClassType(clazz);
    MethodDescriptor[] methods = clazzType.getMethodDescriptors(null, false, false);
    Type[] argTypes = new Type[] { Types.getArrayType(Types.getStringClassType()) };
    MethodDescriptor mainMethod = MethodUtil.getMethodDescriptor(methods, "main", argTypes);
    if (mainMethod == null) {
        MethodNode m = clazz.createMethodNode(Types.VOID_TYPE, "main", Modifier.PUBLIC + Modifier.STATIC);
        ParameterNode p = m.createParameter(argTypes[0], "arg");
        BlockStmt body = m.getBody();
        try {
            NewObjectExpr newScriptExpr = new NewObjectExpr(clazzType);
            ObjectInvokeExpr invokeExpr = ObjectInvokeExpr.create(newScriptExpr, "run", new ExprNode[] { new ParameterExpr(p) });
            body.statements.add(new ExprStmt(invokeExpr));
        } catch (MethodNotFoundException | AmbiguousMethodException ex) {
            throw Exceptions.unexceptedException(ex);
        }
    }
}
Also used : ParameterExpr(kalang.ast.ParameterExpr) BlockStmt(kalang.ast.BlockStmt) NewObjectExpr(kalang.ast.NewObjectExpr) ClassType(kalang.core.ClassType) MethodDescriptor(kalang.core.MethodDescriptor) ArrayType(kalang.core.ArrayType) Type(kalang.core.Type) ObjectType(kalang.core.ObjectType) GenericType(kalang.core.GenericType) ClassType(kalang.core.ClassType) MethodNode(kalang.ast.MethodNode) ParameterNode(kalang.ast.ParameterNode) ExprStmt(kalang.ast.ExprStmt) ObjectInvokeExpr(kalang.ast.ObjectInvokeExpr) MethodNotFoundException(kalang.MethodNotFoundException) AmbiguousMethodException(kalang.AmbiguousMethodException)

Aggregations

BlockStmt (kalang.ast.BlockStmt)20 ExprNode (kalang.ast.ExprNode)10 ObjectType (kalang.core.ObjectType)7 ExprStmt (kalang.ast.ExprStmt)6 MethodNode (kalang.ast.MethodNode)6 GenericType (kalang.core.GenericType)6 LoopStmt (kalang.ast.LoopStmt)5 ParameterNode (kalang.ast.ParameterNode)5 ArrayType (kalang.core.ArrayType)5 ClassType (kalang.core.ClassType)5 Type (kalang.core.Type)5 AmbiguousMethodException (kalang.AmbiguousMethodException)4 MethodNotFoundException (kalang.MethodNotFoundException)4 AssignExpr (kalang.ast.AssignExpr)4 ParameterExpr (kalang.ast.ParameterExpr)4 ClassNode (kalang.ast.ClassNode)3 ClassReference (kalang.ast.ClassReference)3 Statement (kalang.ast.Statement)3 ThisExpr (kalang.ast.ThisExpr)3 LinkedList (java.util.LinkedList)2