|
@@ -38,9 +38,9 @@ class Instruction(object):
|
|
return self
|
|
return self
|
|
|
|
|
|
def __str__(self):
|
|
def __str__(self):
|
|
- cg = PythonGenerator()
|
|
|
|
- self.generate_python_def(cg)
|
|
|
|
- return cg.code
|
|
|
|
|
|
+ code_generator = PythonGenerator()
|
|
|
|
+ self.generate_python_def(code_generator)
|
|
|
|
+ return code_generator.code
|
|
|
|
|
|
class PythonGenerator(object):
|
|
class PythonGenerator(object):
|
|
"""Generates Python code from instructions."""
|
|
"""Generates Python code from instructions."""
|
|
@@ -61,7 +61,7 @@ class PythonGenerator(object):
|
|
|
|
|
|
self.append(self.indentation_string * self.indentation)
|
|
self.append(self.indentation_string * self.indentation)
|
|
|
|
|
|
- def append_line(self, line = None):
|
|
|
|
|
|
+ def append_line(self, line=None):
|
|
"""Appends the indentation string followed by the given string (if any)
|
|
"""Appends the indentation string followed by the given string (if any)
|
|
and a newline to the code generator."""
|
|
and a newline to the code generator."""
|
|
|
|
|
|
@@ -159,7 +159,7 @@ class ReturnInstruction(VoidInstruction):
|
|
"""Represents a return-instruction."""
|
|
"""Represents a return-instruction."""
|
|
|
|
|
|
def __init__(self, value):
|
|
def __init__(self, value):
|
|
- Instruction.__init__(self)
|
|
|
|
|
|
+ VoidInstruction.__init__(self)
|
|
self.value = value
|
|
self.value = value
|
|
|
|
|
|
def simplify(self):
|
|
def simplify(self):
|
|
@@ -179,7 +179,7 @@ class LoopInstruction(VoidInstruction):
|
|
"""Represents a loop-instruction, which loops until broken."""
|
|
"""Represents a loop-instruction, which loops until broken."""
|
|
|
|
|
|
def __init__(self, body):
|
|
def __init__(self, body):
|
|
- Instruction.__init__(self)
|
|
|
|
|
|
+ VoidInstruction.__init__(self)
|
|
self.body = body
|
|
self.body = body
|
|
|
|
|
|
def simplify(self):
|
|
def simplify(self):
|
|
@@ -258,7 +258,7 @@ class LiteralInstruction(Instruction):
|
|
return repr(self.literal)
|
|
return repr(self.literal)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if __name__ == "__main__":
|
|
- exampleTree = SelectInstruction(
|
|
|
|
|
|
+ example_tree = SelectInstruction(
|
|
LiteralInstruction(True),
|
|
LiteralInstruction(True),
|
|
LoopInstruction(
|
|
LoopInstruction(
|
|
CompoundInstruction(
|
|
CompoundInstruction(
|
|
@@ -271,4 +271,4 @@ if __name__ == "__main__":
|
|
),
|
|
),
|
|
ReturnInstruction(
|
|
ReturnInstruction(
|
|
EmptyInstruction()))
|
|
EmptyInstruction()))
|
|
- print(exampleTree.simplify())
|
|
|
|
|
|
+ print(example_tree.simplify())
|