92 lines
2.8 KiB
Python
92 lines
2.8 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
# IMPORTS
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
from __future__ import annotations;
|
|
import os;
|
|
import sys;
|
|
# sys.tracebacklimit = 0;
|
|
from dotenv import dotenv_values;
|
|
from lark import Tree;
|
|
from textwrap import dedent;
|
|
from typing import List;
|
|
|
|
sys.path.insert(0, os.getcwd());
|
|
|
|
from aussagenlogik.schema import string_to_parts;
|
|
from aussagenlogik.rekursion import rekursiv_eval;
|
|
from aussagenlogik.rekursion import rekursiv_atoms;
|
|
from aussagenlogik.rekursion import rekursiv_depth;
|
|
from aussagenlogik.rekursion import rekursiv_length;
|
|
from aussagenlogik.rekursion import rekursiv_parentheses;
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
# GLOBALE KONSTANTEN
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
DATA_ENV = "data.env"; # Pfad zu Daten.
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
# HAUPTVORGANG
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
def main():
|
|
## Daten einlesen:
|
|
expr, I = getData();
|
|
## Formel in Teilformeln zerlegen:
|
|
tree = string_to_parts(expr);
|
|
## Methoden ausführen:
|
|
results = dict(
|
|
eval = rekursiv_eval(tree, I),
|
|
atoms = rekursiv_atoms(tree),
|
|
d = rekursiv_depth(tree),
|
|
l = rekursiv_length(tree),
|
|
p = rekursiv_parentheses(tree),
|
|
);
|
|
## Resultate anzeigen:
|
|
display_results(expr, tree, I, results);
|
|
return;
|
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
# SONSTIGE METHODEN
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
def getData():
|
|
data = dotenv_values(dotenv_path=DATA_ENV);
|
|
expr = data['expr'] or '0';
|
|
I = eval(data['interpretation'] or '[]');
|
|
return expr, I;
|
|
|
|
def display_results(expr: str, tree: Tree, I: List[str], results: dict):
|
|
print(dedent(
|
|
'''
|
|
Syntaxbaum von
|
|
F := \033[92;1m{F}\033[0m:
|
|
'''.format(F=expr)
|
|
));
|
|
print(tree.pretty());
|
|
print(dedent(
|
|
'''
|
|
eval(F, I) = \033[94;1m{eval}\033[0m;
|
|
\033[2matoms(F) = \033[94;1m{atoms}\033[0m; \033[91;1m<- *\033[0m
|
|
\033[2mdepth(F) = \033[94;1m{d}\033[0m; \033[91;1m<- *\033[0m
|
|
\033[2mlength(F) = \033[94;1m{l}\033[0m; \033[91;1m<- *\033[0m
|
|
\033[2m#parentheses(F) = \033[94;1m{p}\033[0m; \033[91;1m<- *\033[0m
|
|
|
|
\033[91;1m*\033[0m \033[2mnoch nicht implementiert!\033[0m
|
|
\033[1;2;4mChallenge:\033[0m \033[2mschreibe diese Methoden! (siehe README.md)\033[0m
|
|
'''.format(**results)
|
|
));
|
|
return;
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
# CODE AUSFÜHREN
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
if __name__ == '__main__':
|
|
main();
|