Exemplo n.º 1
0
        public static InterfaceDefn CreateInterface(ModuleDefn m, ParseNode x)
        {
            string        name = x.GetChild("name").ToString();
            InterfaceDefn r    = new InterfaceDefn(m, name);

            ParseNode inherits = x.GetChild("inherits");

            if (inherits != null)
            {
                foreach (ParseNode node in inherits.Children)
                {
                    r.AddBaseInterface(CreateTypeRef(node));
                }
            }

            ParseNode methods = x.GetChild("methods");

            if (methods != null)
            {
                foreach (ParseNode node in methods.Children)
                {
                    FunctionDefn f = CreateMethod(node, r);
                    r.AddMethod(f);
                }
            }

            m.AddInterface(r);
            return(r);
        }
Exemplo n.º 2
0
 public void ResolveTypes(ModuleDefn global, ModuleDefn m)
 {
     if (baseclass != null)
     {
         baseclass.Resolve(global, m);
     }
     for (int i = 0; i < interfaces.Count; ++i)
     {
         interfaces[i].Resolve(global, m);
         HeronType     t  = interfaces[i].type;
         InterfaceDefn id = t as InterfaceDefn;
         if (id == null)
         {
             throw new Exception(t.name + " is not an interface");
         }
     }
     foreach (FieldDefn f in GetFields())
     {
         f.ResolveTypes(global, m);
     }
     foreach (FunctionDefn f in GetAllMethods())
     {
         f.ResolveTypes(global, m);
     }
 }
Exemplo n.º 3
0
 // TODO: I may want to change the requirement that DuckValue needs a 
 // class instances, same with interface instance
 public DuckValue(ClassInstance obj, InterfaceDefn i)
     : base(obj, i)
 {
     HeronType t = obj.Type;
     foreach (FunctionDefn f in i.GetAllMethods())
     {
         if (!obj.SupportsFunction(f))
             throw new Exception("Failed to duck-type, object of type " + t.GetName() + " does not match interface " + i.GetName());
     }
 }
Exemplo n.º 4
0
 public bool VerifyImplements(InterfaceDefn i)
 {
     foreach (FunctionDefn f in i.GetAllMethods())
     {
         if (!HasFunction(f))
         {
             return(false);
         }
     }
     return(true);
 }
Exemplo n.º 5
0
        public bool Implements(InterfaceDefn i)
        {
            foreach (FunctionDefn fd in i.GetAllMethods())
            {
                if (!ImplementsMethod(fd))
                {
                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 6
0
 public IEnumerable <InterfaceDefn> GetImplementedInterfaces()
 {
     foreach (TypeRef tr in interfaces)
     {
         InterfaceDefn id = tr.type as InterfaceDefn;
         if (id == null)
         {
             throw new Exception("Illegal type as implemented interface ");
         }
         yield return(id);
     }
 }
Exemplo n.º 7
0
        static public InterfaceDefn ParseInterface(ModuleDefn m, string s)
        {
            ParseNode node = ParserState.Parse(HeronGrammar.Interface, s);

            if (node == null)
            {
                return(null);
            }
            InterfaceDefn r = CodeModelBuilder.CreateInterface(m, node);

            return(r);
        }
Exemplo n.º 8
0
 public IEnumerable <InterfaceDefn> GetInheritedInterfaces()
 {
     foreach (TypeRef tr in basetypes)
     {
         InterfaceDefn id = tr.type as InterfaceDefn;
         if (id == null)
         {
             throw new Exception("Interface has an illegal base type");
         }
         yield return(id);
     }
 }
Exemplo n.º 9
0
        // TODO: I may want to change the requirement that DuckValue needs a
        // class instances, same with interface instance
        public DuckValue(ClassInstance obj, InterfaceDefn i)
            : base(obj, i)
        {
            HeronType t = obj.Type;

            foreach (FunctionDefn f in i.GetAllMethods())
            {
                if (!obj.SupportsFunction(f))
                {
                    throw new Exception("Failed to duck-type, object of type " + t.GetName() + " does not match interface " + i.GetName());
                }
            }
        }
Exemplo n.º 10
0
        public override HeronValue As(HeronType t)
        {
            InterfaceDefn id = t as InterfaceDefn;

            if (id == null)
            {
                return(null);
            }
            if (hinterface.Implements(id))
            {
                return(new InterfaceInstance(obj, id));
            }
            return(null);
        }
Exemplo n.º 11
0
        public bool InheritsFrom(InterfaceDefn i)
        {
            string s = i.name;

            if (s == name)
            {
                return(true);
            }
            foreach (TypeRef tr in basetypes)
            {
                InterfaceDefn id = tr.type as InterfaceDefn;
                if (id == null)
                {
                    throw new Exception("Could not find base interface type " + tr.name);
                }
                if (id.InheritsFrom(i))
                {
                    return(true);
                }
            }
            return(false);
        }
Exemplo n.º 12
0
 public InterfaceInstance(ClassInstance obj, InterfaceDefn i)
 {
     this.obj   = obj;
     hinterface = i;
 }
Exemplo n.º 13
0
        public bool Implements(InterfaceDefn i)
        {
            foreach (FunctionDefn fd in i.GetAllMethods())
                if (!ImplementsMethod(fd))
                    return false;

            return true;
        }
Exemplo n.º 14
0
 public void AddInterface(InterfaceDefn x)
 {
     types.Add(x.name, x);
     interfaces.Add(x);
 }
Exemplo n.º 15
0
 public InterfaceInstance(ClassInstance obj, InterfaceDefn i)
 {
     this.obj = obj;
     hinterface = i;
 }
Exemplo n.º 16
0
 public void AddInterface(InterfaceDefn x)
 {
     types.Add(x.name, x);
     interfaces.Add(x);
 }
Exemplo n.º 17
0
 public bool InheritsFrom(InterfaceDefn i)
 {
     string s = i.name;
     if (s == name)
         return true;
     foreach (TypeRef tr in basetypes)
     {
         InterfaceDefn id = tr.type as InterfaceDefn;
         if (id == null)
             throw new Exception("Could not find base interface type " + tr.name);
         if (id.InheritsFrom(i))
             return true;
     }
     return false;
 }