diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-04-04 17:06:09 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-04-04 17:06:09 -0400 |
| commit | 0b8074154e2671691050bdb3bcb33245625a056c (patch) | |
| tree | 1410e0c4e05c6372e876cd08f16d117e12868f41 /examples | |
| parent | fadcb45baf1274e06cfe37b87655b9146aa52874 (diff) | |
First working compile of refactor to add explicit typing to declarations
and support untyped empty collections and `none`s
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/colorful/colorful.tm | 4 | ||||
| -rw-r--r-- | examples/commands/commands.tm | 12 | ||||
| -rw-r--r-- | examples/game/game.tm | 2 | ||||
| -rw-r--r-- | examples/game/world.tm | 2 | ||||
| -rw-r--r-- | examples/http-server/connection-queue.tm | 2 | ||||
| -rw-r--r-- | examples/http-server/http-server.tm | 6 | ||||
| -rw-r--r-- | examples/http/http.tm | 6 | ||||
| -rw-r--r-- | examples/ini/ini.tm | 6 | ||||
| -rw-r--r-- | examples/learnxiny.tm | 4 | ||||
| -rw-r--r-- | examples/log/log.tm | 2 | ||||
| -rw-r--r-- | examples/pthreads/pthreads.tm | 2 | ||||
| -rw-r--r-- | examples/random/README.md | 2 | ||||
| -rw-r--r-- | examples/random/random.tm | 6 | ||||
| -rw-r--r-- | examples/shell/shell.tm | 4 | ||||
| -rw-r--r-- | examples/tomo-install/tomo-install.tm | 4 | ||||
| -rw-r--r-- | examples/tomodeps/tomodeps.tm | 18 | ||||
| -rw-r--r-- | examples/wrap/wrap.tm | 4 |
17 files changed, 43 insertions, 43 deletions
diff --git a/examples/colorful/colorful.tm b/examples/colorful/colorful.tm index 78a831c8..57a2a3d6 100644 --- a/examples/colorful/colorful.tm +++ b/examples/colorful/colorful.tm @@ -24,7 +24,7 @@ lang Colorful: say(c:for_terminal(), newline=newline) -func main(texts:[Text], files=[:Path], by_line=no): +func main(texts:[Text], files:[Path]=[], by_line=no): for i,text in texts: colorful := Colorful.from_text(text) colorful:print(newline=no) @@ -141,7 +141,7 @@ struct _TermState( ): func apply(old,new:_TermState -> Text): - sequences := &[:Text] + sequences : &[Text] = &[] _toggle2(sequences, old.bold, old.dim, new.bold, new.dim, "1", "2", "22") _toggle2(sequences, old.italic, old.fraktur, new.italic, new.fraktur, "3", "20", "23") _toggle(sequences, old.underline, new.underline, "4", "24") diff --git a/examples/commands/commands.tm b/examples/commands/commands.tm index cbf5439b..ddd04d7b 100644 --- a/examples/commands/commands.tm +++ b/examples/commands/commands.tm @@ -46,16 +46,16 @@ struct ProgramResult(stdout:[Byte], stderr:[Byte], exit_type:ExitType): else: return no -struct Command(command:Text, args=[:Text], env={:Text=Text}): - func from_path(path:Path, args=[:Text], env={:Text=Text} -> Command): +struct Command(command:Text, args:[Text]=[], env:{Text=Text}={}): + func from_path(path:Path, args:[Text]=[], env:{Text=Text}={} -> Command): return Command(Text(path), args, env) - func result(command:Command, input="", input_bytes=[:Byte] -> ProgramResult): + func result(command:Command, input="", input_bytes:[Byte]=[] -> ProgramResult): if input.length > 0: (&input_bytes):insert_all(input:bytes()) - stdout := [:Byte] - stderr := [:Byte] + stdout : [Byte] = [] + stderr : [Byte] = [] status := run_command(command.command, command.args, command.env, input_bytes, &stdout, &stderr) if inline C : Bool { WIFEXITED(_$status) }: @@ -80,7 +80,7 @@ struct Command(command:Text, args=[:Text], env={:Text=Text}): func get_output(command:Command, input="", trim_newline=yes -> Text?): return command:result(input=input):output_text(trim_newline=trim_newline) - func get_output_bytes(command:Command, input="", input_bytes=[:Byte] -> [Byte]?): + func get_output_bytes(command:Command, input="", input_bytes:[Byte]=[] -> [Byte]?): result := command:result(input=input, input_bytes=input_bytes) when result.exit_type is Exited(status): if status == 0: return result.stdout diff --git a/examples/game/game.tm b/examples/game/game.tm index b034f68b..36ef14ff 100644 --- a/examples/game/game.tm +++ b/examples/game/game.tm @@ -10,7 +10,7 @@ func main(map=(./map.txt)): world := @World( player=@Player(Vector2(0,0), Vector2(0,0)), goal=@Box(Vector2(0,0), Vector2(50,50), color=Color(0x10,0xa0,0x10)), - boxes=@[:@Box], + boxes=@[], ) world:load_map(map_contents) diff --git a/examples/game/world.tm b/examples/game/world.tm index 809f1f80..8b721c5c 100644 --- a/examples/game/world.tm +++ b/examples/game/world.tm @@ -70,7 +70,7 @@ struct World(player:@Player, goal:@Box, boxes:@[@Box], dt_accum=Num32(0.0), won= func load_map(w:@World, map:Text): if map:has("[]"): map = map:translate({"[]"="#", "@ "="@", " "=" "}) - w.boxes = @[:@Box] + w.boxes = @[] box_size := Vector2(50., 50.) for y,line in map:lines(): for x,cell in line:split(): diff --git a/examples/http-server/connection-queue.tm b/examples/http-server/connection-queue.tm index a198f091..362dab7b 100644 --- a/examples/http-server/connection-queue.tm +++ b/examples/http-server/connection-queue.tm @@ -3,7 +3,7 @@ use pthreads func _assert_success(name:Text, val:Int32; inline): fail("$name() failed!") if val < 0 -struct ConnectionQueue(_connections=@[:Int32], _mutex=pthread_mutex_t.new(), _cond=pthread_cond_t.new()): +struct ConnectionQueue(_connections:@[Int32]=@[], _mutex=pthread_mutex_t.new(), _cond=pthread_cond_t.new()): func enqueue(queue:ConnectionQueue, connection:Int32): queue._mutex:lock() queue._connections:insert(connection) diff --git a/examples/http-server/http-server.tm b/examples/http-server/http-server.tm index f7338b91..56ba3683 100644 --- a/examples/http-server/http-server.tm +++ b/examples/http-server/http-server.tm @@ -17,7 +17,7 @@ use ./connection-queue.tm func serve(port:Int32, handler:func(request:HTTPRequest -> HTTPResponse), num_threads=16): connections := ConnectionQueue() - workers := &[:@pthread_t] + workers : &[@pthread_t] = &[] for i in num_threads: workers:insert(pthread_t.new(func(): repeat: @@ -82,7 +82,7 @@ struct HTTPRequest(method:Text, path:Text, version:Text, headers:[Text], body:Te body := rest[-1] return HTTPRequest(method, path, version, headers, body) -struct HTTPResponse(body:Text, status=200, content_type="text/plain", headers={:Text=Text}): +struct HTTPResponse(body:Text, status=200, content_type="text/plain", headers:{Text=Text}={}): func bytes(r:HTTPResponse -> [Byte]): body_bytes := r.body:bytes() extra_headers := (++: "$k: $v$(\r\n)" for k,v in r.headers) or "" @@ -114,7 +114,7 @@ enum RouteEntry(ServeFile(file:Path), Redirect(destination:Text)): return HTTPResponse("Found", 302, headers={"Location"=destination}) func load_routes(directory:Path -> {Text=RouteEntry}): - routes := &{:Text=RouteEntry} + routes : &{Text=RouteEntry} = &{} for file in (directory ++ (./*)):glob(): skip unless file:is_file() contents := file:read() or skip diff --git a/examples/http/http.tm b/examples/http/http.tm index 12203faf..ee1b8c75 100644 --- a/examples/http/http.tm +++ b/examples/http/http.tm @@ -7,8 +7,8 @@ struct HTTPResponse(code:Int, body:Text) enum _Method(GET, POST, PUT, PATCH, DELETE) -func _send(method:_Method, url:Text, data:Text?, headers=[:Text] -> HTTPResponse): - chunks := @[:Text] +func _send(method:_Method, url:Text, data:Text?, headers:[Text]=[] -> HTTPResponse): + chunks : @[Text] = @[] save_chunk := func(chunk:CString, size:Int64, n:Int64): chunks:insert(inline C:Text { Text$format("%.*s", _$size*_$n, _$chunk) @@ -81,7 +81,7 @@ func _send(method:_Method, url:Text, data:Text?, headers=[:Text] -> HTTPResponse } return HTTPResponse(Int(code), "":join(chunks)) -func get(url:Text, headers=[:Text] -> HTTPResponse): +func get(url:Text, headers:[Text]=[] -> HTTPResponse): return _send(GET, url, none, headers) func post(url:Text, data="", headers=["Content-Type: application/json", "Accept: application/json"] -> HTTPResponse): diff --git a/examples/ini/ini.tm b/examples/ini/ini.tm index 1e8e015f..d67dd0d5 100644 --- a/examples/ini/ini.tm +++ b/examples/ini/ini.tm @@ -11,8 +11,8 @@ _HELP := " func parse_ini(path:Path -> {Text={Text=Text}}): text := path:read() or exit("Could not read INI file: $\[31;1]$(path)$\[]") - sections := @{:Text=@{Text=Text}} - current_section := @{:Text=Text} + sections : @{Text=@{Text=Text}} = @{} + current_section : @{Text=Text} = @{} # Line wraps: text = text:replace_pattern($Pat/\{1 nl}{0+space}/, " ") @@ -22,7 +22,7 @@ func parse_ini(path:Path -> {Text={Text=Text}}): skip if line:starts_with(";") or line:starts_with("#") if line:matches_pattern($Pat/[?]/): section_name := line:replace($Pat/[?]/, "\1"):trim():lower() - current_section = @{:Text=Text} + current_section = @{} sections[section_name] = current_section else if line:matches_pattern($Pat/{..}={..}/): key := line:replace_pattern($Pat/{..}={..}/, "\1"):trim():lower() diff --git a/examples/learnxiny.tm b/examples/learnxiny.tm index a394e581..b585a2ab 100644 --- a/examples/learnxiny.tm +++ b/examples/learnxiny.tm @@ -59,7 +59,7 @@ func main(): my_numbers := [10, 20, 30] # Empty arrays require specifying the type: - empty_array := [:Int] + empty_array : [Int] = [] >> empty_array.length = 0 @@ -123,7 +123,7 @@ func main(): = 0 # Empty tables require specifying the key and value types: - empty_table := {:Text=Int} + empty_table : {Text=Int} = {} # Tables can be iterated over either by key or key,value: for key in table: diff --git a/examples/log/log.tm b/examples/log/log.tm index 5e32a2b9..f4b0b393 100644 --- a/examples/log/log.tm +++ b/examples/log/log.tm @@ -3,7 +3,7 @@ use <stdio.h> timestamp_format := CString("%F %T") -logfiles := @{:Path} +logfiles : @{Path} = @{} func _timestamp(->Text): c_str := inline C:CString { diff --git a/examples/pthreads/pthreads.tm b/examples/pthreads/pthreads.tm index 975b981d..fb79e822 100644 --- a/examples/pthreads/pthreads.tm +++ b/examples/pthreads/pthreads.tm @@ -65,7 +65,7 @@ struct pthread_t(; extern, opaque): func detatch(p:pthread_t): inline C { pthread_detach(_$p); } struct IntQueue(_queue:@[Int], _mutex:@pthread_mutex_t, _cond:@pthread_cond_t): - func new(initial=[:Int] -> IntQueue): + func new(initial:[Int]=[] -> IntQueue): return IntQueue(@initial, pthread_mutex_t.new(), pthread_cond_t.new()) func give(q:IntQueue, n:Int): diff --git a/examples/random/README.md b/examples/random/README.md index 697f3f7c..6233c1ba 100644 --- a/examples/random/README.md +++ b/examples/random/README.md @@ -110,7 +110,7 @@ A copy of the given RNG. **Example:** ```tomo ->> rng := RNG.new([:Byte]) +>> rng := RNG.new([]) >> copy := rng:copy() >> rng:bytes(10) diff --git a/examples/random/random.tm b/examples/random/random.tm index 1d6e560b..0a0167ac 100644 --- a/examples/random/random.tm +++ b/examples/random/random.tm @@ -4,7 +4,7 @@ use ./sysrandom.h use ./chacha.h struct chacha_ctx(j0,j1,j2,j3,j4,j5,j6,j7,j8,j9,j10,j11,j12,j13,j14,j15:Int32; extern, secret): - func from_seed(seed=[:Byte] -> chacha_ctx): + func from_seed(seed:[Byte]=[] -> chacha_ctx): return inline C : chacha_ctx { chacha_ctx ctx; uint8_t seed_bytes[KEYSZ + IVSZ] = {}; @@ -24,10 +24,10 @@ func _os_random_bytes(count:Int64 -> [Byte]): (Array_t){.length=_$count, .data=random_bytes, .stride=1, .atomic=1}; } -struct RandomNumberGenerator(_chacha:chacha_ctx, _random_bytes=[:Byte]; secret): +struct RandomNumberGenerator(_chacha:chacha_ctx, _random_bytes:[Byte]=[]; secret): func new(seed=none:[Byte], -> @RandomNumberGenerator): ctx := chacha_ctx.from_seed(seed or _os_random_bytes(40)) - return @RandomNumberGenerator(ctx, [:Byte]) + return @RandomNumberGenerator(ctx, []) func _rekey(rng:&RandomNumberGenerator): rng._random_bytes = inline C : [Byte] { diff --git a/examples/shell/shell.tm b/examples/shell/shell.tm index 9ca9e059..dd26428f 100644 --- a/examples/shell/shell.tm +++ b/examples/shell/shell.tm @@ -24,7 +24,7 @@ lang Shell: func command(shell:Shell -> Command): return Command("sh", ["-c", shell.text]) - func result(shell:Shell, input="", input_bytes=[:Byte] -> ProgramResult): + func result(shell:Shell, input="", input_bytes:[Byte]=[] -> ProgramResult): return shell:command():result(input=input, input_bytes=input_bytes) func run(shell:Shell -> ExitType): @@ -33,7 +33,7 @@ lang Shell: func get_output(shell:Shell, input="", trim_newline=yes -> Text?): return shell:command():get_output(input=input, trim_newline=trim_newline) - func get_output_bytes(shell:Shell, input="", input_bytes=[:Byte] -> [Byte]?): + func get_output_bytes(shell:Shell, input="", input_bytes:[Byte]=[] -> [Byte]?): return shell:command():get_output_bytes(input=input, input_bytes=input_bytes) func by_line(shell:Shell -> func(->Text?)?): diff --git a/examples/tomo-install/tomo-install.tm b/examples/tomo-install/tomo-install.tm index e584fe66..c705af12 100644 --- a/examples/tomo-install/tomo-install.tm +++ b/examples/tomo-install/tomo-install.tm @@ -11,7 +11,7 @@ _HELP := " " func find_urls(path:Path -> [Text]): - urls := @[:Text] + urls : @[Text] = @[] if path:is_directory(): for f in path:children(): urls:insert_all(find_urls(f)) @@ -25,7 +25,7 @@ func main(paths:[Path]): if paths.length == 0: paths = [(./)] - urls := (++: find_urls(p) for p in paths) or [:Text] + urls := (++: find_urls(p) for p in paths) or [] github_token := (~/.config/tomo/github-token):read() diff --git a/examples/tomodeps/tomodeps.tm b/examples/tomodeps/tomodeps.tm index 96838a66..8da64eb2 100644 --- a/examples/tomodeps/tomodeps.tm +++ b/examples/tomodeps/tomodeps.tm @@ -14,9 +14,9 @@ enum Dependency(File(path:Path), Module(name:Text)) func _get_file_dependencies(file:Path -> {Dependency}): if not file:is_file(): !! Could not read file: $file - return {:Dependency} + return {} - deps := @{:Dependency} + deps : @{Dependency} = @{} if lines := file:by_line(): for line in lines: if line:matches_pattern($Pat/use {..}.tm/): @@ -30,14 +30,14 @@ func _get_file_dependencies(file:Path -> {Dependency}): func _build_dependency_graph(dep:Dependency, dependencies:@{Dependency,{Dependency}}): return if dependencies:has(dep) - dependencies[dep] = {:Dependency} # 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} + 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] @@ -58,7 +58,7 @@ func _build_dependency_graph(dep:Dependency, dependencies:@{Dependency,{Dependen _build_dependency_graph(dep2, dependencies) func get_dependency_graph(dep:Dependency -> {Dependency,{Dependency}}): - graph := @{:Dependency,{Dependency}} + graph : @{Dependency={Dependency}} = @{} _build_dependency_graph(dep, graph) return graph @@ -82,16 +82,16 @@ func _draw_tree(dep:Dependency, dependencies:{Dependency,{Dependency}}, already_ child_prefix := prefix ++ (if is_last: " " else: "│ ") - children := dependencies[dep] or {:Dependency} + children := dependencies[dep] or {} for i,child in children.items: is_child_last := (i == children.length) _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) - deps := dependencies[dep] or {:Dependency} + deps := dependencies[dep] or {} for i,child in deps.items: is_child_last := (i == deps.length) _draw_tree(child, dependencies, already_printed=printed, is_last=is_child_last) diff --git a/examples/wrap/wrap.tm b/examples/wrap/wrap.tm index c90713a9..61ca5821 100644 --- a/examples/wrap/wrap.tm +++ b/examples/wrap/wrap.tm @@ -33,7 +33,7 @@ func wrap(text:Text, width:Int, min_split=3, hyphen="-" -> Text): ... and I can't split it without splitting into chunks smaller than $min_split. ") - lines := @[:Text] + lines : @[Text] = @[] line := "" for word in text:split($/{whitespace}/): letters := word:split() @@ -93,7 +93,7 @@ func main(files:[Path], width=80, inplace=no, min_split=3, rewrap=yes, hyphen=UN (/dev/stdout) first := yes - wrapped_paragraphs := @[:Text] + wrapped_paragraphs : @[Text] = @[] for paragraph in text:split($/{2+ nl}/): wrapped_paragraphs:insert( wrap(paragraph, width=width, min_split=min_split, hyphen=hyphen) |
