// 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: FunctionDefinitionNode.cs,v 1.3 2007-12-27 08:34:39 panacoran Exp $

using System;
using System.Collections;

namespace Protra.Lib.Lang
{
	/// <summary>
	/// 関数定義のノードを表すクラスです。
	/// </summary>
	public class FunctionDefinitionNode : Node
	{
		/// <summary>
		/// 関数名
		/// </summary>
		private string name;
		/// <summary>
		/// 引数の名前のリスト
		/// </summary>
		private ArrayList nameList = new ArrayList();
		/// <summary>
		/// ノードのリスト
		/// </summary>
		private ArrayList nodeList = new ArrayList();


		/// <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;

			// 関数名のチェック
			context.MoveNext();
			ContextToken ct = (ContextToken)context.Current;
			if(! ct.IsFunctionName)
				throw new ParseException("function name expected", this.ContextToken);
			name = ct.ToString();

			// 引数のチェック
			context.MoveNext();
			ct = (ContextToken)context.Current;
			string token = ct.ToString();
			if(token != "(")
			{
				ct = (ContextToken)context.Current;
				token = ct.ToString();
				if(token != ";")
					throw new ParseException("';' expected", this.ContextToken);
			}
			else
			{
				context.MoveNext();
				ct = (ContextToken)context.Current;
				token = ct.ToString();
				if(token == ")")
				{
					context.MoveNext();
					ct = (ContextToken)context.Current;
					token = ct.ToString();
					if(token != ";")
						throw new ParseException("';' expected", this.ContextToken);
				}
				else
				{
					do
					{
						ct = (ContextToken)context.Current;
						if(! ct.IsLocalVariableName)
							throw new ParseException("function argument expected", this.ContextToken);
						nameList.Add(ct.ToString());
						context.MoveNext();
						ct = (ContextToken)context.Current;
						token = ct.ToString();
						if(token == ")")
						{
							context.MoveNext();
							ct = (ContextToken)context.Current;
							token = ct.ToString();
							if(token != ";")
								throw new ParseException("';' expected", this.ContextToken);
							break;
						}
						else if(token != ",")
							throw new ParseException("',' or ')' expected", this.ContextToken);
					}while(context.MoveNext());
				}
			}

			// 本体のチェック
			while(context.MoveNext())
			{
				ct = (ContextToken)context.Current;
				token = ct.ToString();
				if(token == "end")
				{
					context.MoveNext();
					ct = (ContextToken)context.Current;
					token = ct.ToString();
					if(token != ";")
						throw new ParseException("';' expected", this.ContextToken);
					return;
				}

				Node node = new StatementNode();
				node.Parse(context);
				nodeList.Add(node);
			}

			throw new ParseException("unexpected EOF was detected", this.ContextToken);
		}

		/// <summary>
		/// 構文木を簡略化します。
		/// </summary>
		/// <returns>簡略化されたノード</returns>
		public override Node Simplify()
		{
			for(int i = 0; i < nodeList.Count; i++)
			{
				Node node = (Node)nodeList[i];
				nodeList[i] = node.Simplify();
			}
			return this;
		}

		/// <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)
		{
			// 関数テーブルに登録
			FunctionType ft = new FunctionType(name, nameList.Count);
			if(resource.FunctionTable.ContainsFunctionType(ft))
				throw new RuntimeException("function is already defined --- " + ft.ToString(), this.ContextToken);
			resource.FunctionTable[ft] = this;
			
			return null;
		}

		/// <summary>
		/// 引数の名前のリストを取得します。
		/// </summary>
		public string[] ArgumentNames
		{
			get { return (string[])nameList.ToArray(typeof(string)); }
		}

		/// <summary>
		/// ノードのリストを取得します。
		/// </summary>
		public Node[] Nodes
		{
			get { return (Node[])nodeList.ToArray(typeof(Node)); }
		}
	}
}
