Ejemplo n.º 1
0
 public Closure(Operation body, EnviromentFrame en, Symbol[] arguments)
 {
     Argument = arguments;
     En       = en;
     Arity    = arguments.Length;
     Body     = body;
 }
Ejemplo n.º 2
0
        public Lambda(Cons arguments, EnviromentFrame clojure, Cons body)
        {
            Argument = arguments;
            Closure  = clojure;
            Arity    = arguments.Car() == null ? Arity = 0 : arguments.Count();

            Body = body.GetIterator().Select(subExp => (Cons)subExp).ToArray();
        }
Ejemplo n.º 3
0
 public object EvlString(string str, EnviromentFrame env, bool isWithMacro = true)
 {
     if (string.IsNullOrWhiteSpace(str))
     {
         return(null);
     }
     return(EvlSeq(Reader.Read(str), env, isWithMacro));
 }
Ejemplo n.º 4
0
            public EnviromentFrame Extend(List <object> r)
            {
                var argCnt  = Arity;
                var callEnv = new EnviromentFrame(En);

                foreach (var arg in Argument)
                {
                    callEnv.Define(arg, r[--argCnt]);
                }

                return(callEnv);
            }
Ejemplo n.º 5
0
 public EnviromentFrame(EnviromentFrame parent)
 {
     Parent    = parent;
     Variables = new Dictionary <string, dynamic>();
 }
Ejemplo n.º 6
0
 public object EvlSeq(Cons seq, EnviromentFrame env)
 {
     return(VirtualMashine.Eval(Compiler.Compile(seq), env));
 }
Ejemplo n.º 7
0
 public object EvlString(string str, EnviromentFrame env)
 {
     return(VirtualMashine.Eval(Compiler.Compile(str), env));
 }
Ejemplo n.º 8
0
 public Lambda()
 {
     Argument = new Cons();
     Closure  = new EnviromentFrame();
     Body     = new Cons[0];
 }
Ejemplo n.º 9
0
 public object EvlSeq(Cons seq, EnviromentFrame env, bool isWithMacro = true)
 {
     return(VirtualMashine.Eval(isWithMacro ? Compiler.Compile(MacroSystem.MacroProcessing(seq)) : Compiler.Compile(seq), env));
 }
Ejemplo n.º 10
0
        static object VM3(Operation operation, EnviromentFrame e)
        {
            object a   = null;
            var    x   = operation;
            var    rib = 0;

            var trueStack = new TrueStack(100);

            while (true)
            {
                switch (x.Cmd)
                {
                case Operation.Comand.Halt:
                    return(a);

                case Operation.Comand.Refer: {
                    a = e.Get(x.Var);
                    x = x.Branch1;
                    break;
                }

                case Operation.Comand.Constant: {
                    a = x.Value;
                    x = x.Branch1;
                    break;
                }

                case Operation.Comand.Close: {
                    a = new Closure(x.Branch2, e, x.Vars);
                    x = x.Branch1;
                    break;
                }

                case Operation.Comand.Test: {
                    x = GetSBool(a) == false ? x.Branch2 : x.Branch1;
                    break;
                }

                case Operation.Comand.Assign: {
                    e.Set(x.Var, a);
                    x = x.Branch1;
                    break;
                }

                case Operation.Comand.Extend: {
                    e.Define(x.Var, a);
                    x = x.Branch1;
                    break;
                }

                case Operation.Comand.Conti: {
                    var var = "v".O3Symbol();
                    a = new Closure(Operation.Nuate(TrueStack.Clone(trueStack), var), new EnviromentFrame(), new [] { var });
                    x = x.Branch1;
                    break;
                }

                case Operation.Comand.Nuate: {
                    trueStack = TrueStack.Clone(x.Value as TrueStack);
                    a         = e.Get(x.Var);
                    x         = Operation.Return();
                    break;
                }

                case Operation.Comand.Frame: {
                    trueStack.Push(rib);
                    trueStack.Push(e);
                    trueStack.Push(x.Branch2);

                    x = x.Branch1;

                    rib = 0;
                    break;
                }

                case Operation.Comand.Argument: {
                    trueStack.Push(a);
                    x = x.Branch1;
                    rib++;
                    break;
                }

                case Operation.Comand.Apply: {
                    if (a is MulticastDelegate)
                    {
                        var func       = a as MulticastDelegate;
                        var parameters = func.Method.GetParameters();

                        var argCnt = 0;
                        var cArg   = new List <object>();
                        foreach (var pi in parameters)
                        {
                            if (typeof(Params) == pi.ParameterType)
                            {
                                var par = new Params();
                                while (argCnt++ < rib)
                                {
                                    par.Add(trueStack.Pop());
                                }
                                cArg.Add(par);
                            }
                            else
                            {
                                if (argCnt++ < rib)
                                {
                                    cArg.Add(trueStack.Pop());
                                }
                            }
                        }

                        if (parameters.Length != cArg.Count)
                        {
                            throw new Exception($"Arity mismatch {func}, expected {parameters.Length}, given {cArg.Count} arguments");
                        }

                        a = func.DynamicInvoke(cArg.ToArray());
                        x = Operation.Return();
                        break;
                    }

                    if (a is Closure)
                    {
                        var func = a as Closure;
                        x = func.Body;
                        e = new EnviromentFrame(func.En);

                        if (rib != func.Argument.Length)
                        {
                            throw new Exception($"Arity mismatch, expected {func.Argument.Length}, given {rib} arguments");
                        }

                        foreach (var arg in func.Argument)
                        {
                            e.Define(arg, trueStack.Pop());
                        }

                        rib = 0;

                        break;
                    }

                    throw new Exception($"{a} is not a callable");
                }

                case Operation.Comand.Return: {
                    x   = trueStack.Pop() as Operation;
                    e   = trueStack.Pop() as EnviromentFrame;
                    rib = (int)trueStack.Pop();
                    break;
                }
                }
            }
        }
Ejemplo n.º 11
0
 public static object Eval(Operation x, EnviromentFrame e)
 {
     return(VM3(x, e));
 }