public virtual JsStatement VisitSwitchStatement(JsSwitchStatement statement, TData data) { var test = VisitExpression(statement.Expression, data); var clauses = VisitSwitchSections(statement.Sections, data); return(ReferenceEquals(test, statement.Expression) && ReferenceEquals(clauses, statement.Sections) ? statement : new JsSwitchStatement(test, clauses)); }
public virtual void Visit(JsSwitchStatement node) { DefaultVisit(node); node.Expression.Accept(this); foreach (var section in node.Sections) { section.Accept(this); } }
public override JsStatement VisitSwitchStatement(JsSwitchStatement statement, object data) { bool old = _unnamedIsMatch; _unnamedIsMatch = false; VisitSwitchSections(statement.Sections, null); _unnamedIsMatch = old; return(statement); }
public virtual JsNode Visit(JsSwitchStatement node) { return(DefaultVisit(node, x => { x.Expression = (JsExpression)x.Expression.Accept(this); for (var i = 0; i < x.Sections.Count; i++) { x.Sections[i] = (JsSwitchSection)x.Sections[i].Accept(this); } return x; })); }
public override void VisitSwitchStatement(JsSwitchStatement node) { output.Append("switch ("); node.Expression.Accept(this); output.AppendLine(") {"); output.CurrentIndentLevel++; foreach (var section in node.Sections) { section.Accept(this); } output.CurrentIndentLevel--; output.AppendLine("}"); }
public override JsStatement VisitSwitchStatement(JsSwitchStatement statement, object data) { var oldBreak = _breakStack; var oldContinue = _continueStack; try { _breakStack = _breakStack.Push(null); _continueStack = _continueStack.Push(null); var sections = VisitSwitchSections(statement.Sections, data); return(ReferenceEquals(sections, statement.Sections) ? statement : new JsSwitchStatement(statement.Expression, sections)); } finally { _breakStack = oldBreak; _continueStack = oldContinue; } }
public object VisitSwitchStatement(JsSwitchStatement statement, bool addNewline) { _cb.Append("switch").Append(_space + "("); VisitExpression(statement.Expression, false); _cb.Append(")" + _space); _cb.Append("{").Indent(); if (!_minify) { _cb.AppendLine(); } foreach (var clause in statement.Sections) { bool first = true; foreach (var v in clause.Values) { if (!first && !_minify) { _cb.AppendLine(); } if (v != null) { _cb.Append("case "); VisitExpression(v, false); _cb.Append(":"); } else { _cb.Append("default:"); } first = false; } if (!_minify) { _cb.Append(" "); } VisitStatement(clause.Body, !_minify); } _cb.Outdent().Append("}"); if (addNewline) { _cb.AppendLine(); } return(null); }
private bool HandleSwitchStatement(JsSwitchStatement stmt, StackEntry location, ImmutableStack <StackEntry> stack, ImmutableStack <Tuple <string, State> > breakStack, ImmutableStack <Tuple <string, State> > continueStack, State currentState, State returnState, IList <JsStatement> currentBlock) { var stateAfter = GetStateAfterStatement(location, stack, currentState.FinallyStack, returnState); JsExpression expression = stmt.Expression; if (_isExpressionComplexEnoughForATemporaryVariable(expression)) { string newName = _allocateTempVariable(); currentBlock.Add(JsStatement.Var(newName, expression)); expression = JsExpression.Identifier(newName); } var clauses = new List <Tuple <JsExpression, JsBlockStatement> >(); JsStatement defaultClause = null; State? currentFallthroughState = null; for (int i = 0; i < stmt.Sections.Count; i++) { var clause = stmt.Sections[i]; var origBody = new List <JsStatement>(); origBody.AddRange(clause.Body.Statements); State?nextFallthroughState; if (i < stmt.Sections.Count - 1 && (origBody.Count == 0 || IsNextStatementReachable(origBody[origBody.Count - 1]))) { // Fallthrough var nextBody = stmt.Sections[i + 1].Body.Statements; if (nextBody.Count > 0 && nextBody[0] is JsLabelledStatement) { nextFallthroughState = GetOrCreateStateForLabel(((JsLabelledStatement)nextBody[0]).Label, currentState.FinallyStack); } else { nextFallthroughState = CreateNewStateValue(currentState.FinallyStack); } } else { nextFallthroughState = null; } breakStack = breakStack.Push(Tuple.Create(GetLabelForState(currentState), stateAfter.Item1)); IList <JsStatement> body; if (currentFallthroughState != null) { body = new List <JsStatement>(); body.Add(new JsGotoStateStatement(currentFallthroughState.Value, currentState)); Enqueue(ImmutableStack <StackEntry> .Empty.Push(new StackEntry(JsStatement.Block(origBody), 0)), breakStack, continueStack, currentFallthroughState.Value, stateAfter.Item1); } else { body = Handle(ImmutableStack <StackEntry> .Empty.Push(new StackEntry(JsStatement.Block(origBody), 0)), breakStack, continueStack, currentState, nextFallthroughState ?? stateAfter.Item1, false, false); } if (clause.Values.Any(v => v == null)) { defaultClause = JsStatement.Block(body); } else { JsExpression test = clause.Values.Select(v => JsExpression.Same(expression, v)).Aggregate((o, e) => o != null ? JsExpression.LogicalOr(o, e) : e); clauses.Add(Tuple.Create(test, JsStatement.Block(body))); } currentFallthroughState = nextFallthroughState; } clauses.Reverse(); currentBlock.Add(clauses.Where(c => c.Item1 != null).Aggregate(defaultClause, (o, n) => JsStatement.If(n.Item1, n.Item2, o))); currentBlock.Add(new JsGotoStateStatement(stateAfter.Item1, currentState)); if (stateAfter.Item2) { Enqueue(PushFollowing(stack, location), breakStack, continueStack, stateAfter.Item1, returnState); return(false); } return(true); }
private bool HandleSwitchStatement(JsSwitchStatement stmt, StackEntry location, ImmutableStack<StackEntry> stack, ImmutableStack<Tuple<string, State>> breakStack, ImmutableStack<Tuple<string, State>> continueStack, State currentState, State returnState, IList<JsStatement> currentBlock) { var stateAfter = GetStateAfterStatement(location, stack, currentState.FinallyStack, returnState); JsExpression expression = stmt.Expression; if (_isExpressionComplexEnoughForATemporaryVariable(expression)) { string newName = _allocateTempVariable(); currentBlock.Add(new JsVariableDeclarationStatement(newName, expression)); expression = JsExpression.Identifier(newName); } var clauses = new List<Tuple<JsExpression, JsBlockStatement>>(); JsStatement defaultClause = null; State? currentFallthroughState = null; for (int i = 0; i < stmt.Sections.Count; i++) { var clause = stmt.Sections[i]; var origBody = new List<JsStatement>(); origBody.AddRange(clause.Body.Statements); State? nextFallthroughState; if (i < stmt.Sections.Count - 1 && (origBody.Count == 0 || IsNextStatementReachable(origBody[origBody.Count - 1]))) { // Fallthrough var nextBody = stmt.Sections[i + 1].Body.Statements; if (nextBody.Count > 0 && nextBody[0] is JsLabelledStatement) nextFallthroughState = GetOrCreateStateForLabel(((JsLabelledStatement)nextBody[0]).Label, currentState.FinallyStack); else nextFallthroughState = CreateNewStateValue(currentState.FinallyStack); } else { nextFallthroughState = null; } breakStack = breakStack.Push(Tuple.Create(GetLabelForState(currentState), stateAfter.Item1)); IList<JsStatement> body; if (currentFallthroughState != null) { body = new List<JsStatement>(); body.Add(new JsGotoStateStatement(currentFallthroughState.Value, currentState)); Enqueue(ImmutableStack<StackEntry>.Empty.Push(new StackEntry(new JsBlockStatement(origBody), 0)), breakStack, continueStack, currentFallthroughState.Value, stateAfter.Item1); } else { body = Handle(ImmutableStack<StackEntry>.Empty.Push(new StackEntry(new JsBlockStatement(origBody), 0)), breakStack, continueStack, currentState, nextFallthroughState ?? stateAfter.Item1); } if (clause.Values.Any(v => v == null)) { defaultClause = new JsBlockStatement(body); } else { JsExpression test = clause.Values.Select(v => JsExpression.Same(expression, v)).Aggregate((o, e) => o != null ? JsExpression.LogicalOr(o, e) : e); clauses.Add(Tuple.Create(test, new JsBlockStatement(body))); } currentFallthroughState = nextFallthroughState; } clauses.Reverse(); currentBlock.Add(clauses.Where(c => c.Item1 != null).Aggregate(defaultClause, (o, n) => new JsIfStatement(n.Item1, n.Item2, o))); currentBlock.Add(new JsGotoStateStatement(stateAfter.Item1, currentState)); if (stateAfter.Item2) { Enqueue(PushFollowing(stack, location), breakStack, continueStack, stateAfter.Item1, returnState); return false; } return true; }
public void Visit(JsSwitchStatement node) { BeforeVisit(node); DefaultVisit(node, VisitSwitchStatement); AfterVisit(node); }
public virtual void VisitSwitchStatement(JsSwitchStatement node) { }