Exemple #1
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);
        }
        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();
            }
        }
        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);
        }