-
Notifications
You must be signed in to change notification settings - Fork 0
/
cvScript.py
82 lines (67 loc) · 2.59 KB
/
cvScript.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
import re
import os
import json
import subprocess
import argparse
def main():
parser = argparse.ArgumentParser(description = "A compiler for the Cvenska programing language")
parser.add_argument('filename',
help = "The Cvenska file that should be compiled.")
parser.add_argument('-v', '--verbose',
action='store_true',
help = "Prints information as the program runs.")
parser.add_argument('-sc', '--save_c',
action='store_true',
help = "Keeps the temporary C file for inspection.")
parser.add_argument('-o', '--output',
metavar = "filename", default = "a.out",
help = "Output file for program")
args = parser.parse_args()
if args.verbose:
print(f"Reading from file {args.filename}...")
f = open(args.filename, "r")
program = f.read()
program = " " + program
f.close()
if args.verbose:
print(f"Finished reading from file {args.filename}")
lexicon_file = open("lexicon.json", "r")
lexicon = json.load(lexicon_file)
if args.verbose:
print(f"Begin regex... ")
modules = re.findall(r"#inkludera\s<(\w+).h>", program)
modules.append("core")
for module in lexicon.keys():
if module in modules:
if args.verbose:
print(f"Start {module}")
for c, cv in lexicon[module].items():
if (c == cv):
continue
special_sym = r"([\s~@#\$%\^\&\*\(\)\-\+=\{\}\[\];:\'\"/\?\.>,<\\\|])"
pat = special_sym+c+special_sym
if re.search(pat, program):
i = 0
while True:
if not re.search(special_sym+c+str(i)+special_sym, program):
program = re.sub(pat, r"\1"+c+str(i)+r"\2", program)
break
i += 1
pat = special_sym+cv+special_sym
program = re.sub(pat, r"\1"+c+r"\2", program)
program = program[1:]
if args.verbose:
print(f"Finished regex")
if args.verbose:
print(f"Create temporary C file")
with open("temp.c", "w+") as temp_c:
temp_c.write(program)
if args.verbose:
print(f"Run gcc with flags: ")
gcc = subprocess.run(["gcc", "temp.c", "-o", args.output])
if not args.save_c:
if args.verbose:
print(f"Remove temporary C file")
os.remove("temp.c")
if __name__ == "__main__":
main()