-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdocgen.py
More file actions
264 lines (227 loc) · 9.05 KB
/
docgen.py
File metadata and controls
264 lines (227 loc) · 9.05 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import requests
import shutil
import os
import sys
import subprocess
import json
import textwrap
import pypandoc
ZIP_URL = "https://github.com/Princess-org/Princess/archive/refs/heads/master.zip"
PATH = "compiler/Princess-master"
DOCS = [
"std",
"arena",
"getopt",
"io",
"json",
"map",
"optional",
"process",
"set",
"shared",
"strings",
"vector",
"runtime"
]
def download_source():
zip_file = "princess.zip"
data = requests.get(ZIP_URL)
open(zip_file, "wb").write(data.content)
shutil.unpack_archive(zip_file, "compiler")
os.remove(zip_file)
def download_compiler():
os.environ["LIBRARY_PATH"] = "."
sys.argv = ["build.py", "download"]
process = subprocess.Popen([sys.executable, "build.py", "download"], cwd = "compiler/Princess-master")
process.wait()
def generate_json():
process = subprocess.Popen([f"{PATH}/bin/princess", "--typed-ast", "--emit-only-functions", "--emit-all-modules", "--no-incremental", "./docall.pr"], stdout = subprocess.PIPE)
out, err = process.communicate()
if process.returncode != 0:
print("Couldn't create documentation!", file = sys.stderr)
exit(1)
with open("docall.json", "wb") as fp:
fp.write(out)
def ident_to_str(ident):
return "::".join(ident["path"])
def type_to_str(value):
if value["kind"] == "Identifier":
return ident_to_str(value)
elif value["kind"] == "Struct" or value["kind"] == "Union":
ret = "struct {\n" if value["kind"] == "Struct" else "struct #union {\n"
body = ""
for member in value["body"]:
if member["kind"] == "IdDeclStruct":
body += ident_to_str(member["ident"]) + ": " + type_to_str(member["tpe"]) + "\n"
elif member["kind"] == "Struct":
body += type_to_str(member) + "\n"
ret += textwrap.indent(body, " ")
ret += "}"
return ret
elif value["kind"] == "PtrT":
if value["tpe"]:
tpe = type_to_str(value["tpe"])
else: tpe = None
return "*" + tpe if tpe else ""
elif value["kind"] == "RefT":
if value["tpe"]:
tpe = type_to_str(value["tpe"])
else: tpe = None
return "&" + tpe if tpe else ""
elif value["kind"] == "WeakRefT":
if value["tpe"]:
tpe = type_to_str(value["tpe"])
else: tpe = None
return "weak &" + (tpe if tpe else "")
elif value["kind"] == "ArrayT":
return "[" + type_to_str(value["tpe"]) + "]"
elif value["kind"] == "TypeConstructor":
args = ", ".join(map(type_to_str, value["args"]))
return ident_to_str(value["name"]) + "(" + args + ")"
elif value["kind"] == "FunctionT":
ret = "(" + ", ".join(map(type_to_str, value["args"])) + ") -> "
ret += "(" + ", ".join(map(type_to_str, value["ret"])) + ")"
return ret
elif value["kind"] == "Enum":
ret = "enum {\n"
body = ""
for member in value["body"]:
body += ident_to_str(member["ident"])
value = value_to_str(member["value"])
if value:
body += " = " + value
body += "\n"
ret += textwrap.indent(body, " ")
ret += "}"
return ret
elif value["kind"] == "StructuralT":
ret = "interface {\n"
body = ""
for member in value["body"]:
body += "def " + ident_to_str(member["name"])
body += "(" + ", ".join(map(lambda par: ident_to_str(par["name"]) + ": " + type_to_str(par["tpe"]), member["params"])) + ")"
if member["returns"]:
body += " -> "
body += ", ".join(map(type_to_str, member["returns"]))
body += "\n"
ret += textwrap.indent(body, " ")
ret += "}"
return ret
elif value["kind"] == "TypeT":
return "type " + type_to_str(value["expr"])
else:
return value["kind"]
def value_to_str(value):
if value == None: return None
if value["kind"] == "Integer":
return str(int(value["value"]))
elif value["kind"] == "String":
return '"' + value["value"] + '"'
elif value["kind"] == "Float":
return str(value["value"])
elif value["kind"] == "Boolean":
return str(value["value"])
elif value["kind"] == "USub":
return "-" + value_to_str(value["expr"]) # TODO Quotes where necessary
return "..." # We don't know what to do with this
def param_to_str(par):
res = ""
if par["kw"] == "TYPE":
res += "type "
res += ident_to_str(par["name"])
if par["tpe"]:
res += ": " + type_to_str(par["tpe"])
return res
def tc_param_to_str(par):
res = "type "
res += ident_to_str(par["name"])
if par["tpe"]:
res += ": " + type_to_str(par["tpe"])
return res
def print_doc(doc, fp):
print(file = fp)
print(pypandoc.convert_text(doc, "rst", format = "md"), file = fp)
def generate_documentation():
with open("docall.json", "r") as fp:
data = json.load(fp)
with open(f"source/stdlib.rst", "w") as fp:
print("Standard library", file = fp)
print("----------------", file = fp)
for doc in DOCS:
print(doc, file = fp)
print("~" * len(doc), file = fp)
print(file = fp)
functions = []
variables = []
types = []
body = data[doc]["body"]
for symbol in body:
if symbol["share"] == "EXPORT" or symbol["share"] == "BOTH":
if symbol["kind"] == "Def":
functions.append(symbol)
elif symbol["kind"] == "VarDecl":
variables.append(symbol)
elif symbol["kind"] == "TypeDecl":
types.append(symbol)
if types:
print("Types", file = fp)
print("^^^^^", file = fp)
for symbol in types:
for i, type in enumerate(symbol["left"]):
tpe = symbol["right"][i]
print(".. code-block:: princess\n", file = fp)
if type["kind"] == "Identifier":
name = ident_to_str(type)
res = f"type {name} = {type_to_str(tpe)}"
print(textwrap.indent(res, " "), file = fp)
elif type["kind"] == "TypeConstructor":
name = ident_to_str(type["name"])
res = f"type {name}({', '.join(map(tc_param_to_str, type['args']))}) = {type_to_str(tpe)}"
print(textwrap.indent(res, " "), file = fp)
if "doc" in symbol:
print_doc(symbol["doc"], fp)
print(file = fp)
if variables:
print("Variables", file = fp)
print("^^^^^^^^^", file = fp)
for symbol in variables:
kw = symbol["kw"].lower()
right = symbol["right"]
left = symbol["left"]
for i, var in enumerate(left):
value = "..."
if i < len(right):
r = right[i]
value = value_to_str(r)
if var["kind"] == "IdDecl":
print(".. code-block:: princess\n", file = fp)
ident = var["value"]
tpe = ident["type_tag"]["name"]
name = ident_to_str(ident)
print(f" {kw} {name}: {tpe} = {value}", file = fp)
if "doc" in symbol:
print_doc(symbol["doc"], fp)
print(file = fp)
if functions:
print("Functions", file = fp)
print("^^^^^^^^^", file = fp)
for symbol in functions:
name = ident_to_str(symbol["name"])
print(".. code-block:: princess\n", file = fp)
body = " def " + ident_to_str(symbol["name"])
body += "(" + ", ".join(map(param_to_str, symbol["params"])) + ")"
if symbol["returns"]:
body += " -> "
body += ", ".join(map(type_to_str, symbol["returns"]))
body += "\n"
print(body, file = fp)
if "doc" in symbol:
print_doc(symbol["doc"], fp)
print(file = fp)
def main():
download_source()
download_compiler()
generate_json()
generate_documentation()
if __name__ == "__main__":
main()