Пример #1
0
        public static LithpPrimitive BitwiseNot(LithpList parameters, LithpOpChain state,
                                                LithpInterpreter interp)
        {
            LithpInteger n = (LithpInteger)parameters[0];

            return(new LithpInteger((n + 1) * -1));
        }
Пример #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 Scope(LithpList parameters, LithpOpChain state,
                                           LithpInterpreter interp)
        {
            LithpFunctionDefinition def = (LithpFunctionDefinition)parameters[0];

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

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

            return(new LithpList(value.Skip(1).ToArray()));
        }
Пример #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 BitwiseXor(LithpList parameters, LithpOpChain state,
                                                LithpInterpreter interp)
        {
            LithpInteger a = (LithpInteger)parameters[0];
            LithpInteger b = (LithpInteger)parameters[1];

            return(new LithpInteger(a ^ b));
        }
Пример #8
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));
        }
Пример #9
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)));
        }
Пример #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 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);
        }
Пример #12
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);
        }
Пример #13
0
 public static LithpPrimitive Assert(LithpList parameters, LithpOpChain state,
                                     LithpInterpreter interp)
 {
     if (parameters[0] == LithpAtom.False)
     {
         throw new Exception("Assert failed");
     }
     return(LithpAtom.Nil);
 }
Пример #14
0
 public static LithpPrimitive If2(LithpList Values, LithpOpChain state,
                                  LithpInterpreter interp)
 {
     if (Values[0] == LithpAtom.True)
     {
         return(getIfResult(Values[1]));
     }
     return(LithpAtom.Nil);
 }
Пример #15
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);
        }
Пример #16
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);
        }
Пример #17
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);
        }
Пример #18
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);
 }
Пример #19
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);
        }
Пример #20
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);
        }
Пример #21
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();
            }
        }
Пример #22
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();
            }
        }
Пример #23
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);
        }
Пример #24
0
        public static LithpPrimitive While(LithpList Values, LithpOpChain state,
                                           LithpInterpreter interp)
        {
            LithpOpChain test   = (LithpOpChain)Values[0];
            LithpOpChain action = (LithpOpChain)Values[1];

            test.Parent            = state;
            test.Closure.TopMost   = state.Closure.TopMost;
            test.Closure.Parent    = state.Closure;
            action.Parent          = state;
            action.Closure.TopMost = state.Closure.TopMost;
            action.Closure.Parent  = state.Closure;
            test.Rewind();
            action.Rewind();
            while (interp.Run(test) == LithpAtom.True)
            {
                test.Rewind();
                action.Rewind();
                interp.Run(action);
            }
            return(LithpAtom.Nil);
        }
Пример #25
0
        static void Main(string[] args)
        {
            // Initialize LithpBuiltins now
            LithpBuiltins    builtins = new LithpBuiltins();
            LithpInterpreter interp   = new LithpInterpreter();

            // Initialize parser
            LithpJsonParser.Test();

            var watch = Stopwatch.StartNew();

            if (false)
            {
                RunTests();
            }
            var sampleFactorial = LoadSample("factorial.ast");
            var sampleScope     = LoadSample("scope.ast");
            var sampleComplex   = LoadSample("complex.ast");
            var sampleIntegral2 = LoadSample("integral2.ast");
            var sampleWhile     = LoadSample("while.ast");
            var runWatch        = Stopwatch.StartNew();

            interp.Run(sampleFactorial);
            interp.Run(sampleScope);
            interp.Run(sampleComplex);
            interp.Run(sampleIntegral2);
            interp.Run(sampleWhile);
            interp.Run(LoadSample("var_args.ast"));
            interp.Run(LoadSample("recurse.ast"));
            runWatch.Stop();
            Console.WriteLine("Run finished: {0} function calls in {1}ms", LithpInterpreter.FunctionCalls, runWatch.ElapsedMilliseconds);
            watch.Stop();

            Console.WriteLine("Tests finished in {0}ms, hit enter", watch.ElapsedMilliseconds);
#if DEBUG
            Console.ReadLine();
#endif
        }
Пример #26
0
 public static LithpPrimitive Undefined(LithpList parameters, LithpOpChain state,
                                        LithpInterpreter interp)
 {
     return(LithpAtom.Atom("undefined"));
 }
Пример #27
0
        static void RunTests()
        {
            LithpOpChain chain = new LithpOpChain();
            LithpInteger one   = 1;
            LithpAtom    test  = "test";
            LithpDict    dict  = new LithpDict();

            dict["foo"]  = "bar";
            dict["num"]  = 1;
            dict["list"] = LithpList.New("Hello, world!", one, test);
            Console.WriteLine(dict.ToLiteral());
            LithpFunctionCall fncall = LithpFunctionCall.New("print/*", dict);

            Console.WriteLine("fncall tostring: {0}", fncall);

            LithpBuiltins    builtins = new LithpBuiltins();
            LithpInterpreter interp   = new LithpInterpreter();

            builtins[fncall.Function].Invoke(fncall.Parameters, chain, interp);

            // Now put it all together
            chain.Add(fncall);
            chain.ImportBuiltins(builtins);
            Console.WriteLine("Result of print: {0}", interp.Run(chain));

            // More complex
            LithpFunctionCall addStrings     = LithpFunctionCall.New("+/*", "foo", "bar");
            LithpFunctionCall printAddString = LithpFunctionCall.New("print/*",
                                                                     "Adding two strings: ", addStrings);

            chain.Add(printAddString);
            interp.Run(chain);
            chain.Add(addStrings);
            Console.WriteLine("Result of add strings: {0}", interp.Run(chain));

            LithpFunctionCall setVar = LithpFunctionCall.New("set/2",
                                                             new LithpVariableReference("Test"), "Foo");
            LithpFunctionCall printVar = LithpFunctionCall.New("print/*",
                                                               "Value of Test:", LithpFunctionCall.New(
                                                                   "get/1", new LithpVariableReference("Test")
                                                                   ));

            chain.Add(setVar);
            chain.Add(printVar);

            // Now run entire chain from the start
            chain.Rewind();
            interp.Run(chain);

            // Try a user-defined function
            LithpOpChain addBody = new LithpOpChain(chain);

            addBody.Add(LithpFunctionCall.New(
                            LithpAtom.Atom("+/*"),
                            LithpFunctionCall.New("get/1", new LithpVariableReference("A")),
                            LithpFunctionCall.New("get/1", new LithpVariableReference("B"))
                            ));
            chain.Add(LithpFunctionCall.New("def/2",
                                            LithpAtom.Atom("add"),
                                            LithpFunctionDefinition.New(chain, "add", addBody, "A", "B")
                                            ));
            chain.Add(LithpFunctionCall.New("print/*",
                                            "Calling user function add:", LithpFunctionCall.New(
                                                "add/2", 2, 5
                                                )
                                            ));
            interp.Run(chain);
        }
Пример #28
0
 public static LithpPrimitive Modulo(LithpList parameters, LithpOpChain state,
                                     LithpInterpreter interp)
 {
     return(parameters[0] % parameters[1]);
 }
Пример #29
0
 public static LithpPrimitive Sub(LithpList Values, LithpOpChain state,
                                  LithpInterpreter interp)
 {
     return(ApplyAction((A, B, X, Y) => A - B, Values, state, interp));
 }
Пример #30
0
 public static LithpPrimitive Dict(LithpList parameters, LithpOpChain state,
                                   LithpInterpreter interp)
 {
     return(new LithpDict());
 }