-
Notifications
You must be signed in to change notification settings - Fork 0
/
sblang2c.py
141 lines (138 loc) · 4.08 KB
/
sblang2c.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
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
import converter,sys,os,time,logging,secrets
from traceback import print_exc
VERSION="0.0.4-dev-5"
class Timer:
def __init__(self) -> None:
self.stt=0
def start(self):
self.stt=time.time()
def end(self):
return round(1000*(time.time()-self.stt),5)
timer=Timer()
args=sys.argv
name=args[0]
filename=args[-1]
argss= args[1:-1]
nocomp="--no-compile" in argss
debug="--debug" in argss
gcc_executable="g++"
gcc_extra_args=[]
temps=[]
name_executeable=".".join(filename.split(".")[:-1])
if os.name == "nt":
name_executeable+=".exe"
for arg in argss:
arg=arg.split("=",1)
if len(arg) == 1:
continue
if arg[0] == "--gcc-binary":
gcc_executable=arg[1]
if arg[0] == "--gcc-arg":
gcc_extra_args.append("-"+arg[1])
if arg[0] == "--output":
name_executeable=arg[1]
if debug:
logging.basicConfig(level=logging.DEBUG)
if len(args) < 2:
print(f"usage: {name} [options] [filename]")
exit(1)
print(f"SBLang2C Compiler and converter v{VERSION}\n-| Reading File",end="",flush=True)
timer.start()
try:
with open(filename,"r",encoding="utf-8") as f:
code=f.read()
except:
print("\n! Cannot read file, Aborting.")
exit(1)
print(f" {timer.end()} ms")
print("-| Converting to C++\n- -| Creating Runtime",end="",flush=True)
timer.start()
runtime=converter.Runtime()
print(f" {timer.end()} ms")
print("- -| Converting",end="",flush=True)
if debug:
print()
try:
for i in code.splitlines():
runtime.translate(i)
except BaseException as e:
print(f"\n! Error when converting: {e}")
print_exc()
exit(1)
if debug:
print()
print(f" {timer.end()} ms")
print('-| Resolving externs')
links=[]
mapping={}
if len(runtime.externs) == 0:
print("-| No externs")
else:
runtime.externs=list(runtime.externs)
print(f"-| Compiling {len(runtime.externs)} head(s)")
for count in range(1,len(runtime.externs)+1):
print(f"- -|{count}/{len(runtime.externs)}: {runtime.externs[count-1]}")
filename=f"_sblang_header_{secrets.token_hex(16)}.h"
temps.append(filename)
with open(runtime.externs[count-1],"r",encoding="utf-8") as f:
modcode=f.read()
with open(filename,"w+") as f:
rt=converter.Runtime()
try:
for i in modcode.splitlines():
rt.translate(i)
except BaseException as e:
print(f"\n! Error when converting: {e}")
from traceback import print_exc
print_exc()
exit(1)
rt.heads=[]
f.write(rt.export_final())
if rt.externs != []:
print("WARN: Multi-layer extern is not supported yet")
mapping.update({runtime.externs[count-1]:filename})
print(f"- -|{count}/{len(runtime.externs)}: {runtime.externs[count-1]}: Success")
new=[]
for i in runtime.externs:
data=mapping.get(i,i)
if data != i:
data=os.getcwd()+"/"+data
runtime.heads.add(data)
print("- -| Writing to temp file",end="",flush=True)
timer.start()
if debug:
print()
with open("_sblang_temp.cpp","w+",encoding="utf-8") as f:
f.write(runtime.export_final())
temps.append("_sblang_temp.cpp")
if debug:
print()
print(f" {timer.end()} ms")
if nocomp:
print("-| Exiting.")
exit(0)
time.sleep(1)
print("-| Compiling with g++")
command=f"{gcc_executable} _sblang_temp.cpp "
for i in links:
command+=f"{i} "
command+=f"-finput-charset=UTF-8 -std=c++20 -I {os.getcwd()}/sblang_builtin_funcs/require_libs/ -o {name_executeable}"
if runtime.options["USE_LIBCURL"]:
command+=" -lcurl"
if gcc_extra_args != []:
for i in gcc_extra_args:
command+=" "+i
try:
os.remove(name_executeable)
except:
pass
gcc=os.popen(command)
print(f"- -| Awaiting G++ execute: {command}",end="",flush=True)
timer.start()
times=0
gcc.read()
print(f" {timer.end()} ms")
print("-| Removeing Tempfile")
for temp in temps:
os.remove(temp)
print("-| Exiting Compile.")