public virtual object VisitSwitchStatement(SwitchStatement switchStatement, object data) {
			throw new global::System.NotImplementedException("SwitchStatement");
		}
		public virtual object VisitSwitchStatement(SwitchStatement switchStatement, object data) {
			Debug.Assert((switchStatement != null));
			Debug.Assert((switchStatement.SwitchExpression != null));
			Debug.Assert((switchStatement.SwitchSections != null));
			switchStatement.SwitchExpression.AcceptVisitor(this, data);
			foreach (SwitchSection o in switchStatement.SwitchSections) {
				Debug.Assert(o != null);
				o.AcceptVisitor(this, data);
			}
			return null;
		}
		public virtual object VisitSwitchStatement(SwitchStatement switchStatement, object data) {
			Debug.Assert((switchStatement != null));
			Debug.Assert((switchStatement.SwitchExpression != null));
			Debug.Assert((switchStatement.SwitchSections != null));
			nodeStack.Push(switchStatement.SwitchExpression);
			switchStatement.SwitchExpression.AcceptVisitor(this, data);
			switchStatement.SwitchExpression = ((Expression)(nodeStack.Pop()));
			for (int i = 0; i < switchStatement.SwitchSections.Count; i++) {
				SwitchSection o = switchStatement.SwitchSections[i];
				Debug.Assert(o != null);
				nodeStack.Push(o);
				o.AcceptVisitor(this, data);
				o = (SwitchSection)nodeStack.Pop();
				if (o == null)
					switchStatement.SwitchSections.RemoveAt(i--);
				else
					switchStatement.SwitchSections[i] = o;
			}
			return null;
		}
Beispiel #4
0
	void SelectStatement(out Statement statement) {
		Expression expr = null;
		Expect(197);
		if (la.kind == 74) {
			Get();
		}
		Expr(out expr);
		EndOfStmt();
		List<SwitchSection> selectSections = new List<SwitchSection>();
			Statement block = null;

		while (la.kind == 74) {
			List<CaseLabel> caseClauses = null; Location caseLocation = la.Location;
			Get();
			CaseClauses(out caseClauses);
			if (IsNotStatementSeparator()) {
				Expect(21);
			}
			EndOfStmt();
			SwitchSection selectSection = new SwitchSection(caseClauses);
				selectSection.StartLocation = caseLocation;

			Block(out block);
			selectSection.Children = block.Children;
				selectSection.EndLocation = t.EndLocation;
				selectSections.Add(selectSection);

		}
		statement = new SwitchStatement(expr, selectSections);

		Expect(113);
		Expect(197);
	}
		public sealed override object VisitSwitchStatement(SwitchStatement switchStatement, object data) {
			this.BeginVisit(switchStatement);
			object result = this.TrackedVisitSwitchStatement(switchStatement, data);
			this.EndVisit(switchStatement);
			return result;
		}
		public virtual object TrackedVisitSwitchStatement(SwitchStatement switchStatement, object data) {
			return base.VisitSwitchStatement(switchStatement, data);
		}
		public override object VisitSwitchStatement(SwitchStatement switchStatement, object data)
		{
			// switch(arg) { case label1: expr1; case label2: expr2; default: expr3; }
			//
			// Emulate With:
			// 
			//  object _switch1 = arg;
			//  if (arg.Equals(label1))
			//  {
			//      expr1;
			//  }
			//  else
			//  {
			//      if (arg.Equals(label2))
			//      {
			//          expr2;
			//      }
			//      else
			//      {
			//          expr3;
			//      }
			//  }
			//

			switchId++; // in case nested switch() statements
			string name = "_switch" + switchId.ToString();

			breakableStack.Push(new Breakable(false));

			bool isSwitchArg = false;

			CodeVariableReferenceExpression switchArg = null;
			SwitchSection defaultSection = null;

			// get default section
			foreach (SwitchSection section in switchStatement.SwitchSections)
			{
				foreach (CaseLabel label in section.SwitchLabels)
				{
					if (label.IsDefault)
					{
						defaultSection = section;
						break;
					}
				}

				if (defaultSection != null)
					break;
			}


			CodeConditionStatement _if = null;

			// get default section
			foreach (SwitchSection section in switchStatement.SwitchSections)
			{
				if (section != defaultSection)
				{
					if (!isSwitchArg)
					{
						isSwitchArg = true;

						codeStack.Push(NullStmtCollection);
						CodeVariableDeclarationStatement switchStmt = new CodeVariableDeclarationStatement("System.Object", name, (CodeExpression)switchStatement.SwitchExpression.AcceptVisitor(this, data));
						codeStack.Pop();

						switchArg = new CodeVariableReferenceExpression(name);

						AddStmt(switchStmt);
						AddStmt(new CodeSnippetStatement());
					}

					codeStack.Push(NullStmtCollection);

					CodeExpression condition = null;
					foreach (CaseLabel label in section.SwitchLabels)
					{
						CodeMethodInvokeExpression cond = new CodeMethodInvokeExpression(switchArg, "Equals", (CodeExpression)label.Label.AcceptVisitor(this, data));
						if (condition == null)
						{
							condition = cond;
						}
						else
						{
							condition = new CodeBinaryOperatorExpression(condition, CodeBinaryOperatorType.BooleanOr, cond);
						}
					}

					codeStack.Pop();

					if (_if == null)
					{
						_if = new CodeConditionStatement();
						_if.Condition = condition;

						AddStmt(_if);
					}
					else
					{
						CodeConditionStatement _if2 = new CodeConditionStatement();
						_if2.Condition = condition;

						_if.FalseStatements.Add(_if2);

						_if = _if2;
					}

					codeStack.Push(_if.TrueStatements);

					for (int i = 0; i < section.Children.Count; i++)
					{
						INode stmt = section.Children[i];

//						if (i == section.Children.Count - 1 && stmt is BreakStatement)
//							break;

						stmt.AcceptVisitor(this, data);
					}

					codeStack.Pop();
				}
			}

			if (defaultSection != null)
			{
				if (_if != null)
					codeStack.Push(_if.FalseStatements);

				for (int i = 0; i < defaultSection.Children.Count; i++)
				{
					INode stmt = defaultSection.Children[i];

//					if (i == defaultSection.Children.Count - 1 && stmt is BreakStatement)
//						break;

					stmt.AcceptVisitor(this, data);
				}

				if (_if != null)
					codeStack.Pop();
			}

			Breakable breakable = breakableStack.Pop();

			if (breakable.IsContinue)
			{
				throw new Exception("Continue Inside Switch Not Supported");
			}

			if (breakable.IsBreak)
			{
				AddStmt(new CodeLabeledStatement("break" + breakable.Id, new CodeExpressionStatement(new CodeSnippetExpression())));
			}

			return null;
		}