Automatically update the string constants when a builtin module changes.

This commit is contained in:
Bob Nystrom
2015-09-13 10:03:02 -07:00
parent e1cf8e22c3
commit f36a795534
15 changed files with 455 additions and 441 deletions
-45
View File
@@ -1,45 +0,0 @@
#!/usr/bin/env python
# coding: utf-8
import glob
import os.path
import re
PATTERN = re.compile(r'LibSource =\n("(.|[\n])*?);')
def copy_builtin(filename, out_prefix):
name = os.path.basename(filename)
name = name.split('.')[0]
with open(filename, "r") as f:
lines = f.readlines()
wren_source = ""
for line in lines:
line = line.replace('"', "\\\"")
line = line.replace("\n", "\\n\"")
if wren_source: wren_source += "\n"
wren_source += '"' + line
# re.sub() will unescape escape sequences, but we want them to stay escapes
# in the C string literal.
wren_source = wren_source.replace('\\', '\\\\')
constant = "LibSource =\n" + wren_source + ";"
with open(out_prefix + name + ".c", "r") as f:
c_source = f.read()
c_source = PATTERN.sub(constant, c_source)
with open(out_prefix + name + ".c", "w") as f:
f.write(c_source)
print(filename.ljust(25) + " → " + out_prefix + name + ".c")
for f in glob.iglob("builtin/*.wren"):
copy_builtin(f, "src/vm/wren_")
for f in glob.iglob("src/module/*.wren"):
copy_builtin(f, "src/module/")
+9 -2
View File
@@ -22,10 +22,10 @@
CLI_HEADERS := $(wildcard src/cli/*.h)
CLI_SOURCES := $(wildcard src/cli/*.c)
MODULE_HEADERS := $(wildcard src/module/*.h)
MODULE_HEADERS := $(wildcard src/module/*.h) $(wildcard src/module/*.wren.inc)
MODULE_SOURCES := $(wildcard src/module/*.c)
VM_HEADERS := $(wildcard src/vm/*.h)
VM_HEADERS := $(wildcard src/vm/*.h) $(wildcard src/vm/*.wren.inc)
VM_SOURCES := $(wildcard src/vm/*.c)
TEST_HEADERS := $(wildcard test/api/*.h)
@@ -192,4 +192,11 @@ $(LIBUV_DIR)/build/gyp/gyp: script/libuv.py
$(LIBUV): $(LIBUV_DIR)/build/gyp/gyp script/libuv.py
@ ./script/libuv.py build $(LIBUV_ARCH)
# Wren modules that get compiled into the binary as C strings.
src/vm/wren_%.wren.inc: builtin/%.wren script/wren_to_c_string.py
@ ./script/wren_to_c_string.py $@ $<
src/module/%.wren.inc: src/module/%.wren script/wren_to_c_string.py
@ ./script/wren_to_c_string.py $@ $<
.PHONY: all cli test vm
+53
View File
@@ -0,0 +1,53 @@
#!/usr/bin/env python
# coding: utf-8
import argparse
import glob
import os.path
import re
# The source for the Wren modules that are built into the VM or CLI are turned
# include C string literals. This way they can be compiled directly into the
# code so that file IO is not needed to find and read them.
#
# These string literals are stored in files with a ".wren.inc" extension and
# #included directly by other source files. This generates a ".wren.inc" file
# given a ".wren" module.
PREAMBLE = """// Generated automatically from {0}. Do not edit.
static const char* {1}ModuleSource =
{2};
"""
def wren_to_c_string(input_path, wren_source_lines, module):
wren_source = ""
for line in wren_source_lines:
line = line.replace('"', "\\\"")
line = line.replace("\n", "\\n\"")
if wren_source: wren_source += "\n"
wren_source += '"' + line
return PREAMBLE.format(input_path, module, wren_source)
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:
wren_source_lines = f.readlines()
module = os.path.splitext(os.path.basename(args.input))[0]
c_source = wren_to_c_string(args.input, wren_source_lines, module)
with open(args.output, "w") as f:
f.write(c_source)
print(" str " + args.input)
main()