예제 #1
0
        public void UserDefined1()
        {
            AType expected = AArray.Create(
                ATypes.AInteger,
                AInteger.Create(-10),
                AInteger.Create(1),
                AInteger.Create(2),
                AInteger.Create(3)
                );

            AType function = AFunc.Create("T", (Func <Aplus, AType, AType, AType, AType>)TestMethod, 4, "Test method");

            var scope = this.engine.CreateScope();

            scope.SetVariable(".T", function);

            scope.SetVariable(
                ".a",
                AArray.Create(
                    ATypes.AInteger,
                    AInteger.Create(0),
                    AInteger.Create(1),
                    AInteger.Create(2),
                    AInteger.Create(3)
                    )
                );

            this.engine.Execute <AType>("T{0;1;a} := -10", scope);

            Assert.AreEqual <AType>(expected, scope.GetVariable <AType>(".a"), "Incorrect assignment performed");
        }
예제 #2
0
        /// <summary>
        /// Searches for System functions inside the <see cref="SystemFunction"/> class.
        /// </summary>
        /// <returns>Returns a dictionary of system function names and the <see cref="AFunc"/> for each.</returns>
        internal static Dictionary <string, AType> DiscoverSystemFunctions()
        {
            Dictionary <string, AType> functions = new Dictionary <string, AType>();

            Type         systemFunctionType = typeof(SystemFunction);
            BindingFlags flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;

            foreach (MethodInfo method in systemFunctionType.GetMethods(flags))
            {
                SystemFunctionAttribute attibute = method.GetSingleAttribute <SystemFunctionAttribute>();

                if (attibute == null)
                {
                    continue;
                }

                if (functions.ContainsKey(attibute.Name))
                {
                    // TODO: what should we do here? Exception? Overwrite?
                }

                ParameterInfo[] parameters = method.GetParameters();
                List <Type>     types      = new List <Type>(parameters.Select(parameter => parameter.ParameterType));
                types.Add(method.ReturnType);

                Delegate methodDelegate = Delegate.CreateDelegate(DLR.Expression.GetFuncType(types.ToArray()), method);

                functions[attibute.Name] = AFunc.Create(attibute.Name, methodDelegate, parameters.Length, attibute.Description);
            }

            return(functions);
        }
예제 #3
0
        protected override int RunFile(ScriptSource source)
        {
            if (this.ScriptScope == null)
            {
                this.ScriptScope = this.Engine.CreateScope();

                this.ScriptScope.SetVariable(
                    "time",
                    AFunc.Create(
                        "time",
                        (Func <Aplus, AType>)Time,
                        1,
                        "returns the user time of the current process"
                        )
                    );
            }

            try
            {
                AType result = source.Execute <AType>(this.ScriptScope);
                Console.WriteLine(result.ToString(), Style.Out);
            }
            catch (Error error)
            {
                Console.ErrorOutput.Write(error);
                return(1);
            }
            catch (Exception)
            {
                throw;
            }
            return(0);
        }
예제 #4
0
        public void TypeError()
        {
            AType function = AFunc.Create("f", TestUtils.DyadicTypeAlternateFunction, 3, "TypeAlternatingMethod");

            var scope = this.engine.CreateScope();

            scope.SetVariable(".f", function);

            this.engine.Execute <AType>("1 2 3 f @ 0 (1 2 3)", scope);
        }
예제 #5
0
        /// <summary>
        /// Build an AFunction for the given method.
        /// </summary>
        /// <param name="method">The method to wrap inside an AFunction.</param>
        /// <param name="name">The name of AFunction.</param>
        /// <param name="description">Description for the AFunction.</param>
        /// <returns>Returns an AFunction for the given method.</returns>
        internal static AType BuildAFunction(this MethodInfo method, string name, string description)
        {
            ParameterInfo[] parameters = method.GetParameters();
            List <Type>     types      = new List <Type>(parameters.Select(parameter => parameter.ParameterType));

            types.Add(method.ReturnType);

            Delegate methodDelegate = Delegate.CreateDelegate(DLR.Expression.GetFuncType(types.ToArray()), method);

            return(AFunc.Create(name, methodDelegate, parameters.Length, description));
        }
예제 #6
0
        public void FloatIntMixedTypeResult2()
        {
            AType expected = AArray.Create(ATypes.AFloat, AFloat.Create(1.1), AFloat.Create(0));

            AType function = AFunc.Create("f", TestUtils.DyadicTypeAlternateFunction, 3, "TypeAlternatingMethod");

            var scope = this.engine.CreateScope();

            scope.SetVariable(".f", function);

            AType result = this.engine.Execute <AType>("0 1 f @ 0 (0 1)", scope);

            Assert.AreEqual(expected, result);
        }
예제 #7
0
        protected override int RunInteractive()
        {
            this.ScriptScope = this.Engine.CreateScope();

            this.ScriptScope.SetVariable(
                "time",
                AFunc.Create(
                    "time",
                    (Func <Aplus, AType>)Time,
                    1,
                    "returns the user time of the current process"
                    )
                );

            return(base.RunInteractive());
        }