#region using
using Irony.Compiler;
using ScriptNET.Runtime;
#endregion
namespace ScriptNET.Ast
{
///
/// Base class for Script.NET Ast's nodes
///
public class ScriptAst : AstNode
{
///
/// Base constructor
///
/// AstNodeList
public ScriptAst(AstNodeArgs args)
: base(args)
{
}
///
/// Returns Source code for given AST
///
///
public string Code()
{
return "Unimplemented yet";
}
///
/// Returns string representing concrete syntax tree
///
///
public string ConcreteSyntaxTree()
{
return ConcreteSyntaxTree("");
}
private string ConcreteSyntaxTree(string inted)
{
string tree = Term.Name + "\r\n";
inted += " ";
foreach (AstNode node in ChildNodes)
{
ScriptAst scriptNode = node as ScriptAst;
if (scriptNode != null)
tree += inted + scriptNode.ConcreteSyntaxTree(inted);
else
{
if (!string.IsNullOrEmpty(node.Term.DisplayName))
tree += inted + node.ToString() +"\r\n";
}
}
return tree;
}
//TODO: Move To ScriptProg
///
/// Evaluates all child nodes
///
/// ScriptContext object
/// result of the last node evaluation
public object Execute(IScriptContext context)
{
Evaluate(context);
return context.Result;
}
///
/// Evaluates script
///
/// ScriptContext
public virtual void Evaluate(IScriptContext context)
{
if (ChildNodes.Count > 0)
{
int index = 0;
while (index < ChildNodes.Count)
{
ScriptAst node = ChildNodes[index] as ScriptAst;
if (node != null)
node.Evaluate(context);
index++;
}
}
}
}
}