Exemplo n.º 1
0
        private ILanguageValue EvaluateBasicPolyadicMacroCall(GMacMacro macro, OperandsByValueAccess operands)
        {
            PushRecord(macro.ChildScope, false);

            //Initialize parameters to their default values
            foreach (var param in macro.Parameters)
            {
                var paramValue = GMacRootAst.CreateDefaultValue(param.SymbolType);

                ActiveAr.AddSymbolData(param, paramValue);
            }

            //Modify assigned parameters values from macro call operands
            foreach (var command in operands.AssignmentsList)
            {
                Visit(command);
            }

            //Execute macro body
            macro.SymbolBody.AcceptVisitor(this);
            //macro.OptimizedCompiledBody.AcceptVisitor(this);

            //Read output parameter value
            var value = ActiveAr.GetSymbolData(macro.FirstOutputParameter);

            PopRecord();

            return(value);
        }
Exemplo n.º 2
0
        private ILanguageExpression CompileMacroCall(GMacMacro macro, OperandsByValueAccess operands)
        {
            PushRecord(macro.ChildScope, false);

            foreach (var param in macro.Parameters)
            {
                CompileVariable(param);

                //Set the initial values of all parameters of called macro to their default
                var defaultAssignmentCommand =
                    new OperandsByValueAccessAssignment(
                        LanguageValueAccess.Create(param),
                        GMacRootAst.CreateDefaultValue(param.SymbolType)
                        );

                CompileParameterAssignment(defaultAssignmentCommand);
            }

            foreach (var command in operands.AssignmentsList)
            {
                CompileParameterAssignment(command);
            }

            //this.Visit(macro.ProcedureBody);
            Visit(macro.OptimizedCompiledBody);

            var compiledOutputVariable = GetSymbolData(macro.OutputParameter);

            PopRecord();

            return(LanguageValueAccess.Create(compiledOutputVariable));
        }
Exemplo n.º 3
0
        internal void Generate_BasicPolyadic_MacroCall(GMacMacro macro, OperandsByValueAccess operands)
        {
            //this.Log.Append(macro.OperatorName);
            Log.Append("(");

            var flag = false;

            foreach (var operand in operands.AssignmentsList)
            {
                if (flag)
                {
                    Log.Append(", ");
                }
                else
                {
                    flag = true;
                }

                operand.LhsValueAccess.AcceptVisitor(this);

                Log.Append(" : ");

                Log.Append(operand.LhsValueAccess.ExpressionType.TypeSignature);

                Log.Append(" = ");

                operand.RhsExpression.AcceptVisitor(this);
            }

            Log.Append(")");
        }
Exemplo n.º 4
0
 internal BasicPolyadic CreateConstructorExpression(OperandsByValueAccess operands)
 {
     return
         (BasicPolyadic.Create(
              this,
              GMacStructureConstructor.Create(this),
              operands
              ));
 }
Exemplo n.º 5
0
        public OperandsByValueAccess ReplaceLValueByExpression(OperandsByValueAccess oldOperands, SymbolLValue oldLvalue, ILanguageExpressionAtomic newExpr)
        {
            foreach (var assignment in oldOperands.AssignmentsList)
            {
                var newOpExpr = ReplaceLValueByExpression(assignment.RhsExpression, oldLvalue, newExpr);

                assignment.ChangeRhsExpression(newOpExpr);
            }

            return(oldOperands);
        }
Exemplo n.º 6
0
        public IEnumerable <SymbolLValue> GetLValues(OperandsByValueAccess operands)
        {
            var lvalues = Enumerable.Empty <SymbolLValue>();

            return
                (operands
                 .AssignmentsList
                 .Where(assignment => assignment.RhsExpression is LanguageValueAccess)
                 .Aggregate(
                     lvalues,
                     (current, assignment) => current.Concat(((LanguageValueAccess)assignment.RhsExpression).AccessLValues)
                     ));
        }
Exemplo n.º 7
0
        private ILanguageExpression CompileStructureConstructor(GMacStructureConstructor structureCons, OperandsByValueAccess operands)
        {
            ILanguageExpressionAtomic compiledDefaultValueSource = null;

            if (structureCons.HasDefaultValueSource)
            {
                compiledDefaultValueSource =
                    (ILanguageExpressionAtomic)CompileExpression(structureCons.DefaultValueSource);
            }

            var compiledOperands = OperandsByValueAccess.Create();

            foreach (var command in operands.AssignmentsList)
            {
                var compiledLhsValue = CompileLhsValueAccess(command.LhsValueAccess);

                var compiledRhsExpr =
                    (ILanguageExpressionAtomic)CompileExpression(command.RhsExpression);

                compiledOperands.AddOperand(compiledLhsValue, compiledRhsExpr);
            }

            return(structureCons.Structure.CreateConstructorExpression(compiledDefaultValueSource, compiledOperands));
        }
        private ILanguageExpression translate_Expression_Function_Macro(GMacMacro macro, ParseTreeNode node)
        {
            var operands = OperandsByValueAccess.Create();

            if (node.ChildNodes.Count == 0)
            {
                return(BasicPolyadic.Create(macro.OutputParameterType, macro, operands));
            }

            var expressionFunctionInputsNode = node.ChildNodes[0];

            var subNode = expressionFunctionInputsNode.ChildNodes[0];

            switch (subNode.Term.ToString())
            {
            case GMacParseNodeNames.ExpressionFunctionInputsAssignments:
                var expressionFunctionInputsAssignmentsNode = subNode;

                foreach (var nodeExpressionFunctionInputsAssignmentsItem in expressionFunctionInputsAssignmentsNode.ChildNodes)
                {
                    var lhsNode = nodeExpressionFunctionInputsAssignmentsItem.ChildNodes[0].ChildNodes[0];
                    var rhsNode = nodeExpressionFunctionInputsAssignmentsItem.ChildNodes[1];

                    var lhsValAccess = GMacValueAccessGenerator.Translate_LValue_MacroParameter(Context, lhsNode, macro);

                    var rhsExpr =
                        BasicExpressionGenerator.Generate_PolyadicOperand(
                            lhsValAccess.ExpressionType,
                            GMacExpressionGenerator.Translate(BasicExpressionGenerator, rhsNode)
                            );

                    operands.AddOperand(lhsValAccess, rhsExpr);
                }

                break;

            case GMacParseNodeNames.ExpressionFunctionInputsExpressions:
                var nodeExpressionFunctionInputsExpressions = subNode;

                var i = -1;
                foreach (var parameter in macro.Parameters)
                {
                    if (i >= nodeExpressionFunctionInputsExpressions.ChildNodes.Count)
                    {
                        break;
                    }

                    //The first parameter of any macro is the 'result' output parameter; ignore it
                    if (i >= 0)
                    {
                        var rhsNode = nodeExpressionFunctionInputsExpressions.ChildNodes[i];

                        var lhsValAccess = LanguageValueAccess.Create(parameter);

                        var rhsExpr =
                            BasicExpressionGenerator.Generate_PolyadicOperand(
                                lhsValAccess.ExpressionType,
                                GMacExpressionGenerator.Translate(BasicExpressionGenerator, rhsNode)
                                );

                        operands.AddOperand(lhsValAccess, rhsExpr);
                    }

                    i = i + 1;
                }

                if (nodeExpressionFunctionInputsExpressions.ChildNodes.Count > i)
                {
                    return(CompilationLog.RaiseGeneratorError <ILanguageExpression>("Expecting a list of at most " + i + " expression as input to the macro call", node));
                }

                break;

            default:
                return(CompilationLog.RaiseGeneratorError <ILanguageExpression>("Expecting a list of expressions or list of parameter assignments as input to the macro call", node));
            }

            return(BasicPolyadic.Create(macro.OutputParameterType, macro, operands));
        }
        private ILanguageExpression translate_Expression_Function_Structure(GMacStructure structure, ILanguageExpressionAtomic defaultValueSource, ParseTreeNode node)
        {
            var operands = OperandsByValueAccess.Create();

            if (node.ChildNodes.Count == 0)
            {
                return
                    (defaultValueSource == null
                    ? structure.CreateConstructorExpression(operands)
                    : structure.CreateConstructorExpression(defaultValueSource, operands));
            }

            var expressionFunctionInputsNode = node.ChildNodes[0];

            var subNode = expressionFunctionInputsNode.ChildNodes[0];

            switch (subNode.Term.ToString())
            {
            case GMacParseNodeNames.ExpressionFunctionInputsAssignments:
                var expressionFunctionInputsAssignmentsNode = subNode;

                foreach (var nodeExpressionFunctionInputsAssignmentsItem in expressionFunctionInputsAssignmentsNode.ChildNodes)
                {
                    var lhsNode = nodeExpressionFunctionInputsAssignmentsItem.ChildNodes[0].ChildNodes[0];
                    var rhsNode = nodeExpressionFunctionInputsAssignmentsItem.ChildNodes[1];

                    var lhsValAccess = GMacValueAccessGenerator.Translate_LValue_StructureMember(Context, lhsNode, structure);

                    var rhsExpr =
                        BasicExpressionGenerator.Generate_PolyadicOperand(
                            lhsValAccess.ExpressionType,
                            GMacExpressionGenerator.Translate(BasicExpressionGenerator, rhsNode)
                            );

                    operands.AddOperand(lhsValAccess, rhsExpr);
                }

                break;

            case GMacParseNodeNames.ExpressionFunctionInputsExpressions:
                var expressionFunctionInputsExpressionsNode = subNode;

                var i = 0;

                foreach (var dataMember in structure.DataMembers)
                {
                    if (i >= expressionFunctionInputsExpressionsNode.ChildNodes.Count)
                    {
                        break;
                    }

                    var rhsNode = expressionFunctionInputsExpressionsNode.ChildNodes[i];

                    var lhsValAccess = LanguageValueAccess.Create(dataMember);

                    var rhsExpr =
                        BasicExpressionGenerator.Generate_PolyadicOperand(
                            lhsValAccess.ExpressionType,
                            GMacExpressionGenerator.Translate(BasicExpressionGenerator, rhsNode)
                            );

                    operands.AddOperand(lhsValAccess, rhsExpr);

                    i = i + 1;
                }

                if (expressionFunctionInputsExpressionsNode.ChildNodes.Count > i)
                {
                    return(CompilationLog.RaiseGeneratorError <ILanguageExpression>("Expecting a list of at most " + i + " expression as input to the structure construction", node));
                }

                break;

            default:
                return(CompilationLog.RaiseGeneratorError <ILanguageExpression>("Expecting a list of expressions or a list of member assignments as input to the structure construction", node));
            }

            return
                (defaultValueSource == null
                ? structure.CreateConstructorExpression(operands)
                : structure.CreateConstructorExpression(defaultValueSource, operands));
        }
Exemplo n.º 10
0
        /// <summary>
        /// Evaluate a structure construction operation
        /// </summary>
        /// <param name="structureCons"></param>
        /// <param name="operands"></param>
        /// <returns></returns>
        private ValueStructureSparse EvaluateBasicPolyadicStructureConstructor(GMacStructureConstructor structureCons, OperandsByValueAccess operands)
        {
            ValueStructureSparse value;

            if (structureCons.HasDefaultValueSource)
            {
                value =
                    (ValueStructureSparse)structureCons
                    .DefaultValueSource
                    .AcceptVisitor(this)
                    .DuplicateValue(true);
            }

            else
            {
                value =
                    (ValueStructureSparse)GMacRootAst
                    .CreateDefaultValue(structureCons.Structure);
            }

            foreach (var command in operands.AssignmentsList)
            {
                var rhsValue = command.RhsExpression.AcceptVisitor(this);

                if (command.LhsValueAccess.IsFullAccess)
                {
                    value[command.LhsValueAccess.RootSymbol.ObjectName] = rhsValue.DuplicateValue(true);
                }

                else
                {
                    var sourceValue = value[command.LhsValueAccess.RootSymbol.ObjectName];

                    ValueAccessProcessor.WritePartialValue(sourceValue, command.LhsValueAccess, rhsValue.DuplicateValue(true));
                }
            }

            return(value);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Create a composite type initialized constructor RHS expression based on the given command with
        /// partial LHS value access and a new temporary local variable as explained in Optimize_ReplaceAllLHSPartialAccess
        /// </summary>
        /// <param name="commandInfo"></param>
        /// <param name="newLvalue"></param>
        /// <returns></returns>
        private BasicPolyadic CreateConstructorExpression(HlCommandInfo commandInfo, SymbolLValue newLvalue)
        {
            var oldValueAccess = commandInfo.AssociatedCommand.LhsValueAccess;

            var oldLvalue = oldValueAccess.RootSymbolAsLValue;

            var firstDefStId = _lValuesTable.GetFirstDefiningCommandInfo(oldLvalue).CommandInfoId;

            var useDefaultSource =
                firstDefStId < 0 || firstDefStId != commandInfo.CommandInfoId;

            var typeStructure = oldValueAccess.RootSymbolAsLValue.SymbolType as GMacStructure;

            if (typeStructure != null)
            {
                var structure = typeStructure;

                var operands = OperandsByValueAccess.Create();

                var dataMemberName = ((ValueAccessStepByKey <string>)oldValueAccess.LastAccessStep).AccessKey;

                var dataMember = structure.GetDataMember(dataMemberName);

                var operandLhsValueAccess = LanguageValueAccess.Create(dataMember);

                var operandRhsExpr = LanguageValueAccess.Create(newLvalue);

                operandLhsValueAccess.Append(oldValueAccess.AccessSteps.Skip(2));

                operands.AddOperand(operandLhsValueAccess, operandRhsExpr);

                if (useDefaultSource)
                {
                    return(structure.CreateConstructorExpression(
                               LanguageValueAccess.Create(oldLvalue),
                               operands
                               ));
                }

                return(structure.CreateConstructorExpression(operands));
            }

            if (!(oldValueAccess.RootSymbolAsLValue.SymbolType is GMacFrameMultivector))
            {
                throw new InvalidOperationException("Unknown composite type to be constructed");
            }

            var mvType = (GMacFrameMultivector)oldValueAccess.RootSymbolAsLValue.SymbolType;

            var operandsByIndex = OperandsByIndex.Create();

            var stepByKey = oldValueAccess.LastAccessStep as ValueAccessStepByKey <int>;

            if (stepByKey != null)
            {
                var id = stepByKey.AccessKey;

                operandsByIndex.AddOperand(id, LanguageValueAccess.Create(newLvalue));
            }
            else
            {
                var stepByKeyList = oldValueAccess.LastAccessStep as ValueAccessStepByKeyList <int>;

                if (stepByKeyList == null)
                {
                    throw new InvalidOperationException("Invalid access step for a multivector");
                }

                var idsList = stepByKeyList.AccessKeyList;

                foreach (var id in idsList)
                {
                    operandsByIndex.AddOperand(id,
                                               LanguageValueAccess.Create(newLvalue).Append(id, ((GMacAst)BaseMacro.RootAst).ScalarType));
                }
            }

            if (useDefaultSource)
            {
                return(mvType.CreateConstructorExpression(
                           LanguageValueAccess.Create(oldLvalue),
                           operandsByIndex
                           ));
            }

            return(mvType.CreateConstructorExpression(operandsByIndex));
        }
Exemplo n.º 12
0
 internal BasicPolyadic CreateConstructorExpression(ILanguageExpressionAtomic defaultValueSource, OperandsByValueAccess operands)
 {
     return
         (BasicPolyadic.Create(
              this,
              GMacStructureConstructor.Create(this, defaultValueSource),
              operands
              ));
 }
Exemplo n.º 13
0
        internal void Generate_BasicPolyadic_StructureConstruction(GMacStructureConstructor structureCons, OperandsByValueAccess operands)
        {
            Log.Append("{");

            if (structureCons.HasDefaultValueSource)
            {
                structureCons.DefaultValueSource.AcceptVisitor(this);
            }

            Log.Append("}");
            Log.Append("(");

            var flag = false;

            foreach (var operand in operands.AssignmentsList)
            {
                if (flag)
                {
                    Log.Append(", ");
                }
                else
                {
                    flag = true;
                }

                operand.LhsValueAccess.AcceptVisitor(this);

                Log.Append(" : ");

                Log.Append(operand.LhsValueAccess.ExpressionType.TypeSignature);

                Log.Append(" = ");

                operand.RhsExpression.AcceptVisitor(this);
            }

            Log.Append(")");
        }