-
Notifications
You must be signed in to change notification settings - Fork 0
/
eggs.py
65 lines (60 loc) · 2.81 KB
/
eggs.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 argparse
parser = argparse.ArgumentParser(description="Take Eggsembly file (.eggs) and run it.")
parser.add_argument("-t", "--transpile", type=str, metavar="file", default="",
help="A file name. If specified, a Chicken file (.chkn) equivalent to the Eggsembly file given will be generated and saved instead.")
parser.add_argument("-c", "--compile", type=str, metavar="file", default="",
help="A file name. If specified, a Chicken bytecode file (.chkc) equivalent to the Eggsembly file given will be compiled and saved instead. (this can be used to be run by the standalone chkc executable without compiling.)")
parser.add_argument("file", metavar="file", type=str, nargs=1,
help="Eggsembly file to run.")
args = parser.parse_args()
import re, os.path
COMMENT = re.compile(
r"(//[^\n]*|/\*.*?(?:\*/|$)|~~\[==.*?(?:==\]~~/|$))",
re.S)
try:
FILE = open(args.file[0], "r")
except FileNotFoundError:
print(repr(args.file[0]), "does not exist.")
else:
FULLTXT = FILE.read()
ENDEGGS = re.sub(COMMENT, "", FULLTXT)
if len(ENDEGGS.strip()) == 0:
print("\nEmpty file given:", repr(args.file[0]))
else:
import eggs2chkn
ROOT = "/".join(args.file[0].split("/")[:-1]) + "/"
valid = eggs2chkn.validate(ENDEGGS, args.file[0], ROOT)
ENDCHKN = eggs2chkn.transpile(ENDEGGS, args.file[0], ROOT)
# print(ENDCHKN)
if ENDCHKN:
if args.transpile:
CHKNFILE = open(args.transpile + [".chkn", ""][args.transpile.endswith(".chkn")], 'w')
CHKNFILE.write(ENDCHKN)
CHKNFILE.close()
#
# from bytes import randomFile, parse
# import os
# OPs = parse(ENDCHKN)
# if OPs:
# if args.compile:
# with open(args.compile + [".chkc", ""][args.compile.endswith(".chkc")], 'wb') as f:
# f.write(OPs.encode('utf-8'))
# f.close()
# else:
# try:
# inp = input("Input: ")
# with randomFile('wb') as f:
# fileName = f.name
# f.write(OPs.encode('utf-8'))
# f.close()
# finally:
# import os.path, inspect
# os.system("./" + "\\".join(inspect.getfile(inspect.currentframe()).split("/")[:-1]) + "/chkc %r %r" % (fileName, inp))
# os.remove(fileName)
elif valid:
from vm import Machine
VM = Machine(bbq_compat=False)
VM.load_str(ENDCHKN)
VM.load_input(input("Input: "))
print(VM.run())
print(VM.stack)