Skip to content

Commit

Permalink
(fix) Use Path for dirname resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
joshgoebel committed May 31, 2021
1 parent b194c62 commit 431359d
Showing 7 changed files with 135 additions and 74 deletions.
124 changes: 102 additions & 22 deletions src/cli/_wren.inc
Original file line number Diff line number Diff line change
@@ -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"
@@ -341,6 +341,8 @@ static const char* cliModuleSource =
" static runFile(file) {\n"
" var moduleName\n"
"\n"
" System.print(dirForModule(\"C:\\\\windows\\\\test\\\\bob.txt\"))\n"
"\n"
" if (file == \"-\") return runInput()\n"
" if (!File.exists(file)) return missingScript(file)\n"
" \n"
@@ -364,5 +366,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 (last<path.count) {\n"
" segments.add(path[last..-1])\n"
" } else if (last==i) {\n"
" segments.add(\"\")\n"
" }\n"
" return segments\n"
" }\n"
" normalize {\n"
" // var paths = _path.split(sep)\n"
" var paths = Path.split(_path)\n"
" var finalPaths = []\n"
" if (_path.startsWith(\"/\")) finalPaths.add(\"/\") \n"
" if (paths[0]==\".\") finalPaths.add(\".\") \n"
" for (path in paths) {\n"
" var last = finalPaths.count>0 ? 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"
"}";

25 changes: 2 additions & 23 deletions src/cli/cli.wren
Original file line number Diff line number Diff line change
@@ -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()

2 changes: 1 addition & 1 deletion src/cli/path.wren
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#module=resolver
//module=resolver,cli
class Path {
construct new(path) {
_path = path
19 changes: 19 additions & 0 deletions src/cli/path_type.wren
Original file line number Diff line number Diff line change
@@ -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
}
}
19 changes: 0 additions & 19 deletions src/cli/resolver.wren
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 2 additions & 1 deletion src/cli/vm.c
Original file line number Diff line number Diff line change
@@ -225,7 +225,8 @@ WrenInterpretResult runCLI()
rootDirectory = (char*)".";
initVM();

WrenInterpretResult result = wrenInterpret(vm, "<cli>", "import \"cli\"\n");
WrenInterpretResult result = wrenInterpret(vm, "<cli>", "import \"cli\" for CLI\n");
if (result == WREN_RESULT_SUCCESS) { result = wrenInterpret(vm, "<cli>", "CLI.start()"); }

if (result == WREN_RESULT_SUCCESS)
{
17 changes: 9 additions & 8 deletions util/cli_to_c_string.py
Original file line number Diff line number Diff line change
@@ -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 = {}

0 comments on commit 431359d

Please sign in to comment.