Ejemplo n.º 1
0
 public static LithpAtom Atom(string name)
 {
     if (atomsByName.ContainsKey(name) == false)
     {
         int id   = counter++;
         var atom = new LithpAtom(name, id);
         atomsByName[name] = atom;
         atomsById[id]     = atom;
     }
     return((LithpAtom)atomsByName[name]);
 }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        protected override LithpPrimitive cast(LithpType newType)
        {
            switch (newType)
            {
            case LithpBlunt.LithpType.INTEGER:
                return(new LithpInteger(Value));

            case LithpBlunt.LithpType.FLOAT:
                return(new LithpFloat(Value));

            case LithpBlunt.LithpType.ATOM:
                return(LithpAtom.Atom(Value));

            default:
                throw new NotImplementedException();
            }
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 6
0
        public override bool compareEqual(LithpPrimitive other)
        {
            LithpAtom otherAtom = (LithpAtom)other;

            return(Id == otherAtom.Id);
        }