Пример #1
0
        private static MalType Times(MalList <MalType> arg)
        {
            var  items  = arg.GetItems().Select(x => (long)(MalLong)x);
            long result = items.Aggregate <long, long>(1, (current, item) => current * item);

            return(MalLong.Of(result));
        }
Пример #2
0
        private static MalType Minus(MalList <MalType> arg)
        {
            var items = arg.GetItems().Select(x => (long)(MalLong)x).ToList();

            if (items.Count() != 2)
            {
                throw new WrongNumberOfArgumentsException("wrong number of arguments for minus", null);
            }
            return(MalLong.Of(items[0] - items[1]));
        }
Пример #3
0
        private static MalType IntegerDivide(MalList <MalType> arg)
        {
            var items = arg.GetItems().Select(x => (long)(MalLong)x).ToImmutableList();

            if (items.Count() != 2)
            {
                throw new WrongNumberOfArgumentsException("wrong number of arguments for divide", null);
            }
            return(MalLong.Of(items[0] / items[1]));
        }
Пример #4
0
        public MalType Apply(MalSymbol firstItem, MalList <MalType> restItems)
        {
            if (FuncsHasSymbol(firstItem))
            {
                return(_funcs[firstItem](restItems));
            }

            if (_outer != null)
            {
                return(_outer.Apply(firstItem, restItems));
            }

            return(null);
        }
Пример #5
0
        public static MalType Eval(MalType arg, Environment environment)
        {
            var  argList   = arg as MalList <MalType>;
            bool breakHere = (arg.ToString() == "(+ 7 8)");

            if (argList != null)
            {
                if (argList.GetItems()[0].Equals(new MalSymbol("def!")))
                {
                    return(environment.Set(argList.GetItems()[1] as MalSymbol, Eval(argList.GetItems()[2], environment)));
                }

                if (argList.GetItems()[0].Equals(new MalSymbol("let*")))
                {
                    var letEnv   = Environment.Make(environment);
                    var listLike = argList.GetItems()[1] as AbstractMalListlike <MalType>;
                    if (listLike != null)
                    {
                        var letParams = listLike.GetItems();
                        int i         = 0;
                        do
                        {
                            var key   = letParams[i] as MalSymbol;
                            var value = Eval(letParams[i + 1], letEnv);
                            letEnv.Set(key, value);
                            i = i + 2;
                        } while (i < letParams.Count);
                    }

                    return(Eval(argList.GetItems()[2], letEnv));
                }

                MalList <MalType> newEvaluatedList = ((MalList <MalType>)eval_ast(arg, environment));
                var items     = newEvaluatedList.GetItems();
                var firstItem = items.First() as MalSymbol;
                var restItems = new MalList <MalType>(items.GetRange(1, items.Count - 1));

                var application = environment.Apply(firstItem, restItems);
                Console.WriteLine("Result of apply: " + application);
                return(application);
            }

            /*
             * ast is not a list: then return the result of calling eval_ast on it.
             */
            return(eval_ast(arg, environment));
        }
Пример #6
0
 private static MalType Plus(MalList <MalType> arg)
 {
     return(arg.GetItems().Aggregate(MalLong.Of(0), (current, item) => (MalLong)current + (MalLong)item));
 }