Beispiel #1
0
        public LithpPrimitive Invoke(LithpList parameters, LithpOpChain state, LithpInterpreter interp)
        {
            LithpOpChain parent;

            if (body.Scoped)
            {
                parent = body;
            }
            else
            {
                parent = state;
            }
            LithpOpChain chain = new LithpOpChain(parent, body);

            // Arity star functions pass all arguments in first parameter
            if (arity.HasValue == false)
            {
                parameters = LithpList.New(parameters);
            }
            parameters.Each((Value, Index) =>
            {
                if (Index < this.parameters.Length)
                {
                    chain.Closure.SetImmediate(this.parameters[Index], Value);
                }
            });
            chain.FunctionEntry = name;
            return(interp.Run(chain));
        }
Beispiel #2
0
        protected ILithpOpChainMember convert(LithpOpChain chain, JArray curr)
        {
            var            eleFirst = curr[0];
            Classification clsFirst = classify(eleFirst);

            if (curr.Count == 0)
            {
                throw new NotImplementedException();
            }
            parserDebug("  First element: {0}", eleFirst);
            parserDebug("     Classified: {0}", clsFirst.ToString());
            if (curr.Count == 0)
            {
                throw new NotImplementedException();
            }
            if (clsFirst.HasFlag(Classification.STRING_SINGLE))
            {
                // Convert to a (call (get 'FnName') Params...)
                parserDebug("STRING_SINGLE, convert to FunctionCall");
                JValue v   = eleFirst as JValue;
                string tmp = v.ToString();
                eleFirst = tmp.Substring(1, tmp.Length - 2);
                clsFirst = classify(eleFirst);
                parserDebug("  First element: {0}", eleFirst);
                parserDebug("  Re-Classified: {0}", clsFirst.ToString());
            }
            if (clsFirst.HasFlag(Classification.ATOM))
            {
                // Function call
                parserDebug(" PARSE TO FUNCTIONCALL: {0}", curr);
                var       skipped    = curr.Skip(1);
                LithpList parameters = new LithpList();
                foreach (var x in skipped)
                {
                    var y = mapParam(x, chain, eleFirst.ToString());
                    var z = y as LithpPrimitive;
                    if ((object)z == null)
                    {
                        Debug.WriteLine("Failed to convert");
                    }
                    parameters.Add(z);
                }
                if (parameters.Count == 0 && clsFirst.HasFlag(Classification.NUMBER))
                {
                    parserDebug("CONVERT TO LITERAL");
                    return(this.mapParam(eleFirst, chain, eleFirst.ToString()));
                }
                else
                {
                    string plen = parameters.Count.ToString();
                    if (LithpParser.arityBuiltins.ContainsKey(eleFirst.ToString()))
                    {
                        plen = LithpParser.arityBuiltins[eleFirst.ToString()];
                    }
                    parserDebug("FUNCTIONCALL {0}/{1}", eleFirst, plen);
                    string name = eleFirst.ToString() + "/" + plen;
                    return(new LithpFunctionCall(name, parameters));
                }
            }
            else if (curr.Count > 0)
            {
                // Must be an OpChain
                parserDebug(" PARSE TO OPCHAIN");
                LithpOpChain newChain = new LithpOpChain(chain);
                newChain.Closure.TopMost = chain.Closure.TopMost ?? chain.Closure;
                for (int i = 0; i < curr.Count; i++)
                {
                    parserDebug("Member {0} of chain: {1}", i, curr[i]);
                    newChain.Add(mapParam(curr[i], chain, eleFirst.ToString()));
                }
                return(newChain);
            }
            throw new NotImplementedException();
        }
 public LithpFunctionCall(LithpAtom function, LithpList parameters)
 {
     this.Function   = function;
     this.Parameters = parameters;
 }
 public LithpPrimitive Invoke(LithpList parameters, LithpOpChain state,
                              LithpInterpreter interp)
 {
     return(fn_body(parameters, state, interp));
 }