-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcilly.py
More file actions
184 lines (148 loc) · 5.08 KB
/
Copy pathcilly.py
File metadata and controls
184 lines (148 loc) · 5.08 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import argparse
from cilly_interpreter import *
# from cilly_vm import *
def parse_command_line():
command = argparse.ArgumentParser(description='Simple compiler')
command.add_argument('-help', action='store_true', help='to print help message')
command.add_argument('-lex', nargs='?', help='to perform lexical analysis')
command.add_argument('-par', nargs='?', help='to perform parsing')
command.add_argument('-exc', nargs='?', help='(Maintenance) to execute a cilly file')
command.add_argument('-cat', nargs='?', help='to look up cilly files')
command.add_argument('-env', action='store_true', help='(Maintenance) to watch current environment variables')
command.add_argument('-itr', action='store_true', help='to entry interactive mode')
command.add_argument('-vmc', nargs='?', help='to compiler from ast to opcode')
command.add_argument('-vme', nargs='?', help='to execute a cilly program in virtual machine')
return command.parse_args()
def cilly_help():
print('Usage: cilly [options]')
print('Options:')
print(' -help to print help message')
print(' -lex [file] to perform lexical analysis')
print(' -par [file] to perform parsing')
print(' -exc [file] (Maintenance) to execute a cilly file')
print(' -cat [file] to look up cilly files')
print(' -env (Maintenance) to watch current environment variables')
print(' -itr to entry interactive mode')
print(' -vmc [file] to compiler from ast to opcode')
print(' -vme [file] to execute a cilly program in virtual machine')
def cilly_lexer(filename):
if filename is not None:
with open(filename) as f:
print(lexer(f.read()))
def cilly_parser(filename):
if filename is not None:
with open(filename) as f:
print(parser(lexer(f.read())))
def cilly_execute(filename):
envir = {}
if filename is not None:
with open(filename) as f:
print(eval(parser(lexer(f.read())), envir))
def cilly_cat(filename):
if filename is not None:
with open(filename) as f:
print(f.read())
def cilly_env():
pass
def cilly_interact():
def exexute(code, env):
lex = lexer(code)
eval(parser(lex), env)
envir = {}
code = ''
layer = 0
while True:
print('>>>' + ' ' * 2 * layer, end=' ')
line = input()
if line == 'exit':
break
if line.rstrip()[-1] == '{':
layer += 1
code += line + '\n'
elif line.rstrip()[-1] == '}':
layer -= 1
code += line + '\n'
if layer == 0:
exexute(code, envir)
code = ''
elif layer < 0:
print('Invalid synix, please input again, key `exit` to break')
elif line.rstrip()[-1] == ';':
if layer > 0:
code += line + '\n'
elif layer == 0:
exexute(line, envir)
else:
print('Invalid synix, please input again, key `exit` to break')
# def opcode_2_instruction(opcode):
# c = []
# i = 0
# while i < len(opcode):
# for key, value in zl.items():
# if opcode[i] == value:
# st = ""
# for x in c:
# st += x + '\t'
# print(st)
# c = []
# c.append(f"{i}: {key}")
# f = key
# break
# if f in ["LOAD_CONST", "ENTER_SCOPE", "JMP", "JMP_TRUE", "JMP_FALSE", "CALL"]:
# c.append(f"{opcode[i + 1]}")
# f = None
# i += 1
# elif f in ["LOAD_VAR", "STORE_VAR"]:
# c.append(f"{opcode[i + 1]}")
# c.append(f"{opcode[i + 2]}")
# f = None
# i += 2
# i += 1
# def cilly_vmc(filename):
# if filename is not None:
# with open(filename) as f:
# ast = parser(lexer(f.read()))
#
# code, consts, glob_syms = cilly_vm_compiler(ast, [], [], [])
#
# opcode_2_instruction(code)
# print(consts)
# print(glob_syms)
# def cilly_vme(filename):
# if filename is not None:
# with open(filename) as f:
# ast = parser(lexer(f.read()))
#
# code, consts, glob_syms = cilly_vm_compiler(ast, [], [], [])
# cilly_vm(code, consts, glob_syms)
def main():
args = parse_command_line()
if args.help:
cilly_help()
return
if args.lex:
if args.lex.endswith('.cilly'):
cilly_lexer(filename=args.lex)
if args.par:
if args.par.endswith('.cilly'):
cilly_parser(filename=args.par)
if args.exc:
if args.exc.endswith('.cilly'):
cilly_execute(filename=args.exc)
if args.env:
cilly_env()
return
if args.cat:
cilly_cat(filename=args.cat)
if args.itr:
cilly_interact()
return
# if args.vmc:
# cilly_vmc(filename=args.vmc)
# return
#
# if args.vme:
# cilly_vme(filename=args.vme)
# return
if __name__ == '__main__':
main()