Beispiel #1
0
        protected ILithpOpChainMember mapParam(dynamic P, LithpOpChain chain, string fnName)
        {
            var result = mapParamInner(P, chain, fnName);

            parserDebug("mapParam({0}) :: {1}", P, result);
            return(result);
        }
Beispiel #2
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 #3
0
        public LithpFunctionDefinition CloneWithScope(LithpOpChain state)
        {
            LithpOpChain            body  = this.body.CloneWithScope(state);
            LithpFunctionDefinition fnNew = new LithpFunctionDefinition(body.Parent, name, body, parameters);

            return(fnNew);
        }
Beispiel #4
0
        public LithpFunctionDefinition(LithpOpChain parent, string name,
                                       LithpOpChain body, string[] parameters)
        {
            this.name       = name;
            this.body       = body;
            this.parameters = parameters;
            Match x = Regex.Match(name, @"/(\d+|\*)$");

            if (x == Match.Empty)
            {
                arity      = parameters.Count();
                this.name += "/" + arity.Value.ToString();
            }
            else
            {
                string strArity = x.Groups[1].Value;
                if (strArity == "*")
                {
                    arity = null;
                }
                else
                {
                    arity = int.Parse(strArity);
                }
            }
        }
Beispiel #5
0
        public LithpOpChain Finalize()
        {
            LithpOpChain chain = new LithpOpChain();

            foreach (dynamic x in ops)
            {
                ILithpOpChainMember c = convert(chain, x);
                chain.Add(c);
            }
            return(chain);
        }
Beispiel #6
0
        protected ILithpOpChainMember mapParamInner(JValue v, LithpOpChain chain, string fnName)
        {
            Classification cls  = classify(v);
            string         strV = v.ToString();

            parserDebug("Classified: {0}", cls.ToString());
            if (cls.HasFlag(Classification.STRING_DOUBLE) || cls.HasFlag(Classification.STRING_SINGLE))
            {
                strV = strV.Substring(1, strV.Length - 2);
                string parsed = LithpParser.ParseEscapes(strV);
                if (cls.HasFlag(Classification.STRING_DOUBLE))
                {
                    return(new LithpLiteral(new LithpString(parsed)));
                }
                else if (cls.HasFlag(Classification.STRING_SINGLE))
                {
                    return(LithpAtom.Atom(parsed));
                }
            }
            else if (cls.HasFlag(Classification.VARIABLE))
            {
                switch (fnName)
                {
                case "get":
                case "set":
                case "var":
                    return(new LithpVariableReference(strV));

                default:
                    return(LithpFunctionCall.New("get/1", new LithpVariableReference(strV)));
                }
            }
            else if (cls.HasFlag(Classification.NUMBER))
            {
                if (cls.HasFlag(Classification.NUMBER_INTEGER))
                {
                    return(new LithpLiteral(new LithpInteger(strV)));
                }
                else if (cls.HasFlag(Classification.NUMBER_FLOAT))
                {
                    return(new LithpLiteral(new LithpFloat(strV)));
                }
                else
                {
                    throw new NotImplementedException();
                }
            }
            else if (cls.HasFlag(Classification.ATOM))
            {
                return(new LithpLiteral(LithpAtom.Atom(strV)));
            }
            throw new NotImplementedException();
        }
Beispiel #7
0
        protected ILithpOpChainMember convert(LithpOpChain chain, JObject curr)
        {
            JArray        fncode    = (JArray)curr["code"];
            JArray        fnparams  = (JArray)curr["_fnparams"];
            List <string> defParams = new List <string>();

            foreach (JValue p in fnparams)
            {
                defParams.Add(p.ToString());
                parserDebug("Param: {0}", p.ToString());
            }
            parserDebug("FunctionDef with params {0}, code: {1}", defParams.ToArray(), fncode);
            LithpOpChain        defBody = (LithpOpChain)convert(chain, fncode);
            string              name    = "anonymous:" + (AnonymousFnCounter++).ToString();
            ILithpOpChainMember def     = new LithpFunctionDefinition(chain, name, defBody, defParams.ToArray());

            parserDebug("FnDef created: {0}", def);
            return(def);
        }
Beispiel #8
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();
        }
Beispiel #9
0
 protected ILithpOpChainMember mapParamInner(JObject o, LithpOpChain chain, string fnName)
 {
     return(convert(chain, o));
 }
Beispiel #10
0
 protected ILithpOpChainMember mapParamInner(JArray arr, LithpOpChain chain, string fnName)
 {
     return(convert(chain, arr));
 }
Beispiel #11
0
 public LithpPrimitive convert(LithpOpChain chain, object curr)
 {
 }
Beispiel #12
0
 public LithpPrimitive mapParamInner(List <string> P, LithpOpChain chain, string fnName)
 {
 }
 public LithpPrimitive Invoke(LithpList parameters, LithpOpChain state,
                              LithpInterpreter interp)
 {
     return(fn_body(parameters, state, interp));
 }
Beispiel #14
0
 public static LithpFunctionDefinition New(LithpOpChain parent, string name,
                                           LithpOpChain body, params string[] parameters)
 {
     return(new LithpFunctionDefinition(parent, name, body, parameters));
 }