boaold /Backend/Runtime/DynamicType.cs

Language C# Lines 83
MD5 Hash b650ef4437b9efdf9569e577a61118e8 Estimated Cost $1,041 (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
/*
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
Back to Top