Пример #1
0
 /// <summary>
 /// Bind a macro parameter of scalar type to a constant
 /// </summary>
 /// <param name="valueAccess"></param>
 /// <param name="value"></param>
 public GMacMacroBinding BindScalarToConstant(AstDatastoreValueAccess valueAccess, MathematicaScalar value)
 {
     return(BindScalarToPattern(
                valueAccess,
                GMacScalarBinding.CreateConstant(BaseMacro.Root, value.MathExpr)
                ));
 }
Пример #2
0
 /// <summary>
 /// Bind a macro parameter of scalar type to a constant
 /// </summary>
 /// <param name="valueAccessName"></param>
 /// <param name="value"></param>
 public GMacMacroBinding BindScalarToConstant(string valueAccessName, AstValueScalar value)
 {
     return(BindScalarToPattern(
                ToValueAccess(valueAccessName, ScalarType),
                GMacScalarBinding.CreateConstant(BaseMacro.Root, value.AssociatedScalarValue.Value.MathExpr)
                ));
 }
Пример #3
0
 /// <summary>
 /// Bind a macro parameter of scalar type to a constant
 /// </summary>
 /// <param name="valueAccessName"></param>
 /// <param name="value"></param>
 public GMacMacroBinding BindScalarToConstant(string valueAccessName, string value)
 {
     return(BindScalarToPattern(
                ToValueAccess(valueAccessName, ScalarType),
                GMacScalarBinding.CreateConstant(BaseMacro.Root, value.ToExpr(SymbolicUtils.Cas))
                ));
 }
Пример #4
0
 /// <summary>
 /// Bind a macro parameter of scalar type to a constant
 /// </summary>
 /// <param name="valueAccess"></param>
 /// <param name="value"></param>
 public GMacMacroBinding BindScalarToConstant(AstDatastoreValueAccess valueAccess, string value)
 {
     return(BindScalarToPattern(
                valueAccess,
                GMacScalarBinding.CreateConstant(BaseMacro.Root, value.ToExpr(SymbolicUtils.Cas))
                ));
 }
Пример #5
0
 /// <summary>
 /// Bind the given scalar parameter to a constant
 /// </summary>
 /// <param name="valueAccess"></param>
 /// <param name="constExpr"></param>
 /// <returns></returns>
 public GMacMacroBinding BindScalarToConstant(AstDatastoreValueAccess valueAccess, Expr constExpr)
 {
     return(BindScalarToPattern(
                valueAccess,
                GMacScalarBinding.CreateConstant(BaseMacro.Root, constExpr)
                ));
 }
Пример #6
0
 /// <summary>
 /// Bind a macro parameter of scalar type to a constant
 /// </summary>
 /// <param name="valueAccess"></param>
 /// <param name="value"></param>
 public GMacMacroBinding BindScalarToConstant(AstDatastoreValueAccess valueAccess, double value)
 {
     return(BindScalarToPattern(
                valueAccess,
                GMacScalarBinding.CreateConstant(BaseMacro.Root, value.ToExpr())
                ));
 }
Пример #7
0
 /// <summary>
 /// Bind the given scalar parameter to a constant
 /// </summary>
 /// <param name="valueAccessName"></param>
 /// <param name="constExpr"></param>
 /// <returns></returns>
 public GMacMacroBinding BindScalarToConstant(string valueAccessName, Expr constExpr)
 {
     return(BindScalarToPattern(
                ToValueAccess(valueAccessName, ScalarType),
                GMacScalarBinding.CreateConstant(BaseMacro.Root, constExpr)
                ));
 }
Пример #8
0
 /// <summary>
 /// Bind a macro parameter of scalar type to a given scalar pattern
 /// </summary>
 /// <param name="valueAccessName"></param>
 /// <param name="scalarPattern"></param>
 /// <returns></returns>
 public GMacMacroBinding BindScalarToPattern(string valueAccessName, GMacScalarBinding scalarPattern)
 {
     return(BindScalarToPattern(
                ToValueAccess(valueAccessName, ScalarType),
                scalarPattern
                ));
 }
Пример #9
0
        //TODO: Implement more methods to group macro parameters into composite patterns

        /// <summary>
        /// Try to find the scalar pattern associated with the given primitive parameter value access component
        /// </summary>
        /// <param name="paramValueAccess"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public bool TryGetScalarBinding(AstDatastoreValueAccess paramValueAccess, out GMacScalarBinding pattern)
        {
            GMacMacroParameterBinding paramBinding;

            if (_patternDictionary.TryGetValue(paramValueAccess.ValueAccessName, out paramBinding))
            {
                pattern = paramBinding.ToScalarBinding;
                return(true);
            }

            pattern = null;
            return(false);
        }
Пример #10
0
        /// <summary>
        /// Bind a macro parameter of any type to a set of variables
        /// </summary>
        /// <param name="valueAccess"></param>
        /// <returns></returns>
        public GMacMacroBinding BindToVariables(AstDatastoreValueAccess valueAccess)
        {
            if (valueAccess.IsNullOrInvalid())
            {
                throw new ArgumentNullException(nameof(valueAccess));
            }

            if (valueAccess.IsScalar)
            {
                BindScalarToPattern(valueAccess, GMacScalarBinding.CreateVariable(BaseMacro.Root));

                return(this);
            }

            var primitiveValueAccessList = valueAccess.ExpandAll();

            foreach (var primitiveValueAccess in primitiveValueAccessList)
            {
                BindScalarToPattern(primitiveValueAccess, GMacScalarBinding.CreateVariable(BaseMacro.Root));
            }

            return(this);
        }
Пример #11
0
 internal static GMacMacroParameterBinding Create(AstDatastoreValueAccess valueAccess, GMacScalarBinding scalarPattern, Expr testValueExpr = null)
 {
     return(new GMacMacroParameterBinding(valueAccess, scalarPattern.ConstantExpr, testValueExpr));
 }
Пример #12
0
        /// <summary>
        /// All binding methods eventually call this one so all checks are performed here
        /// If the given scalar binding is null the scalar macro parameter is un-bound
        /// </summary>
        /// <param name="valueAccess"></param>
        /// <param name="scalarBinding"></param>
        /// <returns></returns>
        public GMacMacroBinding BindScalarToPattern(AstDatastoreValueAccess valueAccess, GMacScalarBinding scalarBinding)
        {
            if (valueAccess.IsNullOrInvalid())
            {
                throw new ArgumentNullException(nameof(valueAccess));
            }

            var valueAccessName = valueAccess.ValueAccessName;

            if (valueAccess.IsMacroParameter == false || valueAccess.IsScalar == false)
            {
                throw new InvalidOperationException(
                          $"Unable to bind the value access {valueAccessName} as it's either non-scalar or not a macro parameter"
                          );
            }

            if (scalarBinding == null)
            {
                return(UnBind(valueAccess));
            }

            if (valueAccess.AssociatedValueAccess.RootSymbol.ParentLanguageSymbol.ObjectId != AssociatedMacro.ObjectId)
            {
                throw new InvalidOperationException(
                          $"Unable to bind the value access {valueAccessName} as it's not a peremeter of the macro {AssociatedMacro.SymbolAccessName}"
                          );
            }

            GMacMacroParameterBinding paramBinding = null;

            if (valueAccess.IsOutputParameter && scalarBinding.IsConstant)
            {
                switch (BindOutputToConstantBehavior)
                {
                case GMacBindOutputToConstantBehavior.Prevent:
                    throw new InvalidOperationException(
                              $"Unable to bind the output macro parameter {valueAccessName} to a constant"
                              );

                case GMacBindOutputToConstantBehavior.Ignore:
                    return(this);

                case GMacBindOutputToConstantBehavior.BindToVariable:
                    paramBinding = GMacMacroParameterBinding.CreateVariable(valueAccess);
                    break;
                }
            }
            else
            {
                paramBinding = GMacMacroParameterBinding.Create(valueAccess, scalarBinding);
            }

            //If the value access already exists remove it from the dictionary to preserve
            //order of parameters bindings
            if (_patternDictionary.ContainsKey(valueAccessName))
            {
                _patternDictionary.Remove(valueAccessName);
            }

            _patternDictionary.Add(valueAccessName, paramBinding);

            return(this);
        }