コード例 #1
0
ファイル: GMacMacroCodeComposer.cs プロジェクト: phreed/GMac
        /// <summary>
        /// Used to replace the base macro by another one. This sets the defaults of the generator and clears
        /// the internal code composer
        /// </summary>
        /// <param name="baseMacro"></param>
        public void SetBaseMacro(AstMacro baseMacro)
        {
            SyntaxList.Clear();

            CodeBlock = null;

            TargetVariablesNaming = null;

            MacroBinding = GMacMacroBinding.Create(baseMacro);

            SetDefaults();
        }
コード例 #2
0
ファイル: GMacMacroCodeComposer.cs プロジェクト: phreed/GMac
        /// <summary>
        /// Generate optimized macro code in the target language given a list of macro parameters bindings
        /// </summary>
        /// <returns></returns>
        public override string Generate()
        {
            //Initialize components of macro code generator
            SyntaxList.Clear();

            MacroBinding.Clear();

            CodeBlock = null;

            TargetVariablesNaming = null;

            if (AllowGenerateMacroCode == false)
            {
                return(string.Empty);
            }

            LibraryComposer.CheckProgressRequestStop();

            var progressId = this.ReportStart(
                "Generating Macro Code For: " + BaseMacro.AccessName
                );

            try
            {
                //Bind macro parameters to variables and constants as needed
                if (ActionSetMacroParametersBindings == null)
                {
                    DefaultActionSetMacroParametersBindings(MacroBinding);
                }
                else
                {
                    ActionSetMacroParametersBindings(MacroBinding);
                }

                if (IsMacroBindingReady == false)
                {
                    this.ReportWarning("Macro Binding Not Ready", MacroBinding.ToString());
                    this.ReportFinish(progressId);

                    return(string.Empty);
                }

                this.ReportNormal("Macro Binding Ready", MacroBinding.ToString());

                //Create the optimizd code block holding abstract computations based on the macro parameters
                //bindings. This step is typically the most time consuming because of many symbolic computations
                CodeBlock = MacroBinding.CreateOptimizedCodeBlock();


                //Assign target variable names to low-level code block variables. The code block is generated
                //automatically in the call to the OptimizedCodeBlock member
                TargetVariablesNaming = new GMacTargetVariablesNaming(GMacLanguage, CodeBlock);

                if (ActionSetTargetVariablesNames == null)
                {
                    DefaultActionSetTargetVariablesNames(TargetVariablesNaming);
                }
                else
                {
                    ActionSetTargetVariablesNames(TargetVariablesNaming);
                }


                //Generate code before computations for comments, temp declarations, and the like
                var result =
                    ActionBeforeGenerateComputations == null
                        ? DefaultActionBeforeGenerateComputations(this)
                        : ActionBeforeGenerateComputations(this);


                //Generate computations code if allowed by last action result
                if (result)
                {
                    GenerateProcessingCode();
                }


                //Generate code after computations for comments, temp destruction, and the like
                if (ActionAfterGenerateComputations == null)
                {
                    DefaultActionAfterGenerateComputations(this);
                }
                else
                {
                    ActionAfterGenerateComputations(this);
                }
            }
            catch (Exception e)
            {
                this.ReportError(e);
            }

            //Clean everything up and return final generated code
            ExpressionConverter.ActiveCodeBlock = null;

            //Un-parse the SyntaxList into the final code
            var codeText = CodeGenerator.GenerateCode(SyntaxList);

            this.ReportFinish(progressId, codeText);

            return(codeText);
        }