예제 #1
0
        public Lambda(Cons arguments, EnviromentFrame clojure, Cons body)
        {
            Argument = arguments;
            Closure  = clojure;
            Arity    = arguments.Car() == null ? Arity = 0 : arguments.Count();

            Body = body.GetIterator().Select(subExp => (Cons)subExp).ToArray();
        }
예제 #2
0
            public Cons Expand(Cons values)
            {
                var vls = values.GetIterator().ToArray();
                var dic = new Dictionary <string, object>();

                for (int i = 0; i < Args.Length; i++)
                {
                    dic[Args[i].Name] = vls[i].Car();
                }

                return(Replacer(Body, dic));
            }
예제 #3
0
        public Cons MacroProcessing(Cons seq)   // TODO
        {
            var rseq = new Cons();

            foreach (var o in seq.GetIterator())
            {
                var exp = o.Car();

                if (exp is Cons)
                {
                    var op        = exp.Car() as Symbol;
                    var arguments = exp.Cdr() as Cons;

                    if (op?.Name == "define-syntax")
                    {
                        DefineMacro(arguments);
                        continue;
                    }
                    else if (Macro.Lookup(op))
                    {
                        foreach (var o1 in (Macro.Get(op).Expand(arguments)).GetIterator())
                        {
                            if (o1.Car() is Cons)
                            {
                                rseq.Add(MacroProcessing(o1.Car()));
                            }
                            else
                            {
                                rseq.Add(o1.Car());
                            }
                        }

                        continue;
                    }

                    rseq.Add(MacroProcessing(exp));
                }
                else
                {
                    rseq.Add(exp);
                    var rest = o?.Cdr();
                    if (!(rest is Cons))
                    {
                        rseq.Add(rest, true);
                    }
                }
            }
            return(rseq);
        }
예제 #4
0
        private static MemoryStream WriteConsSeq(MemoryStream ms, Cons tree, SymbolTable symbolTable)
        {
            ms.WriteByte(Codes.Open);

            foreach (var o in tree.GetIterator())
            {
                WriteItem(ms, o.Car(), symbolTable);

                var cdr = o.Cdr();

                if (cdr is Cons || (cdr == null))
                {
                    continue;
                }

                ms.WriteByte(Codes.Dot);
                WriteItem(ms, cdr, symbolTable);
            }

            ms.WriteByte(Codes.Close);

            return(ms);
        }
예제 #5
0
            private Cons Replacer(Cons seq, Dictionary <string, object> vals)
            {
                var rSeq = new Cons();

                foreach (var o in seq.GetIterator())
                {
                    var itm = o.Car();
                    if (itm is Cons)
                    {
                        itm = Replacer(itm as Cons, vals);
                    }
                    else if (itm is Symbol)
                    {
                        if (vals.ContainsKey((itm as Symbol).Name))
                        {
                            itm = vals[(itm as Symbol).Name];
                        }
                    }

                    rSeq.Add(itm);
                }
                return(rSeq);
            }
예제 #6
0
        public static Cons MExpand(Cons seq)   // TODO
        {
            var rseq = new Cons();

            foreach (var o in seq.GetIterator())
            {
                var exp = o.Car();

                if (exp is Cons)
                {
                    var op        = exp.Car() as Symbol;
                    var arguments = exp.Cdr() as Cons;

                    if (op?.Name == "defmacro")
                    {
                        var mName = arguments?.Car().Car() as Symbol;
                        var mArg  = arguments?.Car().Cdr() as Cons;
                        var mBody = arguments?.Cdr() as Cons;
                        macro.Define(mName, new Macro(mArg, mBody));
                        continue;
                    }
                    else if (macro.Lookup(op))
                    {
                        foreach (var o1 in (macro.Get(op).Expand(arguments)).GetIterator())
                        {
                            rseq.Add(o1.Car());
                        }

                        continue;
                    }
                }

                rseq.Add(exp);
            }
            return(rseq);
        }
예제 #7
0
 public Macro(Cons args, Cons body)
 {
     Args = args.GetIterator().Select(i => i.Car() as Symbol).ToArray();
     Body = body;
 }
예제 #8
0
        public static Operation CompileBegin(Cons seq, Operation next)
        {
            var res = Operation.Nop();

            return(seq.GetIterator().Reverse().Aggregate(res, (current, exp) => Compile(exp.Car(), current.Cmd == Operation.Comand.Nop ? next : current)));
        }