Exemplo n.º 1
0
        /// <summary>
        /// If str represents a function name (no quotes, letters only), make a FuncArg. Otherwise, return null.
        /// In a declaration, function names can have a type, such as int n.
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static FunctionArg TryParseArg(string str)
        {
            string      functionName;
            FluencyType declType = FluencyType.Any;

            string[] halves = str.Split(_spliton, 2, StringSplitOptions.RemoveEmptyEntries);

            if (halves.Length == 1)
            {
                functionName = halves[0];
            }
            else
            {
                functionName = halves[1];
                declType     = PickType(halves[0]);
            }

            if (functionName.All(c => char.IsLetter(c)) || functionName == "...")
            {
                return(new FunctionArg(functionName)
                {
                    DeclaredType = declType
                });
            }

            return(null);
        }
Exemplo n.º 2
0
 public void CanParseFunctionsWithTypenames(string toparse, FluencyType expectedType, string expectedName)
 {
     if (Argument.TryParse(toparse, out var argument))
     {
         Assert.IsInstanceOfType(argument, typeof(FunctionArg));
         Assert.AreEqual(FluencyType.Function, argument.Type);
         Assert.AreEqual(expectedName, argument.GetAs <string>());
         FunctionArg arg = (FunctionArg)argument;
         Assert.AreEqual(expectedType, arg.DeclaredType);
     }
     else
     {
         Assert.Fail("Could not parse {0}.", toparse);
     }
 }
Exemplo n.º 3
0
 public WrapBinaryFold(Func <TReal, TReal, TReal> function, FluencyType returnType, string name, Value[] arguments, Value shortCircuitOn = null)
 {
     this.function       = function;
     this.type           = returnType;
     this.shortCircuitOn = shortCircuitOn;
     Name = name;
     if (arguments.Length == 1)
     {
         stored = arguments[0];
         if (returnType != FluencyType.Any && stored.Type != returnType)
         {
             throw new ExecutionException("Function {0} takes a {1}. You supplied a {2}: {3}", name, returnType, stored.Type, stored.ToString());
         }
     }
     else if (arguments.Length > 1)
     {
         throw new ExecutionException("Function {0} takes either zero or one parameters. You supplied {1}: {2}", name, arguments.Length, string.Join(", ", arguments.Select(x => x.ToString())));
     }
 }
Exemplo n.º 4
0
        public WrapBinaryStreamOutput(Func <TRealTop, TRealBottom, IEnumerable <TRealOut> > function, FluencyType returnType, FluencyType inputType, string name, Value[] arguments)
        {
            this.function   = function;
            this.returnType = returnType;
            Name            = name;

            if (arguments.Length == 1)
            {
                stored = arguments[0];
                if (inputType != FluencyType.Any && stored.Type != inputType)
                {
                    throw new ExecutionException("Function {0} takes a {1}. You supplied a {2}: {3}", name, inputType, stored.Type, stored.ToString());
                }
            }
            else if (arguments.Length > 1)
            {
                throw new ExecutionException("Function {0} takes either zero or one parameters. You supplied {1}: {2}", name, arguments.Length, string.Join(", ", arguments.Select(x => x.ToString())));
            }
        }
Exemplo n.º 5
0
 private bool ValidateArgs(Value[] arguments, FluencyType argType, string name)
 {
     if (arguments.Length == 1)
     {
         if (argType != FluencyType.Any && arguments[0].Type != argType)
         {
             throw new ExecutionException("Function {0} takes a {1}. You supplied a {2}: {3}", name, argType, stored.Type, stored.ToString());
         }
         else
         {
             return(true);
         }
     }
     else if (arguments.Length > 1)
     {
         throw new ExecutionException("Function {0} takes either zero or one top parameters. You supplied {1}: {2}", name, arguments.Length, string.Join(", ", arguments.Select(x => x.ToString())));
     }
     // if no arguments, don't store this one
     return(false);
 }
Exemplo n.º 6
0
        public WrapBinary(Func <TRealTop, TRealBottom, TRealOut> function, FluencyType argType, FluencyType returnType, string name, Value[] topArguments, Value[] bottomArguments = null)
        {
            this.function = function;
            this.type     = returnType;
            Name          = name;

            bottomArguments = bottomArguments ?? new Value[0];
            if (topArguments.Length > 0 && bottomArguments.Length > 0)
            {
                throw new ExecutionException("You must pass either one top argument, one bottom argument, or neither to {0}, not both.", Name);
            }

            if (ValidateArgs(topArguments, argType, name))
            {
                stored    = topArguments[0];
                topStored = true;
            }

            if (ValidateArgs(bottomArguments, argType, name))
            {
                stored    = bottomArguments[0];
                topStored = false;
            }
        }
Exemplo n.º 7
0
 public WrapUnary(Func <TRealIn, TRealOut> function, FluencyType returnType, string name)
 {
     this.function = function;
     this.type     = returnType;
     Name          = name;
 }
Exemplo n.º 8
0
 /// <summary>
 /// Construct a value from a C# value and a type.
 /// </summary>
 /// <param name="value"></param>
 /// <param name="type"></param>
 public Value(object value, FluencyType type)
 {
     _value = value;
     Type   = type;
     Done   = false;
 }
Exemplo n.º 9
0
 /// <summary>
 /// Construct a value from a parsed function argument.
 /// </summary>
 /// <param name="arg"></param>
 public Value(Argument arg)
 {
     Type   = arg.Type;
     _value = arg.GetAs <object>();
     Done   = false;
 }