Search in sources :

Example 6 with ParameterNode

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

the class ObjectType method getParameterDescriptors.

private ParameterDescriptor[] getParameterDescriptors(MethodNode method) {
    ParameterNode[] pms = method.getParameters();
    Type[] ptypes = parseTypes(MethodUtil.getParameterTypes(method));
    ParameterDescriptor[] pds = new ParameterDescriptor[ptypes.length];
    for (int j = 0; j < pds.length; j++) {
        ParameterNode p = pms[j];
        pds[j] = new ParameterDescriptor(p.getName(), ptypes[j], p.modifier);
    }
    return pds;
}
Also used : ParameterNode(kalang.ast.ParameterNode)

Example 7 with ParameterNode

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

the class AstBuilder method declareLocalVar.

@Nullable
private LocalVarNode declareLocalVar(String name, Type type, int modifier, ParserRuleContext ctx) {
    LocalVarNode localVarNode = new LocalVarNode(type, name, modifier);
    ParameterNode param = this.getNamedParameter(name);
    LocalVarNode var = this.getNamedLocalVar(name);
    if (param != null || var != null) {
        handleSyntaxError("variable is defined", ctx);
        return null;
    }
    if (name != null) {
        this.varTables.put(name, localVarNode);
    }
    return localVarNode;
}
Also used : ParameterNode(kalang.ast.ParameterNode) LocalVarNode(kalang.ast.LocalVarNode) Nullable(javax.annotation.Nullable)

Example 8 with ParameterNode

use of kalang.ast.ParameterNode 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

ParameterNode (kalang.ast.ParameterNode)8 BlockStmt (kalang.ast.BlockStmt)5 ParameterExpr (kalang.ast.ParameterExpr)5 ExprNode (kalang.ast.ExprNode)4 ExprStmt (kalang.ast.ExprStmt)4 MethodNode (kalang.ast.MethodNode)4 ObjectType (kalang.core.ObjectType)4 ThisExpr (kalang.ast.ThisExpr)3 GenericType (kalang.core.GenericType)3 Nullable (javax.annotation.Nullable)2 AssignExpr (kalang.ast.AssignExpr)2 ClassReference (kalang.ast.ClassReference)2 LocalVarNode (kalang.ast.LocalVarNode)2 ObjectInvokeExpr (kalang.ast.ObjectInvokeExpr)2 ArrayType (kalang.core.ArrayType)2 ClassType (kalang.core.ClassType)2 Type (kalang.core.Type)2 AmbiguousMethodException (kalang.AmbiguousMethodException)1 AstNotFoundException (kalang.AstNotFoundException)1 MethodNotFoundException (kalang.MethodNotFoundException)1