Beispiel #1
0
        public override object VisitQuote([NotNull] SchemeParser.QuoteContext context)
        {
            var expr = (ISExpression)Visit(context.datum());

            return(AtomHelper.CreateCons(
                       AtomHelper.SymbolFromString("quote"),
                       AtomHelper.CreateCons(expr, AtomHelper.Nil)));
        }
Beispiel #2
0
        private object CreateList(SchemeParser.DatumContext[] elements)
        {
            if (elements.Length == 0)
            {
                return(AtomHelper.Nil);
            }

            return(AtomHelper.CreateCons((ISExpression)Visit(elements.First()), (ISExpression)CreateList(elements.Skip(1).ToArray())));
        }
Beispiel #3
0
        public override ISExpression Compile(ISchemeVM vm, string i)
        {
            var exprs = ParserHelpers.Parse(i);

            if (exprs.Length == 1)
            {
                return(exprs[0]);
            }

            return(AtomHelper.CreateCons(CompilerConstants.Begin, exprs.Unflatten()));
        }
Beispiel #4
0
        public static ISExpression Cons(IEnumerable <ISExpression> args)
        {
            var a = args.ToArray();

            return(AtomHelper.CreateCons(a[0], a[1]));
        }
        private ISExpression RewriteLet(ISchemeVM vm, ISExpression expr)
        {
            List <ISExpression> vars = new List <ISExpression>(), vals = new List <ISExpression>();
            ISExpression        defs;
            var x = expr as Cons;
            var c = x.ListCount();

            if (c < 3)
            {
                ThrowErr("let", "invalid number of arguments", expr.String());
            }

            //  (let name ((var val) ...) body)
            if (x.Get(1).IsSymbol())
            {
                var sym = x.Get(1);
                defs = x.Get(2);
                while (defs != AtomHelper.Nil)
                {
                    var n = defs.GetUnsafeCar().GetUnsafeCar();
                    var v = defs.GetUnsafeCar().GetUnsafeCdr().GetUnsafeCar();

                    if (!n.IsSymbol())
                    {
                        ThrowErr("let", "invalid argument 'bindings'", x.String());
                    }

                    vars.Add(n);
                    vals.Add(v);
                    defs = defs.GetUnsafeCdr();
                }

                // ((lambda ()
                //   (define name (lambda (vars) body))
                //   (name vals)))
                var body        = x.GetUnsafeCdr().GetUnsafeCdr().GetUnsafeCdr();
                var innerLambda = AtomHelper.CreateCons(
                    CompilerConstants.Lambda,
                    AtomHelper.CreateCons(vars.Unflatten(),
                                          Compile(vm, body))
                    );
                var define      = AtomHelper.CreateList(CompilerConstants.Define, sym, innerLambda);
                var loopCall    = AtomHelper.CreateCons(sym, Compile(vm, vals.Unflatten()));
                var outerLambda = AtomHelper.CreateList(CompilerConstants.Lambda, AtomHelper.Nil, define, loopCall);

                return(AtomHelper.CreateList(outerLambda));
            }
            // (let ((var val) ...) body)
            else if (x.Get(1).IsList())
            {
                defs = x.Get(1);

                while (defs != AtomHelper.Nil)
                {
                    var d = defs.GetUnsafeCar();

                    if (!d.IsList() || d.ListCount() != 2)
                    {
                        ThrowErr("let", "invalid argument 'bindings'", x.String());
                    }

                    var n = d.GetUnsafeCar();
                    var v = d.GetUnsafeCdr().GetUnsafeCar();

                    if (!n.IsSymbol())
                    {
                        ThrowErr("let", "invalid argument 'bindings'", x.String());
                    }

                    vars.Add(n);
                    vals.Add(v);
                    defs = defs.GetUnsafeCdr();
                }
                // ((lambda (vars) body) vals)
                var body = x.GetUnsafeCdr().GetUnsafeCdr();

                var lambda = AtomHelper.CreateCons(CompilerConstants.Lambda,
                                                   AtomHelper.CreateCons(vars.Unflatten(), Compile(vm, body)));
                return(AtomHelper.CreateCons(lambda, Compile(vm, vals.Unflatten())));
            }

            ThrowErr("let", "invalid argument 'bindings'", x.String());
            return(null);
        }
Beispiel #6
0
 private object CreatePair(SchemeParser.DatumContext[] elements)
 {
     return(AtomHelper.CreateCons((ISExpression)Visit(elements[0]), (ISExpression)Visit(elements[1])));
 }