131 lines
2.2 KiB
Python
Executable file
131 lines
2.2 KiB
Python
Executable file
#!/usr/bin/env python3.10
|
|
|
|
import os;
|
|
import sys;
|
|
import io;
|
|
|
|
input_file = None;
|
|
|
|
output_file = None;
|
|
|
|
snippet_directory = "/tmp"
|
|
|
|
i = 1;
|
|
n = len(sys.argv);
|
|
|
|
while i < n:
|
|
arg = sys.argv[i];
|
|
|
|
match arg:
|
|
case "-i":
|
|
input_file = sys.argv[i + 1]
|
|
i += 2;
|
|
|
|
case "-o":
|
|
output_file = sys.argv[i + 1]
|
|
i += 2;
|
|
|
|
case "-s":
|
|
snippet_directory = sys.argv[i + 1]
|
|
i += 2;
|
|
|
|
case _:
|
|
assert(not "TODO");
|
|
|
|
if (not input_file) or (not output_file):
|
|
assert(not "TODO");
|
|
|
|
while input_file.startswith("./"):
|
|
input_file = input_file[2:];
|
|
|
|
while snippet_directory.startswith("./"):
|
|
snippet_directory = snippet_directory[2:];
|
|
|
|
while snippet_directory.endswith("/"):
|
|
snippet_directory = snippet_directory[:-1];
|
|
|
|
myglobals = dict();
|
|
|
|
istream = open(input_file, "r");
|
|
ostream = open(output_file, "w");
|
|
|
|
lines = istream.readlines();
|
|
|
|
i = 0;
|
|
n = len(lines);
|
|
|
|
while i < n:
|
|
if "/*/" in lines[i]:
|
|
indentation = lines[i].index("/*/");
|
|
|
|
start_index = i;
|
|
|
|
i += 1;
|
|
|
|
source = ""
|
|
|
|
while "/*/" not in lines[i]:
|
|
source += lines[i][indentation:];
|
|
i += 1;
|
|
|
|
end_index = i;
|
|
i += 1;
|
|
|
|
real_stdout = sys.stdout
|
|
sys.stdout = captured_stdout = io.StringIO()
|
|
eval(compile(source, "<zog-snippet>", "exec"), myglobals);
|
|
sys.stdout = real_stdout
|
|
|
|
outstring = captured_stdout.getvalue();
|
|
|
|
# if outstring and outstring[-1] != "\n":
|
|
# outstring += "\n"
|
|
|
|
temppath = f"{snippet_directory}/{input_file}-{start_index}.c";
|
|
|
|
tempdir = temppath[:temppath.rindex('/')];
|
|
|
|
os.makedirs(tempdir, exist_ok = True);
|
|
|
|
with open(temppath, "w") as stream:
|
|
for outline in outstring.split("\n"):
|
|
stream.write((" " * indentation) + outline + '\n');
|
|
|
|
ostream.write(f'#include "{temppath}"' + "\n");
|
|
ostream.write("\n" * (end_index - start_index));
|
|
else:
|
|
ostream.write(lines[i]);
|
|
i += 1;
|
|
|
|
istream.close();
|
|
ostream.close();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|