Example #1
0
        public static LithpPrimitive Scope(LithpList parameters, LithpOpChain state,
                                           LithpInterpreter interp)
        {
            LithpFunctionDefinition def = (LithpFunctionDefinition)parameters[0];

            return(def.CloneWithScope(state));
        }
Example #2
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))));
        }
Example #3
0
        protected static string inspect(LithpPrimitive value)
        {
            if (value.LithpType() == LithpType.LIST)
            {
                LithpList list = (LithpList)value;
                if (list.Count > MaxDebugArrayLength)
                {
                    return("[" + list.Count.ToString() + " elements]");
                }
            }
            if (value.LithpType() == LithpType.DICT)
            {
                LithpDict dict = (LithpDict)value;
                if (dict.Keys.Count > MaxDebugArrayLength)
                {
                    return("{Dict:" + dict.Keys.Count.ToString() + " elements}");
                }
            }
            string result = value.ToLiteral();

            if (result.Length > MaxDebugLen)
            {
                result = "(" + value.LithpType().ToString() + ": too large to display)";
            }
            return(result);
        }
Example #4
0
        public static LithpPrimitive Get(LithpList parameters, LithpOpChain state,
                                         LithpInterpreter interp)
        {
            LithpPrimitive name = CallBuiltin(Head, state, interp, parameters);

            return(state.Closure.Get(name));
        }
Example #5
0
        public static LithpPrimitive Tail(LithpList parameters, LithpOpChain state,
                                          LithpInterpreter interp)
        {
            LithpList value = (LithpList)parameters[0];

            return(new LithpList(value.Skip(1).ToArray()));
        }
Example #6
0
        public static LithpPrimitive Head(LithpList parameters, LithpOpChain state,
                                          LithpInterpreter interp)
        {
            LithpList value = (LithpList)parameters[0];

            return(value[0]);
        }
Example #7
0
        public static LithpPrimitive BitwiseNot(LithpList parameters, LithpOpChain state,
                                                LithpInterpreter interp)
        {
            LithpInteger n = (LithpInteger)parameters[0];

            return(new LithpInteger((n + 1) * -1));
        }
Example #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));
        }
Example #9
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]);
        }
Example #10
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);
        }
Example #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)));
        }
Example #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));
        }
Example #13
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);
        }
Example #14
0
 public static LithpPrimitive Assert(LithpList parameters, LithpOpChain state,
                                     LithpInterpreter interp)
 {
     if (parameters[0] == LithpAtom.False)
     {
         throw new Exception("Assert failed");
     }
     return(LithpAtom.Nil);
 }
Example #15
0
 public static LithpPrimitive If2(LithpList Values, LithpOpChain state,
                                  LithpInterpreter interp)
 {
     if (Values[0] == LithpAtom.True)
     {
         return(getIfResult(Values[1]));
     }
     return(LithpAtom.Nil);
 }
Example #16
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);
        }
Example #17
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);
        }
Example #18
0
        protected override LithpPrimitive operatorPlus(LithpPrimitive other)
        {
            LithpList newList = new LithpList(value.ToArray());

            foreach (var x in (LithpList)other)
            {
                newList.Add(x);
            }
            return(newList);
        }
Example #19
0
        public LithpList Map(Func <LithpPrimitive, LithpPrimitive> Callback)
        {
            LithpList result = new LithpList();

            foreach (var x in this)
            {
                result.Add(Callback(x as LithpPrimitive));
            }
            return(result);
        }
Example #20
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);
        }
Example #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);
        }
Example #22
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);
        }
Example #23
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);
        }
Example #24
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);
 }
Example #25
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();
            }
        }
Example #26
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();
            }
        }
Example #27
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);
        }
Example #28
0
        public static string Inspect(LithpList value)
        {
            LithpList mapped = value.Map((v) => inspect((LithpPrimitive)v));
            string    result = "";
            bool      first  = true;

            foreach (var x in mapped)
            {
                if (!first)
                {
                    result += " ";
                }
                else
                {
                    first = false;
                }
                result += x;
            }
            return(result);
        }
Example #29
0
        protected override string toLiteral()
        {
            LithpList mapped = Map((v) => v.ToLiteral());
            string    result = "";
            bool      first  = true;

            foreach (var x in mapped)
            {
                if (!first)
                {
                    result += " ";
                }
                else
                {
                    first = false;
                }
                result += x;
            }

            return(result);
        }
Example #30
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();
            }
        }