Commits

David Schneider  committed 59ebd04

Replace calls to Term constructor with factory method calls

  • Participants
  • Parent commits f7970ea
  • Branches continuation-based

Comments (0)

Files changed (12)

File prolog/builtin/allsolution.py

         copy = collector.found[i]
         d = {}
         copy = copy.copy(heap, d)
-        result = term.Term(".", [copy, result])
+        result = term.Callable.build(".", [copy, result])
     bag.unify(result, heap)

File prolog/builtin/control.py

     scont = continuation.BodyContinuation(engine, scont, then_clause)
     if insert_cutdelimiter:
         scont = fcont = continuation.CutDelimiter(engine, scont, fcont)
-    body = term.Term(",", [if_clause, CUTATOM])
+    body = term.Callable.build(",", [if_clause, CUTATOM])
     return continuation.BodyContinuation(engine, scont, body), fcont, heap

File prolog/builtin/termconstruction.py

             else:
                 name = helper.unwrap_atom(functor)
                 t.unify(
-                    term.Term(name, [term.Var() for i in range(a)]),
+                    term.Callable.build(name, [term.Var() for i in range(a)]),
                     heap)
 
 #@expose_builtin("arg", unwrap_spec=["obj", "obj", "obj"],
             head = l[0]
             if not isinstance(head, term.Atom):
                 error.throw_type_error("atom", head)
-            term.Term(head.name(), l[1:]).unify(first, heap)
+            term.Callable.build(head.name(), l[1:]).unify(first, heap)
 
 @expose_builtin("copy_term", unwrap_spec=["obj", "obj"])
 def impl_copy_term(engine, heap, interm, outterm):

File prolog/interpreter/continuation.py

         raise error.UncaughtError(exc)
 
 #    def throw_system_error(self, term, scont, fcont, heap):
-#        term = Term("error", [term])
+#        term = Callable.build("error", [term])
 #        return self.throw(term, scont, fcont, heap)
 #
 #    def throw_existence_error(self, object_type, obj, scont, fcont, heap):
-#        term = Term("existence_error", [Atom.newatom(object_type), obj])
+#        term = Callable.build("existence_error", [Atom.newatom(object_type), obj])
 #        return self.throw_system_error(term, scont, fcont, heap)
 #
 #    def throw_instantiation_error(self, scont, fcont, heap):
 #        # atom, atomic, byte, callable, character
 #        # evaluable, in_byte, in_character, integer, list
 #        # number, predicate_indicator, variable
-#        term = Term("type_error", [Atom.newatom(valid_type), obj])
+#        term = Callable.build("type_error", [Atom.newatom(valid_type), obj])
 #        return self.throw_system_error(term, scont, fcont, heap)
 #
 #    def throw_domain_error(self, valid_domain, obj, scont, fcont, heap):
 #        # operator_specifier, prolog_flag, read_option, source_sink,
 #        # stream, stream_option, stream_or_alias, stream_position,
 #        # stream_property, write_option
-#        term = Term("domain_error", [Atom.newatom(valid_domain), obj])
+#        term = Callable.build("domain_error", [Atom.newatom(valid_domain), obj])
 #        return self.throw_system_error(term, scont, fcont, heap)
 #
 #    def throw_permission_error(self, operation, permission_type, obj, scont, fcont, heap):
 #        # valid permission_types are:
 #        # binary_stream, flag, operator, past_end_of_stream, private_procedure,
 #        # static_procedure, source_sink, stream, text_stream. 
-#        term = Term("permission_error", [term.Atom.newatom(operation),
+#        term = Callable.build("permission_error", [term.Atom.newatom(operation),
 #                                         term.Atom.newatom(permission_type),
 #                                         obj])
 #        return self.throw_system_error(term, scont, fcont, heap)

File prolog/interpreter/eclipseprologparser.py

             return result
 
     def visit_complexterm(self, node):
-        name = self.general_symbol_visit(node.children[0]).name()        children = self.build_list(node.children[2])
+        name = self.general_symbol_visit(node.children[0]).name()        
+        children = self.build_list(node.children[2])
         result = Term()
         result.setup(self.make_token(node.children[1]), children, name)
         return result

File prolog/interpreter/error.py

 
 def wrap_error(t):
     from prolog.interpreter import term
-    t = term.Term("error", [t])
+    t = term.Callable.build("error", [t])
     return CatchableError(t)
 
 class UnificationFailed(PrologError):
 
 def throw_existence_error(object_type, obj):
     from prolog.interpreter import term
-    t = term.Term("existence_error", [term.Atom.newatom(object_type), obj])
+    t = term.Callable.build("existence_error", [term.Atom.newatom(object_type), obj])
     raise wrap_error(t)
 
 def throw_instantiation_error():
     # number, predicate_indicator, variable
     from prolog.interpreter import term
     raise wrap_error(
-        term.Term("type_error", [term.Atom.newatom(valid_type), obj]))
+        term.Callable.build("type_error", [term.Atom.newatom(valid_type), obj]))
 
 def throw_domain_error(valid_domain, obj):
     from prolog.interpreter import term
     # stream, stream_option, stream_or_alias, stream_position,
     # stream_property, write_option
     raise wrap_error(
-        term.Term("domain_error", [term.Atom.newatom(valid_domain), obj]))
+        term.Callable.build("domain_error", [term.Atom.newatom(valid_domain), obj]))
 
 def throw_permission_error(operation, permission_type, obj):
     from prolog.interpreter import term
     # binary_stream, flag, operator, past_end_of_stream, private_procedure,
     # static_procedure, source_sink, stream, text_stream. 
     raise wrap_error(
-        term.Term("permission_error", [term.Atom.newatom(operation),
+        term.Callable.build("permission_error", [term.Atom.newatom(operation),
                                        term.Atom.newatom(permission_type),
                                        obj]))

File prolog/interpreter/helper.py

 def wrap_list(python_list):
     curr = emptylist
     for i in range(len(python_list) - 1, -1, -1):
-        curr = term.Term(".", [python_list[i], curr])
+        curr = term.Callable.build(".", [python_list[i], curr])
     return curr
 
 def unwrap_list(prolog_list):

File prolog/interpreter/parsing.py

         return self.dispatch(node)
 
     def general_nonterminal_visit(self, node):
-        from prolog.interpreter.term import Term, Number, Float
+        from prolog.interpreter.term import Callable, Number, Float
         children = []
         name = ""
         for child in node.children:
                 return Number(factor * child.num)
             if isinstance(child, Float):
                 return Float(factor * child.floatval)
-        return Term(name, children)
+        return Callable.build(name, children)
 
     def build_list(self, node):
         result = []
             return Float(float(s))
 
     def visit_complexterm(self, node):
-        from prolog.interpreter.term import Term
+        from prolog.interpreter.term import Callable
         name = self.general_symbol_visit(node.children[0]).name()
         children = self.build_list(node.children[2])
-        return Term(name, children[:])
+        return Callable.build(name, children[:])
 
     def visit_expr(self, node):
         from prolog.interpreter.term import Number, Float
         return self.visit(node.children[1])
 
     def visit_listexpr(self, node):
-        from prolog.interpreter.term import Atom, Term
+        from prolog.interpreter.term import Callable
         node = node.children[1]
         if len(node.children) == 1:
             l = self.build_list(node)
-            start = Atom.newatom("[]")
+            start = Callable.build("[]")
         else:
             l = self.build_list(node.children[0])
             start = self.visit(node.children[2])
         l.reverse()
         curr = start
         for elt in l:
-            curr = Term(".", [elt, curr])
+            curr = Callable.build(".", [elt, curr])
         return curr
 
 

File prolog/interpreter/test/test_continuation.py

             all.append(query.getvalue(heap))
             raise error.UnificationFailed
     e = Engine()
-    e.add_rule(Term("f", [Atom.newatom("x")]), True)
-    e.add_rule(Term("f", [Atom.newatom("y")]), True)
-    e.add_rule(Term("g", [Atom.newatom("a")]), True)
-    e.add_rule(Term("g", [Atom.newatom("b")]), True)
+    e.add_rule(Callable.build("f", [Atom.newatom("x")]), True)
+    e.add_rule(Callable.build("f", [Atom.newatom("y")]), True)
+    e.add_rule(Callable.build("g", [Atom.newatom("a")]), True)
+    e.add_rule(Callable.build("g", [Atom.newatom("b")]), True)
             
-    query = Term(",", [Term("f", [Var()]), Term("g", [Var()])])
+    query = Callable.build(",", [Callable.build("f", [Var()]), Callable.build("g", [Var()])])
     py.test.raises(error.UnificationFailed,
                    e.run_query, query, CollectContinuation())
     assert all[0].argument_at(0).argument_at(0).name()== "x"

File prolog/interpreter/test/test_parsing.py

     assert len(facts) == 1
 
 def test_numeral():
-    from prolog.interpreter.term import Term, Atom, Var
+    from prolog.interpreter.term import Callable, Atom, Var
     from prolog.interpreter.continuation import Engine
     t = parse_file("""
 numeral(null). % end of line comment
         print fact
         e.add_rule(fact)
     assert e.signature2function["add_numeral/3"].rulechain.head.argument_at(1).name()== "null"
-    four = Term("succ", [Term("succ", [Term("succ",
-                [Term("succ", [Atom("null")])])])])
+    four = Callable.build("succ", [Callable.build("succ", [Callable.build("succ",
+                [Callable.build("succ", [Callable.build("null")])])])])
     e.run(parse_query_term("numeral(succ(succ(null)))."))
     term = parse_query_term(
         """add_numeral(succ(succ(null)), succ(succ(null)), X).""")

File prolog/interpreter/test/test_unification.py

 import py
 from prolog.interpreter.error import UnificationFailed
-from prolog.interpreter.term import Atom, Var, Number, Term, BlackBox
+from prolog.interpreter.term import Atom, Var, Number, Callable, Term, BlackBox
 from prolog.interpreter.term import NumberedVar
 from prolog.interpreter.continuation import Heap, Engine
 
 def test_recursive():
     b = Var()
     heap = Heap()
-    b.unify(Term("hallo", [b]), heap)
+    b.unify(Callable.build("hallo", [b]), heap)
 
 def test_term():
     X = Var()
     Y = Var()
-    t1 = Term("f", [Atom.newatom("hallo"), X])
-    t2 = Term("f", [Y, Atom.newatom("HALLO")])
+    t1 = Callable.build("f", [Atom.newatom("hallo"), X])
+    t2 = Callable.build("f", [Y, Atom.newatom("HALLO")])
     heap = Heap()
     print t1, t2
     t1.unify(t2, heap)
 def test_enumerate_vars():
     X = Var()
     Y = Var()
-    t1 = Term("f", [X, X, Term("g", [Y, X])])
+    t1 = Callable.build("f", [X, X, Callable.build("g", [Y, X])])
     t2 = t1.enumerate_vars({})
     assert isinstance(t2, Term)
     assert t2.signature()== t1.signature()    
     X = Var()
     Y = Var()
     Z = NumberedVar(0)
-    t1 = Term("f", [Z, Term("g", [Z, Atom.newatom("h")]), Z])
-    t2 = Term("f", [Atom.newatom("i"), X, Y])
+    t1 = Callable.build("f", [Z, Callable.build("g", [Z, Callable.build("h")]), Z])
+    t2 = Callable.build("f", [Callable.build("i"), X, Y])
     env = [None]
     t1.unify_and_standardize_apart(t2, heap, env)
 
 def test_run():
     e = Engine()
-    e.add_rule(Term("f", [Atom.newatom("a"), Atom.newatom("b")]))
+    e.add_rule(Callable.build("f", [Callable.build("a"), Callable.build("b")]))
     X = Var()
     Y = Var()
-    e.add_rule(Term("f", [X, X]))
-    e.add_rule(Term(":-", [Term("f", [X, Y]),
-                           Term("f", [Y, X])]))
+    e.add_rule(Callable.build("f", [X, X]))
+    e.add_rule(Callable.build(":-", [Callable.build("f", [X, Y]),
+                           Callable.build("f", [Y, X])]))
     X = e.heap.newvar()
-    e.run(Term("f", [Atom.newatom("b"), X]))
+    e.run(Callable.build("f", [Callable.build("b"), X]))
     assert X.dereference(e.heap).name()== "b"
-    query = Term("f", [Atom.newatom("b"), Atom.newatom("a")]) 
+    query = Callable.build("f", [Callable.build("b"), Callable.build("a")]) 
     e.run(query)

File prolog/interpreter/translatedmain.py

             run(goal, var_to_pos, engine)
  
 def execute(e, filename):
-    e.run(term.Term("consult", [term.Atom(filename)]))
+    e.run(term.Callable.build("consult", [term.Callable.build(filename)]))
 
 if __name__ == '__main__':
     e = Engine()