public AssignmentStatementAst(IScriptExtent extent, ExpressionAst left, TokenKind @operator, StatementAst right, IScriptExtent errorPosition) : base(extent) { if (((left == null) || (right == null)) || (errorPosition == null)) { throw PSTraceSource.NewArgumentNullException((left == null) ? "left" : ((right == null) ? "right" : "errorPosition")); } if ((@operator.GetTraits() & TokenFlags.AssignmentOperator) == TokenFlags.None) { throw PSTraceSource.NewArgumentException("operator"); } PipelineAst ast = right as PipelineAst; if ((ast != null) && (ast.PipelineElements.Count == 1)) { CommandExpressionAst ast2 = ast.PipelineElements[0] as CommandExpressionAst; if (ast2 != null) { right = ast2; right.ClearParent(); } } this.Operator = @operator; this.Left = left; base.SetParent(left); this.Right = right; base.SetParent(right); this.ErrorPosition = errorPosition; }
public AssignmentStatementAst(IScriptExtent extent, ExpressionAst left, TokenKind @operator, StatementAst right, IScriptExtent errorPosition) : base(extent) { this.Left = left; this.Operator = @operator; this.Right = right; this.ErrorPosition = errorPosition; }
public object VisitStatementBlock(StatementBlockAst statementBlockAst) { if (statementBlockAst.Traps != null) { return(false); } if (statementBlockAst.Statements.Count > 1) { return(false); } StatementAst ast = statementBlockAst.Statements.FirstOrDefault <StatementAst>(); return((ast == null) ? ((object)false) : ((object)((bool)ast.Accept(this)))); }
private AstVisitAction VisitStatement(StatementAst statementAst) { // We execute this in a subcontext, because single enumerable results are // evaluated and its values are written to pipeline (i.e. ". {1,2,3}" writes // all three values to pipeline // therefore we have to catch exceptions for now to read and write // the subVisitors' output stream, so that already written results appear // also in this OutputStream var subVisitor = this.CloneSub(false); subVisitor._pipelineCommandRuntime.ErrorStream.Redirect(_pipelineCommandRuntime.ErrorStream); try { statementAst.Visit(subVisitor); } finally { var result = subVisitor._pipelineCommandRuntime.OutputStream.Read(); if (result.Count == 1) { // this is the actual thing of this whole work: enumerate a single value! _pipelineCommandRuntime.WriteObject(result.Single(), true); } else if (result.Count > 1) { _pipelineCommandRuntime.WriteObject(result, true); } } return AstVisitAction.SkipChildren; }
public AssignmentTarget(AssignmentStatementAst ast) { this._targetAst = ast.Left; this._rightAst = ast.Right; if (_rightAst != null) { Constant = Undetermined.UndeterminedConstant; Type = typeof(Undetermined); } CommandExpressionAst cmExAst = _rightAst as CommandExpressionAst; if (cmExAst != null) { ExpressionAst exprAst = cmExAst.Expression; Type = exprAst.StaticType; if (exprAst is ConvertExpressionAst) { ConvertExpressionAst convertAst = exprAst as ConvertExpressionAst; Type = DeepestRelatedDerivedClass(convertAst.StaticType, Type); if (convertAst.Child is ConstantExpressionAst) { Constant = (convertAst.Child as ConstantExpressionAst).Value; } } else if (exprAst is BinaryExpressionAst) { BinaryExpressionAst binAst = exprAst as BinaryExpressionAst; if (binAst != null && binAst.Operator == TokenKind.As && binAst.Right is TypeExpressionAst) { Type = DeepestRelatedDerivedClass((binAst.Right as TypeExpressionAst).TypeName.GetReflectionType(), binAst.Left.StaticType); if (binAst.Left is ConstantExpressionAst) { Constant = (binAst.Left as ConstantExpressionAst).Value; } else if (binAst.Left is VariableExpressionAst) { _rightHandSideVariable = new VariableTarget(binAst.Left as VariableExpressionAst); } } } else if (exprAst is ConstantExpressionAst) { Constant = (cmExAst.Expression as ConstantExpressionAst).Value; } else if (exprAst is VariableExpressionAst) { _rightHandSideVariable = new VariableTarget(cmExAst.Expression as VariableExpressionAst); if (String.Equals((exprAst as VariableExpressionAst).VariablePath.UserPath, "this", StringComparison.OrdinalIgnoreCase)) { Constant = SpecialVars.ThisVariable; } } //Store the type info for variable assignment from .Net type else if (exprAst is MemberExpressionAst) { Type = DeepestRelatedDerivedClass(Type, GetTypeFromMemberExpressionAst(exprAst as MemberExpressionAst)); } } // We'll consider case where there is only 1 pipeline element for now else if (_rightAst is PipelineAst && (_rightAst as PipelineAst).PipelineElements.Count == 1) { #region Process New-Object command CommandAst cmdAst = (_rightAst as PipelineAst).PipelineElements[0] as CommandAst; if (cmdAst != null && cmdAst.CommandElements.Count > 1) { StringConstantExpressionAst stringAst = cmdAst.CommandElements[0] as StringConstantExpressionAst; if (stringAst != null && String.Equals(stringAst.Value, "new-object", StringComparison.OrdinalIgnoreCase)) { CommandParameterAst secondElement = cmdAst.CommandElements[1] as CommandParameterAst; StringConstantExpressionAst typeName = null; if (secondElement != null) { if (String.Equals(secondElement.ParameterName, "TypeName", StringComparison.OrdinalIgnoreCase)) { if (secondElement.Argument != null) { typeName = secondElement.Argument as StringConstantExpressionAst; } else { if (cmdAst.CommandElements.Count > 2) { typeName = cmdAst.CommandElements[2] as StringConstantExpressionAst; } } } } else { typeName = cmdAst.CommandElements[1] as StringConstantExpressionAst; } if (typeName != null) { Type = System.Type.GetType(typeName.Value) ?? typeof(object); } } } #endregion } SetVariableName(); }
private void CompileTrappableExpression(List<Expression> exprList, StatementAst stmt) { var expr = Compile(stmt); exprList.Add(expr); var pipeAst = stmt as PipelineAst; if (pipeAst != null) { if (pipeAst.PipelineElements.Count == 1 && pipeAst.PipelineElements[0] is CommandExpressionAst) { // A single expression - must set $? after the expression. exprList.Add(s_setDollarQuestionToTrue); } } else { var assignmentStatementAst = stmt as AssignmentStatementAst; if (assignmentStatementAst != null) { Ast right = null; var assignAst = assignmentStatementAst; while (assignAst != null) { right = assignAst.Right; assignAst = right as AssignmentStatementAst; } pipeAst = right as PipelineAst; if (right is CommandExpressionAst || (pipeAst != null && pipeAst.PipelineElements.Count == 1 && pipeAst.PipelineElements[0] is CommandExpressionAst)) { // If the RHS of the assign was an expression, exprList.Add(s_setDollarQuestionToTrue); } } } }
private Expression CaptureStatementResults( StatementAst stmt, CaptureAstContext context, MergeRedirectExprs generateRedirectExprs = null) { var result = CaptureStatementResultsHelper(stmt, context, generateRedirectExprs); // If we're generating code for a condition and the condition contains some command invocation, // we want to be sure that $? is set to true even if the condition fails, e.g.: // if (get-command foo -ea SilentlyContinue) { foo } // $? # never $false here // Many conditions don't invoke commands though, and in trivial empty loops, setting $? = $true // does have a measurable impact, so only set $? = $true if the condition might change $? to $false. // We do this after evaluating the condition so that you could do something like: // if ((dir file1,file2 -ea SilentlyContinue) -and $?) { <# files both exist, otherwise $? would be $false if 0 or 1 files existed #> } if (context == CaptureAstContext.Condition && AstSearcher.FindFirst(stmt, ast => ast is CommandAst, searchNestedScriptBlocks: false) != null) { var tmp = NewTemp(result.Type, "condTmp"); result = Expression.Block(new[] { tmp }, Expression.Assign(tmp, result), s_setDollarQuestionToTrue, tmp); } return result; }
private Expression CaptureStatementResultsHelper( StatementAst stmt, CaptureAstContext context, MergeRedirectExprs generateRedirectExprs) { var commandExpressionAst = stmt as CommandExpressionAst; if (commandExpressionAst != null) { if (commandExpressionAst.Redirections.Count > 0) { return GetRedirectedExpression(commandExpressionAst, captureForInput: true); } return Compile(commandExpressionAst.Expression); } var assignmentStatementAst = stmt as AssignmentStatementAst; if (assignmentStatementAst != null) { var expr = Compile(assignmentStatementAst); if (stmt.Parent is StatementBlockAst) { expr = Expression.Block(expr, ExpressionCache.Empty); } return expr; } var pipelineAst = stmt as PipelineAst; if (pipelineAst != null) { var expr = pipelineAst.GetPureExpression(); if (expr != null) { return Compile(expr); } } return CaptureAstResults(stmt, context, generateRedirectExprs); }
private ScriptBlockAst ScriptBlockRule(Token lCurly, bool isFilter, StatementAst predefinedStatementAst) { //G script-block: //G using-statements:opt param-block:opt statement-terminators:opt script-block-body:opt //G //G using-statements: //G using-statement //G using-statements using-statement // We could set the mode here, but we can avoid rescanning keywords if the caller // sets the mode before skipping newlines. Diagnostics.Assert(_tokenizer.Mode == TokenizerMode.Command, "Caller to make sure the mode is correct."); // Skipping newlines here saves a more expensive resync if there is no parameter block. SkipNewlines(); var usingStatements = lCurly == null ? UsingStatementsRule() : null; var restorePoint = _tokenizer.GetRestorePoint(); ParamBlockAst paramBlock = ParamBlockRule(); if (paramBlock == null) { // In case we scanned some attributes or type constraints but they didn't // belong to a param statement, we need to reparse them (because they will // mean something different, such as a type literal expression, or a cast.) Resync(restorePoint); } SkipNewlinesAndSemicolons(); return ScriptBlockBodyRule(lCurly, usingStatements, paramBlock, isFilter, predefinedStatementAst); }
/// <summary> /// Check that label exists inside the method. /// Only call it, when label is present and can be calculated in compile time. /// </summary> /// <param name="ast">BreakStatementAst or ContinueStatementAst</param> /// <param name="label">label name. Can be null</param> private void CheckLabelExists(StatementAst ast, string label) { if (String.IsNullOrEmpty(label)) { return; } Ast parent; for (parent = ast.Parent; parent != null; parent = parent.Parent) { if (parent is FunctionDefinitionAst) { if (parent.Parent is FunctionMemberAst) { _parser.ReportError(ast.Extent, () => ParserStrings.LabelNotFound, label); } break; } var loop = parent as LoopStatementAst; if (loop != null) { if (LoopFlowException.MatchLoopLabel(label, loop.Label ?? "")) break; } } }
private void CompileTrappableExpression(List<Expression> exprList, StatementAst stmt) { Expression item = this.Compile(stmt); exprList.Add(item); PipelineAst ast = stmt as PipelineAst; if (ast != null) { if ((ast.PipelineElements.Count == 1) && (ast.PipelineElements[0] is CommandExpressionAst)) { exprList.Add(_setDollarQuestionToTrue); } } else if (stmt is AssignmentStatementAst) { Ast right = null; for (AssignmentStatementAst ast3 = (AssignmentStatementAst) stmt; ast3 != null; ast3 = right as AssignmentStatementAst) { right = ast3.Right; } ast = right as PipelineAst; if ((right is CommandExpressionAst) || (((ast != null) && (ast.PipelineElements.Count == 1)) && (ast.PipelineElements[0] is CommandExpressionAst))) { exprList.Add(_setDollarQuestionToTrue); } } }
private Expression CaptureStatementResultsHelper(StatementAst stmt, CaptureAstContext context, MergeRedirectExprs generateRedirectExprs) { CommandExpressionAst commandExpr = stmt as CommandExpressionAst; if (commandExpr != null) { if (commandExpr.Redirections.Any<RedirectionAst>()) { return this.GetRedirectedExpression(commandExpr, true); } return this.Compile(commandExpr.Expression); } AssignmentStatementAst ast = stmt as AssignmentStatementAst; if (ast != null) { Expression expression = this.Compile(ast); if (stmt.Parent is StatementBlockAst) { expression = Expression.Block(expression, ExpressionCache.Empty); } return expression; } PipelineAst ast3 = stmt as PipelineAst; if (ast3 != null) { ExpressionAst pureExpression = ast3.GetPureExpression(); if (pureExpression != null) { return this.Compile(pureExpression); } } return this.CaptureAstResults(stmt, context, generateRedirectExprs); }
private Expression CaptureStatementResults(StatementAst stmt, CaptureAstContext context, MergeRedirectExprs generateRedirectExprs = null) { Expression right = this.CaptureStatementResultsHelper(stmt, context, generateRedirectExprs); if ((context == CaptureAstContext.Condition) && (AstSearcher.FindFirst(stmt, ast => ast is CommandAst, false) != null)) { ParameterExpression left = this.NewTemp(right.Type, "condTmp"); right = Expression.Block(new ParameterExpression[] { left }, new Expression[] { Expression.Assign(left, right), _setDollarQuestionToTrue, left }); } return right; }
public DropDownEntryInfo(StatementAst body) { Body = body; }
private ScriptBlockAst ScriptBlockBodyRule(Token lCurly, List<UsingStatementAst> usingStatements, ParamBlockAst paramBlockAst, bool isFilter, StatementAst predefinedStatementAst) { //G script-block-body: //G named-block-list //G statement-list Token token = PeekToken(); if ((token.TokenFlags & TokenFlags.ScriptBlockBlockName) == TokenFlags.ScriptBlockBlockName) { return NamedBlockListRule(lCurly, usingStatements, paramBlockAst); } List<TrapStatementAst> traps = new List<TrapStatementAst>(); List<StatementAst> statements = new List<StatementAst>(); if (predefinedStatementAst != null) { statements.Add(predefinedStatementAst); } IScriptExtent statementListExtent = paramBlockAst != null ? paramBlockAst.Extent : null; IScriptExtent scriptBlockExtent; while (true) { IScriptExtent extent = StatementListRule(statements, traps); if (statementListExtent == null) { statementListExtent = extent; } else if (extent != null) { statementListExtent = ExtentOf(statementListExtent, extent); } if (CompleteScriptBlockBody(lCurly, ref statementListExtent, out scriptBlockExtent)) { break; } } return new ScriptBlockAst(scriptBlockExtent, usingStatements, paramBlockAst, new StatementBlockAst(statementListExtent ?? PositionUtilities.EmptyExtent, statements, traps), isFilter); }