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;
}
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;
}
}
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);
}
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));
}
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);
}
}
}
Aggregations