boaold /Backend/Runtime/BoaType.cs

Language C# Lines 147
MD5 Hash 53d55c1c4a8598d1d5500ac2b135f700 Estimated Cost $2,408 (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
/*
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;
using System.Collections;
using System.Collections.Specialized;

namespace Boa.Runtime
{

[AttributeUsage(AttributeTargets.Class|AttributeTargets.Interface|AttributeTargets.Struct)]
public class BoaTypeAttribute : Attribute
{ public BoaTypeAttribute(string name) { Name=name; }
  public string Name;
}

public abstract class BoaType : DynamicType, IDynamicObject, ICallable, IHasAttributes
{ public BoaType(Type type)
  { this.type=type;
    inheritType = type;
    while(typeof(IInstance).IsAssignableFrom(inheritType)) inheritType = inheritType.BaseType;

    if(type==typeof(object)) __name__ = "object";
    else if(type==typeof(string)) __name__ = "string";
    else if(type==typeof(int)) __name__ = "int";
    else if(type==typeof(double)) __name__ = "float";
    else
    { BoaTypeAttribute attr = (BoaTypeAttribute)Attribute.GetCustomAttribute(type, typeof(BoaTypeAttribute));
      __name__ = attr==null ? type.FullName : attr.Name;
    }
  }

  #region ICallable
  public abstract object Call(params object[] args);
  #endregion

  #region IDynamicObject
  public abstract DynamicType GetDynamicType();
  #endregion

  #region IHasAttributes
  // TODO: this should include virtual slots like __name__, __dict__, etc., I think
  public virtual List __attrs__()
  { Initialize();
    return dict.keys();
  }

  public void __delattr__(string name)
  { object slot = RawGetSlot(name);
    if(slot!=Ops.Missing && Ops.DelDescriptor(slot, null)) return;
    RawRemoveSlot(name);
  }

  public object __getattr__(string name)
  { object slot = LookupSlot(name);
    if(slot!=Ops.Missing) return Ops.GetDescriptor(slot, null);
    if(name=="__name__") return __name__;
    return Ops.Missing;
  }

  public void __setattr__(string name, object value)
  { object slot = RawGetSlot(name);
    if(slot!=Ops.Missing && Ops.SetDescriptor(slot, null, value)) return;
    RawSetSlot(name, value);
  }
  #endregion

  public Type RealType { get { return type; } }
  public Type TypeToInheritFrom { get { return inheritType; } }

  public virtual void DelAttr(Tuple mro, int index, object self, string name) { DelAttr(self, name); }

  public override List GetAttrNames(object self) { return __attrs__(); }

  public virtual object GetAttr(Tuple mro, int index, object self, string name) { return GetAttr(self, name); }
  public override bool GetAttr(object self, string name, out object value)
  { value = __getattr__(name);
    return value!=Ops.Missing;
  }

  public override object GetRawAttr(string name) { return LookupSlot(name); }

  internal object LookupSlot(string name)
  { foreach(BoaType type in mro)
    { object slot = type.RawGetSlot(name);
      if(slot!=Ops.Missing) return slot;
    }
    return Ops.Missing;
  }
  
  protected object LookupSlot(Tuple mro, int index, string name)
  { for(; index<mro.Count; index++)
    { object slot = ((BoaType)mro.items[index]).RawGetSlot(name);
      if(slot!=Ops.Missing) return slot;
    }
    return Ops.Missing;
  }

  internal Tuple mro;

  protected virtual void Initialize()
  { if(!initialized)
    { dict=new Dict();
      initialized=true;
    }
  }

  protected object RawGetSlot(string name)
  { Initialize();
    if(name=="__dict__") return dict;
    if(name=="mro") return mro;
    object obj = dict[name];
    return obj!=null || dict.Contains(name) ? obj : Ops.Missing;
  }

  protected void RawRemoveSlot(string name) { dict.Remove(name); }

  protected void RawSetSlot(string name, object value) { dict[name] = value; }

  public virtual void SetAttr(Tuple mro, int index, object self, string name, object value)
  { SetAttr(self, name, value);
  }

  protected Dict dict;
  protected Type type, inheritType;
  protected bool initialized;
}

} // namespace Boa.Runtime
Back to Top