PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/Backend/AST/Node.cs

https://bitbucket.org/AdamMil/boaold
C# | 200 lines | 144 code | 36 blank | 20 comment | 24 complexity | 704729b5c448ceb456b67b85cfd616ac MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. Boa is the reference implementation for a language similar to Python,
  3. also called Boa. This implementation is both interpreted and compiled,
  4. targeting the Microsoft .NET Framework.
  5. http://www.adammil.net/
  6. Copyright (C) 2004-2005 Adam Milazzo
  7. This program is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU General Public License
  9. as published by the Free Software Foundation; either version 2
  10. of the License, or (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. using System;
  20. namespace Boa.AST
  21. {
  22. public enum Scope : byte { Free, Local, Global, Temporary }
  23. public interface IWalker
  24. { void PostWalk(Node node);
  25. bool Walk(Node node);
  26. }
  27. public enum ArgType { Normal, List, Dict };
  28. public struct Argument
  29. { public Argument(Expression expr) { Name=null; Expression=expr; Type=ArgType.Normal; }
  30. public Argument(string name, Expression expr) { Name=name; Expression=expr; Type=ArgType.Normal; }
  31. public Argument(Expression expr, ArgType type) { Name=null; Expression=expr; Type=type; }
  32. public void ToCode(System.Text.StringBuilder sb)
  33. { if(Name!=null) sb.Append(Name);
  34. else if(Type==ArgType.List) sb.Append('*');
  35. else if(Type==ArgType.Dict) sb.Append("**");
  36. Expression.ToCode(sb, 0);
  37. }
  38. public string Name;
  39. public Expression Expression;
  40. public ArgType Type;
  41. }
  42. public sealed class ExceptClause : Node
  43. { public ExceptClause(Expression type, NameExpression target, Statement body) { Type=type; Target=target; Body=body; }
  44. public override void ToCode(System.Text.StringBuilder sb, int indent)
  45. { sb.Append(Type==null ? "except" : "except ");
  46. if(Type!=null) Type.ToCode(sb, 0);
  47. if(Target!=null)
  48. { sb.Append(", ");
  49. Target.ToCode(sb, 0);
  50. }
  51. sb.Append(": ");
  52. Body.ToCode(sb, indent+Options.IndentSize);
  53. if(!(Body is Suite)) sb.Append('\n');
  54. }
  55. public override void Walk(IWalker w)
  56. { if(w.Walk(this))
  57. { if(Type!=null) Type.Walk(w);
  58. if(Target!=null) Target.Walk(w);
  59. Body.Walk(w);
  60. }
  61. w.PostWalk(this);
  62. }
  63. public Expression Type;
  64. public NameExpression Target;
  65. public Statement Body;
  66. }
  67. public struct ImportName
  68. { public ImportName(string name) { Name=name; AsName=null; }
  69. public ImportName(string name, string asName) { Name=name; AsName=asName; }
  70. public string SlotName
  71. { get
  72. { int index = Name.IndexOf('.');
  73. return index==-1 ? Name : Name.Substring(0, index);
  74. }
  75. }
  76. public void ToCode(System.Text.StringBuilder sb)
  77. { sb.Append(Name);
  78. if(AsName!=null)
  79. { sb.Append(" as ");
  80. sb.Append(AsName);
  81. }
  82. }
  83. public string Name, AsName;
  84. }
  85. public struct ListCompFor
  86. { public ListCompFor(Name[] names, Expression list, Expression test)
  87. { List=list; Test=test;
  88. for(int i=0; i<names.Length; i++) names[i].Scope = Scope.Temporary;
  89. if(names.Length==1) Names = new NameExpression(names[0]);
  90. else
  91. { Expression[] ne = new NameExpression[names.Length];
  92. for(int i=0; i<names.Length; i++) ne[i] = new NameExpression(names[i]);
  93. Names = new TupleExpression(ne);
  94. }
  95. }
  96. public void ToCode(System.Text.StringBuilder sb)
  97. { sb.Append(" for ");
  98. Names.ToCode(sb, 0);
  99. sb.Append(" in ");
  100. List.ToCode(sb, 0);
  101. if(Test!=null)
  102. { sb.Append(" if ");
  103. Test.ToCode(sb, 0);
  104. }
  105. }
  106. public Expression Names, List, Test;
  107. }
  108. public sealed class Name
  109. { public Name(string name) { String=name; Scope=Scope.Free; }
  110. public Name(string name, Scope scope) { String=name; Scope=scope; }
  111. public override int GetHashCode() { return String.GetHashCode(); }
  112. public string String;
  113. public Scope Scope;
  114. }
  115. public abstract class Node
  116. { public bool IsConstant
  117. { get { return (Flags&NodeFlag.Constant)!=0; }
  118. set { if(value) Flags|=NodeFlag.Constant; else Flags&=~NodeFlag.Constant; }
  119. }
  120. public virtual object GetValue() { throw new NotSupportedException(); }
  121. public virtual void Optimize() { }
  122. public void SetLocation(Node node) { Source=node.Source; Line=node.Line; Column=node.Column; }
  123. public void SetLocation(string source, int line, int column) { Source=source; Line=line; Column=column; }
  124. public string ToCode()
  125. { System.Text.StringBuilder sb = new System.Text.StringBuilder();
  126. ToCode(sb, 0);
  127. return sb.ToString();
  128. }
  129. public abstract void ToCode(System.Text.StringBuilder sb, int indent);
  130. public virtual void Walk(IWalker w)
  131. { w.Walk(this);
  132. w.PostWalk(this);
  133. }
  134. public string Source="<unknown>";
  135. public int Line, Column;
  136. protected void StatementToCode(System.Text.StringBuilder sb, Statement stmt, int indent)
  137. { bool isSuite = stmt is Suite;
  138. if(!isSuite) sb.Append(' ');
  139. stmt.ToCode(sb, indent);
  140. if(!isSuite) sb.Append('\n');
  141. }
  142. [Flags] enum NodeFlag : byte { Constant=1 }
  143. NodeFlag Flags;
  144. }
  145. public enum ParamType { Required, Optional, List, Dict }
  146. public struct Parameter
  147. { public Parameter(string name) : this(name, ParamType.Required) { }
  148. public Parameter(string name, Expression defaultValue) : this(name, ParamType.Optional) { Default=defaultValue; }
  149. public Parameter(string name, ParamType type) { Name=new Name(name, Scope.Local); Type=type; Default=null; }
  150. public Parameter(Name name, Expression defaultValue, ParamType type) { Name=name; Default=defaultValue; Type=type; }
  151. public override int GetHashCode() { return Name.GetHashCode(); }
  152. public void ToCode(System.Text.StringBuilder sb)
  153. { if(Type==ParamType.List) sb.Append('*');
  154. else if(Type==ParamType.Dict) sb.Append("**");
  155. sb.Append(Name.String);
  156. if(Default!=null) { sb.Append('='); Default.ToCode(sb, 0); }
  157. }
  158. public Name Name;
  159. public Expression Default;
  160. public ParamType Type;
  161. }
  162. } // namespace Boa.AST