use of kalang.ast.NewObjectExpr in project kalang by kasonyang.
the class AstBuilder method visitMapExpr.
@Override
public MultiStmtExpr visitMapExpr(KalangParser.MapExprContext ctx) {
Type keyType = ctx.keyType != null ? requireClassType(ctx.keyType) : Types.getRootType();
Type valueType = ctx.valueType != null ? requireClassType(ctx.valueType) : Types.getRootType();
if (keyType == null || valueType == null)
return null;
LocalVarNode vo = declareTempLocalVar(Types.getClassType(Types.getMapImplClassType().getClassNode(), new Type[] { keyType, valueType }));
VarDeclStmt vds = new VarDeclStmt(vo);
NewObjectExpr newExpr;
try {
newExpr = new NewObjectExpr(Types.getMapImplClassType());
} catch (MethodNotFoundException | AmbiguousMethodException ex) {
throw Exceptions.unexceptedException(ex);
}
List<Statement> stmts = new LinkedList<>();
stmts.add(vds);
stmts.add(new ExprStmt(new AssignExpr(new VarExpr(vo), newExpr)));
VarExpr ve = new VarExpr(vo);
List<TerminalNode> ids = ctx.Identifier();
for (int i = 0; i < ids.size(); i++) {
ExpressionContext e = ctx.expression(i);
ExprNode v = (ExprNode) visit(e);
ConstExpr k = new ConstExpr(ctx.Identifier(i).getText());
ExprNode[] args = new ExprNode[] { k, v };
InvocationExpr iv;
try {
iv = ObjectInvokeExpr.create(ve, "put", args);
} catch (MethodNotFoundException | AmbiguousMethodException ex) {
throw Exceptions.unexceptedException(ex);
}
ExprStmt es = new ExprStmt(iv);
stmts.add(es);
}
MultiStmtExpr mse = new MultiStmtExpr(stmts, ve);
mapAst(mse, ctx);
return mse;
}
use of kalang.ast.NewObjectExpr in project kalang by kasonyang.
the class AstBuilder method visitNewExpr.
@Override
public AstNode visitNewExpr(NewExprContext ctx) {
ObjectType clsType = parseClassType(ctx.classType());
if (clsType == null)
return null;
ExprNode[] params = visitAll(ctx.params).toArray(new ExprNode[0]);
List<ExprNode> paramList = new LinkedList(Arrays.asList(params));
NewObjectExpr newExpr;
try {
if (this.isNonStaticInnerClass(clsType.getClassNode())) {
paramList.add(0, new ThisExpr(this.getThisType()));
}
params = paramList.toArray(new ExprNode[paramList.size()]);
newExpr = new NewObjectExpr(clsType, params);
mapAst(newExpr, ctx);
return newExpr;
} catch (MethodNotFoundException ex) {
methodNotFound(ctx.classType().rawClass, clsType.getName(), "<init>", params);
return null;
} catch (AmbiguousMethodException ex) {
methodIsAmbiguous(ctx.classType().rawClass, ex);
return null;
}
}
use of kalang.ast.NewObjectExpr 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.NewObjectExpr 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