-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhonk.py
65 lines (53 loc) · 1.53 KB
/
honk.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
import config
from sys import argv
from os import path
import argparse
from ply import lex, yacc
# CLI Arguments
cli = argparse.ArgumentParser(description='h o n k')
cli.add_argument('-t', '--tokens', help='Print tokens', action='store_true')
cli.add_argument('-p', '--parser', help='Enable debug info for parsing', action='store_true')
cli.add_argument('-v', '--vm', help='Enable debug info for virtual machine', action='store_true')
cli.add_argument('file', help='Specify a file to run through')
args = cli.parse_args()
# Set configuration before importing lexer and parser
filepath = path.splitext(args.file)
config.objFilename = filepath[0]
config.debugParser = args.parser
# Import the brains and machines
if filepath[1] == '.honk':
import lexhonker as lexer, parshonker as parser
else:
import lexer, parser
from honkVM import honk
# Read file
data = ''
try:
with open(args.file) as f:
for line in f:
data += line
except FileNotFoundError:
raise Exception(f'{args.file} does not exist!')
# Scan file
ducklexer = lex.lex(module=lexer)
ducklexer.input(data)
# Print tokens when allowed
if args.tokens:
while True:
token = ducklexer.token()
if not token:
break
print(token)
# Parse file and build .o file)
duckparser = yacc.yacc(module=parser)
resultQM = duckparser.parse(data)
# Read .o file
data = ''
try:
with open(f'{config.objFilename}.o') as f:
for line in f:
data += line
except FileNotFoundError:
raise Exception(f'{config.objFilename}.o does not exist!')
# Honk away
honk(data, args.vm)