// Copyright (C) 2003, 2005 Daisuke Arai <darai@users.sourceforge.jp>
// 
// This program is part of Protra.
//
// Protra is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, see <http://www.gnu.org/licenses/>.
// 
// $Id: StatementNode.cs,v 1.3 2007-12-27 08:34:39 panacoran Exp $

using System;

namespace Protra.Lib.Lang
{
	/// <summary>
	/// 文のノードを表すクラスです。
	/// </summary>
	public class StatementNode : Node
	{
		/// <summary>
		/// ノード
		/// </summary>
		private Node node;

		/// <summary>
		/// 文脈を解析します。
		/// </summary>
		/// <exception cref="Protra.Lib.Lang.ParseException">
		/// 文脈にエラーがある場合にthrowされます。
		/// </exception>
		/// <param name="context">コンテキスト</param>
		public override void Parse(Context context)
		{
			this.ContextToken = (ContextToken)context.Current;

			ContextToken ct = (ContextToken)context.Current;
			string token = ct.ToString();

			switch(token)
			{
				case "if ": // if文
					node = new IfStatementNode();
					node.Parse(context);
					break;
				case "while ": // while文
					node = new WhileStatementNode();
					node.Parse(context);
					break;
				case "return ": // return文
				case "return":
					node = new ReturnStatementNode();
					node.Parse(context);
					break;
				case "break": // break文
					node = new BreakStatementNode();
					node.Parse(context);
					break;
				case "continue": // continue文
					node = new ContinueStatementNode();
					node.Parse(context);
					break;
				case ";": // 空文
					node = new EmptyStatementNode();
					node.Parse(context);
					break;
				default: // 式文
					node = new ExpressionStatementNode();
					node.Parse(context);
					break;
			}
		}

		/// <summary>
		/// 構文木を簡略化します。
		/// </summary>
		/// <returns>簡略化されたノード</returns>
		public override Node Simplify()
		{
			return node.Simplify();
		}

		/// <summary>
		/// プログラムを実行します。
		/// </summary>
		/// <exception cref="Protra.Lib.Lang.RuntimeException">
		/// プログラム実行中にエラーが発生した場合にthrowされます。
		/// </exception>
		/// <param name="resource">リソース</param>
		/// <param name="at">@作用素の値</param>
		/// <returns>実行結果</returns>
		public override Value Execute(Resource resource, int at)
		{
			node.Execute(resource, at);
			return null;
		}
	}
}
