Skip to content

Commit

Permalink
Added __eq__ to various classes
Browse files Browse the repository at this point in the history
  • Loading branch information
vanschelven committed Nov 2, 2017
1 parent eabcd11 commit d0e6851
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
4 changes: 4 additions & 0 deletions doctests/form_analysis_construct.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ to help me wrap my head around those in the context of a dynamically typed langu
>>>
>>> form.parameters.the_list[0].symbol
'a'

Testing various __eq__ instances indirectly (arguably, this test should be moved to an independent location)
>>> form_note == form_note
True
49 changes: 48 additions & 1 deletion dsn/form_analysis/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ class MalformedForm(Form):

# For now, the only actual malformed s-expression I can think of is: the empty list. One could also say: that's
# simply a case of an application without either a procedure nor any arguments. I.e. a non-3-lambda.
pass

def as_s_expr(self):
# Note: the introduction of the `as_s_expr` method raises the following question: should the MalformedForm know
Expand All @@ -97,6 +96,9 @@ def as_s_expr(self):
# isn't done.
return TreeText("MALFORMED", None)

def __eq__(self, other):
return isinstance(other, MalformedForm)


class VariableForm(Form):

Expand All @@ -107,6 +109,9 @@ def __init__(self, symbol):
def as_s_expr(self):
return TreeText(self.symbol.symbol, metadata=None)

def __eq__(self, other):
return isinstance(other, VariableForm) and self.symbol == other.symbol


class ValueForm(Form):
def __init__(self, type_, value):
Expand All @@ -116,6 +121,9 @@ def __init__(self, type_, value):
def as_s_expr(self):
return TreeText(repr(self.value), metadata=None)

def __eq__(self, other):
return isinstance(other, ValueForm) and self.type_ == other.type_ and self.value == other.value


class QuoteForm(Form):
def __init__(self, data):
Expand All @@ -124,6 +132,9 @@ def __init__(self, data):
def as_s_expr(self):
return TreeNode([TreeText("quote", None), self.data])

def __eq__(self, other):
return isinstance(other, QuoteForm) and self.data == other.data


class IfForm(Form):

Expand All @@ -139,6 +150,12 @@ def as_s_expr(self):
self.consequent.as_s_expr(),
self.alternative.as_s_expr(), ])

def __eq__(self, other):
return isinstance(other, IfForm) and (
self.predicate == other.predicate and
self.consequent == other.consequent and
self.alternative == other.alternative)


class DefineForm(Form):

Expand All @@ -153,6 +170,11 @@ def as_s_expr(self):
self.definition.as_s_expr(),
])

def __eq__(self, other):
return isinstance(other, DefineForm) and (
self.symbol == other.symbol and
self.definition == other.definition)


class LambdaForm(Form):
def __init__(self, parameters, body):
Expand All @@ -165,6 +187,11 @@ def as_s_expr(self):
TreeNode([TreeText(s.symbol, None) for s in self.parameters]),
] + [f.as_s_expr() for f in self.body])

def __eq__(self, other):
return isinstance(other, LambdaForm) and (
self.parameters == other.parameters and
self.body == other.body)


class ApplicationForm(Form):
def __init__(self, procedure, arguments):
Expand All @@ -176,6 +203,11 @@ def as_s_expr(self):
self.procedure.as_s_expr(),
] + [a.as_s_expr() for a in self.arguments])

def __eq__(self, other):
return isinstance(other, ApplicationForm) and (
self.procedure == other.procedure and
self.arguments == other.arguments)


class SequenceForm(Form):
def __init__(self, sequence):
Expand All @@ -186,6 +218,9 @@ def as_s_expr(self):
TreeText("begin", None),
] + [e.as_s_expr() for e in self.sequence])

def __eq__(self, other):
return isinstance(other, SequenceForm) and self.sequence == other.sequence


# Below this line: _not_ Form, but used as a part of a Form. May still have its own independent history.
class FormList(object):
Expand All @@ -197,6 +232,10 @@ def __init__(self, the_list, metadata=None):
def __iter__(self):
return self.the_list.__iter__()

def __eq__(self, other):
return isinstance(other, FormList) and len(self.the_list) == len(other.the_list) and all(
a == b for (a, b) in zip(self.the_list, other.the_list))


class Symbol(object):

Expand All @@ -208,6 +247,9 @@ def __repr__(self):
return "<Symbol of malformed type>"
return "<Symbol %s>" % self.symbol

def __eq__(self, other):
return isinstance(other, Symbol) and self.symbol == other.symbol


class SymbolList(object):
def __init__(self, the_list, metadata=None):
Expand All @@ -217,3 +259,8 @@ def __init__(self, the_list, metadata=None):

def __iter__(self):
return self.the_list.__iter__()

def __eq__(self, other):
# TBD: should metadata be part of equality-test?
return isinstance(other, SymbolList) and len(self.the_list) == len(other.the_list) and all(
a == b for (a, b) in zip(self.the_list, other.the_list))
8 changes: 8 additions & 0 deletions dsn/s_expr/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def __init__(self, children, t2s=None, s2t=None, metadata=None):
def __repr__(self):
return pp_flat(self)

def __eq__(self, other):
"""NOTE: __eq__ ignores any time-aspects (i.e. metadata, t2s), only eqqing on the present structure"""
return isinstance(other, TreeNode) and self.children == other.children and self.broken == other.broken

def as_bytes(self):
# as of yet unused
return bytes([TREE_NODE]) + to_vlq(len(self.children)) + b''.join([c.as_bytes() for c in self.children])
Expand All @@ -50,6 +54,10 @@ def __init__(self, unicode_, metadata):
def __repr__(self):
return pp_flat(self)

def __eq__(self, other):
"""NOTE: __eq__ ignores any time-aspects (i.e. metadata), only eqqing on the present structure"""
return isinstance(other, TreeText) and self.unicode_ == other.unicode_ and self.broken == other.broken

def as_bytes(self):
# as of yet unused
utf8 = self.unicode_.encode('utf-8')
Expand Down

0 comments on commit d0e6851

Please sign in to comment.