From 8dc44673fac744c60d87dae12dfaec0df0fb9481 Mon Sep 17 00:00:00 2001 From: Chris Pressey Date: Tue, 12 Dec 2017 12:45:47 +0000 Subject: [PATCH] LocationRefs are equal if names/type are; this needed __str__ tho. --- src/sixtypical/analyzer.py | 27 ++++----------------------- src/sixtypical/model.py | 10 ++++++++++ tests/SixtyPical Analysis.md | 4 ++-- 3 files changed, 16 insertions(+), 25 deletions(-) diff --git a/src/sixtypical/analyzer.py b/src/sixtypical/analyzer.py index 34fce75..8e55ae9 100644 --- a/src/sixtypical/analyzer.py +++ b/src/sixtypical/analyzer.py @@ -89,17 +89,8 @@ class Context(object): self._writeable.add(ref) def __str__(self): - def locstr(loc): - if isinstance(loc, LocationRef): - return "{}:{}".format(loc.name, loc.type) - else: - return str(loc) - - def locsetstr(s): - return '{' + ', '.join([locstr(loc) for loc in list(s)]) + '}' - return "Context(\n _touched={},\n _meaningful={},\n _writeable={}\n)".format( - locsetstr(self._touched), locsetstr(self._meaningful), locsetstr(self._writeable) + LocationRef.format_set(self._touched), LocationRef.format_set(self._meaningful), LocationRef.format_set(self._writeable) ) def clone(self): @@ -185,22 +176,12 @@ class Analyzer(object): ) def assert_affected_within(self, name, affected, limited_to): - # We reduce the set of LocationRefs to a set of strings (their labels). - # This is necessary because currently, two LocationRefs that refer to the - # same location are not considered euqal. (But two LocationRefs with the - # same label should always be the same type.) - - affected = set([loc.name for loc in affected]) - limited_to = set([loc.name for loc in limited_to]) - - def loc_list(label_set): - return ', '.join(sorted(label_set)) - overage = affected - limited_to if not overage: return - message = 'in %s: %s are {%s} but affects {%s} which exceeds by: {%s} ' % ( - self.current_routine.name, name, loc_list(limited_to), loc_list(affected), loc_list(overage) + message = 'in %s: %s are %s but affects %s which exceeds it by: %s ' % ( + self.current_routine.name, name, + LocationRef.format_set(limited_to), LocationRef.format_set(affected), LocationRef.format_set(overage) ) raise IncompatibleConstraintsError(message) diff --git a/src/sixtypical/model.py b/src/sixtypical/model.py index 2c4e730..6b72321 100644 --- a/src/sixtypical/model.py +++ b/src/sixtypical/model.py @@ -8,6 +8,9 @@ class Type(object): def __repr__(self): return 'Type(%r)' % self.name + def __str__(self): + return self.name + def __eq__(self, other): return isinstance(other, Type) and other.name == self.name @@ -98,9 +101,16 @@ class LocationRef(Ref): def __repr__(self): return '%s(%r, %r)' % (self.__class__.__name__, self.type, self.name) + def __str__(self): + return "{}:{}".format(self.name, self.type) + def is_constant(self): return isinstance(self.type, RoutineType) + @classmethod + def format_set(cls, location_refs): + return '{%s}' % ', '.join([str(loc) for loc in sorted(location_refs)]) + class IndirectRef(Ref): def __init__(self, ref): diff --git a/tests/SixtyPical Analysis.md b/tests/SixtyPical Analysis.md index 9b95718..2673c91 100644 --- a/tests/SixtyPical Analysis.md +++ b/tests/SixtyPical Analysis.md @@ -1234,7 +1234,7 @@ a, z, and n are trashed, and must be declared as such | { | copy 0, lives | } - ? ForbiddenWriteError: a in main + ? ForbiddenWriteError: n in main a, z, and n are trashed, and must not be declared as outputs. @@ -1244,7 +1244,7 @@ a, z, and n are trashed, and must not be declared as outputs. | { | copy 0, lives | } - ? UnmeaningfulOutputError: a in main + ? UnmeaningfulOutputError: n in main Unless of course you subsequently initialize them.