diff --git a/src/cli/_wren.inc b/src/cli/_wren.inc index de8d0466..2f141225 100644 --- a/src/cli/_wren.inc +++ b/src/cli/_wren.inc @@ -120,6 +120,17 @@ static const char* resolverModuleSource = " }\n" "}\n" "\n" +"class File {\n" +" foreign static loadDynamicLibrary(name, path)\n" +" foreign static existsSync(s)\n" +" foreign static realPathSync(s)\n" +"}\n" +"\n" +"Resolver.boot()\n" +"\n" +"\n" +"\n\n" +"//module=resolver,cli\n" "class PathType {\n" " static SIMPLE { 1 }\n" " static ABSOLUTE { 2 }\n" @@ -138,18 +149,8 @@ static const char* resolverModuleSource = " return PathType.SIMPLE\n" " }\n" "}\n" -"\n" -"class File {\n" -" foreign static loadDynamicLibrary(name, path)\n" -" foreign static existsSync(s)\n" -" foreign static realPathSync(s)\n" -"}\n" -"\n" -"Resolver.boot()\n" -"\n" -"\n" "\n\n" -"#module=resolver\n" +"//module=resolver,cli\n" "class Path {\n" " construct new(path) { \n" " _path = path \n" @@ -228,15 +229,7 @@ static const char* resolverModuleSource = // Generated automatically from src/cli/*.wren. Do not edit. static const char* cliModuleSource = -"import \"repl\" for Repl, AnsiRepl, SimpleRepl\n" -"import \"os\" for Platform, Process\n" -"import \"io\" for Stdin, Stderr, File, Stdout, Stat\n" -"import \"mirror\" for Mirror\n" -"import \"meta\" for Meta\n" -"import \"runtime\" for Runtime\n" -"\n" -"// TODO: how to avoid duplication?\n" -"// we only use this for absolute path\n" +"//module=resolver,cli\n" "class PathType {\n" " static SIMPLE { 1 }\n" " static ABSOLUTE { 2 }\n" @@ -255,6 +248,13 @@ static const char* cliModuleSource = " return PathType.SIMPLE\n" " }\n" "}\n" +"\n\n" +"import \"repl\" for Repl, AnsiRepl, SimpleRepl\n" +"import \"os\" for Platform, Process\n" +"import \"io\" for Stdin, Stderr, File, Stdout, Stat\n" +"import \"mirror\" for Mirror\n" +"import \"meta\" for Meta\n" +"import \"runtime\" for Runtime\n" "\n" "class StackTrace {\n" " construct new(fiber) {\n" @@ -314,7 +314,7 @@ static const char* cliModuleSource = " \n" " }\n" " static dirForModule(file) {\n" -" return file.split(\"/\")[0..-2].join(\"/\")\n" +" return Path.new(file).dirname.toString\n" " }\n" " static missingScript(file) {\n" " Stderr.print(\"wrenc: No such file -- %(file)\")\n" @@ -364,5 +364,83 @@ static const char* cliModuleSource = " }\n" " foreign static setRootDirectory_(dir) \n" "}\n" -"CLI.start()\n"; +"// CLI.start()\n" +"\n" +"\n\n" +"//module=resolver,cli\n" +"class Path {\n" +" construct new(path) { \n" +" _path = path \n" +" _sep = appearsWindows() ? \"\\\\\" : \"/\"\n" +" }\n" +" appearsWindows() {\n" +" if (_path.contains(\"\\\\\")) return true\n" +" if (_path.count>=2 && _path[1] == \":\") return true\n" +" }\n" +" sep { _sep || \"/\" }\n" +" toString { _path }\n" +" up() { join(\"..\") }\n" +" join(path) { Path.new(_path + sep + path).normalize }\n" +" isRoot { \n" +" return _path == \"/\" || \n" +" // C:\n" +" (_path.count == 2 && _path[1] == \":\") ||\n" +" // F:\\\n" +" (_path.count == 3 && _path[1..2] == \":\\\\\") \n" +" }\n" +" dirname {\n" +" if (_path==\"/\") return this\n" +" if (_path.endsWith(sep)) return Path.new(_path[0..-2])\n" +" return up()\n" +" }\n" +" static split(path) {\n" +" var segments = []\n" +" var last = 0\n" +" var i = 0\n" +" while (i < path.count) {\n" +" var char = path[i]\n" +" if (char == \"/\" || char == \"\\\\\") {\n" +" if (last==i) {\n" +" segments.add(\"\")\n" +" } else {\n" +" segments.add(path[last...i])\n" +" }\n" +" last = i + 1\n" +" }\n" +" i = i + 1\n" +" }\n" +" if (last0 ? finalPaths[-1] : null\n" +" if (path == \"..\") {\n" +" if (last == \"/\") continue\n" +" if (last == \"..\" || last == null) {\n" +" finalPaths.add(\"%(path)\") \n" +" } else {\n" +" if (finalPaths.count > 0) finalPaths.removeAt(finalPaths.count - 1)\n" +" }\n" +" } else if (path == \"\" || path == \".\") {\n" +" continue\n" +" } else {\n" +" finalPaths.add(path)\n" +" }\n" +" }\n" +" if (finalPaths.count>1 && finalPaths[0] == \"/\") finalPaths[0] = \"\"\n" +" var path = finalPaths.join(sep)\n" +" if (path == \"\") path = \".\"\n" +" return Path.new(path)\n" +" }\n" +"}"; diff --git a/src/cli/cli.wren b/src/cli/cli.wren index 2fa6d6be..de80ed37 100644 --- a/src/cli/cli.wren +++ b/src/cli/cli.wren @@ -5,27 +5,6 @@ import "mirror" for Mirror import "meta" for Meta import "runtime" for Runtime -// TODO: how to avoid duplication? -// we only use this for absolute path -class PathType { - static SIMPLE { 1 } - static ABSOLUTE { 2 } - static RELATIVE { 3 } - - static unixAbsolute(path) { path.startsWith("/") } - static windowsAbsolute(path) { - // TODO: is this not escaped properly by the stock Python code generator - return path.count >= 3 && path[1..2] == ":\\" - } - static resolve(path) { - if (path.startsWith(".")) return PathType.RELATIVE - if (unixAbsolute(path)) return PathType.ABSOLUTE - if (windowsAbsolute(path)) return PathType.ABSOLUTE - - return PathType.SIMPLE - } -} - class StackTrace { construct new(fiber) { _fiber = fiber @@ -84,7 +63,7 @@ class CLI { } static dirForModule(file) { - return file.split("/")[0..-2].join("/") + return Path.new(file).dirname.toString } static missingScript(file) { Stderr.print("wrenc: No such file -- %(file)") @@ -134,5 +113,5 @@ class CLI { } foreign static setRootDirectory_(dir) } -CLI.start() +// CLI.start() diff --git a/src/cli/path.wren b/src/cli/path.wren index 0bbd5484..ad95558f 100644 --- a/src/cli/path.wren +++ b/src/cli/path.wren @@ -1,4 +1,4 @@ -#module=resolver +//module=resolver,cli class Path { construct new(path) { _path = path diff --git a/src/cli/path_type.wren b/src/cli/path_type.wren new file mode 100644 index 00000000..17a0180b --- /dev/null +++ b/src/cli/path_type.wren @@ -0,0 +1,19 @@ +//module=resolver,cli +class PathType { + static SIMPLE { 1 } + static ABSOLUTE { 2 } + static RELATIVE { 3 } + + static unixAbsolute(path) { path.startsWith("/") } + static windowsAbsolute(path) { + // TODO: is this not escaped properly by the stock Python code generator + return path.count >= 3 && path[1..2] == ":\\" + } + static resolve(path) { + if (path.startsWith(".")) return PathType.RELATIVE + if (unixAbsolute(path)) return PathType.ABSOLUTE + if (windowsAbsolute(path)) return PathType.ABSOLUTE + + return PathType.SIMPLE + } +} diff --git a/src/cli/resolver.wren b/src/cli/resolver.wren index ba2fa29e..b2275e49 100644 --- a/src/cli/resolver.wren +++ b/src/cli/resolver.wren @@ -118,25 +118,6 @@ class Resolver { } } -class PathType { - static SIMPLE { 1 } - static ABSOLUTE { 2 } - static RELATIVE { 3 } - - static unixAbsolute(path) { path.startsWith("/") } - static windowsAbsolute(path) { - // TODO: is this not escaped properly by the stock Python code generator - return path.count >= 3 && path[1..2] == ":\\" - } - static resolve(path) { - if (path.startsWith(".")) return PathType.RELATIVE - if (unixAbsolute(path)) return PathType.ABSOLUTE - if (windowsAbsolute(path)) return PathType.ABSOLUTE - - return PathType.SIMPLE - } -} - class File { foreign static loadDynamicLibrary(name, path) foreign static existsSync(s) diff --git a/src/cli/vm.c b/src/cli/vm.c index 73ac5fb5..b41e1953 100644 --- a/src/cli/vm.c +++ b/src/cli/vm.c @@ -225,7 +225,8 @@ WrenInterpretResult runCLI() rootDirectory = (char*)"."; initVM(); - WrenInterpretResult result = wrenInterpret(vm, "", "import \"cli\"\n"); + WrenInterpretResult result = wrenInterpret(vm, "", "import \"cli\" for CLI\n"); + if (result == WREN_RESULT_SUCCESS) { result = wrenInterpret(vm, "", "CLI.start()"); } if (result == WREN_RESULT_SUCCESS) { diff --git a/util/cli_to_c_string.py b/util/cli_to_c_string.py index 4abcc7bb..5fb029d0 100755 --- a/util/cli_to_c_string.py +++ b/util/cli_to_c_string.py @@ -43,17 +43,18 @@ def process_file(path, modules): wren_source_lines = f.readlines() + ["\n\n"] first = wren_source_lines[0] - m = re.search(r'#module=(.*)',first) + m = re.search(r'//module=(.*)',first) if (m): - module = m.group(1) + moduleNames = m.group(1).split(",") else: - module = os.path.splitext(infile)[0] - module = module.replace("opt_", "") - module = module.replace("wren_", "") + moduleNames = [os.path.splitext(infile)[0]] - modules[module] = modules.get(module,[]) - modules[module].extend(wren_source_lines) - # return wren_to_c_string(infile, wren_source_lines, module) + for module in moduleNames: + module = module.replace("opt_", "") + module = module.replace("wren_", "") + modules[module] = modules.get(module,[]) + modules[module].extend(wren_source_lines) + # return wren_to_c_string(infile, wren_source_lines, module) module_files = {}