-
Notifications
You must be signed in to change notification settings - Fork 0
/
lisp
executable file
·116 lines (93 loc) · 3.17 KB
/
lisp
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
#!/usr/bin/python3
import sys
import os
import argparse
import configparser
import environment
import compiler
from lisp import LispError, LispInt
DEFAULT_CONFIG_PATH = "~/.lisp.ini"
DEFAULT_RUNTIME_PATH = "runtime"
# parse arguments
#
parser = argparse.ArgumentParser(description="lisp x86_64")
parser.add_argument("files", metavar="file", nargs='*',
help="lisp files to load")
parser.add_argument("-i", dest="interactive", action="store_true",
help="interactive mode")
parser.add_argument("-s", dest="symbol", default="main",
help="start symbol, default is main")
parser.add_argument("-c", dest="compile", action="store_true",
help="compile files to executable")
parser.add_argument("-o", dest="output", default="a.out",
help="compilation output file")
parser.add_argument("-a", dest="leave_asm", action="store_true",
help="don't delete assembly file after compiling")
parser.add_argument("-p", dest="print", action="store_true",
help="give assembly listing to stdout")
parser.add_argument("-r", dest="runtime", default=None,
help="alternative path to runtime")
args = parser.parse_args()
# parse config file
#
conf = configparser.ConfigParser()
conf.read(os.path.expanduser(DEFAULT_CONFIG_PATH))
# create an environment and load files into it
#
env = environment.Environment()
for fn in args.files:
try:
env.import_file(fn)
except LispError as e:
print('%s: Error: %s' % (fn, e))
exit(1)
except RecursionError:
print('%s: Error: maximum recursion depth reached' % (fn))
exit(1)
except OSError as e:
print("%s: can't open file: %s" % (fn, e.strerror))
exit(1)
if args.compile or args.print:
try:
comp = compiler.Compiler(env)
comp.set_target_symbol(args.symbol)
comp.set_leave_asm(args.leave_asm)
if args.runtime:
comp.set_runtime(args.runtime)
else:
comp.set_runtime(conf.get('compiler',
'runtime',
fallback=DEFAULT_RUNTIME_PATH))
if args.print:
sys.stdout.write(comp.get_assembly())
else:
comp.build(args.output)
except LispError as e:
print('Error: %s' % (e))
exit(1)
elif args.interactive or len(args.files) == 0:
for line in sys.stdin:
try:
line = line.strip()
if not line:
continue
result = env.interpret_single_line(line)
print(" = %s\n" % (result))
except LispError as e:
print('Error: %s\n' % (e))
except RecursionError:
print('Error: maximum recursion depth reached\n')
else:
try:
result = env.interpret_single_line('(%s)' % (args.symbol))
if type(result) == LispInt:
exit(int(result))
except LispError as e:
print('Error: %s' % (e))
exit(1)
except RecursionError:
print('Error: maximum recursion depth reached')
exit(1)
except KeyboardInterrupt:
print()
exit(1)