public void IfEmptyStatementTest() { IfStatementAst ifStatementAst = ParseStatement("if ($true) {}"); CollectionAssert.IsEmpty(ifStatementAst.Clauses.Single().Item2.Statements); Assert.IsNull(ifStatementAst.ElseClause); }
public void IfElseifTest() { IfStatementAst ifStatementAst = ParseStatement("if ($true) {} elseif ($false) {}"); Assert.IsNull(ifStatementAst.ElseClause); Assert.AreEqual(2, ifStatementAst.Clauses.Count); }
public void IfWithStatementTest() { IfStatementAst ifStatementAst = ParseStatement("if ($true) { Get-ChildItem }"); Assert.AreEqual(1, ifStatementAst.Clauses.Single().Item2.Statements.Count); Assert.IsNull(ifStatementAst.ElseClause); }
public object VisitIfStatement(IfStatementAst ifStmtAst) { var elseClause = VisitAst(ifStmtAst.ElseClause); var clauses = ifStmtAst.Clauses.Select(t => Tuple.Create(VisitAst(t.Item1), VisitAst(t.Item2))); return(new IfStatementAst(ifStmtAst.Extent, clauses, elseClause)); }
public override object VisitIfStatement(IfStatementAst ifStmtAst) { var firstClause = ifStmtAst.Clauses.First(); script_.Write("if("); VisitElement(firstClause.Item1); script_.Write("){"); VisitElement(firstClause.Item2); script_.Write("}"); foreach (Tuple <PipelineBaseAst, StatementBlockAst> clause in ifStmtAst.Clauses.Skip(1)) { script_.Write("elseif("); VisitElement(clause.Item1); script_.Write("){"); VisitElement(clause.Item2); script_.Write("}"); } if (ifStmtAst.ElseClause != null) { script_.Write("else{"); VisitElement(ifStmtAst.ElseClause); script_.Write("}"); } else { VisitElement(ifStmtAst.ElseClause); } return(ifStmtAst); }
public virtual StatementAst VisitIfStatement(IfStatementAst ifStmtAst) { return(new IfStatementAst( ifStmtAst.Extent, ifStmtAst.Clauses?.RewriteAllTuples(this, SyntaxKind.Pipeline, SyntaxKind.StatementBlock), ifStmtAst.ElseClause?.Rewrite(this, SyntaxKind.StatementBlock))); }
// Return Else Clause as a StatementBlockAst public static StatementBlockAst GetElse(this IfStatementAst _ast) { // throw new NotImplementedException("Not implemented at the moment"); if (_ast.ElseClause != null) { return(_ast.ElseClause); } return(null); }
public void IfElseTest() { IfStatementAst ifStatementAst = ParseInput("if ($true) {} else {}") .EndBlock .Statements[0] ; Assert.IsNotNull(ifStatementAst.ElseClause); }
public override AstVisitAction VisitIfStatement(IfStatementAst ifStatementAst) { //// 8.3 The if statement //// The pipeline controlling expressions must have type bool or be implicitly convertible to that //// type. The else-clause is optional. There may be zero or more elseif-clauses. //// //// If the top-level pipeline tests True, then its statement-block is executed and execution of //// the statement terminates. Otherwise, if an elseif-clause is present, if its pipeline tests //// True, then its statement-block is executed and execution of the statement terminates. //// Otherwise, if an else-clause is present, its statement-block is executed. foreach (var clause in ifStatementAst.Clauses) { var condition = EvaluateAst(clause.Item1); // null is false if (condition == null) { continue; } else if (condition is IList && ((IList)condition).Count == 0) { continue; } else if (condition is PSObject) { var baseObject = ((PSObject)condition).BaseObject; if (baseObject is bool && ((bool)baseObject) == false) { continue; } } else { throw new NotImplementedException(clause.Item1.ToString()); } this._pipelineCommandRuntime.WriteObject(EvaluateAst(clause.Item2)); return(AstVisitAction.SkipChildren); } if (ifStatementAst.ElseClause != null) { // iterating over a statement list should be its own method. foreach (var statement in ifStatementAst.ElseClause.Statements) { this._pipelineCommandRuntime.WriteObject(EvaluateAst(statement)); } } return(AstVisitAction.SkipChildren); }
public static IfStatementAst Update( this IfStatementAst ast, IEnumerable <Tuple <PipelineBaseAst, StatementBlockAst> > clauses = null, IEnumerable <StatementAst> elseStatements = null) { return(new IfStatementAst( ast.Extent, clauses?.Select(CloneAstTuple) ?? ast.Clauses?.Select(CloneAstTuple), ast.ElseClause?.Update(elseStatements))); }
public object VisitIfStatement(IfStatementAst ifStmtAst) { var newClauses = (from clause in ifStmtAst.Clauses let newClauseTest = VisitElement(clause.Item1) let newStatementBlock = VisitElement(clause.Item2) select new Tuple <PipelineBaseAst, StatementBlockAst>(newClauseTest, newStatementBlock)); var newElseClause = VisitElement(ifStmtAst.ElseClause); return(new IfStatementAst(ifStmtAst.Extent, newClauses, newElseClause)); }
public virtual object VisitIfStatement(IfStatementAst ifStmtAst) { foreach (var clause in ifStmtAst.Clauses) { VisitElement(clause.Item1); VisitElement(clause.Item2); } VisitElement(ifStmtAst.ElseClause); return(ifStmtAst); }
public override AstVisitAction VisitIfStatement(IfStatementAst ifStmtAst) { explanations.Add( new Explanation() { Description = "if-statement, run statement lists based on the results of one or more conditional tests", CommandName = "if-statement", HelpResult = HelpTableQuery("about_if"), TextToHighlight = "if" }.AddDefaults(ifStmtAst, explanations)); return(AstVisitAction.Continue); }
// Return ElseIf Clauses as a list of StatementBlockAst public static IEnumerable <StatementBlockAst> GetElseIf(this IfStatementAst _ast) { if (_ast.Clauses.Count > 1) { List <StatementBlockAst> ElseIfs = new List <StatementBlockAst>(); for (int i = 1; i < _ast.Clauses.Count; i++) { ElseIfs.Add(_ast.Clauses[i].Item2); } return(ElseIfs); } return(null); }
public override AstVisitAction VisitIfStatement(IfStatementAst ifStmtAst) { var firstCondition = ifStmtAst.Clauses.First(); var condition = VisitSyntaxNode(firstCondition.Item1); var body = VisitSyntaxNode(firstCondition.Item2); body = EnsureBlock(body); // If var ifStatement = new IfStatement(condition, body); var previousIf = ifStatement; // Else ifs foreach (var clause in ifStmtAst.Clauses.Skip(1)) { condition = VisitSyntaxNode(clause.Item1); body = VisitSyntaxNode(clause.Item2); body = EnsureBlock(body); var nextCondition = new IfStatement(condition, body); previousIf.ElseClause = new ElseClause(nextCondition); previousIf = nextCondition; } // Else if (ifStmtAst.ElseClause != null) { var statements = new List <Node>(); foreach (var statement in ifStmtAst.ElseClause.Statements) { var statementNode = VisitSyntaxNode(statement); statements.Add(statementNode); } body = new Block(statements); previousIf.ElseClause = new ElseClause(body); } _currentNode = ifStatement; return(AstVisitAction.SkipChildren); }
public override AstVisitAction VisitIfStatement(IfStatementAst ifStmtAst) { Console.WriteLine("Visited an IfStatementAst."); Console.WriteLine(" " + ifStmtAst.ToString().Replace(Environment.NewLine, Environment.NewLine + " ")); return(AstVisitAction.Continue); }
public object VisitIfStatement(IfStatementAst ifStmtAst) { Console.WriteLine("Visited an IfStatementAst."); return(ifStmtAst); }
public override StatementAst VisitIfStatement(IfStatementAst ifStmtAst) => VisitStatement(base.VisitIfStatement(ifStmtAst));
// IfStatementAst Extension Methods // New Methods Available: // - CreateNodeFromAST(NodeDepth, NodePosition, Parent) => Creates a Node // - GetChildAst() => retourne only ASTs we are looking for ... public static IfNode CreateNodeFromAst(this IfStatementAst _ast, int _depth, int _position, Node _parent, Tree _tree) { return(new IfNode(_ast, _depth, _position, _parent, _tree)); }
public object VisitIfStatement(IfStatementAst ifStmtAst) { throw PSTraceSource.NewArgumentException("ast"); }
public bool ProcessIfStatement(IfStatementAst ifStatement) { // 能脱离函数的充要条件: // 1. 不能只有if, 要有else。只有if 相当于 可能脱离。“可能”视为0. // 2. if-block可以脱离 && else也可以脱离 Guard.Against.Null(ifStatement, nameof(ifStatement)); // if_stmt -> 'if' expr block_stmt ('else' (block_stmt | if_stmt))? var conditionType = ProcessExpression(ifStatement.ConditionExpression); if (!(conditionType == DataType.Bool || conditionType == DataType.Double || conditionType == DataType.Long)) { throw new SemanticException( $"If.Condition should be of bool,double or long type. Provided: {conditionType}"); } // codegen: cond-expr if (CodeGenerationEnabled) { CurrentFunction.Builder.Bucket.AddRange(ExpressionBucket.Pop()); } bool canReturn1 = false; bool canReturn2 = false; if (ifStatement.HasElseAndFollowing) { var else0 = new Instruction("nop"); var done0 = new Instruction("nop"); // jmp-if-false .ELSE0 if (CodeGenerationEnabled) { CurrentFunction.Builder.Bucket.Add(new object[] { "br.false", else0 }); } // if-block canReturn1 = ProcessBlockStatement(ifStatement.BlockStatement); // jmp .DONE0 if (CodeGenerationEnabled) { CurrentFunction.Builder.Bucket.Add(new object[] { "br", done0 }); } // .ELSE0: nop if (CodeGenerationEnabled) { CurrentFunction.Builder.Bucket.Add(else0); } // else-block if (ifStatement.FollowingIf != null) { // if-else-if canReturn2 = ProcessIfStatement(ifStatement.FollowingIf); } else { // if-else canReturn2 = ProcessBlockStatement(ifStatement.FollowingBlock); } // .DONE0: nop if (CodeGenerationEnabled) { CurrentFunction.Builder.Bucket.Add(done0); } return(canReturn1 && canReturn2); } else { var doneInstruction = new Instruction("nop"); if (CodeGenerationEnabled) { CurrentFunction.Builder.Bucket.Add(new object[] { "br.false", doneInstruction }); } // if-block canReturn1 = ProcessBlockStatement(ifStatement.BlockStatement); // .DONE if (CodeGenerationEnabled) { CurrentFunction.Builder.Bucket.Add(doneInstruction); } } return(false); }
public void IfElseTest() { IfStatementAst ifStatementAst = ParseStatement("if ($true) {} else {}"); Assert.IsNotNull(ifStatementAst.ElseClause); }
public override AstVisitAction VisitIfStatement(IfStatementAst ast) { return(Check(ast)); }
public object VisitIfStatement(IfStatementAst ifStmtAst) { return(false); }
// We skip a bunch of random statements because we can't really be accurate detecting functions/classes etc. that // are conditionally defined. public override AstVisitAction VisitIfStatement(IfStatementAst ifStmtAst) { return(AstVisitAction.SkipChildren); }
/// <summary/> public virtual AstVisitAction VisitIfStatement(IfStatementAst ifStmtAst) => DefaultVisit(ifStmtAst);
public static IEnumerable <Ast> GetChildAst(this IfStatementAst _ast) { return(_ast.Clauses[0].Item2.FindAll(Args => Args is Ast && Args.Parent == _ast.Clauses[0].Item2, false)); }
public object VisitIfStatement(IfStatementAst ifStmtAst) => null;
public override AstVisitAction VisitIfStatement(IfStatementAst ast) { return(AstVisitAction.Continue); }
public override AstVisitAction VisitIfStatement(IfStatementAst ast) { return(DoNextAction(ast)); }