Permalink
Browse files

Works with more examples

Supports all the 001.* calibrations
  • Loading branch information...
1 parent f08726a commit b1eeb5b91d90e307d06d404ce7cd717188aa82ea Eric Garside committed May 18, 2012
Showing with 47 additions and 16 deletions.
  1. +4 −1 converter.py
  2. +3 −0 definitions.py
  3. +4 −0 inferences.py
  4. +36 −4 interpreter.py
  5. +0 −11 zoidberg.py
View
@@ -1,6 +1,6 @@
#!/usr/bin/env python
"""Zoidberg conversions"""
-from definitions import Operation
+from definitions import Operation, Relation
# Utility
def members(cls):
@@ -21,3 +21,6 @@ def to_number(s):
def to_operation(s):
return convert(s, Operation)
+
+def to_relation(s):
+ return convert(s, Relation)
View
@@ -28,3 +28,6 @@ def do(op, t1, t2):
class Relation:
EQUIVALENCE="="
+
+ class Defs:
+ EQUIVALENCE=["="]
View
@@ -25,3 +25,7 @@ def is_number(s):
def is_operation(s):
convert.to_operation(s)
+@test_conversion
+def is_relation(s):
+ convert.to_relation(s)
+
View
@@ -21,6 +21,7 @@ def __init__(self, problem):
# Current state
self.last_value = None
self.last_operation = None
+ self.last_relation = None
# Output
self.expression = None
@@ -49,15 +50,19 @@ def _analyze(self):
self._think("No statement created.")
if self.expression:
self._create_statement("of assumed equality.")
- self._add("expression")
- self.statement.relation = Relation.EQUIVALENCE
+ self.last_relation = Relation.EQUIVALENCE
+ self._handle_relation()
is_equivalence = self.statement.relation == Relation.EQUIVALENCE
if is_equivalence and len(self.statement.expressions) == 1:
+ if self.expression:
+ self._think("Adding current expression to statement.")
+ self._add("expression")
+ else:
self._create_expression("for hanging equality.")
self._set_variable()
self._add("expression")
- self._add("statement")
+ self._add("statement")
# Output
def _create_statement(self, reasoning):
@@ -74,9 +79,20 @@ def _create_variable(self):
def _create_expression(self, reasoning):
self._think("New expression {0}", reasoning)
self.expression = Expression()
+ self._handle_relation()
+
+ # Handlers
+ def _handle_relation(self):
+ if self.last_relation:
+ if not self.statement:
+ self._create_statement("for relationship.")
+ self._add("expression")
+ self._add("last_relation")
# Expression building
def _add_term(self, term):
+ if self.build_expression and not self.expression:
+ self._create_expression("for new term")
self.expression.terms.append(term)
def _add(self, v):
@@ -90,6 +106,10 @@ def _add(self, v):
self.statements.append(val)
return
+ if v == "last_relation":
+ self.statement.relation = val
+ return
+
if v == "last_value":
t = Term(Term.VALUE, val)
if v == "last_operation":
@@ -100,7 +120,7 @@ def _add(self, v):
# States
def _set_variable(self, name=None):
if name:
- self._think("'{0}' is a value (a variable).", v)
+ self._think("'{0}' is a value (a variable).", name)
v = Symbol(name)
else:
v = self._create_variable()
@@ -115,6 +135,14 @@ def _set_constant(self, v):
if self.build_expression:
self._add("last_value")
+ def _set_relation(self, v):
+ v = convert.to_relation(v)
+ self.last_relation = v
+ self._think("'{0}' is a relationship.", v)
+
+ if self.expression:
+ self._handle_relation()
+
def _set_operation(self, v):
v = convert.to_operation(v)
self.last_operation = v
@@ -136,8 +164,12 @@ def _set_operation(self, v):
def _interpret(self, word, tag):
if word.isalpha():
self._think("{0} is alpha", word)
+ if self.build_expression:
+ self._set_variable(word)
else:
if infer.is_number(word):
self._set_constant(word)
+ if infer.is_relation(word):
+ self._set_relation(word)
if infer.is_operation(word):
self._set_operation(word)
View
@@ -22,17 +22,6 @@ class Operation:
"-"
]
- @staticmethod
- def do(op, t1, t2):
- if op == "+":
- result = t1 + t2
- if op == "-":
- result = t1 - t2
- if op == "*":
- result = t1 * t2
- if op == "/":
- result = t1 / t2
- return Term(Term.VARIABLE, result)
class AnswerType:
# Answer is likely a number

0 comments on commit b1eeb5b

Please sign in to comment.