PageRenderTime 88ms CodeModel.GetById 28ms RepoModel.GetById 7ms app.codeStats 0ms

/Backend/Runtime/BoaType.cs

https://bitbucket.org/AdamMil/boaold
C# | 147 lines | 100 code | 26 blank | 21 comment | 20 complexity | 53d55c1c4a8598d1d5500ac2b135f700 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. using System.Collections;
  21. using System.Collections.Specialized;
  22. namespace Boa.Runtime
  23. {
  24. [AttributeUsage(AttributeTargets.Class|AttributeTargets.Interface|AttributeTargets.Struct)]
  25. public class BoaTypeAttribute : Attribute
  26. { public BoaTypeAttribute(string name) { Name=name; }
  27. public string Name;
  28. }
  29. public abstract class BoaType : DynamicType, IDynamicObject, ICallable, IHasAttributes
  30. { public BoaType(Type type)
  31. { this.type=type;
  32. inheritType = type;
  33. while(typeof(IInstance).IsAssignableFrom(inheritType)) inheritType = inheritType.BaseType;
  34. if(type==typeof(object)) __name__ = "object";
  35. else if(type==typeof(string)) __name__ = "string";
  36. else if(type==typeof(int)) __name__ = "int";
  37. else if(type==typeof(double)) __name__ = "float";
  38. else
  39. { BoaTypeAttribute attr = (BoaTypeAttribute)Attribute.GetCustomAttribute(type, typeof(BoaTypeAttribute));
  40. __name__ = attr==null ? type.FullName : attr.Name;
  41. }
  42. }
  43. #region ICallable
  44. public abstract object Call(params object[] args);
  45. #endregion
  46. #region IDynamicObject
  47. public abstract DynamicType GetDynamicType();
  48. #endregion
  49. #region IHasAttributes
  50. // TODO: this should include virtual slots like __name__, __dict__, etc., I think
  51. public virtual List __attrs__()
  52. { Initialize();
  53. return dict.keys();
  54. }
  55. public void __delattr__(string name)
  56. { object slot = RawGetSlot(name);
  57. if(slot!=Ops.Missing && Ops.DelDescriptor(slot, null)) return;
  58. RawRemoveSlot(name);
  59. }
  60. public object __getattr__(string name)
  61. { object slot = LookupSlot(name);
  62. if(slot!=Ops.Missing) return Ops.GetDescriptor(slot, null);
  63. if(name=="__name__") return __name__;
  64. return Ops.Missing;
  65. }
  66. public void __setattr__(string name, object value)
  67. { object slot = RawGetSlot(name);
  68. if(slot!=Ops.Missing && Ops.SetDescriptor(slot, null, value)) return;
  69. RawSetSlot(name, value);
  70. }
  71. #endregion
  72. public Type RealType { get { return type; } }
  73. public Type TypeToInheritFrom { get { return inheritType; } }
  74. public virtual void DelAttr(Tuple mro, int index, object self, string name) { DelAttr(self, name); }
  75. public override List GetAttrNames(object self) { return __attrs__(); }
  76. public virtual object GetAttr(Tuple mro, int index, object self, string name) { return GetAttr(self, name); }
  77. public override bool GetAttr(object self, string name, out object value)
  78. { value = __getattr__(name);
  79. return value!=Ops.Missing;
  80. }
  81. public override object GetRawAttr(string name) { return LookupSlot(name); }
  82. internal object LookupSlot(string name)
  83. { foreach(BoaType type in mro)
  84. { object slot = type.RawGetSlot(name);
  85. if(slot!=Ops.Missing) return slot;
  86. }
  87. return Ops.Missing;
  88. }
  89. protected object LookupSlot(Tuple mro, int index, string name)
  90. { for(; index<mro.Count; index++)
  91. { object slot = ((BoaType)mro.items[index]).RawGetSlot(name);
  92. if(slot!=Ops.Missing) return slot;
  93. }
  94. return Ops.Missing;
  95. }
  96. internal Tuple mro;
  97. protected virtual void Initialize()
  98. { if(!initialized)
  99. { dict=new Dict();
  100. initialized=true;
  101. }
  102. }
  103. protected object RawGetSlot(string name)
  104. { Initialize();
  105. if(name=="__dict__") return dict;
  106. if(name=="mro") return mro;
  107. object obj = dict[name];
  108. return obj!=null || dict.Contains(name) ? obj : Ops.Missing;
  109. }
  110. protected void RawRemoveSlot(string name) { dict.Remove(name); }
  111. protected void RawSetSlot(string name, object value) { dict[name] = value; }
  112. public virtual void SetAttr(Tuple mro, int index, object self, string name, object value)
  113. { SetAttr(self, name, value);
  114. }
  115. protected Dict dict;
  116. protected Type type, inheritType;
  117. protected bool initialized;
  118. }
  119. } // namespace Boa.Runtime