public IList <CoolMethod> GetVirtualTable(CoolType type) { var currentVirtualTable = Methods[type].ToList(); if (type.Parent == null) { return(currentVirtualTable); } var parentVirtualTable = GetVirtualTable(type.Parent).ToList(); for (int i = 0; i < parentVirtualTable.Count; i++) { int index; for (index = 0; index < currentVirtualTable.Count; index++) { if (currentVirtualTable[index].Name == parentVirtualTable[i].Name) { break; } } if (index == currentVirtualTable.Count) { continue; } parentVirtualTable[i] = currentVirtualTable[index]; currentVirtualTable.RemoveAt(index); } return(parentVirtualTable.Concat(currentVirtualTable).ToList()); }
public CoolMethod(CoolType contextType, string name, List <CoolType> formals, CoolType returnType, ISymbolTable symbolTable) { Type = contextType; Name = name; ParamsSignature = formals; ReturnType = returnType; SymbolTable = symbolTable; }
public void AddMethod(CoolType type, string method, List <CoolType> formals, CoolType returnType, ISymbolTable symbolTable) { if (!Methods.ContainsKey(type)) { Methods[type] = new List <CoolMethod>(); } Methods[type].Add(new CoolMethod(type, method, formals, returnType, symbolTable)); }
public bool GetMethodOnIt(CoolType type, string method, out CoolMethod CoolMethod) { if (!Methods.ContainsKey(type)) { Methods[type] = new List <CoolMethod>(); } CoolMethod = Methods[type].FirstOrDefault(x => x.Name == method); return(CoolMethod != null); }
public bool GetTypeDefinition(string typeName, ISymbolTable symbolTable, out CoolType coolType) { if (typeName == SuperCOOL.Constants.Types.SelfType) { coolType = SelfType(symbolTable); return(true); } return(Types.TryGetValue(typeName, out coolType)); }
public override bool Equals(object obj) { if (this is NullType || obj is NullType) { return(true); } CoolType coolType = obj as CoolType; return(coolType.Name == this.Name); }
public CoolMethod GetMethod(CoolType type, string method) { if (GetMethodOnIt(type, method, out var CoolMethod)) { return(CoolMethod); } if (type.Parent == null) { return(null); } GetMethodIfDef(type.Parent, method, out CoolMethod); return(CoolMethod); }
public bool GetTypeForObject(ISymbolTable symbolTable, string nameObject, out CoolType coolType) { var result = symbolTable.IsDefObject(nameObject, out var info); if (!result) { coolType = new NullType(); return(false); } GetTypeDefinition(info.Type, symbolTable, out coolType); coolType = coolType ?? new NullType(); return(true); }
public bool GetMethodIfDef(CoolType type, string method, out CoolMethod CoolMethod) { CoolMethod = new NullMethod(method); if (type is NullType) { return(true); } if (type is SelfType selftype) { return(GetMethodIfDef(selftype.ContextType, method, out CoolMethod)); } if (GetMethodOnIt(type, method, out CoolMethod)) { return(true); } if (type.Parent != null) { return(GetMethodIfDef(type.Parent, method, out CoolMethod)); } return(false); }
public bool IsIt(CoolType Tatara) { if (this is NullType || Tatara is NullType) { return(true); } if (this is SelfType self && Tatara is SelfType tatara) { return(self.ContextType == tatara.ContextType); } if (this is SelfType me) { return(me.ContextType.IsIt(Tatara)); } if (Tatara is SelfType ancestor) { return(false); } var type = this; if (type == Tatara) { return(true); } while (type.Parent != null) { type = type.Parent; if (type.Equals(Tatara)) { return(true); } } return(false); }
public CoolType GetTypeLCA(CoolType type1, CoolType type2) { if (this.lca_table == null) { LCATable(); } if (type1 is NullType) { return(type2); } if (type2 is NullType) { return(type1); } if (type1 is SelfType A && type2 is SelfType B && A.ContextType == B.ContextType) { return(A); } if (type1 is SelfType C) { return(GetTypeLCA(C.ContextType, type2)); } if (type2 is SelfType D) { return(GetTypeLCA(type1, D.ContextType)); } int l1 = this.distance[type1], l2 = this.distance[type2]; if (l1 > l2) { var temp1 = type1; type1 = type2; type2 = temp1; var temp2 = l1; l1 = l2; l2 = temp2; } for (int i = (int)Math.Log(l2, 2); i >= 0; --i) { if (l2 - (1 << i) >= l1) { type2 = this.lca_table[type2][i]; l2 = this.distance[type2]; } } if (type1 == type2) { return(type1); } for (int i = (int)Math.Log(l1, 2); i >= 0; --i) { var t1 = this.lca_table[type1]; var t2 = this.lca_table[type2]; if (t1[i] != null && t1[i] != t2[i]) { type1 = this.lca_table[type1][i]; type2 = this.lca_table[type2][i]; } } return(this.lca_table[type1][0]); }
public bool InheritsFrom(CoolType A, CoolType B) { return(A.IsIt(B)); }
public override bool EnsureParameter(int index, CoolType type) { return(true); }
public virtual bool EnsureParameter(int index, CoolType type) { return(type.IsIt(ParamsSignature[index])); }
public SelfType(CoolType contextType) : base("SELF_TYPE") { ContextType = contextType; }