-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
71 lines (56 loc) · 1.86 KB
/
main.py
File metadata and controls
71 lines (56 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from __future__ import annotations
import copy
import dataclasses
import json
from pprint import pprint
from typing import Any
import tatsu
from tatsu.util import asjson
import LaTeX_renderer
import unit_type
from interpreter import RENDER, Interpreter
# "1m10N100mol2K-3459cd-56V-23 * -(3 + 5 * ( 10 - 100**(-0.5) ))"
GRAMMAR: str | None = None
run_commands = []
@dataclasses.dataclass
class Line:
model: Any
result: unit_type.UnitType
def main() -> None:
global GRAMMAR
with open("grammar.ebnf") as f:
GRAMMAR = f.read()
parser = tatsu.compile(GRAMMAR, asmodel=True)
interpreter = Interpreter()
while True:
model = parser.parse(input(">>> "))
print(json.dumps(asjson(model), indent=4))
pprint(model)
# with open("ast.dot", "w") as f:
# f.write("digraph {\n")
# f.write(AstRenderer().walk(model))
# f.write("}")
#
to_be_saved = copy.deepcopy(model)
result = interpreter.walk(model)
run_commands.append(Line(to_be_saved, result))
pprint(run_commands)
if isinstance(result, RENDER):
latex_render = LaTeX_renderer.Renderer()
with open(result.path, "w") as f:
f.write("\\begin{align}\n")
for line in run_commands:
latex = latex_render.walk(line.model)
if latex is not None:
f.write(latex)
if line.result is not None and not isinstance(
line.result, RENDER
):
f.write(" &=& ")
f.write(line.result.to_latex())
f.write("\\\\\n")
f.write(r"\end{align}")
elif result is not None:
print(result)
if __name__ == "__main__":
main()