/// <summary> /// 读取if(){}else if(){}else{}语句。 /// </summary> private void ReadIf(CodeReader reader, Syntax syntax) { var current = new IfSyntax(); current.Name = "@if"; current.Parameters = reader.ReadParameters(); ParseChildren(reader, current); //读取elseif语句 while (reader.IsNextNonWhiteSpace("elseif", stringComparison: StringComparison.OrdinalIgnoreCase)) { var elseif = new IfSyntax(); elseif.Name = "elseif"; elseif.Parameters = reader.ReadParameters(); ParseChildren(reader, elseif); if (current.ElseIf == null) { current.ElseIf = new List <IfSyntax>(); } current.ElseIf.Add(elseif); } //读取elseif语句 if (reader.IsNextNonWhiteSpace("else", stringComparison: StringComparison.OrdinalIgnoreCase)) { var @else = new CodeSyntax(); @else.Name = "else"; ParseChildren(reader, @else); current.Else = @else; } syntax.Append(current); }
public override SyntaxNode Visit(IfSyntax pNode) { using (new MetadataCache.LocalScope()) { return(base.Visit(pNode)); } }
private ElseSyntax ParseElse() { if (!PeekAndExpect(TokenType.Else)) { return(null); } StartSpan(); IfSyntax i = null; BlockSyntax body = null; if (Peek(TokenType.If)) { i = ParseIf(); } else { Ignore(TokenType.Newline); if (Peek(TokenType.LeftScope)) { body = ParseBlock(); } else { body = SyntaxFactory.Block(new List <SyntaxNode> { ParseStatement() }); } } return(SyntaxFactory.Else(i, body).SetSpan <ElseSyntax>(EndSpan())); }
public override void Visit(IfSyntax pNode) { using (new ContextValue(this, "InIf", true)) { base.Visit(pNode); pNode.ReturnsInBody = GetValue("LastStatement", false); } }
protected override void VisitIfSyntax(IfSyntax pNode) { if (!CanCast(pNode.Condition.Type, SmallTypeCache.Boolean)) { CompilerErrors.TypeCastError(pNode.Condition.Type, SmallTypeCache.Boolean, pNode.Condition.Span); } base.VisitIfSyntax(pNode); }
static Expression BuildIf(IfSyntax ifSyntax) { return(new If { Condition = BuildExpression(ifSyntax.condition), Then = BuildExpression(ifSyntax.consequent), Else = BuildExpression(ifSyntax.alternative), Span = ifSyntax.span, }); }
public override void Visit(IfSyntax pNode) { if (pNode.Condition.Type == SmallType.Undefined) { Compiler.ReportError(CompilerErrorType.UndefinedType, pNode); } else { if (!SmallType.Boolean.IsAssignableFrom(pNode.Condition.Type)) { Compiler.ReportError(CompilerErrorType.TypeMismatch, pNode, SmallType.Boolean.ToString(), pNode.Condition.Type.ToString()); } } base.Visit(pNode); }
protected override void VisitIfSyntax(IfSyntax pNode) { Visit(pNode.Condition); Visit(pNode.Body); //Reset found before looking through Else var found = Store.GetValue <bool>("ReturnFound"); Store.SetValue("ReturnFound", false); if (pNode.Else != null) { Visit(pNode.Else); //Returns must be found in ALL else blocks Store.SetValue("ReturnFound", found && Store.GetValue <bool>("ReturnFound")); } //Else found = false because we still need a return in the main method body }
protected virtual void VisitIfSyntax(IfSyntax pNode) { Visit(pNode.Condition); Visit(pNode.Body); Visit(pNode.Else); }
protected override void VisitIfSyntax(IfSyntax pNode) { TrySetImplicitCastType(pNode.Condition, SmallTypeCache.Boolean); base.VisitIfSyntax(pNode); }
protected virtual SyntaxNode VisitIfSyntax(IfSyntax pNode) { return(SyntaxFactory.If(Visit(pNode.Condition), (BlockSyntax)Visit(pNode.Body), (ElseSyntax)Visit(pNode.Else))); }
protected override SyntaxNode VisitSelectSyntax(SelectSyntax pNode) { var rw = _rewrite; _rewrite = false; //Save itVar in case we hit a nested for or select statement var it = _itVar; _itVar = pNode.Condition; SyntaxNode retval = base.VisitSelectSyntax(pNode); if (_rewrite) { if (pNode.Annotation.Value == KeyAnnotations.Complete) { CompilerErrors.IgnoredComplete(pNode.Span); } //Only rewrite if we have "it" for (int i = pNode.Cases.Count - 1; i >= 0; i--) { var currentCase = pNode.Cases[i]; //Default cause needs to be the last one. Make a else statement if (currentCase.IsDefault) { _currentElse = SyntaxFactory.Else(null, currentCase.Body); } else { //The condition needs to be a comparison binary expression SyntaxNode baseExpression = Visit(currentCase.Conditions[0]); if (!IsComparison(baseExpression)) { //If it isn't make it one baseExpression = SyntaxFactory.BinaryExpression(_itVar, BinaryExpressionOperator.Equals, baseExpression); ((BinaryExpressionSyntax)baseExpression).SetType(SmallTypeCache.Boolean); } for (int j = 0; j < currentCase.Conditions.Count - 1; j++) { var newExpression = currentCase.Conditions[j + 1]; if (!IsComparison(newExpression)) { //If it isn't make it one newExpression = SyntaxFactory.BinaryExpression(_itVar, BinaryExpressionOperator.Equals, newExpression); ((BinaryExpressionSyntax)newExpression).SetType(SmallTypeCache.Boolean); } baseExpression = SyntaxFactory.BinaryExpression(baseExpression, BinaryExpressionOperator.Or, newExpression); ((BinaryExpressionSyntax)baseExpression).SetType(SmallTypeCache.Boolean); } //Visit body so we can rewrite any "it" var b = (BlockSyntax)Visit(currentCase.Body); _currentIf = SyntaxFactory.If(baseExpression, b, _currentElse); if (i > 0) { //As long as this isn't the last statement, make an else if _currentElse = SyntaxFactory.Else(_currentIf, null); } } } retval = _currentIf; } _itVar = it; _rewrite = rw; return(retval); }