Exemplo n.º 1
0
        internal static MalType eval_ast(MalType ast, Environment environment)
        {
            var symbol = ast as MalSymbol;

            if (symbol != null)
            {
                if (environment.HasSymbol(symbol))
                {
                    return(environment.GetSymbol(symbol));
                }
                return(symbol);
            }

            var hash = ast as MalHashMap <MalType, MalType>;

            if (hash != null)
            {
                var items = hash.GetItems();
                Dictionary <MalType, MalType> output = items.Keys.ToDictionary(key => key, key => Eval(items[key], environment));

                return(hash.Repackage(output));
            }

            var listlike = ast as AbstractMalListlike <MalType>;

            if (listlike != null)
            {
                List <MalType> output = listlike.GetItems().Select(item => Eval(item, environment)).ToList();

                return(listlike.Repackage(output));
            }

            return(ast);
        }
Exemplo n.º 2
0
        private MalType ReadMetaData()
        {
            _tokens.RemoveAt(0); // remove the ^
            MalType meta = ReadForm();
            MalType item = ReadForm();

            return(new MalMetaData(item, meta));
        }
Exemplo n.º 3
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));
        }
Exemplo n.º 4
0
//        private static MalType Eval(MalType arg, Environment environment)
//        {
//            if (arg is MalList<MalType>)
//            {
//                MalList<MalType> newEvaluatedList = ((MalList<MalType>)eval_ast(arg, environment));
//                var items = newEvaluatedList.GetItems();
//                MalType firstItem = items.First();
//                var restItems = new MalList<MalType>(items.GetRange(1, items.Count - 1));
//                return environment.Apply((MalSymbol)firstItem, restItems);
//            }
//            /*
//             * ast is not a list: then return the result of calling eval_ast on it.
//             */
//            return eval_ast(arg, environment);
//        }

        private static string Print(MalType arg)
        {
            return(Printer.PrStr(arg));
        }
Exemplo n.º 5
0
 private static MalType Eval(MalType arg)
 {
     return(arg);
 }
Exemplo n.º 6
0
 public MalDeref(MalType val)
 {
     _val = val;
 }
Exemplo n.º 7
0
 public MalMetaData(MalType a, MalType b)
 {
     _a = a;
     _b = b;
 }
Exemplo n.º 8
0
 public MalQuasiQuote(MalType val)
 {
     _val = val;
 }
Exemplo n.º 9
0
 public MalUnquote(MalType val)
 {
     _val = val;
 }
Exemplo n.º 10
0
 public MalSpliceUnquote(MalType input)
 {
     _val = input;
 }
Exemplo n.º 11
0
 public MalQuote(MalType input)
 {
     _val = input;
 }
Exemplo n.º 12
0
 public MalType Set(MalSymbol key, MalType value)
 {
     _dict[key] = value;
     return(value);
 }