# HG changeset patch # User Paul Boddie # Date 1340753558 -7200 # Node ID 499e42c00d574af271adf17212bfd7a61a86ad4a # Parent 1805fbc407add84465ea3c16433a202d4921064f Moved errors and type deduction operations into separate modules. Moved AtLeast into the data module. diff -r 1805fbc407ad -r 499e42c00d57 micropython/__init__.py --- a/micropython/__init__.py Wed Jun 27 01:02:32 2012 +0200 +++ b/micropython/__init__.py Wed Jun 27 01:32:38 2012 +0200 @@ -37,9 +37,10 @@ which the functionality of the micropython package may be accessed. """ -from micropython.common import * from micropython.data import * +from micropython.errors import * from micropython.program import Location +from micropython.types import * import micropython.ast import micropython.native import micropython.opt diff -r 1805fbc407ad -r 499e42c00d57 micropython/ast.py --- a/micropython/ast.py Wed Jun 27 01:02:32 2012 +0200 +++ b/micropython/ast.py Wed Jun 27 01:32:38 2012 +0200 @@ -21,6 +21,7 @@ from micropython.common import * from micropython.data import * +from micropython.errors import * from micropython.rsvp import * from micropython.trans import Helper from micropython.code import Assembler diff -r 1805fbc407ad -r 499e42c00d57 micropython/code.py --- a/micropython/code.py Wed Jun 27 01:02:32 2012 +0200 +++ b/micropython/code.py Wed Jun 27 01:32:38 2012 +0200 @@ -20,8 +20,8 @@ """ from micropython.opt import Optimiser -from micropython.common import * from micropython.data import * +from micropython.errors import * from micropython.rsvp import * import compiler.ast diff -r 1805fbc407ad -r 499e42c00d57 micropython/common.py --- a/micropython/common.py Wed Jun 27 01:02:32 2012 +0200 +++ b/micropython/common.py Wed Jun 27 01:32:38 2012 +0200 @@ -97,169 +97,6 @@ return hasattr(node, "unit") and node.unit.parent.has_key(node.unit.name) -def get_object_types_for_usage(usage, objtable, name, unit_name, all_attributes): - - """ - Return for the given attribute 'usage', using the 'objtable', the object - types which satisfy such usage, reporting any problems for the given 'name' - and 'unit_name'. Each object type is given as a tuple of the form (type, - is_static). - - Where 'all_attributes' is set to a true value, all attributes in a usage - list must be matched to provide object types. Otherwise, the presence of any - attribute from a usage list in a type's set of attributes will be enough to - provide that type as a suitable object type. - """ - - all_objtypes = set() - - for attrnames in usage: - attrnames = attrnames or () - - # Determine whether all attributes are required. - - if all_attributes: - objtypes = objtable.all_possible_objects_plus_status(attrnames) - if not objtypes: - print >>sys.stderr, "Warning: usage in %r for %r finds no object supporting all attributes %r" % ( - unit_name, name, attrnames.keys()) - else: - objtypes = objtable.any_possible_objects_plus_status(attrnames) - if not objtypes: - print >>sys.stderr, "Warning: usage in %r for %r finds no object supporting any attributes %r" % ( - unit_name, name, attrnames.keys()) - - all_objtypes.update(objtypes) - - return all_objtypes - -def filter_using_self(objtypes, cls): - - """ - Filter the given 'objtypes' (a collection of object names and static - indicators) for the given 'cls', returning only the (name, static) elements - compatible with 'cls' and its descendants. - """ - - descendants = cls.all_descendants() - filtered = [] - for objname, is_static in objtypes: - if objname == cls.full_name() or objname in descendants: - filtered.append((objname, is_static)) - return filtered - -# Errors. - -class ProcessingError(Exception): - - "A processing error." - - pass - -class TableError(ProcessingError): - - "An error occurring during access to a lookup table." - - pass - -class TableGenerationError(ProcessingError): - - "An error occurring when generating a lookup table." - - pass - -class NodeProcessingError(ProcessingError): - - "A processing error associated with a particular program node." - - def __init__(self, message): - self.message = message - self.unit_name = None - self.astnode = None - - def get_lineno(self, node): - if node is None: - return None - - lineno = node.lineno - if lineno is not None: - return lineno - else: - for child in node.getChildNodes(): - lineno = self.get_lineno(child) - if lineno is not None: - return lineno - return None - - def __repr__(self): - return "Error in %r at line %r: %s" % (self.unit_name, self.get_lineno(self.astnode), self.message) - - def __str__(self): - return repr(self) - -class InspectError(NodeProcessingError): - - "An error during the module inspection process." - - pass - -class TranslateError(NodeProcessingError): - - "An error during the module translation process." - - pass - -class TranslationNotImplementedError(TranslateError): - - "An error caused by a node not being supported in translation." - - pass - -# Special representations. - -class AtLeast: - - "A special representation for numbers of a given value or greater." - - def __init__(self, count): - self.count = count - - def __eq__(self, other): - return 0 - - __lt__ = __le__ = __eq__ - - def __ne__(self, other): - return 1 - - def __gt__(self, other): - if isinstance(other, AtLeast): - return 0 - else: - return self.count > other - - def __ge__(self, other): - if isinstance(other, AtLeast): - return 0 - else: - return self.count >= other - - def __iadd__(self, other): - if isinstance(other, AtLeast): - self.count += other.count - else: - self.count += other - return self - - def __radd__(self, other): - if isinstance(other, AtLeast): - return AtLeast(self.count + other.count) - else: - return AtLeast(self.count + other) - - def __repr__(self): - return "AtLeast(%r)" % self.count - # Useful data. operator_functions = { diff -r 1805fbc407ad -r 499e42c00d57 micropython/data.py --- a/micropython/data.py Wed Jun 27 01:02:32 2012 +0200 +++ b/micropython/data.py Wed Jun 27 01:32:38 2012 +0200 @@ -54,10 +54,16 @@ from micropython.program import ReplaceableContext, PlaceholderContext from micropython.basicdata import * -from micropython.common import * +from micropython.errors import * from micropython.objectset import * +from micropython.types import * import sys +try: + set +except NameError: + from sets import Set as set + class NamespaceDict: "A mix-in providing dictionary methods." @@ -2068,4 +2074,49 @@ lambda_index += 1 return lambda_index +# Special representations. + +class AtLeast: + + "A special representation for numbers of a given value or greater." + + def __init__(self, count): + self.count = count + + def __eq__(self, other): + return 0 + + __lt__ = __le__ = __eq__ + + def __ne__(self, other): + return 1 + + def __gt__(self, other): + if isinstance(other, AtLeast): + return 0 + else: + return self.count > other + + def __ge__(self, other): + if isinstance(other, AtLeast): + return 0 + else: + return self.count >= other + + def __iadd__(self, other): + if isinstance(other, AtLeast): + self.count += other.count + else: + self.count += other + return self + + def __radd__(self, other): + if isinstance(other, AtLeast): + return AtLeast(self.count + other.count) + else: + return AtLeast(self.count + other) + + def __repr__(self): + return "AtLeast(%r)" % self.count + # vim: tabstop=4 expandtab shiftwidth=4 diff -r 1805fbc407ad -r 499e42c00d57 micropython/errors.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/micropython/errors.py Wed Jun 27 01:32:38 2012 +0200 @@ -0,0 +1,87 @@ +#!/usr/bin/env python + +""" +Error classes. + +Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 Paul Boddie + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . +""" + +class ProcessingError(Exception): + + "A processing error." + + pass + +class TableError(ProcessingError): + + "An error occurring during access to a lookup table." + + pass + +class TableGenerationError(ProcessingError): + + "An error occurring when generating a lookup table." + + pass + +class NodeProcessingError(ProcessingError): + + "A processing error associated with a particular program node." + + def __init__(self, message): + self.message = message + self.unit_name = None + self.astnode = None + + def get_lineno(self, node): + if node is None: + return None + + lineno = node.lineno + if lineno is not None: + return lineno + else: + for child in node.getChildNodes(): + lineno = self.get_lineno(child) + if lineno is not None: + return lineno + return None + + def __repr__(self): + return "Error in %r at line %r: %s" % (self.unit_name, self.get_lineno(self.astnode), self.message) + + def __str__(self): + return repr(self) + +class InspectError(NodeProcessingError): + + "An error during the module inspection process." + + pass + +class TranslateError(NodeProcessingError): + + "An error during the module translation process." + + pass + +class TranslationNotImplementedError(TranslateError): + + "An error caused by a node not being supported in translation." + + pass + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 1805fbc407ad -r 499e42c00d57 micropython/graph.py --- a/micropython/graph.py Wed Jun 27 01:02:32 2012 +0200 +++ b/micropython/graph.py Wed Jun 27 01:32:38 2012 +0200 @@ -19,8 +19,8 @@ this program. If not, see . """ -from micropython.common import TableError from micropython.data import Class, Function +from micropython.errors import TableError import sys, os def get_graph(program, out=None, filename=None): diff -r 1805fbc407ad -r 499e42c00d57 micropython/inspect.py --- a/micropython/inspect.py Wed Jun 27 01:02:32 2012 +0200 +++ b/micropython/inspect.py Wed Jun 27 01:32:38 2012 +0200 @@ -74,6 +74,7 @@ from micropython.common import * from micropython.data import * +from micropython.errors import * import compiler.ast import sys diff -r 1805fbc407ad -r 499e42c00d57 micropython/report.py --- a/micropython/report.py Wed Jun 27 01:02:32 2012 +0200 +++ b/micropython/report.py Wed Jun 27 01:32:38 2012 +0200 @@ -21,6 +21,7 @@ from micropython.common import * from micropython.data import * +from micropython.errors import * from os.path import exists, extsep, join import sys import os diff -r 1805fbc407ad -r 499e42c00d57 micropython/table.py --- a/micropython/table.py Wed Jun 27 01:02:32 2012 +0200 +++ b/micropython/table.py Wed Jun 27 01:32:38 2012 +0200 @@ -19,8 +19,8 @@ this program. If not, see . """ -from micropython.common import * from micropython.data import * +from micropython.errors import * class List: diff -r 1805fbc407ad -r 499e42c00d57 micropython/trans.py --- a/micropython/trans.py Wed Jun 27 01:02:32 2012 +0200 +++ b/micropython/trans.py Wed Jun 27 01:32:38 2012 +0200 @@ -19,8 +19,9 @@ this program. If not, see . """ -from micropython.common import * +from micropython.common import operator_functions from micropython.data import * +from micropython.errors import * from micropython.rsvp import * import compiler.ast diff -r 1805fbc407ad -r 499e42c00d57 micropython/types.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/micropython/types.py Wed Jun 27 01:32:38 2012 +0200 @@ -0,0 +1,78 @@ +#!/usr/bin/env python + +""" +Type deduction operations. + +Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 Paul Boddie + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . +""" + +try: + set +except NameError: + from sets import Set as set + +def get_object_types_for_usage(usage, objtable, name, unit_name, all_attributes): + + """ + Return for the given attribute 'usage', using the 'objtable', the object + types which satisfy such usage, reporting any problems for the given 'name' + and 'unit_name'. Each object type is given as a tuple of the form (type, + is_static). + + Where 'all_attributes' is set to a true value, all attributes in a usage + list must be matched to provide object types. Otherwise, the presence of any + attribute from a usage list in a type's set of attributes will be enough to + provide that type as a suitable object type. + """ + + all_objtypes = set() + + for attrnames in usage: + attrnames = attrnames or () + + # Determine whether all attributes are required. + + if all_attributes: + objtypes = objtable.all_possible_objects_plus_status(attrnames) + if not objtypes: + print >>sys.stderr, "Warning: usage in %r for %r finds no object supporting all attributes %r" % ( + unit_name, name, attrnames.keys()) + else: + objtypes = objtable.any_possible_objects_plus_status(attrnames) + if not objtypes: + print >>sys.stderr, "Warning: usage in %r for %r finds no object supporting any attributes %r" % ( + unit_name, name, attrnames.keys()) + + all_objtypes.update(objtypes) + + return all_objtypes + +def filter_using_self(objtypes, cls): + + """ + Filter the given 'objtypes' (a collection of object names and static + indicators) for the given 'cls', returning only the (name, static) elements + compatible with 'cls' and its descendants. + """ + + descendants = cls.all_descendants() + filtered = [] + for objname, is_static in objtypes: + if objname == cls.full_name() or objname in descendants: + filtered.append((objname, is_static)) + return filtered + +# vim: tabstop=4 expandtab shiftwidth=4