美文网首页
clojure clj -> java

clojure clj -> java

作者: onedam | 来源:发表于2022-04-08 22:55 被阅读0次
    (ns ast.testclass)
    (defn one [x] x)    
    

    -> 1个 extends AFunction 的class 2个 invoke 的 method .
    (汉景帝 梁王 ) qinxiongdi....

    package ast;
    
    import clojure.lang.AFunction;
    
    public final class testclass$one extends AFunction {
        public classfile$one() {
        }
    
        public static Object invokeStatic(Object x) {
            Object var10000 = x;
            x = null;
            return var10000;
        }
    
        public Object invoke(Object var1) {
            Object var10000 = var1;
            var1 = null;
            return invokeStatic(var10000);
        }
    }
    
    (defn two [x] (+ 2 x))
    (defn three [x] (let [a 10] (prn x) (list 2 x)))
    
    package ast;
    
    import clojure.lang.AFunction;
    import clojure.lang.Numbers;
    
    public final class testclass$two extends AFunction {
        public classfile$two() {
        }
    
        public static Object invokeStatic(Object x) {
            Object var10001 = x;
            x = null;
            return Numbers.add(2L, var10001);
        }
    
        public Object invoke(Object var1) {
            Object var10000 = var1;
            var1 = null;
            return invokeStatic(var10000);
        }
    }
    
    
    
    package ast;
    
    import clojure.lang.AFunction;
    import clojure.lang.IFn;
    import clojure.lang.RT;
    import clojure.lang.Var;
    
    public final class testclass$three extends AFunction {
        public static final Var const__1 = (Var)RT.var("clojure.core", "prn");
        public static final Var const__2 = (Var)RT.var("clojure.core", "list");
        public static final Object const__3 = 2L;
    
        public classfile$three() {
        }
    
        public static Object invokeStatic(Object x) {
            long a = 10L;
            ((IFn)const__1.getRawRoot()).invoke(x);
            IFn var10000 = (IFn)const__2.getRawRoot();
            Object var10002 = x;
            x = null;
            return var10000.invoke(const__3, var10002);
        }
    
        public Object invoke(Object var1) {
            Object var10000 = var1;
            var1 = null;
            return invokeStatic(var10000);
        }
    }
    
    ; 加了 类型 后 会使用 IFN 中定义的奇怪的 接口  . 性能 提高
    (defn jialei
     ^long
     [^long hash-basis ^long count] (+ hash-basis count))
    
    package ast;
    
    import clojure.lang.AFunction;
    import clojure.lang.Numbers;
    import clojure.lang.RT;
    import clojure.lang.IFn.LLL;
    
    public final class testclass$jialei extends AFunction implements LLL {
        public testclass$jialei() {
        }
    
        public static long invokeStatic(long hash_basis, long var2) {
            return Numbers.add(hash_basis, var2);
        }
    
        public Object invoke(Object var1, Object var2) {
            return invokeStatic(RT.longCast((Number)var1), RT.longCast((Number)var2));
        }
    
        public final long invokePrim(long var1, long var3) {
            return invokeStatic(var1, var3);
        }
    }
    

    注意 defmacro

    (defmacro m1 [x] (list x 3))
    

    生成的类中没法体现出是 macro . 答案在 testclass__init 类中 的

     const__32 = (Var)RT.var("ast.testclass", "m1");
    
     Var var10009 = const__32;
     var10009.setMeta((IPersistentMap)const__35);
     var10009.bindRoot(new m1());
     ((Var)const__32).setMacro();
    
     const__35 = (AFn)RT.map(new Object[]{RT.keyword((String)null, "arglists"), PersistentList.create(Arrays.asList(Tuple.create(Symbol.intern((String)null, "x")))), RT.keyword((String)null, "line"), 18, RT.keyword((String)null, "column"), 1, RT.keyword((String)null, "file"), "ast/testclass.clj"});
    
    
    package ast;
    
    import clojure.lang.AFunction;
    import clojure.lang.IFn;
    import clojure.lang.RT;
    import clojure.lang.Var;
    
    public final class testclass$m1 extends AFunction {
        public static final Var const__0 = (Var)RT.var("clojure.core", "list");
        public static final Object const__1 = 3L;
    
        public testclass$m1() {
        }
    
        public static Object invokeStatic(Object _AMPERSAND_form, Object _AMPERSAND_env, Object x) {
            IFn var10000 = (IFn)const__0.getRawRoot();
            Object var10001 = x;
            x = null;
            return var10000.invoke(var10001, const__1);
        }
    
        public Object invoke(Object var1, Object var2, Object var3) {
            Object var10000 = var1;
            var1 = null;
            Object var10001 = var2;
            var2 = null;
            Object var10002 = var3;
            var3 = null;
            return invokeStatic(var10000, var10001, var10002);
        }
    }
    
    package ast;
    
    import ast.testclass.five;
    import ast.testclass.fn__2076;
    import ast.testclass.four;
    import ast.testclass.jialei;
    import ast.testclass.loading__6721__auto____2074;
    import ast.testclass.m1;
    import ast.testclass.one;
    import ast.testclass.three;
    import ast.testclass.two;
    import ast.testclass.when;
    import clojure.lang.AFn;
    import clojure.lang.Compiler;
    import clojure.lang.IFn;
    import clojure.lang.IObj;
    import clojure.lang.IPersistentMap;
    import clojure.lang.LockingTransaction;
    import clojure.lang.PersistentList;
    import clojure.lang.RT;
    import clojure.lang.Symbol;
    import clojure.lang.Tuple;
    import clojure.lang.Var;
    import java.util.Arrays;
    import java.util.concurrent.Callable;
    
    public class testclass__init {
        public static final Var const__0;
        public static final AFn const__1;
        public static final AFn const__2;
        public static final Var const__3;
        public static final AFn const__11;
        public static final Var const__12;
        public static final AFn const__15;
        public static final Var const__16;
        public static final AFn const__19;
        public static final Var const__20;
        public static final AFn const__23;
        public static final Var const__24;
        public static final AFn const__27;
        public static final Var const__28;
        public static final AFn const__31;
        public static final Var const__32;
        public static final AFn const__35;
        public static final Var const__37;
        public static final AFn const__42;
    
        public static void load() {
            ((IFn)const__0.getRawRoot()).invoke(const__1);
            ((IFn)(new loading__6721__auto____2074())).invoke();
            Object var10002;
            if (((Symbol)const__1).equals(const__2)) {
                var10002 = null;
            } else {
                LockingTransaction.runInTransaction((Callable)(new fn__2076()));
                var10002 = null;
            }
    
            Var var10003 = const__3;
            var10003.setMeta((IPersistentMap)const__11);
            var10003.bindRoot(new one());
            Var var10004 = const__12;
            var10004.setMeta((IPersistentMap)const__15);
            var10004.bindRoot(new two());
            Var var10005 = const__16;
            var10005.setMeta((IPersistentMap)const__19);
            var10005.bindRoot(new three());
            Var var10006 = const__20;
            var10006.setMeta((IPersistentMap)const__23);
            var10006.bindRoot(new four());
            Var var10007 = const__24;
            var10007.setMeta((IPersistentMap)const__27);
            var10007.bindRoot(new five());
            Var var10008 = const__28;
            var10008.setMeta((IPersistentMap)const__31);
            var10008.bindRoot(new jialei());
            Var var10009 = const__32;
            var10009.setMeta((IPersistentMap)const__35);
            var10009.bindRoot(new m1());
            ((Var)const__32).setMacro();
            Object var10010 = null;
            Var var10011 = const__32;
            Var var10012 = const__37;
            var10012 = var10012.ns.refer(var10012.sym, var10012);
            var10012.setMeta((IPersistentMap)const__42);
            var10012.bindRoot(new when());
            ((Var)const__37).setMacro();
            Object var10013 = null;
            Var var10014 = const__37;
        }
    
        public static void __init0() {
            const__0 = (Var)RT.var("clojure.core", "in-ns");
            const__1 = (AFn)Symbol.intern((String)null, "ast.testclass");
            const__2 = (AFn)Symbol.intern((String)null, "clojure.core");
            const__3 = (Var)RT.var("ast.testclass", "one");
            const__11 = (AFn)RT.map(new Object[]{RT.keyword((String)null, "arglists"), PersistentList.create(Arrays.asList(Tuple.create(Symbol.intern((String)null, "x")))), RT.keyword((String)null, "line"), 3, RT.keyword((String)null, "column"), 1, RT.keyword((String)null, "file"), "ast/testclass.clj"});
            const__12 = (Var)RT.var("ast.testclass", "two");
            const__15 = (AFn)RT.map(new Object[]{RT.keyword((String)null, "arglists"), PersistentList.create(Arrays.asList(Tuple.create(Symbol.intern((String)null, "x")))), RT.keyword((String)null, "line"), 4, RT.keyword((String)null, "column"), 1, RT.keyword((String)null, "file"), "ast/testclass.clj"});
            const__16 = (Var)RT.var("ast.testclass", "three");
            const__19 = (AFn)RT.map(new Object[]{RT.keyword((String)null, "arglists"), PersistentList.create(Arrays.asList(Tuple.create(Symbol.intern((String)null, "x")))), RT.keyword((String)null, "line"), 5, RT.keyword((String)null, "column"), 1, RT.keyword((String)null, "file"), "ast/testclass.clj"});
            const__20 = (Var)RT.var("ast.testclass", "four");
            const__23 = (AFn)RT.map(new Object[]{RT.keyword((String)null, "arglists"), PersistentList.create(Arrays.asList(Tuple.create(Symbol.intern((String)null, "a"), Symbol.intern((String)null, "b"), Symbol.intern((String)null, "c")))), RT.keyword((String)null, "line"), 6, RT.keyword((String)null, "column"), 1, RT.keyword((String)null, "file"), "ast/testclass.clj"});
            const__24 = (Var)RT.var("ast.testclass", "five");
            const__27 = (AFn)RT.map(new Object[]{RT.keyword((String)null, "arglists"), PersistentList.create(Arrays.asList(Tuple.create(Symbol.intern((String)null, "x")), Tuple.create(Symbol.intern((String)null, "x"), Symbol.intern((String)null, "y")), Tuple.create(Symbol.intern((String)null, "x"), Symbol.intern((String)null, "y"), Symbol.intern((String)null, "z")))), RT.keyword((String)null, "line"), 8, RT.keyword((String)null, "column"), 1, RT.keyword((String)null, "file"), "ast/testclass.clj"});
            const__28 = (Var)RT.var("ast.testclass", "jialei");
            const__31 = (AFn)RT.map(new Object[]{RT.keyword((String)null, "arglists"), PersistentList.create(Arrays.asList(((IObj)Tuple.create(((IObj)Symbol.intern((String)null, "hash-basis")).withMeta((IPersistentMap)RT.map(new Object[]{RT.keyword((String)null, "tag"), Symbol.intern((String)null, "long")})), ((IObj)Symbol.intern((String)null, "count")).withMeta((IPersistentMap)RT.map(new Object[]{RT.keyword((String)null, "tag"), Symbol.intern((String)null, "long")})))).withMeta((IPersistentMap)RT.map(new Object[]{RT.keyword((String)null, "tag"), Symbol.intern((String)null, "long")})))), RT.keyword((String)null, "line"), 14, RT.keyword((String)null, "column"), 1, RT.keyword((String)null, "file"), "ast/testclass.clj"});
            const__32 = (Var)RT.var("ast.testclass", "m1");
            const__35 = (AFn)RT.map(new Object[]{RT.keyword((String)null, "arglists"), PersistentList.create(Arrays.asList(Tuple.create(Symbol.intern((String)null, "x")))), RT.keyword((String)null, "line"), 18, RT.keyword((String)null, "column"), 1, RT.keyword((String)null, "file"), "ast/testclass.clj"});
            const__37 = (Var)RT.var("ast.testclass", "when");
            const__42 = (AFn)RT.map(new Object[]{RT.keyword((String)null, "arglists"), PersistentList.create(Arrays.asList(Tuple.create(Symbol.intern((String)null, "test"), Symbol.intern((String)null, "&"), Symbol.intern((String)null, "body")))), RT.keyword((String)null, "doc"), "Evaluates test. If logical true, evaluates body in an implicit do.", RT.keyword((String)null, "added"), "1.0", RT.keyword((String)null, "line"), 19, RT.keyword((String)null, "column"), 1, RT.keyword((String)null, "file"), "ast/testclass.clj"});
        }
    
        static {
            __init0();
            Compiler.pushNSandLoader(RT.classForName("ast.testclass__init").getClassLoader());
    
            try {
                load();
            } catch (Throwable var1) {
                Var.popThreadBindings();
                throw var1;
            }
    
            Var.popThreadBindings();
        }
    }
    

    编译原理 需要掌握 源语言 和 目标语言

    上面生成的 xxx__init.class 调用点 RT.java

    static final public String LOADER_SUFFIX = "__init";
    static public void load(String scriptbase, boolean failIfNotFound) throws IOException, ClassNotFoundException{
        String classfile = scriptbase + LOADER_SUFFIX + ".class";
    

    宏生成的类。 每次load 加载到 ns 中的Var 时已变成了 可用的无宏 xxx

    相关文章

      网友评论

          本文标题:clojure clj -> java

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