Esempio n. 1
0
        public void DefineAMultiFunction()
        {
            FnStarPrimitive fnprim = new FnStarPrimitive();
            Machine machine = new Machine();
            Parser parser = new Parser("([x] (+ x 1)) ([x y] (+ x y 1))");

            object[] parameters = new object[2];
            parameters[0] = parser.ParseForm();
            parameters[1] = parser.ParseForm();

            object result = fnprim.Apply(machine, machine.Environment, parameters);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(DefinedMultiFunction));
        }
Esempio n. 2
0
        public void DefineAFunctionWithVariableArguments()
        {
            FnStarPrimitive fnprim = new FnStarPrimitive();
            Machine machine = new Machine();
            Parser parser = new Parser("[x & xs] (+ x xs)");

            object arguments = parser.ParseForm();
            object body = parser.ParseForm();

            object result = fnprim.Apply(machine, machine.Environment, new object[] { arguments, body });

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(DefinedFunction));

            DefinedFunction func = (DefinedFunction)result;
            Assert.AreEqual(1, func.Arity);
            Assert.IsTrue(func.VariableArity);
        }
Esempio n. 3
0
        public void RaiseIfTooManyVarsMarkers()
        {
            FnStarPrimitive fnprim = new FnStarPrimitive();
            Machine machine = new Machine();
            Parser parser = new Parser("[& x & y] (+ x xs)");

            object arguments = parser.ParseForm();
            object body = parser.ParseForm();

            fnprim.Apply(machine, machine.Environment, new object[] { arguments, body });
        }
Esempio n. 4
0
        public void RaiseIfQualifiedArgumentNameInFunction()
        {
            FnStarPrimitive fnprim = new FnStarPrimitive();
            Machine machine = new Machine();
            Parser parser = new Parser("[foo/bar] (+ x xs)");

            object arguments = parser.ParseForm();
            object body = parser.ParseForm();

            fnprim.Apply(machine, machine.Environment, new object[] { arguments, body });
        }
Esempio n. 5
0
        public void DefineAndInvokeAMultiFunction()
        {
            FnStarPrimitive fnprim = new FnStarPrimitive();
            Machine machine = new Machine();
            Parser parser = new Parser("([x] (+ x 1)) ([x y] (+ x y 1))");

            object[] parameters = new object[2];
            parameters[0] = parser.ParseForm();
            parameters[1] = parser.ParseForm();

            object result = fnprim.Apply(machine, machine.Environment, parameters);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(DefinedMultiFunction));

            DefinedMultiFunction func = (DefinedMultiFunction)result;

            object result1 = func.Apply(machine, machine.Environment, new object[] { 1 });

            Assert.IsNotNull(result1);
            Assert.IsInstanceOfType(result1, typeof(int));
            Assert.AreEqual(2, result1);

            object result2 = func.Apply(machine, machine.Environment, new object[] { 1, 2 });

            Assert.IsNotNull(result2);
            Assert.IsInstanceOfType(result2, typeof(int));
            Assert.AreEqual(4, result2);
        }
Esempio n. 6
0
        public void SetMacroOnDefinedMultiFunction()
        {
            Machine machine = new Machine();
            machine.CreateNamespace("ns");
            FnStarPrimitive fnprim = new FnStarPrimitive();
            Parser parser = new Parser("([x] (+ x 1)) ([x y] (+ x y 1))");

            object[] parameters = new object[2];
            parameters[0] = parser.ParseForm();
            parameters[1] = parser.ParseForm();

            object result = fnprim.Apply(machine, machine.Environment, parameters);
            DefinedMultiFunction multifn = (DefinedMultiFunction)result;

            Variable variable = Variable.Intern(machine, "ns/func");

            machine.SetVariableValue(variable, multifn);

            variable.SetMacro(machine);

            object mresult = machine.GetVariableValue(variable);

            Assert.IsNotNull(mresult);
            Assert.IsInstanceOfType(mresult, typeof(DefinedMultiMacro));
        }