Пример #1
0
 private static SchemeEnvironment InitializeDefaultEnvironment()
 {
     SchemeEnvironment env = new SchemeEnvironment(null);
     env.Add(Symbol.For("+"), new Primitive(Primitives.Add));
     env.Add(Symbol.For("-"), new Primitive(Primitives.Subtract));
     env.Add(Symbol.For("/"), new Primitive(Primitives.Divide));
     env.Add(Symbol.For("*"), new Primitive(Primitives.Multiply));
     env.Add(Symbol.For("="), new Primitive(Primitives.NumericEqual));
     env.Add(Symbol.For("cons"), new Primitive(Primitives.Cons));
     env.Add(Symbol.For("car"), new Primitive(Primitives.Car));
     env.Add(Symbol.For("cdr"), new Primitive(Primitives.Cdr));
     env.Add(Symbol.For("display"), new Primitive(Primitives.Display));
     env.Add(Symbol.For("exit"), new Primitive(Primitives.Exit));
     return env;
 }
Пример #2
0
        private static SchemeEnvironment BuildCallEnvironment(SchemeObject[] arguments, Closure closure)
        {
            ReadOnlyCollection<Variable> parameters = closure.Parameters;
            Variable restParameter = closure.RestParameter;

            if (arguments.Length != parameters.Count && restParameter == null)
                throw InvalidSyntaxException.Format(Strings.IncorrectArgumentCount, parameters.Count, arguments.Length);

            if (arguments.Length < parameters.Count)
                throw InvalidSyntaxException.Format(Strings.TooFewArguments, parameters.Count, arguments.Length);

            SchemeEnvironment result = new SchemeEnvironment(closure.Environment);
            int index = 0;
            for (; index < parameters.Count; index++)
                result.Add(parameters[index], arguments[index]);

            if (restParameter != null)
            {
                Datum restArgument = BuildRestArgument(arguments, index);
                result.Add(closure.RestParameter, restArgument);
            }

            return result;
        }