Skip to content

Commit

Permalink
sd
Browse files Browse the repository at this point in the history
  • Loading branch information
uxmal committed Jul 22, 2015
1 parent 4ffffd1 commit 1d4e507
Show file tree
Hide file tree
Showing 15 changed files with 69 additions and 82 deletions.
14 changes: 7 additions & 7 deletions src/TypeInference/Analyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface Analyzer
{
IFileSystem FileSystem { get; }

DataType loadFile(string path);
DataType LoadFile(string path);
DataType loadModule(List<Name> name, State state);
Module getAstForFile(string file);
string moduleQname(string file);
Expand Down Expand Up @@ -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);
Expand All @@ -383,7 +383,7 @@ List<Diagnostic> getFileErrs(string file, Dictionary<string, List<Diagnostic>> m
return msgs;
}

public DataType loadFile(string path)
public DataType LoadFile(string path)
{
path = FileSystem.GetFullPath(path);

Expand All @@ -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);
Expand Down Expand Up @@ -563,7 +563,7 @@ public DataType loadModule(List<Name> 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;
Expand All @@ -586,7 +586,7 @@ public DataType loadModule(List<Name> name, State state)
string startFile = path + suffix;
if (FileSystem.FileExists( startFile))
{
DataType mod = loadFile(startFile);
DataType mod = LoadFile(startFile);
if (mod == null)
{
return null;
Expand Down Expand Up @@ -636,7 +636,7 @@ public void loadFileRecursive(string fullname)
{
if (file_or_dir.EndsWith(suffix))
{
loadFile(file_or_dir);
LoadFile(file_or_dir);
}
}
}
Expand Down
13 changes: 5 additions & 8 deletions src/TypeInference/Binding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Pytocs.TypeInference
{
public class Binding : IComparable<object>
public class Binding : IComparable<Binding>
{
public enum Kind
{
Expand Down Expand Up @@ -193,9 +193,9 @@ public bool isURL()
/// <summary>
/// Bindings can be sorted by their location for outlining purposes.
/// </summary>
public int CompareTo(object o)
public int CompareTo(Binding o)
{
return start - ((Binding) o).start;
return start - o.start;
}

public override string ToString()
Expand All @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/TypeInference/Builtins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static Url newTutUrl(string path)
"UnicodeEncodeError", "UnicodeError", "UnicodeTranslateError",
"UnicodeWarning", "UserWarning", "ValueError", "Warning",
"ZeroDivisionError"
};
};

ClassType newClass(string name, State table)
{
Expand Down
1 change: 0 additions & 1 deletion src/TypeInference/ILogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/TypeInference/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@

namespace org.yinwang.pysonar
{

public class Options
{
private Dictionary<string, object> optionsMap = new Dictionary<string, object>();
private List<string> args = new List<string>();


public Options(params string[] args)
{
for (int i = 0; i < args.Length; i++)
Expand Down
20 changes: 10 additions & 10 deletions src/TypeInference/Progress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
8 changes: 4 additions & 4 deletions src/TypeInference/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -310,9 +310,9 @@ public ISet<Binding> lookupAttr(string attr)
}


/**
* Look for a binding named {@code name} and if found, return its type.
*/
/// <summary>
/// Look for a binding named {@code name} and if found, return its type.
/// </summary>
public DataType lookupType(string name)
{
ISet<Binding> bs = lookup(name);
Expand Down
36 changes: 7 additions & 29 deletions src/TypeInference/Statistics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,17 @@ namespace Pytocs.TypeInference
{
public class Statistics
{
IDictionary<string, object> contents = new Dictionary<string, object>();
IDictionary<string, long> contents = new Dictionary<string, long>();

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)
Expand All @@ -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);
}
}
}
17 changes: 10 additions & 7 deletions src/TypeInference/TypeStack.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
using System.Collections.Generic;
using System;
using Pytocs.Types;

namespace Pytocs.TypeInference
{
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;
Expand All @@ -19,7 +20,7 @@ public Pair(object first, object second)

private List<Pair> stack = new List<Pair>();

public void push(object first, object second)
public void push(DataType first, DataType second)
{
stack.Add(new Pair(first, second));
}
Expand All @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions src/TypeInference/TypeTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class TypeTransformer : // INodeVisitor<DataType>,
IExpVisitor<DataType>
{
private State scope;
public Analyzer analyzer;
private Analyzer analyzer;

public TypeTransformer(State s, Analyzer analyzer)
{
Expand Down Expand Up @@ -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);
}
Expand Down
7 changes: 5 additions & 2 deletions src/Types/DictType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion src/Types/FloatType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public override bool Equals(object other)

public override int GetHashCode()
{
return "FloatType".GetHashCode();
return GetType().Name.GetHashCode();
}
}
}
7 changes: 5 additions & 2 deletions src/Types/ListType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 5 additions & 2 deletions src/Types/TupleType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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]))
Expand Down
Loading

0 comments on commit 1d4e507

Please sign in to comment.