|
| 1 | +#!./bin/wren_cli |
| 2 | + |
| 3 | +import "os" for Process |
| 4 | +import "io" for File |
| 5 | + |
| 6 | +// The source for the Wren modules that are built into the VM or CLI are turned |
| 7 | +// include C string literals. This way they can be compiled directly into the |
| 8 | +// code so that file IO is not needed to find and read them. |
| 9 | +// |
| 10 | +// These string literals are stored in files with a ".wren.inc" extension and |
| 11 | +// #included directly by other source files. This generates a ".wren.inc" file |
| 12 | +// given a ".wren" module. |
| 13 | + |
| 14 | +var PREAMBLE = "// Generated automatically from {0}. Do not edit. |
| 15 | +static const char* {1}ModuleSource = |
| 16 | +{2}; |
| 17 | +" |
| 18 | + |
| 19 | +class WrenToCInclude { |
| 20 | + construct new(path) { |
| 21 | + _source = File.read(path).trim() |
| 22 | + _path = path |
| 23 | + } |
| 24 | + compile() { |
| 25 | + var source = _source.split("\n").map { |line| |
| 26 | + line = line.replace("\"", "\\\"") |
| 27 | + return "\"" + line + "\\n\"" |
| 28 | + }.join("\n") |
| 29 | + |
| 30 | + return PREAMBLE. |
| 31 | + replace("{0}", _path). |
| 32 | + replace("{1}", File.splitext(File.basename(_path))[0]). |
| 33 | + replace("{2}", source) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +var HELP = "usage: wren_to_c_string.wren output input |
| 38 | +error: too few arguments" |
| 39 | + |
| 40 | +var main = Fn.new { |
| 41 | + if (Process.arguments.count < 2) { |
| 42 | + System.print(HELP) |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + var output = Process.arguments[0] |
| 47 | + var input_path = Process.arguments[1] |
| 48 | + |
| 49 | + var header = WrenToCInclude.new(input_path) |
| 50 | + |
| 51 | + var f = File.create(output) |
| 52 | + f.writeBytes(header.compile()) |
| 53 | +} |
| 54 | + |
| 55 | +// def main(): |
| 56 | +// parser = argparse.ArgumentParser( |
| 57 | +// description="Convert a Wren library to a C string literal.") |
| 58 | +// parser.add_argument("output", help="The output file to write") |
| 59 | +// parser.add_argument("input", help="The source .wren file") |
| 60 | + |
| 61 | +// args = parser.parse_args() |
| 62 | + |
| 63 | +// with open(args.input, "r") as f: |
| 64 | +// wren_source_lines = f.readlines() |
| 65 | + |
| 66 | +// module = os.path.splitext(os.path.basename(args.input))[0] |
| 67 | +// module = module.replace("opt_", "") |
| 68 | +// module = module.replace("wren_", "") |
| 69 | + |
| 70 | +// c_source = wren_to_c_string(args.input, wren_source_lines, module) |
| 71 | + |
| 72 | +// with open(args.output, "w") as f: |
| 73 | +// f.write(c_source) |
| 74 | + |
| 75 | + |
| 76 | +main.call() |
0 commit comments