-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path__main__.py
125 lines (94 loc) · 2.1 KB
/
__main__.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python3.4
# coding: utf-8
"""
Entry for the Acid parser/lexer/compiler/interpreter.
Contributors: myrma
"""
import os
import argparse
from acid.parser import Parser, tokenize
from acid.compiler import Compiler
from acid.exception import ParseError
from acid.repl import REPL
class Call(argparse.Action):
def __init__(self, func, *args, **kwds):
super().__init__(*args, **kwds)
self.func = func
def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, self.func(values))
def execute(path):
if path.endswith('.acidc'):
Compiler.execute_compiled_file(path)
else:
compiler = Compiler.from_file(path)
compiler.execute()
def lex(path):
with open(path) as file:
code = file.read()
try:
token_queue = tokenize(code)
except ParseError as err:
print(err)
else:
for token in tokenize(code):
print(token)
def parse(path):
parser = Parser.from_file(path)
try:
tree = parser.run()
except ParseError as err:
print(err)
else:
print(tree)
def compile(path):
compiler = Compiler.from_file(path)
compiler.dump()
def interactive(path=None):
repl = REPL()
if path is not None:
repl.load(path)
repl.run()
arg_parser = argparse.ArgumentParser(
prog='acid',
description="Tokenize, parse, compile or execute the given input file"
)
action = arg_parser.add_mutually_exclusive_group()
action.add_argument(
'--exec', '-e',
dest='path',
metavar='PATH',
action=Call,
func=execute,
help='executes the given file')
action.add_argument(
'--lex', '-l',
dest='path',
metavar='PATH',
action=Call,
func=lex,
help='tokenize the given file')
action.add_argument(
'--parse', '--ast', '-p',
dest='path',
metavar='PATH',
action=Call,
func=parse,
help='parse the given file')
action.add_argument(
'--compile', '-c',
dest='path',
metavar='PATH',
action=Call,
func=compile,
help='compile the given file')
action.add_argument(
'--repl', '-i',
dest='path',
metavar='PATH',
nargs='?',
action=Call,
func=interactive,
default=None,
help='starts an interactive interpreter')
if __name__ == '__main__':
arg_parser.parse_args()