Пример #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 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)));
        }
Пример #3
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));
        }
Пример #4
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);
        }
Пример #5
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);
        }
Пример #6
0
        protected override bool compareMoreThan(LithpPrimitive other)
        {
            LithpInteger iOther = (LithpInteger)other;

            return(value > iOther.value);
        }
Пример #7
0
        public override bool compareEqual(LithpPrimitive other)
        {
            LithpInteger iOther = (LithpInteger)other;

            return(value == iOther.value);
        }
Пример #8
0
        protected override LithpPrimitive operatorDivide(LithpPrimitive other)
        {
            LithpInteger iOther = (LithpInteger)other;

            return(new LithpInteger(value / iOther.value));
        }
Пример #9
0
        protected override LithpPrimitive operatorMultiply(LithpPrimitive other)
        {
            LithpInteger iOther = (LithpInteger)other;

            return(new LithpInteger(value * iOther.value));
        }
Пример #10
0
        protected override LithpPrimitive operatorBinaryXor(LithpPrimitive other)
        {
            LithpInteger iOther = (LithpInteger)other.Cast(LithpBlunt.LithpType.INTEGER);

            return(new LithpInteger(value ^ iOther.value));
        }