~kf5jwc/imp-parser

ref: 4a8b0d0c4a9b3e235854c13031f981f55fc7ece5 imp-parser/tests/test_imp_interpreter.py -rw-r--r-- 590 bytes View raw
4a8b0d0c — Kyle Jones More typing! 1 year, 7 months ago
                                                                                
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
import argparse
import sys

from imp_parser import imp_lex, imp_parse


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("filename", type=argparse.FileType("r"), default=sys.stdin)
    args = parser.parse_args()

    text = args.filename.read()
    tokens = imp_lex(text)
    result = imp_parse(tokens)
    if not result:
        print("Unknown parse error!", file=sys.stderr)
        sys.exit(1)

    ast = result.value
    env = {}
    ast.eval(env)

    print("Final variable values:")
    for name in env:
        print(f" {name}: {env[name]}")