/Backend/Runtime/DynamicType.cs
C# | 83 lines | 45 code | 17 blank | 21 comment | 5 complexity | b650ef4437b9efdf9569e577a61118e8 MD5 | raw file
Possible License(s): GPL-2.0
- /*
- 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.
- */
- // FIXME: look into how obj.attr can throw "no such attribute" when obj.attr is actually just null
- using System;
- using System.Collections;
- namespace Boa.Runtime
- {
- public abstract class DynamicType
- { public virtual void DelAttr(object self, string name) { throw new NotSupportedException(); }
- public object GetAttr(object self, string name)
- { object value;
- if(GetAttr(self, name, out value)) return value;
- throw Ops.AttributeError("'{0}' object has no attribute '{1}'", __name__, name);
- }
- public virtual bool GetAttr(object self, string name, out object value)
- { value = null;
- return false;
- }
- public virtual object GetRawAttr(string name)
- { object ret;
- return GetAttr(null, name, out ret) ? ret : Ops.Missing;
- }
- public virtual List GetAttrNames(object self) { return new List(0); }
- public virtual bool IsSubclassOf(object other) { throw new NotSupportedException(); }
- public bool IsTryMatch(object other)
- { Tuple tup = other as Tuple;
- if(tup!=null)
- { foreach(object o in tup.items) if(IsSubclassOf(o)) return true;
- return false;
- }
- else return IsSubclassOf(other);
- }
- public virtual string Repr(object self)
- { object ret;
- if(Ops.TryInvoke(self, "__repr__", out ret)) return (string)ret;
- return Ops.Str(self);
- }
-
- public virtual void SetAttr(object self, string name, object value)
- { throw Ops.AttributeError("'{0}' object has no attribute '{1}'", __name__, name);
- }
- public object __name__;
- }
- public class NullType : DynamicType
- { NullType() { __name__ = "NullType"; }
- public override bool IsSubclassOf(object other) { return other==this; }
- public static readonly NullType Value = new NullType();
- }
- } // namespace Boa.Runtime