OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright 2008 the V8 project authors. All rights reserved. | 3 # Copyright 2012 the V8 project authors. All rights reserved. |
4 # Redistribution and use in source and binary forms, with or without | 4 # Redistribution and use in source and binary forms, with or without |
5 # modification, are permitted provided that the following conditions are | 5 # modification, are permitted provided that the following conditions are |
6 # met: | 6 # met: |
7 # | 7 # |
8 # * Redistributions of source code must retain the above copyright | 8 # * Redistributions of source code must retain the above copyright |
9 # notice, this list of conditions and the following disclaimer. | 9 # notice, this list of conditions and the following disclaimer. |
10 # * Redistributions in binary form must reproduce the above | 10 # * Redistributions in binary form must reproduce the above |
11 # copyright notice, this list of conditions and the following | 11 # copyright notice, this list of conditions and the following |
12 # disclaimer in the documentation and/or other materials provided | 12 # disclaimer in the documentation and/or other materials provided |
13 # with the distribution. | 13 # with the distribution. |
(...skipping 829 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
843 self.right = right | 843 self.right = right |
844 | 844 |
845 def Evaluate(self, env, defs): | 845 def Evaluate(self, env, defs): |
846 if self.op == '||' or self.op == ',': | 846 if self.op == '||' or self.op == ',': |
847 return self.left.Evaluate(env, defs) or self.right.Evaluate(env, defs) | 847 return self.left.Evaluate(env, defs) or self.right.Evaluate(env, defs) |
848 elif self.op == 'if': | 848 elif self.op == 'if': |
849 return False | 849 return False |
850 elif self.op == '==': | 850 elif self.op == '==': |
851 inter = self.left.GetOutcomes(env, defs).Intersect(self.right.GetOutcomes(
env, defs)) | 851 inter = self.left.GetOutcomes(env, defs).Intersect(self.right.GetOutcomes(
env, defs)) |
852 return not inter.IsEmpty() | 852 return not inter.IsEmpty() |
| 853 elif self.op == '!=': |
| 854 inter = self.left.GetOutcomes(env, defs).Intersect(self.right.GetOutcomes(
env, defs)) |
| 855 return inter.IsEmpty() |
853 else: | 856 else: |
854 assert self.op == '&&' | 857 assert self.op == '&&' |
855 return self.left.Evaluate(env, defs) and self.right.Evaluate(env, defs) | 858 return self.left.Evaluate(env, defs) and self.right.Evaluate(env, defs) |
856 | 859 |
857 def GetOutcomes(self, env, defs): | 860 def GetOutcomes(self, env, defs): |
858 if self.op == '||' or self.op == ',': | 861 if self.op == '||' or self.op == ',': |
859 return self.left.GetOutcomes(env, defs).Union(self.right.GetOutcomes(env,
defs)) | 862 return self.left.GetOutcomes(env, defs).Union(self.right.GetOutcomes(env,
defs)) |
860 elif self.op == 'if': | 863 elif self.op == 'if': |
861 if self.right.Evaluate(env, defs): return self.left.GetOutcomes(env, defs) | 864 if self.right.Evaluate(env, defs): return self.left.GetOutcomes(env, defs) |
862 else: return Nothing() | 865 else: return Nothing() |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
925 self.AddToken(buf) | 928 self.AddToken(buf) |
926 elif self.Current(2) == '&&': | 929 elif self.Current(2) == '&&': |
927 self.AddToken('&&') | 930 self.AddToken('&&') |
928 self.Advance(2) | 931 self.Advance(2) |
929 elif self.Current(2) == '||': | 932 elif self.Current(2) == '||': |
930 self.AddToken('||') | 933 self.AddToken('||') |
931 self.Advance(2) | 934 self.Advance(2) |
932 elif self.Current(2) == '==': | 935 elif self.Current(2) == '==': |
933 self.AddToken('==') | 936 self.AddToken('==') |
934 self.Advance(2) | 937 self.Advance(2) |
| 938 elif self.Current(2) == '!=': |
| 939 self.AddToken('!=') |
| 940 self.Advance(2) |
935 else: | 941 else: |
936 return None | 942 return None |
937 return self.tokens | 943 return self.tokens |
938 | 944 |
939 | 945 |
940 class Scanner(object): | 946 class Scanner(object): |
941 """A simple scanner that can serve out tokens from a given list""" | 947 """A simple scanner that can serve out tokens from a given list""" |
942 | 948 |
943 def __init__(self, tokens): | 949 def __init__(self, tokens): |
944 self.tokens = tokens | 950 self.tokens = tokens |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
977 scan.Advance() | 983 scan.Advance() |
978 result = ParseLogicalExpression(scan) | 984 result = ParseLogicalExpression(scan) |
979 if (not result) or (scan.Current() != ')'): | 985 if (not result) or (scan.Current() != ')'): |
980 return None | 986 return None |
981 scan.Advance() | 987 scan.Advance() |
982 return result | 988 return result |
983 else: | 989 else: |
984 return None | 990 return None |
985 | 991 |
986 | 992 |
987 BINARIES = ['=='] | 993 BINARIES = ['==', '!='] |
988 def ParseOperatorExpression(scan): | 994 def ParseOperatorExpression(scan): |
989 left = ParseAtomicExpression(scan) | 995 left = ParseAtomicExpression(scan) |
990 if not left: return None | 996 if not left: return None |
991 while scan.HasMore() and (scan.Current() in BINARIES): | 997 while scan.HasMore() and (scan.Current() in BINARIES): |
992 op = scan.Current() | 998 op = scan.Current() |
993 scan.Advance() | 999 scan.Advance() |
994 right = ParseOperatorExpression(scan) | 1000 right = ParseOperatorExpression(scan) |
995 if not right: | 1001 if not right: |
996 return None | 1002 return None |
997 left = Operation(left, op, right) | 1003 left = Operation(left, op, right) |
998 return left | 1004 return left |
999 | 1005 |
1000 | 1006 |
1001 def ParseConditionalExpression(scan): | 1007 def ParseConditionalExpression(scan): |
1002 left = ParseOperatorExpression(scan) | 1008 left = ParseOperatorExpression(scan) |
1003 if not left: return None | 1009 if not left: return None |
1004 while scan.HasMore() and (scan.Current() == 'if'): | 1010 while scan.HasMore() and (scan.Current() == 'if'): |
1005 scan.Advance() | 1011 scan.Advance() |
1006 right = ParseOperatorExpression(scan) | 1012 right = ParseOperatorExpression(scan) |
1007 if not right: | 1013 if not right: |
1008 return None | 1014 return None |
1009 left= Operation(left, 'if', right) | 1015 left = Operation(left, 'if', right) |
1010 return left | 1016 return left |
1011 | 1017 |
1012 | 1018 |
1013 LOGICALS = ["&&", "||", ","] | 1019 LOGICALS = ["&&", "||", ","] |
1014 def ParseLogicalExpression(scan): | 1020 def ParseLogicalExpression(scan): |
1015 left = ParseConditionalExpression(scan) | 1021 left = ParseConditionalExpression(scan) |
1016 if not left: return None | 1022 if not left: return None |
1017 while scan.HasMore() and (scan.Current() in LOGICALS): | 1023 while scan.HasMore() and (scan.Current() in LOGICALS): |
1018 op = scan.Current() | 1024 op = scan.Current() |
1019 scan.Advance() | 1025 scan.Advance() |
(...skipping 512 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1532 for entry in timed_tests[:20]: | 1538 for entry in timed_tests[:20]: |
1533 t = FormatTime(entry.duration) | 1539 t = FormatTime(entry.duration) |
1534 sys.stderr.write("%4i (%s) %s\n" % (index, t, entry.GetLabel())) | 1540 sys.stderr.write("%4i (%s) %s\n" % (index, t, entry.GetLabel())) |
1535 index += 1 | 1541 index += 1 |
1536 | 1542 |
1537 return result | 1543 return result |
1538 | 1544 |
1539 | 1545 |
1540 if __name__ == '__main__': | 1546 if __name__ == '__main__': |
1541 sys.exit(Main()) | 1547 sys.exit(Main()) |
OLD | NEW |