boaold /Backend/AST/Node.cs

Language C# Lines 201
MD5 Hash 704729b5c448ceb456b67b85cfd616ac Estimated Cost $3,531 (why?)
Repository https://bitbucket.org/AdamMil/boaold View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
Boa is the reference implementation for a language similar to Python,
also called Boa. This implementation is both interpreted and compiled,
targeting the Microsoft .NET Framework.

http://www.adammil.net/
Copyright (C) 2004-2005 Adam Milazzo

This program 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 2
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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

using System;

namespace Boa.AST
{

public enum Scope : byte { Free, Local, Global, Temporary }

public interface IWalker
{ void PostWalk(Node node);
  bool Walk(Node node);
}

public enum ArgType { Normal, List, Dict };
public struct Argument
{ public Argument(Expression expr) { Name=null; Expression=expr; Type=ArgType.Normal; }
  public Argument(string name, Expression expr) { Name=name; Expression=expr; Type=ArgType.Normal; }
  public Argument(Expression expr, ArgType type) { Name=null; Expression=expr; Type=type; }

  public void ToCode(System.Text.StringBuilder sb)
  { if(Name!=null) sb.Append(Name);
    else if(Type==ArgType.List) sb.Append('*');
    else if(Type==ArgType.Dict) sb.Append("**");
    Expression.ToCode(sb, 0);
  }

  public string Name;
  public Expression Expression;
  public ArgType Type;
}

public sealed class ExceptClause : Node
{ public ExceptClause(Expression type, NameExpression target, Statement body) { Type=type; Target=target; Body=body; }

  public override void ToCode(System.Text.StringBuilder sb, int indent)
  { sb.Append(Type==null ? "except" : "except ");
    if(Type!=null) Type.ToCode(sb, 0);
    if(Target!=null)
    { sb.Append(", ");
      Target.ToCode(sb, 0);
    }
    sb.Append(": ");
    Body.ToCode(sb, indent+Options.IndentSize);
    if(!(Body is Suite)) sb.Append('\n');
  }

  public override void Walk(IWalker w)
  { if(w.Walk(this))
    { if(Type!=null) Type.Walk(w);
      if(Target!=null) Target.Walk(w);
      Body.Walk(w);
    }
    w.PostWalk(this);
  }

  public Expression Type;
  public NameExpression Target;
  public Statement  Body;
}

public struct ImportName
{ public ImportName(string name) { Name=name; AsName=null; }
  public ImportName(string name, string asName) { Name=name; AsName=asName; }

  public string SlotName
  { get
    { int index = Name.IndexOf('.');
      return index==-1 ? Name : Name.Substring(0, index);
    }
  }

  public void ToCode(System.Text.StringBuilder sb)
  { sb.Append(Name);
    if(AsName!=null)
    { sb.Append(" as ");
      sb.Append(AsName);
    }
  }

  public string Name, AsName;
}

public struct ListCompFor
{ public ListCompFor(Name[] names, Expression list, Expression test)
  { List=list; Test=test;

    for(int i=0; i<names.Length; i++) names[i].Scope = Scope.Temporary;
    if(names.Length==1) Names = new NameExpression(names[0]);
    else
    { Expression[] ne = new NameExpression[names.Length];
      for(int i=0; i<names.Length; i++) ne[i] = new NameExpression(names[i]);
      Names = new TupleExpression(ne);
    }
  }

  public void ToCode(System.Text.StringBuilder sb)
  { sb.Append(" for ");
    Names.ToCode(sb, 0);
    sb.Append(" in ");
    List.ToCode(sb, 0);
    if(Test!=null)
    { sb.Append(" if ");
      Test.ToCode(sb, 0);
    }
  }

  public Expression Names, List, Test;
}

public sealed class Name
{ public Name(string name) { String=name; Scope=Scope.Free; }
  public Name(string name, Scope scope) { String=name; Scope=scope; }

  public override int GetHashCode() { return String.GetHashCode(); }

  public string String;
  public Scope  Scope;
}

public abstract class Node
{ public bool IsConstant
  { get { return (Flags&NodeFlag.Constant)!=0; }
    set { if(value) Flags|=NodeFlag.Constant; else Flags&=~NodeFlag.Constant; }
  }

  public virtual object GetValue() { throw new NotSupportedException(); }
  public virtual void Optimize() { }

  public void SetLocation(Node node) { Source=node.Source; Line=node.Line; Column=node.Column; }
  public void SetLocation(string source, int line, int column) { Source=source; Line=line; Column=column; }

  public string ToCode()
  { System.Text.StringBuilder sb = new System.Text.StringBuilder();
    ToCode(sb, 0);
    return sb.ToString();
  }
  public abstract void ToCode(System.Text.StringBuilder sb, int indent);

  public virtual void Walk(IWalker w)
  { w.Walk(this);
    w.PostWalk(this);
  }

  public string Source="<unknown>";
  public int Line, Column;

  protected void StatementToCode(System.Text.StringBuilder sb, Statement stmt, int indent)
  { bool isSuite = stmt is Suite;
    if(!isSuite) sb.Append(' ');
    stmt.ToCode(sb, indent);
    if(!isSuite) sb.Append('\n');
  }
  
  [Flags] enum NodeFlag : byte { Constant=1 }
  NodeFlag Flags;
}

public enum ParamType { Required, Optional, List, Dict }

public struct Parameter
{ public Parameter(string name) : this(name, ParamType.Required) { }
  public Parameter(string name, Expression defaultValue) : this(name, ParamType.Optional) { Default=defaultValue; }
  public Parameter(string name, ParamType type) { Name=new Name(name, Scope.Local); Type=type; Default=null; }
  public Parameter(Name name, Expression defaultValue, ParamType type) { Name=name; Default=defaultValue; Type=type; }

  public override int GetHashCode() { return Name.GetHashCode(); }

  public void ToCode(System.Text.StringBuilder sb)
  { if(Type==ParamType.List) sb.Append('*');
    else if(Type==ParamType.Dict) sb.Append("**");
    sb.Append(Name.String);
    if(Default!=null) { sb.Append('='); Default.ToCode(sb, 0); }
  }

  public Name Name;
  public Expression Default;
  public ParamType Type;
}

} // namespace Boa.AST
Back to Top