Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(enh) wren_to_c_string processes all files at once #80

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions util/wren_to_c_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,39 @@

def wren_to_c_string(input_path, wren_source_lines, module):
wren_source = ""
# cut off blank lines at the bottom
while (wren_source_lines[-1].strip()==""):
wren_source_lines.pop()
for line in wren_source_lines:
line = line.replace('"', "\\\"")
line = line.replace("\n", "\\n\"")
if wren_source: wren_source += "\n"
wren_source += '"' + line
line = line.replace("\n", "\\n")
wren_source += '"' + line + '"\n'

wren_source = wren_source.strip()

return PREAMBLE.format(input_path, module, wren_source)

def process_file(path):
infile = os.path.basename(path)
outfile = infile + ".inc"
print("{} => {}").format(path.replace("src/",""), outfile)

def main():
parser = argparse.ArgumentParser(
description="Convert a Wren library to a C string literal.")
parser.add_argument("output", help="The output file to write")
parser.add_argument("input", help="The source .wren file")

args = parser.parse_args()

with open(args.input, "r") as f:
with open(path, "r") as f:
wren_source_lines = f.readlines()

module = os.path.splitext(os.path.basename(args.input))[0]
module = os.path.splitext(infile)[0]
module = module.replace("opt_", "")
module = module.replace("wren_", "")

c_source = wren_to_c_string(args.input, wren_source_lines, module)
c_source = wren_to_c_string(infile, wren_source_lines, module)

with open(args.output, "w") as f:
with open(outfile, "w") as f:
f.write(c_source)


def main():
files = glob.glob("src/module/*.wren")
for file in files:
process_file(file)

main()