PageRenderTime 54ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/Backend/Runtime/DynamicType.cs

https://bitbucket.org/AdamMil/boaold
C# | 83 lines | 45 code | 17 blank | 21 comment | 5 complexity | b650ef4437b9efdf9569e577a61118e8 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. // FIXME: look into how obj.attr can throw "no such attribute" when obj.attr is actually just null
  20. using System;
  21. using System.Collections;
  22. namespace Boa.Runtime
  23. {
  24. public abstract class DynamicType
  25. { public virtual void DelAttr(object self, string name) { throw new NotSupportedException(); }
  26. public object GetAttr(object self, string name)
  27. { object value;
  28. if(GetAttr(self, name, out value)) return value;
  29. throw Ops.AttributeError("'{0}' object has no attribute '{1}'", __name__, name);
  30. }
  31. public virtual bool GetAttr(object self, string name, out object value)
  32. { value = null;
  33. return false;
  34. }
  35. public virtual object GetRawAttr(string name)
  36. { object ret;
  37. return GetAttr(null, name, out ret) ? ret : Ops.Missing;
  38. }
  39. public virtual List GetAttrNames(object self) { return new List(0); }
  40. public virtual bool IsSubclassOf(object other) { throw new NotSupportedException(); }
  41. public bool IsTryMatch(object other)
  42. { Tuple tup = other as Tuple;
  43. if(tup!=null)
  44. { foreach(object o in tup.items) if(IsSubclassOf(o)) return true;
  45. return false;
  46. }
  47. else return IsSubclassOf(other);
  48. }
  49. public virtual string Repr(object self)
  50. { object ret;
  51. if(Ops.TryInvoke(self, "__repr__", out ret)) return (string)ret;
  52. return Ops.Str(self);
  53. }
  54. public virtual void SetAttr(object self, string name, object value)
  55. { throw Ops.AttributeError("'{0}' object has no attribute '{1}'", __name__, name);
  56. }
  57. public object __name__;
  58. }
  59. public class NullType : DynamicType
  60. { NullType() { __name__ = "NullType"; }
  61. public override bool IsSubclassOf(object other) { return other==this; }
  62. public static readonly NullType Value = new NullType();
  63. }
  64. } // namespace Boa.Runtime