Пример #1
0
 public LithpOpChain(LithpOpChain parent, LithpOpChain body, bool scoped)
     : base(body.ToArray())
 {
     Closure = new LithpClosure(parent.Closure);
     Parent  = parent;
     Scoped  = scoped;
 }
Пример #2
0
        public static LithpPrimitive Get(LithpList parameters, LithpOpChain state,
                                         LithpInterpreter interp)
        {
            LithpPrimitive name = CallBuiltin(Head, state, interp, parameters);

            return(state.Closure.Get(name));
        }
Пример #3
0
        public static LithpPrimitive Tail(LithpList parameters, LithpOpChain state,
                                          LithpInterpreter interp)
        {
            LithpList value = (LithpList)parameters[0];

            return(new LithpList(value.Skip(1).ToArray()));
        }
Пример #4
0
        public LithpOpChain CallImmediate()
        {
            LithpOpChain ch = new LithpOpChain(Parent, this);

            ch.IsImmediate = true;
            return(ch);
        }
Пример #5
0
        public static LithpPrimitive Head(LithpList parameters, LithpOpChain state,
                                          LithpInterpreter interp)
        {
            LithpList value = (LithpList)parameters[0];

            return(value[0]);
        }
Пример #6
0
        public static LithpPrimitive Round(LithpList parameters, LithpOpChain state,
                                           LithpInterpreter interp)
        {
            LithpFloat n = (LithpFloat)parameters[0].Cast(LithpType.FLOAT);

            return(new LithpInteger(Convert.ToUInt64(Math.Round(n.value))));
        }
Пример #7
0
        public static LithpPrimitive BitwiseNot(LithpList parameters, LithpOpChain state,
                                                LithpInterpreter interp)
        {
            LithpInteger n = (LithpInteger)parameters[0];

            return(new LithpInteger((n + 1) * -1));
        }
Пример #8
0
        public static LithpPrimitive Scope(LithpList parameters, LithpOpChain state,
                                           LithpInterpreter interp)
        {
            LithpFunctionDefinition def = (LithpFunctionDefinition)parameters[0];

            return(def.CloneWithScope(state));
        }
Пример #9
0
        public static LithpPrimitive DictPresent(LithpList parameters, LithpOpChain state,
                                                 LithpInterpreter interp)
        {
            LithpDict   dict = (LithpDict)parameters[0];
            LithpString key  = (LithpString)parameters[1].Cast(LithpType.STRING);

            return(dict.ContainsKey(key) ? LithpAtom.True : LithpAtom.False);
        }
Пример #10
0
        public static LithpPrimitive DictGet(LithpList parameters, LithpOpChain state,
                                             LithpInterpreter interp)
        {
            LithpDict   dict = (LithpDict)parameters[0];
            LithpString key  = (LithpString)parameters[1].Cast(LithpType.STRING);

            return(dict[key]);
        }
Пример #11
0
        public static LithpPrimitive Repeat(LithpList parameters, LithpOpChain state,
                                            LithpInterpreter interp)
        {
            LithpString  str = (LithpString)parameters[0];
            LithpInteger n   = (LithpInteger)parameters[1].Cast(LithpType.INTEGER);

            return(new LithpString(new string(str.Value[0], n)));
        }
Пример #12
0
        public static LithpPrimitive BitwiseXor(LithpList parameters, LithpOpChain state,
                                                LithpInterpreter interp)
        {
            LithpInteger a = (LithpInteger)parameters[0];
            LithpInteger b = (LithpInteger)parameters[1];

            return(new LithpInteger(a ^ b));
        }
Пример #13
0
        public static LithpPrimitive ParseInt(LithpList parameters, LithpOpChain state,
                                              LithpInterpreter interp)
        {
            LithpString str          = (LithpString)parameters[0].Cast(LithpType.STRING);
            string      dropDecimals = Regex.Replace(str, @"[.][0-9]+$", "");

            return(new LithpInteger(dropDecimals));
        }
Пример #14
0
        public static LithpPrimitive Recurse(LithpList Values, LithpOpChain state,
                                             LithpInterpreter interp)
        {
            var Target = state.Parent;

            while (Target && Target.FunctionEntry == null)
            {
                Target = Target.Parent;
            }
            if (!Target)
            {
                throw new Exception("Function entry not found");
            }

            // Rewind the target opchain
            Target.Rewind();

            // Get the OpChain function name with arity
            string FnAndArity = Target.FunctionEntry;
            ILithpFunctionDefinition def;

            if (state.Closure.TopMost.IsDefined(FnAndArity))
            {
                def = (ILithpFunctionDefinition)state.Closure.TopMost[FnAndArity];
            }
            else if (state.Closure.IsDefinedAny(FnAndArity))
            {
                def = (ILithpFunctionDefinition)state.Closure[FnAndArity];
            }
            else
            {
                FnAndArity = Target.FunctionEntry + "/*";
                if (state.Closure.TopMost.IsDefined(FnAndArity))
                {
                    def = (ILithpFunctionDefinition)state.Closure.TopMost[FnAndArity];
                }
                else if (state.Closure.IsDefinedAny(FnAndArity))
                {
                    def = (ILithpFunctionDefinition)state.Closure[FnAndArity];
                }
                else
                {
                    throw new MissingMethodException();
                }
            }

            // Set parameters for given function
            int i = 0;

            foreach (string p in def.Parameters)
            {
                Target.Closure.SetImmediate(p, Values[i]);
                i++;
            }

            // Return nothing
            return(LithpAtom.Nil);
        }
Пример #15
0
 public static LithpPrimitive If2(LithpList Values, LithpOpChain state,
                                  LithpInterpreter interp)
 {
     if (Values[0] == LithpAtom.True)
     {
         return(getIfResult(Values[1]));
     }
     return(LithpAtom.Nil);
 }
Пример #16
0
 public static LithpPrimitive Assert(LithpList parameters, LithpOpChain state,
                                     LithpInterpreter interp)
 {
     if (parameters[0] == LithpAtom.False)
     {
         throw new Exception("Assert failed");
     }
     return(LithpAtom.Nil);
 }
Пример #17
0
        public static LithpPrimitive IndexSet(LithpList parameters, LithpOpChain state,
                                              LithpInterpreter interp)
        {
            LithpList      list  = (LithpList)parameters[0];
            LithpInteger   index = (LithpInteger)parameters[1];
            LithpPrimitive value = parameters[2];

            list[index] = value;
            return(list);
        }
Пример #18
0
        public LithpList ResolveParameters(LithpFunctionCall call, LithpOpChain chain)
        {
            LithpList result = new LithpList();

            foreach (var x in call.Parameters)
            {
                result.Add((LithpPrimitive)resolve((LithpPrimitive)x, chain));
            }
            return(result);
        }
Пример #19
0
        public static LithpPrimitive Set(LithpList parameters, LithpOpChain state,
                                         LithpInterpreter interp)
        {
            LithpPrimitive name  = CallBuiltin(Head, state, interp, parameters);
            LithpList      tail  = (LithpList)CallBuiltin(Tail, state, interp, parameters);
            LithpPrimitive value = CallBuiltin(Head, state, interp, tail);

            state.Closure.Set(name, value);
            return(value);
        }
Пример #20
0
        static LithpOpChain LoadSample(string file)
        {
            var          compileWatch = Stopwatch.StartNew();
            LithpOpChain chain        = LithpJsonParser.LoadSample(file);

            chain.ImportBuiltins(Builtins);
            compileWatch.Stop();
            Console.WriteLine("Compile for {1} finished in {0}ms",
                              compileWatch.ElapsedMilliseconds, file);
            return(chain);
        }
Пример #21
0
        public static LithpPrimitive Print(LithpList parameters, LithpOpChain state,
                                           LithpInterpreter interp)
        {
            LithpPrimitive result = ApplyAction((A, B, X, Y) =>
            {
                return(A.ToString() + " " + B.ToString());
            }, parameters, state, interp);

            Console.WriteLine(result.ToString());
            return(LithpAtom.Nil);
        }
Пример #22
0
 public static LithpPrimitive CompareOr(LithpList Values, LithpOpChain state,
                                        LithpInterpreter interp)
 {
     foreach (var x in Values)
     {
         if (Values[0] == LithpAtom.True)
         {
             return(LithpAtom.True);
         }
     }
     return(LithpAtom.False);
 }
Пример #23
0
        protected static LithpPrimitive ApplyAction(LithpAction action, LithpList list,
                                                    LithpOpChain state, LithpInterpreter interp)
        {
            LithpPrimitive value = CallBuiltin(Head, state, interp, list);
            LithpList      tail  = (LithpList)CallBuiltin(Tail, state, interp, list);

            foreach (var x in tail)
            {
                value = action(value, x, state, interp);
            }
            return(value);
        }
Пример #24
0
        public static LithpOpChain Factorial()
        {
            // (
            LithpOpChain result = new LithpOpChain();
            // (if (== 1 N) (
            //   (1)
            // ) (else (
            //   (* N (fac (- N 1)))
            // )))
            LithpOpChain facBody = new LithpOpChain(result);
            LithpOpChain return1 = new LithpOpChain(facBody);

            return1.Add(new LithpLiteral(1));
            LithpOpChain elseBody = new LithpOpChain(facBody);

            elseBody.Add(
                LithpFunctionCall.New("*/*",
                                      LithpFunctionCall.New("get/1", new LithpVariableReference("N")),
                                      LithpFunctionCall.New("fac/1",
                                                            LithpFunctionCall.New("-/*",
                                                                                  LithpFunctionCall.New("get/1", new LithpVariableReference("N")),
                                                                                  new LithpLiteral(1)
                                                                                  )
                                                            )
                                      )
                );
            facBody.Add(
                LithpFunctionCall.New("if/3",
                                      LithpFunctionCall.New("==/2",
                                                            new LithpLiteral(1),
                                                            LithpFunctionCall.New("get/1", new LithpVariableReference("N"))
                                                            ),
                                      return1,
                                      LithpFunctionCall.New("else/1", elseBody)
                                      )
                );
            // (def fac #N :: (
            LithpFunctionCall def = LithpFunctionCall.New("def/2", LithpAtom.Atom("fac"),
                                                          LithpFunctionDefinition.New(result, "fac", facBody, "N")
                                                          );

            // )
            result.Add(def);
            int n;

#if DEBUG
            n = 10;
#else
            n = 500;
#endif
            result.Add(LithpFunctionCall.New("print/*", "Factorial result: ", LithpFunctionCall.New("fac/1", n)));
            return(result);
        }
Пример #25
0
        public static LithpPrimitive DictKeys(LithpList parameters, LithpOpChain state,
                                              LithpInterpreter interp)
        {
            LithpDict dict = (LithpDict)parameters[0];
            LithpList keys = new LithpList();

            foreach (var x in dict.Keys)
            {
                keys.Add(x);
            }
            return(keys);
        }
Пример #26
0
        public static LithpPrimitive SquareRoot(LithpList parameters, LithpOpChain state,
                                                LithpInterpreter interp)
        {
            switch (parameters[0].LithpType())
            {
            case LithpType.INTEGER:
                return(((LithpInteger)(parameters[0])).Sqrt());

            case LithpType.FLOAT:
                return(((LithpFloat)(parameters[0])).Sqrt());

            default:
                throw new NotImplementedException();
            }
        }
Пример #27
0
        public static LithpPrimitive Call(LithpList parameters, LithpOpChain state,
                                          LithpInterpreter interp)
        {
            LithpPrimitive def       = parameters[0];
            LithpList      defParams = (LithpList)Tail(LithpList.New(parameters), state, interp);

            switch (def.LithpType())
            {
            case LithpType.FN_NATIVE:
                return(((LithpFunctionDefinitionNative)def).Invoke(defParams, state, interp));

            case LithpType.FN:
                return(((LithpFunctionDefinition)def).Invoke(defParams, state, interp));

            case LithpType.ATOM:
            case LithpType.STRING:
                string strName = def.ToString();
                ILithpFunctionDefinition search;
                if ((object)state.Closure.TopMost != null && state.Closure.TopMost.IsDefined(strName))
                {
                    search = (ILithpFunctionDefinition)state.Closure.TopMost[strName];
                }
                else if (state.Closure.IsDefined(strName))
                {
                    search = (ILithpFunctionDefinition)state.Closure[strName];
                }
                else
                {
                    string arityStar = strName + "/*";
                    if ((object)state.Closure.TopMost != null && state.Closure.TopMost.IsDefined(arityStar))
                    {
                        search = (ILithpFunctionDefinition)state.Closure.TopMost[arityStar];
                    }
                    else if (state.Closure.IsDefined(arityStar))
                    {
                        search = (ILithpFunctionDefinition)state.Closure[arityStar];
                    }
                    else
                    {
                        throw new MissingMethodException();
                    }
                }
                return(search.Invoke(defParams, state, interp));

            default:
                throw new NotImplementedException();
            }
        }
Пример #28
0
        public static LithpPrimitive Def(LithpList parameters, LithpOpChain state,
                                         LithpInterpreter interp)
        {
            ILithpPrimitive name = parameters[0];

            if (name.LithpType() != LithpType.ATOM)
            {
                throw new ArgumentException("Function name must be an atom");
            }
            if (parameters[1].LithpType() != LithpType.FN)
            {
                throw new ArgumentException("Function body must be a FunctionDefinition");
            }
            LithpFunctionDefinition body = (LithpFunctionDefinition)parameters[1];

            body.SetName(parameters[0]);
            state.Closure.SetImmediate(body.Name, body);
            return(body);
        }
Пример #29
0
        private ILithpPrimitive resolve(ILithpPrimitive current, LithpOpChain chain)
        {
            switch (current.LithpType())
            {
            case LithpType.LITERAL:
                return(((LithpLiteral)current).Value);

            case LithpType.ATOM:
            case LithpType.DICT:
            case LithpType.INTEGER:
            case LithpType.LIST:
            case LithpType.STRING:
            case LithpType.FN:
            case LithpType.FN_NATIVE:
            case LithpType.OPCHAIN:
                return(current);

            case LithpType.FUNCTIONCALL:
                LithpFunctionCall call     = (LithpFunctionCall)current;
                LithpList         resolved = ResolveParameters(call, chain);
                LithpPrimitive    value    = InvokeResolved(call, resolved, chain);
                if (value.LithpType() == LithpType.OPCHAIN)
                {
                    LithpOpChain subchain = (LithpOpChain)value;
                    if (subchain.IsImmediate)
                    {
                        value = this.Run(new LithpOpChain(chain, subchain));
                    }
                }
                return(value);

            case LithpType.VAR:
                // TODO: Could just lookup the value now
                LithpVariableReference v = (LithpVariableReference)current;
                return(new LithpString(v.Name));

            default:
                throw new NotImplementedException();
            }
        }
Пример #30
0
        public LithpPrimitive Run(LithpOpChain chain)
        {
            ILithpPrimitive value = LithpAtom.Atom("nil");

            while (chain.AtEnd() == false)
            {
                ILithpOpChainMember current = chain.Next();
                switch (current.LithpType())
                {
                case LithpType.OPCHAIN:
                    value = this.Run(new LithpOpChain(chain, (LithpOpChain)current));
                    break;

                case LithpType.FUNCTIONCALL:
                    LithpFunctionCall call     = (LithpFunctionCall)current;
                    LithpList         resolved = ResolveParameters(call, chain);
                    value = InvokeResolved(call, resolved, chain);
                    if (value.LithpType() == LithpType.OPCHAIN)
                    {
                        LithpOpChain subchain = (LithpOpChain)value;
                        value = this.Run(new LithpOpChain(chain, subchain));
                    }
                    break;

                case LithpType.LITERAL:
                    value = ((LithpLiteral)current).Value;
                    break;

                case LithpType.FN:
                case LithpType.FN_NATIVE:
                    value = current;
                    break;

                default:
                    throw new NotImplementedException();
                }
            }

            return((LithpPrimitive)value);
        }