diff --git a/src/TypeInference/Analyzer.cs b/src/TypeInference/Analyzer.cs index 783ba1c..64a2f1e 100644 --- a/src/TypeInference/Analyzer.cs +++ b/src/TypeInference/Analyzer.cs @@ -13,7 +13,7 @@ public interface Analyzer { IFileSystem FileSystem { get; } - DataType loadFile(string path); + DataType LoadFile(string path); DataType loadModule(List name, State state); Module getAstForFile(string file); string moduleQname(string file); @@ -365,7 +365,7 @@ public void putProblem(string file, int begin, int end, String msg) } - void addFileErr(String file, int begin, int end, String msg) + void addFileErr(string file, int begin, int end, String msg) { Diagnostic d = new Diagnostic(file, Diagnostic.Category.ERROR, begin, end, msg); getFileErrs(file, semanticErrors).Add(d); @@ -383,7 +383,7 @@ List getFileErrs(string file, Dictionary> m return msgs; } - public DataType loadFile(string path) + public DataType LoadFile(string path) { path = FileSystem.GetFullPath(path); @@ -405,7 +405,7 @@ public DataType loadFile(string path) } // set new CWD and save the old one on stack - String oldcwd = cwd; + string oldcwd = cwd; setCWD(FileSystem.GetDirectoryName(path)); pushImportStack(path); @@ -563,7 +563,7 @@ public DataType loadModule(List name, State state) string initFile = FileSystem.CombinePath(path, "__init__.py"); if (FileSystem.FileExists(initFile)) { - DataType mod = loadFile(initFile); + DataType mod = LoadFile(initFile); if (mod == null) { return null; @@ -586,7 +586,7 @@ public DataType loadModule(List name, State state) string startFile = path + suffix; if (FileSystem.FileExists( startFile)) { - DataType mod = loadFile(startFile); + DataType mod = LoadFile(startFile); if (mod == null) { return null; @@ -636,7 +636,7 @@ public void loadFileRecursive(string fullname) { if (file_or_dir.EndsWith(suffix)) { - loadFile(file_or_dir); + LoadFile(file_or_dir); } } } diff --git a/src/TypeInference/Binding.cs b/src/TypeInference/Binding.cs index fda8e55..02e4912 100644 --- a/src/TypeInference/Binding.cs +++ b/src/TypeInference/Binding.cs @@ -7,7 +7,7 @@ namespace Pytocs.TypeInference { - public class Binding : IComparable + public class Binding : IComparable { public enum Kind { @@ -193,9 +193,9 @@ public bool isURL() /// /// Bindings can be sorted by their location for outlining purposes. /// - public int CompareTo(object o) + public int CompareTo(Binding o) { - return start - ((Binding) o).start; + return start - o.start; } public override string ToString() @@ -207,17 +207,14 @@ public override string ToString() sb.Append(":type=").Append(type); sb.Append(":qname=").Append(qname); sb.Append(":refs="); + sb.Append("["); if (refs.Count > 10) { - sb.Append("["); sb.Append(refs.First()); - sb.Append(", ...("); - sb.Append(refs.Count - 1); - sb.Append(" more)]"); + sb.AppendFormat(", ...({0} more)]", refs.Count - 1); } else { - sb.Append("["); var sep = ""; foreach (var r in refs) { diff --git a/src/TypeInference/Builtins.cs b/src/TypeInference/Builtins.cs index e5f6dd2..e999df3 100644 --- a/src/TypeInference/Builtins.cs +++ b/src/TypeInference/Builtins.cs @@ -90,7 +90,7 @@ public static Url newTutUrl(string path) "UnicodeEncodeError", "UnicodeError", "UnicodeTranslateError", "UnicodeWarning", "UserWarning", "ValueError", "Warning", "ZeroDivisionError" - }; + }; ClassType newClass(string name, State table) { diff --git a/src/TypeInference/ILogger.cs b/src/TypeInference/ILogger.cs index 7828201..41843f9 100644 --- a/src/TypeInference/ILogger.cs +++ b/src/TypeInference/ILogger.cs @@ -14,7 +14,6 @@ public interface ILogger void Error(Exception ex, string p); void Inform(string p); void Verbose(string p); - } public class Logger : ILogger diff --git a/src/TypeInference/Options.cs b/src/TypeInference/Options.cs index d62b0e2..5bddf47 100644 --- a/src/TypeInference/Options.cs +++ b/src/TypeInference/Options.cs @@ -3,13 +3,11 @@ namespace org.yinwang.pysonar { - public class Options { private Dictionary optionsMap = new Dictionary(); private List args = new List(); - public Options(params string[] args) { for (int i = 0; i < args.Length; i++) diff --git a/src/TypeInference/Progress.cs b/src/TypeInference/Progress.cs index 91b9627..158bc42 100644 --- a/src/TypeInference/Progress.cs +++ b/src/TypeInference/Progress.cs @@ -7,16 +7,16 @@ public class Progress private const int MAX_SPEED_DIGITS = 5; private Analyzer analyzer; - DateTime startTime; - DateTime lastTickTime; - long lastCount; - int lastRate; - int lastAvgRate; - long total; - long count; - long width; - long segSize; - bool quiet; + private DateTime startTime; + private DateTime lastTickTime; + private long lastCount; + private int lastRate; + private int lastAvgRate; + private long total; + private long count; + private long width; + private long segSize; + private bool quiet; public Progress(Analyzer analyzer, long total, long width, bool quiet) { diff --git a/src/TypeInference/State.cs b/src/TypeInference/State.cs index fc5f8c6..0f07e7c 100644 --- a/src/TypeInference/State.cs +++ b/src/TypeInference/State.cs @@ -57,7 +57,7 @@ public State(State s) } // erase and overwrite this to s's contents - public void overwrite(State s) + public void Overwrite(State s) { this.table = s.table; this.Parent = s.Parent; @@ -310,9 +310,9 @@ public ISet lookupAttr(string attr) } - /** - * Look for a binding named {@code name} and if found, return its type. - */ + /// + /// Look for a binding named {@code name} and if found, return its type. + /// public DataType lookupType(string name) { ISet bs = lookup(name); diff --git a/src/TypeInference/Statistics.cs b/src/TypeInference/Statistics.cs index 260a46d..da8876a 100644 --- a/src/TypeInference/Statistics.cs +++ b/src/TypeInference/Statistics.cs @@ -6,25 +6,17 @@ namespace Pytocs.TypeInference { public class Statistics { - IDictionary contents = new Dictionary(); + IDictionary contents = new Dictionary(); public void putInt(string key, long value) { contents[key] = value; } - public void inc(string key, long x) { - long? old = getInt(key); - if (!old.HasValue) - { - contents[key] = 1; - } - else - { - contents[key] = old.Value + x; - } + long old = getInt(key); + contents[key] = old + x; } public void inc(string key) @@ -34,35 +26,21 @@ public void inc(string key) public long getInt(string key) { - object ret; + long ret; if (!contents.TryGetValue(key, out ret)) return 0; - return (long) ret; + return ret; } - public string print() { StringBuilder sb = new StringBuilder(); - foreach (var e in contents) { - sb.AppendFormat("\n- {0}: {1}", e.Key, e.Value); + sb.AppendLine(); + sb.AppendFormat("- {0}: {1}", e.Key, e.Value); } return sb.ToString(); } - - internal void putDate(string key, DateTime dateTime) - { - contents[key] = dateTime; - } - - internal DateTime getDateTime(string key) - { - object dt; - if (contents.TryGetValue(key, out dt)) - return (DateTime) dt; - return default(DateTime); - } } } \ No newline at end of file diff --git a/src/TypeInference/TypeStack.cs b/src/TypeInference/TypeStack.cs index ac1c878..222d4f2 100644 --- a/src/TypeInference/TypeStack.cs +++ b/src/TypeInference/TypeStack.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System; +using Pytocs.Types; namespace Pytocs.TypeInference { @@ -7,10 +8,10 @@ public class TypeStack { class Pair { - public object first; - public object second; + public DataType first; + public DataType second; - public Pair(object first, object second) + public Pair(DataType first, DataType second) { this.first = first; this.second = second; @@ -19,7 +20,7 @@ public Pair(object first, object second) private List stack = new List(); - public void push(object first, object second) + public void push(DataType first, DataType second) { stack.Add(new Pair(first, second)); } @@ -29,12 +30,14 @@ public void pop(object first, object second) stack.RemoveAt(stack.Count - 1); } - public bool contains(object first, object second) + public bool contains(DataType first, DataType second) { foreach (Pair p in stack) { - if (p.first == first && p.second == second || - p.first == second && p.second == first) + if (object.ReferenceEquals(p.first, first) && + object.ReferenceEquals(p.second, second) || + object.ReferenceEquals(p.first, second) && + object.ReferenceEquals(p.second, first)) { return true; } diff --git a/src/TypeInference/TypeTransformer.cs b/src/TypeInference/TypeTransformer.cs index deca152..256692c 100644 --- a/src/TypeInference/TypeTransformer.cs +++ b/src/TypeInference/TypeTransformer.cs @@ -11,7 +11,7 @@ public class TypeTransformer : // INodeVisitor, IExpVisitor { private State scope; - public Analyzer analyzer; + private Analyzer analyzer; public TypeTransformer(State s, Analyzer analyzer) { @@ -812,15 +812,15 @@ public DataType VisitIf(IfStatement i) if (cont1 && cont2) { s1.merge(s2); - scope.overwrite(s1); + scope.Overwrite(s1); } else if (cont1) { - scope.overwrite(s1); + scope.Overwrite(s1); } else if (cont2) { - scope.overwrite(s2); + scope.Overwrite(s2); } return UnionType.union(type1, type2); } diff --git a/src/Types/DictType.cs b/src/Types/DictType.cs index 2371e93..35f55f9 100644 --- a/src/Types/DictType.cs +++ b/src/Types/DictType.cs @@ -36,13 +36,16 @@ public TupleType toTupleType(int n) public override bool Equals(object other) { - if (typeStack.contains(this, other)) + var dtOther = other as DataType; + if (dtOther == null) + return false; + if (typeStack.contains(this, dtOther)) { return true; } else if (other is DictType) { - typeStack.push(this, other); + typeStack.push(this, dtOther); DictType co = (DictType) other; bool ret = (co.keyType.Equals(keyType) && co.valueType.Equals(valueType)); diff --git a/src/Types/FloatType.cs b/src/Types/FloatType.cs index 8027b99..2675579 100644 --- a/src/Types/FloatType.cs +++ b/src/Types/FloatType.cs @@ -14,7 +14,7 @@ public override bool Equals(object other) public override int GetHashCode() { - return "FloatType".GetHashCode(); + return GetType().Name.GetHashCode(); } } } \ No newline at end of file diff --git a/src/Types/ListType.cs b/src/Types/ListType.cs index eb0114e..566a164 100644 --- a/src/Types/ListType.cs +++ b/src/Types/ListType.cs @@ -66,14 +66,17 @@ public TupleType toTupleType() public override bool Equals(object other) { - if (typeStack.contains(this, other)) + var dtOther = other as DataType; + if (dtOther == null) + return false; + if (typeStack.contains(this, dtOther)) { return true; } else if (other is ListType) { ListType co = (ListType) other; - typeStack.push(this, other); + typeStack.push(this, co); bool ret = co.eltType.Equals(eltType); typeStack.pop(this, other); return ret; diff --git a/src/Types/TupleType.cs b/src/Types/TupleType.cs index 43490ec..b76de7a 100644 --- a/src/Types/TupleType.cs +++ b/src/Types/TupleType.cs @@ -71,7 +71,10 @@ public ListType toListType() public override bool Equals(object other) { - if (typeStack.contains(this, other)) + var dtOther = other as DataType; + if (dtOther == null) + return false; + if (typeStack.contains(this, dtOther)) { return true; } @@ -82,7 +85,7 @@ public override bool Equals(object other) if (types1.Count != types2.Count) return false; - typeStack.push(this, other); + typeStack.push(this, dtOther); for (int i = 0; i < types1.Count; i++) { if (!types1[i].Equals(types2[i])) diff --git a/src/Types/UnionType.cs b/src/Types/UnionType.cs index f8b09e8..e69a042 100644 --- a/src/Types/UnionType.cs +++ b/src/Types/UnionType.cs @@ -148,7 +148,10 @@ public DataType firstUseful() public override bool Equals(object other) { - if (typeStack.contains(this, other)) + var dtOther = other as DataType; + if (dtOther == null) + return false; + if (typeStack.contains(this, dtOther)) { return true; } @@ -162,7 +165,7 @@ public override bool Equals(object other) } else { - typeStack.push(this, other); + typeStack.push(this, dtOther); foreach (DataType t in types2) { if (!types1.Contains(t))