美文网首页
clojure java 交互 语法糖 展开点

clojure java 交互 语法糖 展开点

作者: onedam | 来源:发表于2022-04-13 12:21 被阅读0次
    public static Object macroexpand1(Object x) {
            if (x instanceof ISeq) {
                ISeq form = (ISeq)x;
                Object op = RT.first(form);
                if (isSpecial(op)) {
                    return x;
                }
    
                Var v = isMacro(op);
                if (v != null) {
                    ISeq args = RT.cons(form, RT.cons(LOCAL_ENV.get(), form.next()));
                    return v.applyTo(args);
                }
    
     // 这里如果是 java.util.Date/pares 等形式. 则变(展开)成 new 或  dot(.) special form 
                if (op instanceof Symbol) { 
                    Symbol sym = (Symbol)op;
                    String sname = sym.name;
                    Symbol target;
                    if (sym.name.charAt(0) == '.') {
                        if (RT.length(form) < 2) {
                            throw new IllegalArgumentException("Malformed member expression, expecting (.member target ...)");
                        }
    
                        target = Symbol.intern(sname.substring(1));
                        Object target = RT.second(form);
                        if (Compiler.HostExpr.maybeClass(target, false) != null) {
                            target = ((IObj)RT.list(IDENTITY, target)).withMeta(RT.map(new Object[]{RT.TAG_KEY, CLASS}));
                        }
    
                        return preserveTag(form, RT.listStar(DOT, target, target, form.next().next()));
                    }
    
                    if (namesStaticMember(sym)) {
                        target = Symbol.intern(sym.ns);
                        Class c = Compiler.HostExpr.maybeClass(target, false);
                        if (c != null) {
                            Symbol meth = Symbol.intern(sym.name);
                            return preserveTag(form, RT.listStar(DOT, target, meth, form.next()));
                        }
                    } else {
                        int idx = sname.lastIndexOf(46);
                        if (idx == sname.length() - 1) {
                            return RT.listStar(NEW, Symbol.intern(sname.substring(0, idx)), form.next());
                        }
                    }
                }
            }
    
            return x;
        }
    

    相关文章

      网友评论

          本文标题:clojure java 交互 语法糖 展开点

          本文链接:https://www.haomeiwen.com/subject/amqasrtx.html