예제 #1
0
def assert_true(query, e=None):
    if e is None:
        e = Engine()
    terms, vars = e.parse(query)
    term, = terms
    e.run(term)
    return dict([(name, var.dereference(e.heap))
                     for name, var in vars.iteritems()])
예제 #2
0
파일: tool.py 프로젝트: griels/pypy-sc
def assert_true(query, e=None):
    if e is None:
        e = Engine()
    terms, vars = e.parse(query)
    term, = terms
    e.run(term)
    return dict([(name, var.dereference(e.heap))
                 for name, var in vars.iteritems()])
예제 #3
0
def test_atom_formatting():
    f = formatting.TermFormatter(Engine(), quoted=False, ignore_ops=False)
    t = parse_query_term("'abc def'.")
    assert f.format(t) == "abc def"
    f = formatting.TermFormatter(Engine(), quoted=True, ignore_ops=False)
    t = parse_query_term("'abc def'.")
    assert f.format(t) == "'abc def'"
    t = parse_query_term("abc.")
    assert f.format(t) == "abc"
예제 #4
0
def test_numeral():
    from pypy.lang.prolog.interpreter.term import Term, Atom, Var
    from pypy.lang.prolog.interpreter.engine import Engine
    t = parse_file("""
numeral(null). % end of line comment
numeral(succ(X)) :- numeral(X). % another one

add_numeral(X, null, X).
add_numeral(X, succ(Y), Z) :- add_numeral(succ(X), Y, Z).

greater_than(succ(null), null).
greater_than(succ(X), null) :- greater_than(X, null).
greater_than(succ(X), succ(Y)) :- greater_than(X, Y).
""")
    builder = TermBuilder()
    facts = builder.build(t)
    e = Engine()
    for fact in facts:
        print fact
        e.add_rule(fact)
    assert e.signature2function["add_numeral/3"].rulechain.rule.head.args[1].name == "null"
    four = Term("succ", [Term("succ", [Term("succ",
                [Term("succ", [Atom("null")])])])])
    e.run(parse_query_term("numeral(succ(succ(null)))."))
    term = parse_query_term(
        """add_numeral(succ(succ(null)), succ(succ(null)), X).""")
    e.run(term)
    var = Var(0).getvalue(e.heap)
    print var, e.heap
    # does not raise
    var.unify(four, e.heap)
    term = parse_query_term(
        """greater_than(succ(succ(succ(null))), succ(succ(null))).""")
    e.run(term)
예제 #5
0
def test_list():
    f = formatting.TermFormatter(Engine(), quoted=False, ignore_ops=False)
    t = parse_query_term("[1, 2, 3, 4, 5 | X].")
    assert f.format(t) == "[1, 2, 3, 4, 5|_G0]"
    t = parse_query_term("[a, b, 'A$%%$$'|[]].")
    assert f.format(t) == "[a, b, A$%%$$]"
    t = parse_query_term("'.'(a, b, c).")
    assert f.format(t) == ".(a, b, c)"
예제 #6
0
def test_op_formatting():
    f = formatting.TermFormatter(Engine(), quoted=False, ignore_ops=False)
    t = parse_query_term("'+'(1, 2).")
    assert f.format(t) == "1+2"
    t = parse_query_term("'+'(1, *(3, 2)).")
    assert f.format(t) == "1+3*2"
    t = parse_query_term("'*'(1, *(3, 2)).")
    assert f.format(t) == "1*(3*2)"
예제 #7
0
def test_atom_concat():
    assert_true("atom_concat(ab, cdef, abcdef).")
    assert_true("atom_concat(ab, cdef, X), X = abcdef.")
    assert_true("atom_concat(ab, X, abcdef), X = cdef.")
    assert_true("atom_concat(X, cdef, abcdef), X = ab.")
    assert_true("atom_concat(1, Y, '1def'), Y = def.")
    heaps = collect_all(Engine(), "atom_concat(X, Y, abcd), atom(X), atom(Y).")
    assert len(heaps) == 5
예제 #8
0
def test_between():
    assert_true("between(12, 15, 12).")
    assert_true("between(-5, 15, 0).")
    assert_false("between(12, 15, 6).")
    assert_false("between(12, 15, 16).")
    heaps = collect_all(Engine(), "between(1, 4, X).")
    assert len(heaps) == 4
    assert heaps[0]['X'].num == 1
예제 #9
0
def test_parser_access():
    assert_true("current_op(200, xfx, **).")
    f = collect_all(Engine(), "current_op(200, Form, X).")
    assert len(f) == 2
    e = get_engine("""
        foo(a, b).
    """)
    assert_true("op(450, xfy, foo).", e)
    assert_true("a foo b.", e)
    assert_true("op(0, xfy, foo).", e)
    # XXX really a ParseError
    py.test.raises(Exception, assert_false, "a foo b.", e)
    # change precedence of + for funny results :-)
    assert_true("14 is 2 + 3 * 4.", e)
    assert_true("op(350, xfy, +).", e)
    assert_true("20 is 2 + 3 * 4.", e)
    assert_true("op(500, xfy, +).", e)
예제 #10
0
def main():
    import readline
    oldps1 = getattr(sys, "ps1", ">>> ")
    oldps2 = getattr(sys, "ps2", "... ")
    try:
        sys.ps1 = ">?- "
        sys.ps2 = "... "
        if not len(sys.argv) == 2:
            e = Engine()
        else:
            try:
                source = py.path.local(sys.argv[1]).read()
                e = get_engine(source)
            except ParseError, exc:
                print exc.nice_error_message("<stdin>", source) + "\n"
                sys.exit(1)
            except LexerError, exc:
                print exc.nice_error_message("<stdin>") + "\n"
                sys.exit(1)
예제 #11
0
파일: tool.py 프로젝트: griels/pypy-sc
def assert_false(query, e=None):
    if e is None:
        e = Engine()
    term = e.parse(query)[0][0]
    py.test.raises(UnificationFailed, e.run, term)
예제 #12
0
"""
A simple standalone target for the prolog interpreter.
"""

import sys
from pypy.lang.prolog.interpreter.translatedmain import repl, execute

# __________  Entry point  __________

from pypy.lang.prolog.interpreter.engine import Engine
from pypy.lang.prolog.interpreter import engine, term
e = Engine()
engine.DEBUG = False
term.DEBUG = False


def entry_point(argv):
    #from pypy.jit.codegen.hlinfo import highleveljitinfo
    #if highleveljitinfo.sys_executable is None:
    #    highleveljitinfo.sys_executable = argv[0]
    if len(argv) == 2:
        execute(e, argv[1])
    try:
        repl(e)
    except SystemExit:
        return 1
    return 0


# _____ Define and setup target ___
예제 #13
0
def test_run():
    e = Engine()
    e.add_rule(Term("f", [Atom.newatom("a"), Atom.newatom("b")]))
    X = Var()
    Y = Var()
    e.add_rule(Term("f", [X, X]))
    e.add_rule(Term(":-", [Term("f", [X, Y]),
                           Term("f", [Y, X])]))
    X = e.heap.newvar()
    e.run(Term("f", [Atom.newatom("b"), X]))
    assert X.dereference(e.heap).name == "b"
    e.run(Term("f", [Atom.newatom("b"), Atom.newatom("a")]))
예제 #14
0
def assert_false(query, e=None):
    if e is None:
        e = Engine()
    term = e.parse(query)[0][0]
    py.test.raises(UnificationFailed, e.run, term)
예제 #15
0
def test_repeat():
    assert_true("repeat, true.")
    py.test.raises(UnificationFailed,
                   Engine().run, parse_query_term("repeat, !, fail."))