Search in sources :

Example 1 with StatContext

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

the class AstBuilder method visit.

@Override
public Object visit(ParseTree tree) {
    if (tree == null) {
        System.err.print("visit null");
        Exception ex = new Exception();
        ex.printStackTrace(System.err);
        return null;
    }
    if (tree instanceof StatContext && returned) {
        diagnosisReporter.report(Diagnosis.Kind.ERROR, "unreachable statement", (StatContext) tree);
    }
    return super.visit(tree);
}
Also used : IfStatContext(kalang.antlr.KalangParser.IfStatContext) ContinueStatContext(kalang.antlr.KalangParser.ContinueStatContext) ExprStatContext(kalang.antlr.KalangParser.ExprStatContext) WhileStatContext(kalang.antlr.KalangParser.WhileStatContext) BreakStatContext(kalang.antlr.KalangParser.BreakStatContext) StatContext(kalang.antlr.KalangParser.StatContext) TryStatContext(kalang.antlr.KalangParser.TryStatContext) ForStatContext(kalang.antlr.KalangParser.ForStatContext) ReturnStatContext(kalang.antlr.KalangParser.ReturnStatContext) VarDeclStatContext(kalang.antlr.KalangParser.VarDeclStatContext) DoWhileStatContext(kalang.antlr.KalangParser.DoWhileStatContext) MethodNotFoundException(kalang.MethodNotFoundException) InvalidModifierException(kalang.util.InvalidModifierException) AmbiguousMethodException(kalang.AmbiguousMethodException) FieldNotFoundException(kalang.FieldNotFoundException)

Example 2 with StatContext

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

the class AstBuilder method visitBlockStmt.

public void visitBlockStmt(StatContext[] stats, BlockStmt blockStmt) {
    this.newFrame();
    if (stats == null) {
        return;
    }
    for (StatContext s : stats) {
        blockStmt.statements.add(visitStat(s));
    }
    this.popFrame();
}
Also used : IfStatContext(kalang.antlr.KalangParser.IfStatContext) ContinueStatContext(kalang.antlr.KalangParser.ContinueStatContext) ExprStatContext(kalang.antlr.KalangParser.ExprStatContext) WhileStatContext(kalang.antlr.KalangParser.WhileStatContext) BreakStatContext(kalang.antlr.KalangParser.BreakStatContext) StatContext(kalang.antlr.KalangParser.StatContext) TryStatContext(kalang.antlr.KalangParser.TryStatContext) ForStatContext(kalang.antlr.KalangParser.ForStatContext) ReturnStatContext(kalang.antlr.KalangParser.ReturnStatContext) VarDeclStatContext(kalang.antlr.KalangParser.VarDeclStatContext) DoWhileStatContext(kalang.antlr.KalangParser.DoWhileStatContext)

Example 3 with StatContext

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

the class AstBuilder method visitBlockStmt.

@Override
public AstNode visitBlockStmt(BlockStmtContext ctx) {
    BlockStmt bs = newBlock();
    List<StatContext> stats = ctx.stat();
    if (stats != null) {
        this.visitBlockStmt(stats.toArray(new StatContext[stats.size()]), bs);
    }
    mapAst(bs, ctx);
    popBlock();
    return bs;
}
Also used : BlockStmt(kalang.ast.BlockStmt) IfStatContext(kalang.antlr.KalangParser.IfStatContext) ContinueStatContext(kalang.antlr.KalangParser.ContinueStatContext) ExprStatContext(kalang.antlr.KalangParser.ExprStatContext) WhileStatContext(kalang.antlr.KalangParser.WhileStatContext) BreakStatContext(kalang.antlr.KalangParser.BreakStatContext) StatContext(kalang.antlr.KalangParser.StatContext) TryStatContext(kalang.antlr.KalangParser.TryStatContext) ForStatContext(kalang.antlr.KalangParser.ForStatContext) ReturnStatContext(kalang.antlr.KalangParser.ReturnStatContext) VarDeclStatContext(kalang.antlr.KalangParser.VarDeclStatContext) DoWhileStatContext(kalang.antlr.KalangParser.DoWhileStatContext)

Example 4 with StatContext

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

the class AstBuilder method visitMethods.

private void visitMethods(ClassNode clazz) {
    thisClazz = clazz;
    for (MethodNode m : thisClazz.getDeclaredMethodNodes()) {
        BlockStmt mbody = m.getBody();
        StatContext[] stats = classNodeMetaBuilder.getStatContexts(m);
        if (stats != null) {
            method = m;
            returned = false;
            visitBlockStmt(stats, mbody);
            boolean needReturn = (m.getType() != null && !m.getType().equals(Types.VOID_TYPE));
            if (m.getBody() != null && needReturn && !returned) {
                this.diagnosisReporter.report(Diagnosis.Kind.ERROR, "Missing return statement in method:" + MethodUtil.toString(method), classNodeMetaBuilder.getMethodDeclContext(m));
            }
            new InitializationAnalyzer(compilationUnit, astLoader).check(clazz, m);
        }
        if (AstUtil.isConstructor(m)) {
            @SuppressWarnings("null") List<Statement> bodyStmts = mbody.statements;
            if (!AstUtil.hasConstructorCallStatement(bodyStmts)) {
                try {
                    bodyStmts.add(0, AstUtil.createDefaultSuperConstructorCall(thisClazz));
                } catch (MethodNotFoundException | AmbiguousMethodException ex) {
                    diagnosisReporter.report(Diagnosis.Kind.ERROR, "default constructor not found", classNodeMetaBuilder.getMethodDeclContext(m));
                }
            }
            // check super()
            int stmtsSize = mbody.statements.size();
            assert stmtsSize > 0;
            Statement firstStmt = mbody.statements.get(0);
            if (!AstUtil.isConstructorCallStatement(firstStmt)) {
                // TODO handle error
                throw new RuntimeException("missing constructor call");
            }
            mbody.statements.addAll(1, this.thisClazz.initStmts);
        }
    }
    for (ClassNode c : clazz.classes) {
        this.visitMethods(c);
    }
}
Also used : ClassNode(kalang.ast.ClassNode) Statement(kalang.ast.Statement) BlockStmt(kalang.ast.BlockStmt) IfStatContext(kalang.antlr.KalangParser.IfStatContext) ContinueStatContext(kalang.antlr.KalangParser.ContinueStatContext) ExprStatContext(kalang.antlr.KalangParser.ExprStatContext) WhileStatContext(kalang.antlr.KalangParser.WhileStatContext) BreakStatContext(kalang.antlr.KalangParser.BreakStatContext) StatContext(kalang.antlr.KalangParser.StatContext) TryStatContext(kalang.antlr.KalangParser.TryStatContext) ForStatContext(kalang.antlr.KalangParser.ForStatContext) ReturnStatContext(kalang.antlr.KalangParser.ReturnStatContext) VarDeclStatContext(kalang.antlr.KalangParser.VarDeclStatContext) DoWhileStatContext(kalang.antlr.KalangParser.DoWhileStatContext) MethodNode(kalang.ast.MethodNode) MethodNotFoundException(kalang.MethodNotFoundException) AmbiguousMethodException(kalang.AmbiguousMethodException)

Aggregations

BreakStatContext (kalang.antlr.KalangParser.BreakStatContext)4 ContinueStatContext (kalang.antlr.KalangParser.ContinueStatContext)4 DoWhileStatContext (kalang.antlr.KalangParser.DoWhileStatContext)4 ExprStatContext (kalang.antlr.KalangParser.ExprStatContext)4 ForStatContext (kalang.antlr.KalangParser.ForStatContext)4 IfStatContext (kalang.antlr.KalangParser.IfStatContext)4 ReturnStatContext (kalang.antlr.KalangParser.ReturnStatContext)4 StatContext (kalang.antlr.KalangParser.StatContext)4 TryStatContext (kalang.antlr.KalangParser.TryStatContext)4 VarDeclStatContext (kalang.antlr.KalangParser.VarDeclStatContext)4 WhileStatContext (kalang.antlr.KalangParser.WhileStatContext)4 AmbiguousMethodException (kalang.AmbiguousMethodException)2 MethodNotFoundException (kalang.MethodNotFoundException)2 BlockStmt (kalang.ast.BlockStmt)2 FieldNotFoundException (kalang.FieldNotFoundException)1 ClassNode (kalang.ast.ClassNode)1 MethodNode (kalang.ast.MethodNode)1 Statement (kalang.ast.Statement)1 InvalidModifierException (kalang.util.InvalidModifierException)1