Skip to content

Commit

Permalink
Dump grammar debug info for some tests that are not so slow
Browse files Browse the repository at this point in the history
  • Loading branch information
brunokim committed Nov 21, 2021
1 parent 1b2df95 commit 116fb7a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 17 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ name of a file where it [writes the machine's internal state](https://twitter.co
as a JSON for each iteration.
You may visualize the execution by running an HTTP server within the debug/ folder, e.g.
with `python -m http.server`, and loading the JSONL file.
As a sample, `debugtest/interpreter.jsonl` contains the execution for `interpreter.py`.

![Screenshot of debug view, showcasing instructions, register values and controls](docs/debug-view.png)

As a sample, `debugtest/interpreter.jsonl` contains the execution for `interpreter.py`.
If you wish to dump execution for some grammar tests, run `pytest --debug_grammar`.
Warning: debug dumping is slooow.

## Documentation

1. [About Prolog](docs/about-prolog.md): a hurried primer if you don't know what it is about.
Expand Down
5 changes: 4 additions & 1 deletion README.pt-BR.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ de um arquivo onde ele [escreve o estado interno da máquina](https://twitter.co
como um JSON para cada iteração.
Você pode visualizar a execução da máquina rodando um servidor HTTP dentro da pasta debug/,
por exemplo, com `python -m http.server`, e carregando o arquivo JSONL.
Como uma amostra, `debugtest/interpreter.jsonl` contém a execução de `interpreter.py`.

![Screenshot do visualizador de debug, mostrando as instruções, valores de registros e controles](docs/debug-view.png)

Como uma amostra, `debugtest/interpreter.jsonl` contém a execução de `interpreter.py`.
Se você desejar escrever a execução de alguns testes de `grammar`, rode `pytest --debug_grammar`.
Aviso: a escrita de debug é leeenta.

## Documentação

1. [Sobre Prolog](docs.pt-BR/about-prolog.md): uma introdução apressada se você não sabe do que isso se trata.
Expand Down
10 changes: 10 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import pytest


def pytest_addoption(parser):
parser.addoption("--debug_grammar", action='store_true')


@pytest.fixture(scope='session')
def debug_grammar(request):
return request.config.option.debug_grammar
6 changes: 3 additions & 3 deletions interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,10 @@ def stack(env: Optional["Env"]) -> List["Env"]:
env = env.prev
return reversed(envs)

def to_json(self, envs):
def to_json(self, codes, envs):
return {
'PrevPos': index(self.prev, envs),
'Continuation': to_json(self.continuation),
'Continuation': to_json(self.continuation, codes),
'PermanentVars': to_json(self.slots),
}

Expand Down Expand Up @@ -754,7 +754,7 @@ def to_json(self, include_clauses=False):
'ComplexArg': to_json(self.state.struct_arg),
'UnifFrames': [],
'EnvPos': index(self.state.env, envs),
'Envs': to_json(envs, envs),
'Envs': to_json(envs, self.debug_codes, envs),
'ChoicePos': index(self.choice, choices),
'ChoicePoints': to_json(choices, self.debug_codes, choices, envs),
'LastRefId': self.state.top_ref_id,
Expand Down
27 changes: 15 additions & 12 deletions test_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,21 @@ def test_parse_query(text, query):
assert parse_query(text) == query


@pytest.mark.parametrize('text, clauses', [
("f().", [Clause(Struct("f"))]),
("f(). g().", [Clause(Struct("f")), Clause(Struct("g"))]),
("f(a).", [Clause(Struct("f", Atom("a")))]),
("f(a, b).", [Clause(Struct("f", Atom("a"), Atom("b")))]),
("f() :- a.", [Clause(Struct("f"), Struct("a"))]),
("f(X) :- g(X).", [Clause(Struct("f", Var("X")), Struct("g", Var("X")))]),
("""f(X) :- g(X).
@pytest.mark.parametrize('desc, text, clauses', [
('nilary-fact', "f().", [Clause(Struct("f"))]),
('nilary-facts', "f(). g().", [Clause(Struct("f")), Clause(Struct("g"))]),
('unary-fact', "f(a).", [Clause(Struct("f", Atom("a")))]),
('binary-fact', "f(a, b).", [Clause(Struct("f", Atom("a"), Atom("b")))]),
(None, "f() :- a.", [Clause(Struct("f"), Struct("a"))]),
(None, "f(X) :- g(X).", [Clause(Struct("f", Var("X")), Struct("g", Var("X")))]),
(None, """f(X) :- g(X).
g(a).
""", [
Clause(Struct("f", Var("X")), Struct("g", Var("X"))),
Clause(Struct("g", Atom("a"))),
]),
("p() :- a, X, q().", [Clause(Struct("p"), Struct("a"), Struct("call", Var("X")), Struct("q"))]),
("""
(None, "p() :- a, X, q().", [Clause(Struct("p"), Struct("a"), Struct("call", Var("X")), Struct("q"))]),
(None, """
parse_term(Chars, Term) :- parse_term(Term, Chars, []).
parse_term(Chars, T0, T3) :-
ws(T0, T1),
Expand All @@ -75,5 +75,8 @@ def test_parse_query(text, query):
Struct("ws", Var("T2"), Var("T3"))),
]),
])
def test_parse_kb(text, clauses):
assert parse_kb(text) == clauses
def test_parse_kb(desc, text, clauses, debug_grammar):
debug_filename = None
if debug_grammar and desc is not None:
debug_filename = f'debugtest/{desc}.jsonl'
assert parse_kb(text, debug_filename=debug_filename) == clauses

0 comments on commit 116fb7a

Please sign in to comment.