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); }
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); } }
// 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()); } }
public bool VerifyImplements(InterfaceDefn i) { foreach (FunctionDefn f in i.GetAllMethods()) { if (!HasFunction(f)) { return(false); } } return(true); }
public bool Implements(InterfaceDefn i) { foreach (FunctionDefn fd in i.GetAllMethods()) { if (!ImplementsMethod(fd)) { return(false); } } return(true); }
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); } }
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); }
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); } }
// 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()); } } }
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); }
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); }
public InterfaceInstance(ClassInstance obj, InterfaceDefn i) { this.obj = obj; hinterface = i; }
public bool Implements(InterfaceDefn i) { foreach (FunctionDefn fd in i.GetAllMethods()) if (!ImplementsMethod(fd)) return false; return true; }
public void AddInterface(InterfaceDefn x) { types.Add(x.name, x); interfaces.Add(x); }
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; }