Search in sources :

Example 41 with Type

use of kalang.core.Type in project kalang by kasonyang.

the class AstUtil method matchTypes.

/**
 * @param args
 * @param from
 * @param target
 * @return array when matched,null when not
 */
@Nullable
public static ExprNode[] matchTypes(ExprNode[] args, Type[] from, Type[] target) {
    if (args == null)
        return null;
    if (from == null || from.length == 0) {
        if (target == null || target.length == 0) {
            return args;
        } else {
            return null;
        }
    }
    if (from.length != target.length || from.length != args.length) {
        return null;
    }
    if (from.length == 0)
        return new ExprNode[0];
    ExprNode[] newParams = new ExprNode[from.length];
    for (int i = 0; i < from.length; i++) {
        Type f = from[i];
        Type t = target[i];
        newParams[i] = matchType(f, t, args[i]);
        if (newParams[i] == null)
            return null;
    }
    return newParams;
}
Also used : ExprNode(kalang.ast.ExprNode) ArrayType(kalang.core.ArrayType) Type(kalang.core.Type) ObjectType(kalang.core.ObjectType) GenericType(kalang.core.GenericType) ClassType(kalang.core.ClassType) Nullable(javax.annotation.Nullable)

Example 42 with Type

use of kalang.core.Type 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)

Example 43 with Type

use of kalang.core.Type in project kalang by kasonyang.

the class Ast2Java method visitMethodNode.

@Override
public String visitMethodNode(MethodNode node) {
    this.method = node;
    List<String> psList = new LinkedList<>();
    for (ParameterNode p : node.getParameters()) {
        psList.add(this.getVarStr(p));
    }
    // .join(",");
    String ps = String.join(",", psList);
    String exStr = "";
    Type[] exceptionTypes = node.getExceptionTypes();
    if (exceptionTypes.length > 0) {
        List<String> types = new ArrayList<>(exceptionTypes.length);
        for (Type et : exceptionTypes) types.add(et.getName());
        // .join(",");
        exStr = "throws " + String.join(",", types);
    }
    String mname = node.getName();
    String typeStr = "";
    int mdf = node.getModifier();
    if (mname.equals("<init>")) {
        mdf = mdf & (~Modifier.STATIC);
        Objects.requireNonNull(cls, "cls must be not null");
        int lastIdx = cls.name.lastIndexOf(".");
        if (lastIdx < 0) {
            mname = cls.name;
        } else {
            mname = cls.name.substring(lastIdx + 1);
        }
    } else {
        typeStr = node.getType().getName();
    }
    c(visitModifier(mdf) + " " + typeStr + " " + mname + "(" + ps + ")" + " " + exStr);
    if (node.getBody() != null) {
        c(visit(node.getBody()));
    } else {
        c(";");
    }
    return null;
}
Also used : Type(kalang.core.Type) ObjectType(kalang.core.ObjectType) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList)

Example 44 with Type

use of kalang.core.Type in project kalang by kasonyang.

the class Ast2Java method toJavaString.

private String toJavaString(ConstExpr ce) {
    Object v = ce.getValue();
    Type t = ce.getType();
    if (v instanceof String) {
        return "\"" + v + "\"";
    } else if (Types.CHAR_TYPE.equals(t)) {
        return "'" + v + "'";
    } else if (Types.NULL_TYPE.equals(t)) {
        return "null";
    } else {
        return String.valueOf(v);
    }
}
Also used : Type(kalang.core.Type) ObjectType(kalang.core.ObjectType)

Aggregations

ObjectType (kalang.core.ObjectType)44 Type (kalang.core.Type)44 ArrayType (kalang.core.ArrayType)37 ClassType (kalang.core.ClassType)36 GenericType (kalang.core.GenericType)36 PrimitiveType (kalang.core.PrimitiveType)33 WildcardType (kalang.core.WildcardType)30 ExprNode (kalang.ast.ExprNode)17 LinkedList (java.util.LinkedList)7 ExprStmt (kalang.ast.ExprStmt)7 VarExpr (kalang.ast.VarExpr)7 Nullable (javax.annotation.Nullable)6 AssignExpr (kalang.ast.AssignExpr)6 LocalVarNode (kalang.ast.LocalVarNode)6 BlockStmt (kalang.ast.BlockStmt)5 VarDeclStmt (kalang.ast.VarDeclStmt)5 AmbiguousMethodException (kalang.AmbiguousMethodException)4 MethodNotFoundException (kalang.MethodNotFoundException)4 ConstExpr (kalang.ast.ConstExpr)4 MethodNode (kalang.ast.MethodNode)4