aboutsummaryrefslogtreecommitdiff
path: root/examples/tomodeps/tomodeps.tm
diff options
context:
space:
mode:
Diffstat (limited to 'examples/tomodeps/tomodeps.tm')
-rw-r--r--examples/tomodeps/tomodeps.tm58
1 files changed, 29 insertions, 29 deletions
diff --git a/examples/tomodeps/tomodeps.tm b/examples/tomodeps/tomodeps.tm
index c6c2171e..dd7bec10 100644
--- a/examples/tomodeps/tomodeps.tm
+++ b/examples/tomodeps/tomodeps.tm
@@ -12,44 +12,44 @@ _HELP := "
enum Dependency(File(path:Path), Module(name:Text))
func _get_file_dependencies(file:Path -> {Dependency}):
- if not file:is_file():
+ if not file.is_file():
say("Could not read file: $file")
- return {}
+ return {/}
- deps : @{Dependency} = @{}
- if lines := file:by_line():
+ deps : @{Dependency} = @{/}
+ if lines := file.by_line():
for line in lines:
- if line:matches_pattern($Pat/use {..}.tm/):
- file_import := Path.from_text(line:replace_pattern($Pat/use {..}/, "\1")):resolved(relative_to=file)
- deps:add(Dependency.File(file_import))
- else if line:matches_pattern($Pat/use {id}/):
- module_name := line:replace_pattern($Pat/use {..}/, "\1")
- deps:add(Dependency.Module(module_name))
+ if line.matches_pattern($Pat/use {..}.tm/):
+ file_import := Path.from_text(line.replace_pattern($Pat/use {..}/, "\1")).resolved(relative_to=file)
+ deps.add(Dependency.File(file_import))
+ else if line.matches_pattern($Pat/use {id}/):
+ module_name := line.replace_pattern($Pat/use {..}/, "\1")
+ deps.add(Dependency.Module(module_name))
return deps[]
func _build_dependency_graph(dep:Dependency, dependencies:@{Dependency={Dependency}}):
- return if dependencies:has(dep)
+ return if dependencies.has(dep)
- dependencies[dep] = {} # Placeholder
+ dependencies[dep] = {/} # Placeholder
dep_deps := when dep is File(path):
_get_file_dependencies(path)
is Module(module):
dir := (~/.local/share/tomo/installed/$module)
- module_deps : @{Dependency} = @{}
- visited : @{Path} = @{}
- unvisited := @{f:resolved() for f in dir:files() if f:extension() == ".tm"}
+ module_deps : @{Dependency} = @{/}
+ visited : @{Path} = @{/}
+ unvisited := @{f.resolved() for f in dir.files() if f.extension() == ".tm"}
while unvisited.length > 0:
file := unvisited.items[-1]
- unvisited:remove(file)
- visited:add(file)
+ unvisited.remove(file)
+ visited.add(file)
for file_dep in _get_file_dependencies(file):
when file_dep is File(f):
- if not visited:has(f):
- unvisited:add(f)
+ if not visited.has(f):
+ unvisited.add(f)
is Module(m):
- module_deps:add(file_dep)
+ module_deps.add(file_dep)
module_deps[]
dependencies[dep] = dep_deps
@@ -66,19 +66,19 @@ func _printable_name(dep:Dependency -> Text):
when dep is Module(module):
return "$(\x1b)[34;1m$module$(\x1b)[m"
is File(f):
- f = f:relative_to((.))
- if f:exists():
+ f = f.relative_to((.))
+ if f.exists():
return Text(f)
else:
return "$(\x1b)[31;1m$(f) (not found)$(\x1b)[m"
func _draw_tree(dep:Dependency, dependencies:{Dependency={Dependency}}, already_printed:@{Dependency}, prefix="", is_last=yes):
- if already_printed:has(dep):
+ if already_printed.has(dep):
say(prefix ++ (if is_last: "└── " else: "├── ") ++ _printable_name(dep) ++ " $\x1b[2m(recursive)$\x1b[m")
return
say(prefix ++ (if is_last: "└── " else: "├── ") ++ _printable_name(dep))
- already_printed:add(dep)
+ already_printed.add(dep)
child_prefix := prefix ++ (if is_last: " " else: "│ ")
@@ -88,9 +88,9 @@ func _draw_tree(dep:Dependency, dependencies:{Dependency={Dependency}}, already_
_draw_tree(child, dependencies, already_printed, child_prefix, is_child_last)
func draw_tree(dep:Dependency, dependencies:{Dependency={Dependency}}):
- printed : @{Dependency} = @{}
+ printed : @{Dependency} = @{/}
say(_printable_name(dep))
- printed:add(dep)
+ printed.add(dep)
deps := dependencies[dep] or {/}
for i,child in deps.items:
is_child_last := (i == deps.length)
@@ -104,11 +104,11 @@ func main(files:[Text]):
")
for arg in files:
- if arg:matches_pattern($Pat/{..}.tm/):
- path := Path.from_text(arg):resolved()
+ if arg.matches_pattern($Pat/{..}.tm/):
+ path := Path.from_text(arg).resolved()
dependencies := get_dependency_graph(File(path))
draw_tree(File(path), dependencies)
- else if arg:matches_pattern($Pat/{id}/):
+ else if arg.matches_pattern($Pat/{id}/):
dependencies := get_dependency_graph(Module(arg))
draw_tree(Module(arg), dependencies)
else: