/* * Copyright © 2011, Petro Protsyk, Denys Vuika * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using Scripting.SSharp.Runtime; namespace Scripting.SSharp.Parser.Ast { /// /// Base class for Script.NET Ast's nodes /// internal class ScriptAst : AstNode { /// /// Base constructor /// /// AstNodeList public ScriptAst(AstNodeArgs args) : base(args) { } /// /// Returns Source code for given AST /// /// internal string Code(IScriptContext context) { return context.Owner.SourceCode.Substring(Span.Start.Position, Span.Length); } /// /// Returns string representing concrete syntax tree /// /// internal string ConcreteSyntaxTree() { return ConcreteSyntaxTree(""); } private string ConcreteSyntaxTree(string inted) { string tree = Term.Name + "\r\n"; inted += " "; foreach (var node in ChildNodes) { var scriptNode = node as ScriptAst; if (scriptNode != null) tree += inted + scriptNode.ConcreteSyntaxTree(inted); else { if (!string.IsNullOrEmpty(node.Term.DisplayName)) tree += inted + node +"\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) return; int index = 0; while (index < ChildNodes.Count) { var node = ChildNodes[index] as ScriptAst; if (node != null) node.Evaluate(context); index++; } } } }