Ejemplo n.º 1
0
        public void Bootstrap()
        {
            if (_bootstrapped)
            {
                return;
            }
            _bootstrapped = true;

            this["="]    = InteropCompiler.Create("RT/Equiv");
            this["hash"] = InteropCompiler.Create("RT/Hash");
            this["-"]    = InteropCompiler.Create("RT/Subtract");
            this["/"]    = InteropCompiler.Create("RT/Divide");

            this["inc"] = InteropCompiler.Create("RT/Inc");
            this["dec"] = InteropCompiler.Create("RT/Dec");

            this[">"] = InteropCompiler.Create("RT/Gt");
            this["<"] = InteropCompiler.Create("RT/Lt");

            this["quot"] = InteropCompiler.Create("RT/Quot");
            this["mod"]  = InteropCompiler.Create("RT/Mod");

            this["odd?"]  = InteropCompiler.Create("RT/Odd");
            this["even?"] = InteropCompiler.Create("RT/Even");

            this["string?"] = InteropCompiler.Create("RT/IsString");
            this["bool?"]   = InteropCompiler.Create("RT/IsBool");
            this["int?"]    = InteropCompiler.Create("RT/IsInt");
            this["double?"] = InteropCompiler.Create("RT/IsDouble");
            this["list?"]   = InteropCompiler.Create("RT/IsList");
            this["nil?"]    = InteropCompiler.Create("RT/IsNil");

            this["seq"] = InteropCompiler.Create("Seq/Seq_");
            // this["first"] = InteropCompiler.Create("Seq/First");
            // this["count"] = InteropCompiler.Create("Seq/Count");
            // this["take"] = InteropCompiler.Create("Seq/Take");
            // this["drop"] = InteropCompiler.Create("Seq/Drop");
            // this["next"] = InteropCompiler.Create("Seq/Next");
            // this["last"] = InteropCompiler.Create("Seq/Last");
            // this["distinct"] = InteropCompiler.Create("Seq/Distinct");
            // this["concat"] = InteropCompiler.Create("Seq/Concat");
            // this["repeat"] = InteropCompiler.Create("Seq/Repeat");
            // this["range"] = InteropCompiler.Create("Seq/Range");

            this["list"] = new Function(args => new List <object>((IEnumerable <object>)args).ToImmutableList());
            // new Compiler().Compile("(def + (fn [& args] (reduce RT/Add args)))").Invoke();
            new Compiler().Compile("(def + (fn [x y] (RT/Add x y)))").Invoke();
            // this["fast+"] = new Function(args => RT.Add(args[0], args[1]));
            this["_*"] = new Function(args => RT.Multiply(args[0], args[1]));
            // this["+"] = new Func<int, int, int>((a,b) => a + b);
            new Compiler().Compile("(def * (fn [& args] (reduce _* args)))").Invoke();
            // new Compiler().Compile("(defn concat ([coll] (Seq/Seq_ coll)) ([& args] (reduce Seq/Concat args)))").Invoke();

            this["print"]     = new Function(args => { Console.WriteLine(args[0].Stringify()); return(null); });
            this["read-line"] = new Function(args => Console.ReadLine());
            this["loop"]      = new Loop();
            this["recur"]     = new Recur();
            this["deftest"]   = new DefTest();
        }
Ejemplo n.º 2
0
        // Could use number of args first then type to speed this up
        public object Invoke(object[] args)
        {
            var key = GetCachingKey(args);

            if (!cached.ContainsKey(key))
            {
                var mi = GetMethodInfo(args);
                var fn = InteropCompiler.Create(mi, info.GenericTypeParameters);
                cached[key] = fn;
            }
            return(cached[key].Invoke(args));
        }
Ejemplo n.º 3
0
 public Environment()
 {
     Bindings        = new Dictionary <string, object>();
     this["if"]      = new If();
     this["defn"]    = new Defn();
     this["fn"]      = InteropCompiler.Create("RT/Fn");
     this["let"]     = new Let();
     this["def"]     = new Function(args => RT.Def(args[0], args[1]));
     this["str"]     = new Function(args => RT.Str(args));         // InteropCompiler.Create("RT/Str");
     this["apply"]   = InteropCompiler.Create("RT/Apply");
     this["map"]     = InteropCompiler.Create("RT/Map");
     this["reduce"]  = new Reduce();
     this["interop"] = new InteropFn();
 }
Ejemplo n.º 4
0
        public object Invoke(object[] args)
        {
            if (!IsInterop)
            {
                try
                {
                    return(Environment.Current[Name]);
                }
                catch
                {
                    throw new System.Exception($"Unable to resolve symbol: {Name} in this context ({Token.Position})");
                }
            }

            if (_interop == null)
            {
                _interop = InteropCompiler.Create(Name);
            }

            return(_interop);
        }