/// <summary> /// Evaluate all simple RHS expressions (the RHS is a constant value or a basic expressions on constant values) /// then propagate the constant values in the forward direction of commands if possible /// </summary> private void Optimize_FoldSimpleExpressions() { foreach (var commandInfo in _commandsInfoList) { //Try to evaluate the RHS of each command var rhsValue = GMacExpressionEvaluator.EvaluateExpressionIfSimple( BaseMacro.ChildScope, commandInfo.AssociatedCommand.RhsExpression ); if (ReferenceEquals(rhsValue, null)) { continue; } //If evaluation is possible change the RHS of the command into the evaluated constant value commandInfo.AssociatedCommand.SetRhsExpression(rhsValue); //If the LHS of this command is a full definition of an input macro parameter or local variable //used in following commands propagate the RHS constant value and remove this command from optimized code if (!commandInfo.LhslValueIsOutputParameter && commandInfo.IsFullLhsDefinition && commandInfo.LhslValueIsUsedLater) { PropagateCommand(commandInfo); } } RemoveMarkedObjects(); UpdateChains(); }
public AstValue Evaluate(string codeText) { var expr = GetExpression(codeText); if (expr.IsNullOrInvalid()) { return(null); } var progressTitle = CompileProgressTitle("Expression"); try { var value = GMacExpressionEvaluator.EvaluateExpression( RefResContext.MainScope, expr.AssociatedExpression ); this.ReportNormal(progressTitle, value.ToString(), ProgressEventArgsResult.Success); return(value.ToAstValue()); } catch (Exception e) { this.ReportError(progressTitle, e); return(null); } }
/// <summary> /// Generate a constant defined inside a frame /// </summary> /// <param name="frame"></param> /// <param name="constantName"></param> /// <returns></returns> private GMacConstant Create_Frame_Constant(GMacFrame frame, string constantName) { Context.PushState(frame.ChildSymbolScope); if (frame.CanDefineChildSymbol(constantName) == false) { CompilationLog.RaiseGeneratorError <int>("Symbol name already used", RootParseNode); } var constantExpr = GMacExpressionGenerator.Translate(Context, RootParseNode.ChildNodes[1]); var constantValue = GMacExpressionEvaluator.EvaluateExpression(Context.ActiveParentScope, constantExpr); Context.PopState(); return(frame.DefineFrameConstant(constantName, constantValue)); }
//public override void ResetOnAcquire() //{ // base.ResetOnAcquire(); // _generatedConstant = null; //} /// <summary> /// Generate a constant defined inside a namespace /// </summary> /// <param name="nameSpace"></param> /// <param name="constantName"></param> /// <returns></returns> private GMacConstant Create_Namespace_Constant(GMacNamespace nameSpace, string constantName) { if (GMacCompilerFeatures.CanDefineNamespaceConstants == false) { CompilationLog.RaiseGeneratorError <int>("Can't define a constant inside a namespace", RootParseNode); } Context.PushState(nameSpace.ChildSymbolScope); if (nameSpace.CanDefineChildSymbol(constantName) == false) { CompilationLog.RaiseGeneratorError <int>("Symbol name already used", RootParseNode); } var constantExpr = GMacExpressionGenerator.Translate(Context, RootParseNode.ChildNodes[1]); var constantValue = GMacExpressionEvaluator.EvaluateExpression(Context.ActiveParentScope, constantExpr); Context.PopState(); return(nameSpace.DefineNamespaceConstant(constantName, constantValue)); }
/// <summary> /// The main Reset method called by all others. The given symbol must be a frame or a namespace /// </summary> /// <param name="symbol"></param> public void Reset(AstSymbol symbol) { if (symbol.IsNullOrInvalid() || (symbol.IsValidFrame == false && symbol.IsValidNamespace == false)) { this.ReportNormal("Reset Interpreter", ProgressEventArgsResult.Failure); return; } Output.Clear(); Root = symbol.Root; _mainCommandBlock = CommandBlock.Create(symbol.AssociatedSymbol as SymbolWithScope); RefResContext.Clear(_mainCommandBlock); _expressionEvaluator = GMacExpressionEvaluator.CreateForDynamicEvaluation(_mainCommandBlock); _symbolsCache.Clear(); this.ReportNormal("Reset Interpreter", symbol.AccessName, ProgressEventArgsResult.Success); }