old_contents: '''Term writing.'''
  
  
  from aterm import types
  from aterm import visitor
  
  
  class Writer(visitor.Visitor):
  	'''Base class for term writers.'''
  
  	def __init__(self, fp):
  		visitor.Visitor.__init__(self)
  		self.fp = fp
  
  
  class TextWriter(Writer):
  	'''Writes a term to a text stream.'''
  
  	def writeAnnotations(self, term):
  		annotations = term.annotations
  		if annotations:
  			self.fp.write('{')
  			self.visit(annotations, inside_list = True)
  			self.fp.write('}')
  
  	def visitInt(self, term):
  		self.fp.write(str(term.value))
  
  	def visitReal(self, term):
  		value = term.value
  		if float(int(value)) == value:
  			self.fp.write('%0.1f' % value)
  		else:
  			self.fp.write('%g' % value)
  
  	def visitStr(self, term):
  		s = str(term.value)
  		s = s.replace('\"', '\\"')
  		s = s.replace('\t', '\\t')
  		s = s.replace('\r', '\\r')
  		s = s.replace('\n', '\\n')
  		self.fp.write('"' + s + '"')
  
  	def visitNil(self, term, inside_list = False):
  		if not inside_list:
  			self.fp.write('[]')
  
  	def visitCons(self, term, inside_list = False):
  		if not inside_list:
  			self.fp.write('[')
  		head = term.head
  		self.visit(head)
  		tail = term.tail
  		last = types.isNil(tail)
  		if not last:
  			self.fp.write(",")
  			self.visit(tail, inside_list = True)
  		if not inside_list:
  			self.fp.write(']')
  
  	def visitAppl(self, term):
  		self.fp.write(term.name)
  		args = term.args
  		if term.name == '' or term.args:
  			self.fp.write('(')
  			sep = ""
  			for arg in term.args:
  				self.fp.write(sep)
  				self.visit(arg)
  				sep = ","
  			self.fp.write(')')
  		self.writeAnnotations(term)
  
  
  # TODO: implement a pretty-printer
  
  
  class AbbrevTextWriter(TextWriter):
  	'''Write an abbreviated term representation.'''
  
  	def __init__(self, fp, depth):
  		TextWriter.__init__(self, fp)
  		self.depth = depth
  
  	def visitCons(self, term, inside_list = False):
  		if not inside_list:
  			self.fp.write('[')
  		if self.depth > 1:
  			self.depth -= 1
  			head = term.head
  			self.visit(head)
  			self.depth += 1
  			tail = term.tail
  			last = types.isNil(tail)
  			if not last:
  				self.fp.write(",")
  				self.visit(tail, inside_list = True)
  		else:
  			self.fp.write('...')
  		if not inside_list:
  			self.fp.write(']')
  
  
  # TODO: implement a XML writer
new_contents: '''Term writing.'''
  
  
  from aterm import types
  from aterm import visitor
  
  
  class Writer(visitor.Visitor):
  	'''Base class for term writers.'''
  
  	def __init__(self, fp):
  		visitor.Visitor.__init__(self)
  		self.fp = fp
  
  
  class TextWriter(Writer):
  	'''Writes a term to a text stream.'''
  
  	def writeList(self, seq, begin, end, sep=','):
  		self.fp.write(begin)
  		cursep = ""
  		for term in seq:
  			self.fp.write(cursep)
  			self.visit(term)
  			cursep = sep
  		self.fp.write(end)
  
  	def visitInt(self, term):
  		self.fp.write(str(term.value))
  
  	def visitReal(self, term):
  		value = term.value
  		if float(int(value)) == value:
  			self.fp.write('%0.1f' % value)
  		else:
  			self.fp.write('%g' % value)
  
  	def visitStr(self, term):
  		s = str(term.value)
  		s = s.replace('\"', '\\"')
  		s = s.replace('\t', '\\t')
  		s = s.replace('\r', '\\r')
  		s = s.replace('\n', '\\n')
  		self.fp.write('"' + s + '"')
  
  	def visitList(self, term):
  		self.writeList(term, '[', ']')
  
  	def visitAppl(self, term):
  		self.fp.write(term.name)
  		args = term.args
  		if term.name == '' or term.args:
  			self.writeList(term.args, '(', ')')
  		if term.annotations:
  			self.writeList(term.annotations, '{', '}')
  
  
  # TODO: implement a pretty-printer
  
  
  class AbbrevTextWriter(TextWriter):
  	'''Write an abbreviated term representation.'''
  
  	def __init__(self, fp, depth):
  		TextWriter.__init__(self, fp)
  		self.depth = depth
  
  	def visitList(self, term):
  		if self.depth > 1:
  			self.depth -= 1
  			TextWriter.visitList(self, term)
  			self.depth += 1
  		else:
  			self.fp.write('[...]')
  
  
  # TODO: implement a XML writer
subject: Simplify aterm text writting.