Skip to content

Commit 7cfe612

Browse files
committed
dogfood: write wren_to_c_string in Wren
1 parent 5f1a194 commit 7cfe612

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

src/module/io.wren

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@ foreign class File {
9999
return Scheduler.runNextScheduled_()
100100
}
101101

102+
static splitext(path) {
103+
var segments = path.split(".")
104+
return [segments[0], segments[1..-1]]
105+
}
106+
107+
static basename(path) {
108+
return path.split("/")[-1]
109+
}
110+
102111
static size(path) {
103112
ensurePath_(path)
104113
sizePath_(path, Fiber.current)

src/module/io.wren.inc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@ static const char* ioModuleSource =
101101
" return Scheduler.runNextScheduled_()\n"
102102
" }\n"
103103
"\n"
104+
" static splitext(path) {\n"
105+
" var segments = path.split(\".\")\n"
106+
" return [segments[0], segments[1..-1]]\n"
107+
" }\n"
108+
"\n"
109+
" static basename(path) {\n"
110+
" return path.split(\"/\")[-1]\n"
111+
" }\n"
112+
"\n"
104113
" static size(path) {\n"
105114
" ensurePath_(path)\n"
106115
" sizePath_(path, Fiber.current)\n"

util/wren_to_c_string.wren

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)