/// <summary>
        /// Sets the target variables names for a multivector input\output variable parameter in the code block
        /// </summary>
        /// <param name="valueAccess"></param>
        /// <param name="grade"></param>
        /// <param name="indexList"></param>
        /// <param name="getTargetVarName"></param>
        /// <returns></returns>
        public GMacTargetVariablesNaming SetMultivectorParameters(AstDatastoreValueAccess valueAccess, int grade, IEnumerable <int> indexList, Func <int, string> getTargetVarName)
        {
            if (valueAccess.IsNullOrInvalid())
            {
                return(this);
            }

            if (valueAccess.AssociatedValueAccess.ExpressionType.IsFrameMultivector() == false)
            {
                throw new InvalidOperationException("Specified macro parameter is not of multivector type");
            }

            //Find the parameters in the code block that are components of the given multivector value access
            var primitiveValueAccessList =
                CodeBlock.GetParametersValueAccess(
                    valueAccess.SelectMultivectorComponents(grade, indexList)
                    );

            foreach (var primitiveValueAccess in primitiveValueAccessList)
            {
                var id = primitiveValueAccess.GetBasisBladeId();

                var targetVarName = getTargetVarName(id);

                SetScalarParameter(primitiveValueAccess, targetVarName);
            }

            return(this);
        }
Exemple #2
0
        /// <summary>
        /// Bind a macro parameter of multivector type to a set of target language variables or constant values
        /// using a generating function acting on each basis blade of the multivector
        /// </summary>
        /// <param name="valueAccess"></param>
        /// <param name="bindingFunction"></param>
        /// <param name="ignoreNullPatterns"></param>
        /// <returns></returns>
        public GMacMacroBinding BindMultivectorUsing(AstDatastoreValueAccess valueAccess, Func <AstFrame, int, GMacScalarBinding> bindingFunction, bool ignoreNullPatterns = true)
        {
            if (valueAccess.IsNullOrInvalid())
            {
                throw new ArgumentNullException(nameof(valueAccess));
            }

            if (valueAccess.AssociatedValueAccess.ExpressionType.IsFrameMultivector() == false)
            {
                throw new InvalidOperationException("Specified macro parameter is not of multivector type");
            }

            var frameInfo =
                new AstFrame(
                    ((GMacFrameMultivector)valueAccess.AssociatedValueAccess.ExpressionType).ParentFrame
                    );

            var primitiveValueAccessList =
                valueAccess.ExpandAll();

            foreach (var primitiveValueAccess in primitiveValueAccessList)
            {
                var id = primitiveValueAccess.GetBasisBladeId();

                var scalarPattern = bindingFunction(frameInfo, id);

                if (ignoreNullPatterns == false || scalarPattern != null)
                {
                    BindScalarToPattern(primitiveValueAccess, scalarPattern);
                }
            }

            return(this);
        }
Exemple #3
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))
                ));
 }
Exemple #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, MathematicaScalar value)
 {
     return(BindScalarToPattern(
                valueAccess,
                GMacScalarBinding.CreateConstant(BaseMacro.Root, value.MathExpr)
                ));
 }
Exemple #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)
                ));
 }
Exemple #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())
                ));
 }
Exemple #7
0
        /// <summary>
        /// Remove the given parameters from the binding pattern
        /// </summary>
        /// <param name="valueAccess"></param>
        /// <returns></returns>
        public GMacMacroBinding UnBind(AstDatastoreValueAccess valueAccess)
        {
            if (valueAccess.IsNullOrInvalid())
            {
                return(this);
            }

            if (valueAccess.IsScalar)
            {
                _patternDictionary.Remove(valueAccess.ValueAccessName);
                return(this);
            }

            var valueAccessNamesList =
                valueAccess
                .AssociatedValueAccess
                .ExpandAll()
                .Select(v => v.GetName());

            foreach (var valueAccessName in valueAccessNamesList)
            {
                _patternDictionary.Remove(valueAccessName);
            }

            return(this);
        }
Exemple #8
0
        /// <summary>
        /// Bind a macro parameter of any type to a pattern of the same type
        /// </summary>
        /// <param name="valueAccess"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public GMacMacroBinding BindToTreePattern(AstDatastoreValueAccess valueAccess, IGMacTypedBinding pattern)
        {
            if (valueAccess.IsNullOrInvalid())
            {
                throw new ArgumentNullException(nameof(valueAccess));
            }

            if (valueAccess.AssociatedValueAccess.ExpressionType.IsSameType(pattern.GMacType.AssociatedType) == false)
            {
                throw new InvalidOperationException(
                          $"Specified macro parameter {valueAccess.ValueAccessName} of type {valueAccess.GMacTypeSignature} is not of same type {pattern.GMacType.GMacTypeSignature} as given pattern"
                          );
            }

            var assignmentsList = valueAccess.AssociatedValueAccess.ExpandAndAssignAll(pattern);

            foreach (var assignment in assignmentsList)
            {
                BindScalarToPattern(
                    assignment.Item1.ToAstDatastoreValueAccess(),
                    assignment.Item2
                    );
            }

            return(this);
        }
        /// <summary>
        /// Sets the target variables names for a multivector input\output variable parameter in the code block
        /// </summary>
        /// <param name="valueAccess"></param>
        /// <param name="getTargetVarName"></param>
        /// <returns></returns>
        public GMacTargetVariablesNaming SetMultivectorParameters(AstDatastoreValueAccess valueAccess, IDictionary <int, string> getTargetVarName)
        {
            if (valueAccess.IsNullOrInvalid())
            {
                return(this);
            }

            if (valueAccess.AssociatedValueAccess.ExpressionType.IsFrameMultivector() == false)
            {
                throw new InvalidOperationException("Specified macro parameter is not of multivector type");
            }

            //Find the parameters in the code block that are components of the given multivector value access
            var primitiveValueAccessList = CodeBlock.GetParametersValueAccess(valueAccess);

            foreach (var primitiveValueAccess in primitiveValueAccessList)
            {
                var id = primitiveValueAccess.GetBasisBladeId();

                string targetVarName;

                if (getTargetVarName.TryGetValue(id, out targetVarName) == false)
                {
                    continue;
                }

                SetScalarParameter(primitiveValueAccess, targetVarName);
            }

            return(this);
        }
        /// <summary>
        /// Sets the target variables names for a multivector input\output variable parameter in the code block
        /// </summary>
        /// <param name="valueAccess"></param>
        /// <param name="subspace"></param>
        /// <param name="getTargetVarName"></param>
        /// <returns></returns>
        public GMacTargetVariablesNaming SetMultivectorParameters(AstDatastoreValueAccess valueAccess, AstFrameSubspace subspace, Func <int, string> getTargetVarName)
        {
            if (valueAccess.IsNullOrInvalid())
            {
                return(this);
            }

            return(SetMultivectorParameters(valueAccess, subspace.BasisBladeIDs, getTargetVarName));
        }
Exemple #11
0
        /// <summary>
        /// If the given value access is a multivector this binds a given subspace to variables
        /// </summary>
        /// <param name="valueAccess"></param>
        /// <param name="grade"></param>
        /// <returns></returns>
        public GMacMacroBinding BindMultivectorPartToVariables(AstDatastoreValueAccess valueAccess, int grade)
        {
            if (valueAccess.IsNullOrInvalid())
            {
                throw new ArgumentNullException(nameof(valueAccess));
            }

            return(BindToVariables(valueAccess.SelectMultivectorComponents(grade)));
        }
Exemple #12
0
        /// <summary>
        /// Find all primitive parameters in this code block that are sub-components of the given
        /// macro parameter value access component
        /// </summary>
        /// <param name="paramValueAccess"></param>
        /// <returns></returns>
        public IEnumerable <IGMacCbParameterVariable> GetParameters(AstDatastoreValueAccess paramValueAccess)
        {
            var result = new List <IGMacCbParameterVariable>();

            if (paramValueAccess.IsNullOrInvalid())
            {
                return(result);
            }

            var name = paramValueAccess.ValueAccessName;

            //A single parameter is to be found
            if (paramValueAccess.IsPrimitive)
            {
                var item = ParameterVariables.FirstOrDefault(
                    p => p.ValueAccessName == name
                    );

                if (ReferenceEquals(item, null) == false)
                {
                    result.Add(item);
                }

                return(result);
            }

            //Partial multivectors require special treatment
            if (paramValueAccess.IsPartialMultivector)
            {
                var primitiveValueAccessList = paramValueAccess.ExpandAll();

                foreach (var primitiveValueAccess in primitiveValueAccessList)
                {
                    IGMacCbParameterVariable paramVar;

                    if (TryGetParameterVariable(primitiveValueAccess, out paramVar))
                    {
                        result.Add(paramVar);
                    }
                }

                return(result);
            }

            //All other cases can be searched by name
            name = name + ".";

            result.AddRange(
                ParameterVariables.Where(
                    p => p.ValueAccessName.IndexOf(name, StringComparison.Ordinal) == 0
                    )
                );

            return(result);
        }
Exemple #13
0
        /// <summary>
        /// Test if the given primitive macro parameter is used as a variable in the code block
        /// </summary>
        /// <param name="paramValueAccess"></param>
        /// <returns></returns>
        public bool IsParameterVariable(AstDatastoreValueAccess paramValueAccess)
        {
            var name = paramValueAccess.ValueAccessName;

            if (_parametersDictionary.Count > 0)
            {
                return(_parametersDictionary.ContainsKey(name));
            }

            return(ParameterVariables.FirstOrDefault(
                       item => item.ValueAccess.ValueAccessName == name
                       ) != null);
        }
Exemple #14
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);
        }
Exemple #15
0
        /// <summary>
        /// Try to get an input parameter variable by its associated datastore value access
        /// </summary>
        /// <param name="paramValueAccess"></param>
        /// <param name="inputParamVar"></param>
        /// <returns></returns>
        public bool TryGetInputParameterVariable(AstDatastoreValueAccess paramValueAccess, out GMacCbInputVariable inputParamVar)
        {
            inputParamVar = null;
            IGMacCbParameterVariable paramVar;

            if (!TryGetParameterVariable(paramValueAccess, out paramVar))
            {
                return(false);
            }

            inputParamVar = paramVar as GMacCbInputVariable;
            return(inputParamVar != null);
        }
Exemple #16
0
        /// <summary>
        /// Try to get a parameter variable by its associated datastore value access
        /// </summary>
        /// <param name="paramValueAccess"></param>
        /// <param name="paramVar"></param>
        /// <returns></returns>
        public bool TryGetParameterVariable(AstDatastoreValueAccess paramValueAccess, out IGMacCbParameterVariable paramVar)
        {
            var name = paramValueAccess.ValueAccessName;

            if (_parametersDictionary.Count > 0)
            {
                return(_parametersDictionary.TryGetValue(name, out paramVar));
            }

            paramVar = ParameterVariables.FirstOrDefault(
                item => item.ValueAccess.ValueAccessName == name
                );

            return(paramVar != null);
        }
Exemple #17
0
        private GMacMacroParameterBinding(AstDatastoreValueAccess valueAccess, Expr constExpr)
        {
            if (valueAccess == null)
            {
                throw new ArgumentNullException(nameof(valueAccess));
            }

            if (valueAccess.IsScalar == false)
            {
                throw new InvalidOperationException("Macro parameter must be of type 'scalar'");
            }

            ValueAccess  = valueAccess;
            ConstantExpr = constExpr;
        }
        /// <summary>
        /// Sets the target variables name for a scalar input\output variable parameter in the code block
        /// </summary>
        /// <param name="valueAccess"></param>
        /// <param name="targetVarName"></param>
        /// <returns></returns>
        public GMacTargetVariablesNaming SetScalarParameter(AstDatastoreValueAccess valueAccess, string targetVarName)
        {
            if (valueAccess.IsNullOrInvalid())
            {
                return(this);
            }

            IGMacCbParameterVariable paramVar;

            if (CodeBlock.TryGetParameterVariable(valueAccess, out paramVar))
            {
                paramVar.TargetVariableName = targetVarName;
            }

            return(this);
        }
        /// <summary>
        /// Sets the target variables names for an input\output variable parameter in the code block
        /// </summary>
        /// <param name="valueAccess"></param>
        /// <param name="getTargetVarName"></param>
        /// <returns></returns>
        public GMacTargetVariablesNaming SetParameters(AstDatastoreValueAccess valueAccess, Func <IGMacCbParameterVariable, string> getTargetVarName)
        {
            if (valueAccess.IsNullOrInvalid())
            {
                return(this);
            }

            //Find the parameters in the code block that are components of the given value access
            var paramVarsList = CodeBlock.GetParameters(valueAccess);

            foreach (var paramVar in paramVarsList)
            {
                paramVar.TargetVariableName = getTargetVarName(paramVar);
            }

            return(this);
        }
        /// <summary>
        /// Sets the target variables names for an input\output parameter
        /// </summary>
        /// <param name="valueAccess"></param>
        /// <param name="getTargetVarName"></param>
        /// <returns></returns>
        public GMacTargetVariablesNaming SetParameters(AstDatastoreValueAccess valueAccess, Func <AstDatastoreValueAccess, string> getTargetVarName)
        {
            if (valueAccess.IsNullOrInvalid())
            {
                return(this);
            }

            //Find the parameters in the code block that are components of the given value access
            var primitiveValueAccessList = CodeBlock.GetParametersValueAccess(valueAccess);

            foreach (var primitiveValueAccess in primitiveValueAccessList)
            {
                var targetVarName = getTargetVarName(primitiveValueAccess);

                SetScalarParameter(primitiveValueAccess, targetVarName);
            }

            return(this);
        }
Exemple #21
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);
        }
        /// <summary>
        /// Sets the target variables names for an input\output variable parameter in the code block
        /// </summary>
        /// <param name="valueAccess"></param>
        /// <param name="targetVarNamesDict"></param>
        /// <returns></returns>
        public GMacTargetVariablesNaming SetParameters(AstDatastoreValueAccess valueAccess, IDictionary <string, string> targetVarNamesDict)
        {
            if (valueAccess.IsNullOrInvalid())
            {
                return(this);
            }

            //Find the parameters in the code block that are components of the given value access
            var primitiveValueAccessList = CodeBlock.GetParametersValueAccess(valueAccess);

            foreach (var primitiveValueAccess in primitiveValueAccessList)
            {
                var name = primitiveValueAccess.ValueAccessName;

                string targetVarName;

                if (targetVarNamesDict.TryGetValue(name, out targetVarName))
                {
                    SetScalarParameter(primitiveValueAccess, targetVarName);
                }
            }

            return(this);
        }
Exemple #23
0
        /// <summary>
        /// Bind a macro parameter of any type to a constant of the same type
        /// </summary>
        /// <param name="valueAccess"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public GMacMacroBinding BindToConstants(AstDatastoreValueAccess valueAccess, AstValue value)
        {
            if (valueAccess.IsNullOrInvalid())
            {
                throw new ArgumentNullException(nameof(valueAccess));
            }

            //This is checked inside each primitive binding to select appropriate action according to the
            // BindOutputToConstantBehavior mamber
            //if (valueAccess.IsInputParameter == false)
            //    throw new InvalidOperationException(
            //        String.Format(
            //            "Specified value access {0} is not a macro input parameter",
            //            valueAccess.ValueAccessName
            //            )
            //        );

            if (valueAccess.AssociatedValueAccess.ExpressionType.IsSameType(value.AssociatedValue.ExpressionType) == false)
            {
                throw new InvalidOperationException(
                          $"Specified macro parameter {valueAccess.ValueAccessName} of type {valueAccess.GMacTypeSignature} is not of same type {value.GMacTypeSignature} as given value"
                          );
            }

            var assignmentsList = valueAccess.AssociatedValueAccess.ExpandAndAssignAll(value.AssociatedValue);

            foreach (var assignment in assignmentsList)
            {
                BindScalarToConstant(
                    assignment.Item1.ToAstDatastoreValueAccess(),
                    assignment.Item2.ToExpr()
                    );
            }

            return(this);
        }
Exemple #24
0
 internal static GMacMacroParameterBinding CreateConstant(AstDatastoreValueAccess valueAccess, Expr valueExpr)
 {
     return(new GMacMacroParameterBinding(valueAccess, valueExpr));
 }
Exemple #25
0
 internal static GMacMacroParameterBinding CreateVariable(AstDatastoreValueAccess valueAccess, Expr testValueExpr = null)
 {
     return(new GMacMacroParameterBinding(valueAccess, null, testValueExpr));
 }
Exemple #26
0
 /// <summary>
 /// Constructs a binding pattern from the parameters actual values.
 /// </summary>
 /// <param name="paramValueAccess"></param>
 /// <returns></returns>
 public IGMacTypedBinding GetParameterBindingPattern(AstDatastoreValueAccess paramValueAccess)
 {
     throw new NotImplementedException();
 }
Exemple #27
0
 /// <summary>
 /// Find all primitive parameters in this code block that are sub-components of the given
 /// macro parameter value access component and return their associated value access names
 /// </summary>
 /// <param name="paramValueAccess"></param>
 /// <returns></returns>
 public IEnumerable <string> GetParametersValueAccessNames(AstDatastoreValueAccess paramValueAccess)
 {
     return(GetParameters(paramValueAccess).Select(p => p.ValueAccessName));
 }
Exemple #28
0
 internal static GMacMacroParameterBinding Create(AstDatastoreValueAccess valueAccess, GMacScalarBinding scalarPattern, Expr testValueExpr = null)
 {
     return(new GMacMacroParameterBinding(valueAccess, scalarPattern.ConstantExpr, testValueExpr));
 }
Exemple #29
0
 internal GMacCbOutputVariable(string lowLevelName, int lowLevelId, LanguageValueAccess valueAccess, SteExpression rhsExpr)
     : base(lowLevelName, rhsExpr)
 {
     LowLevelId  = lowLevelId;
     ValueAccess = valueAccess.ToAstDatastoreValueAccess();
 }
        //internal void SetBladeParameterBinding(GMacMacroBinding macroBinding, string macroParamName, int macroParamGrade)
        //{
        //    macroBinding.BindMultivectorToVariables(macroParamName, macroParamGrade);
        //}

        internal void SetBasisBladeToArrayNaming(GMacTargetVariablesNaming targetNaming, AstDatastoreValueAccess macroParam, int macroParamGrade, string arrayVarName)
        {
            targetNaming.SetMultivectorParameters(
                macroParam,
                macroParamGrade,
                id => BasisBladeIdToTargetArrayItem(arrayVarName, id)
                );
        }