diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-08-31 18:03:05 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-08-31 18:03:05 -0400 |
| commit | a0ac652cd1eebdc42425b34f1685f8cb20cb4eea (patch) | |
| tree | b41c99f8f0fa62eb03d1f5df44d2c501cd5f2976 /examples | |
| parent | a571ccffd795a595e990a3405dcf977aafc33c6c (diff) | |
Simplify quotes by limiting to `,',"
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/colorful/colorful.tm | 12 | ||||
| -rw-r--r-- | examples/coroutines/coroutines.tm | 20 | ||||
| -rw-r--r-- | examples/game/player.tm | 8 | ||||
| -rw-r--r-- | examples/http-server/http-server.tm | 18 | ||||
| -rw-r--r-- | examples/http/http.tm | 50 | ||||
| -rw-r--r-- | examples/ini/ini.tm | 14 | ||||
| -rw-r--r-- | examples/log/log.tm | 4 | ||||
| -rw-r--r-- | examples/wrap/wrap.tm | 8 |
8 files changed, 68 insertions, 66 deletions
diff --git a/examples/colorful/colorful.tm b/examples/colorful/colorful.tm index 5b01cfd5..8a1d46b0 100644 --- a/examples/colorful/colorful.tm +++ b/examples/colorful/colorful.tm @@ -45,18 +45,18 @@ func main(texts:[Text], files:[Path]=[], by_line=no) func _for_terminal(c:Colorful, state:_TermState -> Text) - return c.text.map_pattern(recursive=no, $Pat/@(?)/, func(m:PatternMatch) _add_ansi_sequences(m.captures[1], state)) + return c.text.map_pattern(recursive=no, $Pat"@(?)", func(m:PatternMatch) _add_ansi_sequences(m.captures[1], state)) enum _Color(Default, Bright(color:Int16), Color8Bit(color:Int16), Color24Bit(color:Int32)) func from_text(text:Text -> _Color?) - if text.matches_pattern($Pat/#{3-6 hex}/) + if text.matches_pattern($Pat"#{3-6 hex}") hex := text.from(2) return none unless hex.length == 3 or hex.length == 6 if hex.length == 3 hex = hex[1]++hex[1]++hex[2]++hex[2]++hex[3]++hex[3] n := Int32.parse("0x" ++ hex) or return none return Color24Bit(n) - else if text.matches_pattern($Pat/{1-3 digit}/) + else if text.matches_pattern($Pat"{1-3 digit}") n := Int16.parse(text) or return none if n >= 0 and n <= 255 return Color8Bit(n) else if text == "black" return _Color.Color8Bit(0) @@ -171,10 +171,10 @@ func _add_ansi_sequences(text:Text, prev_state:_TermState -> Text) else if text == "rparen" return ")" else if text == "@" or text == "at" return "@" parts := ( - text.pattern_captures($Pat/{0+..}:{0+..}/) or + text.pattern_captures($Pat"{0+..}:{0+..}") or return "@("++_for_terminal(Colorful.from_text(text), prev_state)++")" ) - attributes := parts[1].split_pattern($Pat/{0+space},{0+space}/) + attributes := parts[1].split_pattern($Pat"{0+space},{0+space}") new_state := prev_state for attr in attributes if attr.starts_with("fg=") @@ -215,6 +215,6 @@ func _add_ansi_sequences(text:Text, prev_state:_TermState -> Text) fail("Invalid attribute: '$attr'") result := prev_state.apply(new_state) - result ++= parts[2].map_pattern(recursive=no, $Pat/@(?)/, func(m:PatternMatch) _add_ansi_sequences(m.captures[1], new_state)) + result ++= parts[2].map_pattern(recursive=no, $Pat"@(?)", func(m:PatternMatch) _add_ansi_sequences(m.captures[1], new_state)) result ++= new_state.apply(prev_state) return result diff --git a/examples/coroutines/coroutines.tm b/examples/coroutines/coroutines.tm index b530a685..c4ef1a97 100644 --- a/examples/coroutines/coroutines.tm +++ b/examples/coroutines/coroutines.tm @@ -37,31 +37,31 @@ struct Coroutine(co:@aco_t) main_co := _main_co shared_stack := _shared_stack - aco_ptr := C_code:@aco_t( + aco_ptr := C_code:@aco_t ` aco_create(@main_co, @shared_stack, 0, (void*)@fn.fn, @fn.userdata) - ) + ` return Coroutine(aco_ptr) func is_finished(co:Coroutine->Bool; inline) - return C_code:Bool(((aco_t*)@co.co)->is_finished) + return C_code:Bool`((aco_t*)@co.co)->is_finished` func resume(co:Coroutine->Bool) if co.is_finished() return no - C_code { aco_resume(@co.co); } + C_code `aco_resume(@co.co);` return yes func _init() - C_code { + C_code ` aco_set_allocator(GC_malloc, NULL); aco_thread_init(aco_exit_fn); - } - _main_co = C_code:@aco_t(aco_create(NULL, NULL, 0, NULL, NULL)) + ` + _main_co = C_code:@aco_t`aco_create(NULL, NULL, 0, NULL, NULL)` - _shared_stack = C_code:@aco_shared_stack_t(aco_shared_stack_new(0)) + _shared_stack = C_code:@aco_shared_stack_t`aco_shared_stack_new(0)` func yield(; inline) - C_code { + C_code ` aco_yield(); - } + ` diff --git a/examples/game/player.tm b/examples/game/player.tm index 2e5e54f6..b34eadd0 100644 --- a/examples/game/player.tm +++ b/examples/game/player.tm @@ -10,12 +10,12 @@ struct Player(pos,prev_pos:Vector2) COLOR := Color(0x60, 0x60, 0xbF) func update(p:@Player) - target_x := C_code:Num32( + target_x := C_code:Num32` (Num32_t)((IsKeyDown(KEY_A) ? -1 : 0) + (IsKeyDown(KEY_D) ? 1 : 0)) - ) - target_y := C_code:Num32( + ` + target_y := C_code:Num32` (Num32_t)((IsKeyDown(KEY_W) ? -1 : 0) + (IsKeyDown(KEY_S) ? 1 : 0)) - ) + ` target_vel := Vector2(target_x, target_y).norm() * Player.WALK_SPEED vel := (p.pos - p.prev_pos)/World.DT diff --git a/examples/http-server/http-server.tm b/examples/http-server/http-server.tm index 8e8aff7e..dbe57805 100644 --- a/examples/http-server/http-server.tm +++ b/examples/http-server/http-server.tm @@ -22,7 +22,7 @@ func serve(port:Int32, handler:func(request:HTTPRequest -> HTTPResponse), num_th workers.insert(pthread_t.new(func() repeat connection := connections.dequeue() - request_text := C_code:Text( + request_text := C_code:Text``` Text_t request = EMPTY_TEXT; char buf[1024] = {}; for (ssize_t n; (n = read(@connection, buf, sizeof(buf) - 1)) > 0; ) { @@ -32,20 +32,20 @@ func serve(port:Int32, handler:func(request:HTTPRequest -> HTTPResponse), num_th break; } request - ) + ``` request := HTTPRequest.from_text(request_text) or skip response := handler(request).bytes() - C_code { + C_code ` if (@response.stride != 1) List$compact(&@response, 1); write(@connection, @response.data, @response.length); close(@connection); - } + ` )) - sock := C_code:Int32( + sock := C_code:Int32 ` int s = socket(AF_INET, SOCK_STREAM, 0); if (s < 0) err(1, "Couldn't connect to socket!"); @@ -60,10 +60,10 @@ func serve(port:Int32, handler:func(request:HTTPRequest -> HTTPResponse), num_th err(1, "Couldn't listen on socket"); s - ) + ` repeat - conn := C_code:Int32(accept(@sock, NULL, NULL)) + conn := C_code:Int32`accept(@sock, NULL, NULL)` stop if conn < 0 connections.enqueue(conn) @@ -77,8 +77,8 @@ struct HTTPRequest(method:Text, path:Text, version:Text, headers:[Text], body:Te method := m[1] path := m[2].replace_pattern($Pat'{2+ /}', '/') version := m[3] - rest := m[-1].pattern_captures($Pat/{..}{2 crlf}{0+ ..}/) or return none - headers := rest[1].split_pattern($Pat/{crlf}/) + rest := m[-1].pattern_captures($Pat'{..}{2 crlf}{0+ ..}') or return none + headers := rest[1].split_pattern($Pat'{crlf}') body := rest[-1] return HTTPRequest(method, path, version, headers, body) diff --git a/examples/http/http.tm b/examples/http/http.tm index 3fe41ae2..8d111904 100644 --- a/examples/http/http.tm +++ b/examples/http/http.tm @@ -10,73 +10,73 @@ enum _Method(GET, POST, PUT, PATCH, DELETE) func _send(method:_Method, url:Text, data:Text?, headers:[Text]=[] -> HTTPResponse) chunks : @[Text] save_chunk := func(chunk:CString, size:Int64, n:Int64) - chunks.insert(C_code:Text(Text$from_strn(@chunk, @size*@n))) + chunks.insert(C_code:Text`Text$from_strn(@chunk, @size*@n)`) return n*size - C_code { + C_code ` CURL *curl = curl_easy_init(); struct curl_slist *chunk = NULL; curl_easy_setopt(curl, CURLOPT_URL, @(CString(url))); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, @save_chunk.fn); curl_easy_setopt(curl, CURLOPT_WRITEDATA, @save_chunk.userdata); - } + ` defer - C_code { + C_code ` if (chunk) curl_slist_free_all(chunk); curl_easy_cleanup(curl); - } + ` when method is POST - C_code { + C_code ` curl_easy_setopt(curl, CURLOPT_POST, 1L); - } + ` if posting := data - C_code { + C_code ` curl_easy_setopt(curl, CURLOPT_POSTFIELDS, @(CString(posting))); - } + ` is PUT - C_code { + C_code ` curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT"); - } + ` if putting := data - C_code { + C_code ` curl_easy_setopt(curl, CURLOPT_POSTFIELDS, @(CString(putting))); - } + ` is PATCH - C_code { + C_code ` curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PATCH"); - } + ` if patching := data - C_code { + C_code ` curl_easy_setopt(curl, CURLOPT_POSTFIELDS, @(CString(patching))); - } + ` is DELETE - C_code { + C_code ` curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE"); - } + ` else pass for header in headers - C_code { + C_code ` chunk = curl_slist_append(chunk, @(CString(header))); - } + ` - C_code { + C_code ` if (chunk) curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk); - } + ` code := Int64(0) - C_code { + C_code ``` CURLcode res = curl_easy_perform(curl); if (res != CURLE_OK) fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &@code); - } + ``` return HTTPResponse(Int(code), "".join(chunks)) func get(url:Text, headers:[Text]=[] -> HTTPResponse) diff --git a/examples/ini/ini.tm b/examples/ini/ini.tm index 4dc27725..576d273f 100644 --- a/examples/ini/ini.tm +++ b/examples/ini/ini.tm @@ -15,24 +15,24 @@ func parse_ini(path:Path -> {Text={Text=Text}}) current_section : @{Text=Text} # Line wraps: - text = text.replace_pattern($Pat/\\{1 nl}{0+space}/, " ") + text = text.replace_pattern($Pat`\\{1 nl}{0+space}`, " ") for line in text.lines() line = line.trim() skip if line.starts_with(";") or line.starts_with("#") - if line.matches_pattern($Pat/[?]/) - section_name := line.replace($Pat/[?]/, "@1").trim().lower() + if line.matches_pattern($Pat`[?]`) + section_name := line.replace($Pat`[?]`, "@1").trim().lower() current_section = @{} sections[section_name] = current_section - else if line.matches_pattern($Pat/{..}={..}/) - key := line.replace_pattern($Pat/{..}={..}/, "@1").trim().lower() - value := line.replace_pattern($Pat/{..}={..}/, "@2").trim() + else if line.matches_pattern($Pat`{..}={..}`) + key := line.replace_pattern($Pat`{..}={..}`, "@1").trim().lower() + value := line.replace_pattern($Pat`{..}={..}`, "@2").trim() current_section[key] = value return {k=v[] for k,v in sections[]} func main(path:Path, key:Text?) - keys := (key or "").split($|/|) + keys := (key or "").split(`/`) if keys.length > 2 exit(" Too many arguments! diff --git a/examples/log/log.tm b/examples/log/log.tm index 4b7893fd..2798b7ae 100644 --- a/examples/log/log.tm +++ b/examples/log/log.tm @@ -6,13 +6,13 @@ timestamp_format := CString("%F %T") logfiles : @|Path| func _timestamp(->Text) - c_str := C_code:CString( + c_str := C_code:CString` char *str = GC_MALLOC_ATOMIC(20); time_t t; time(&t); struct tm *tm_info = localtime(&t); strftime(str, 20, "%F %T", tm_info); str - ) + ` return c_str.as_text() func info(text:Text, newline=yes) diff --git a/examples/wrap/wrap.tm b/examples/wrap/wrap.tm index bae01739..402d22af 100644 --- a/examples/wrap/wrap.tm +++ b/examples/wrap/wrap.tm @@ -1,3 +1,5 @@ +use patterns + HELP := " wrap: A tool for wrapping lines of text @@ -15,7 +17,7 @@ UNICODE_HYPHEN := "\{hyphen}" func unwrap(text:Text, preserve_paragraphs=yes, hyphen=UNICODE_HYPHEN -> Text) if preserve_paragraphs - paragraphs := text.split($/{2+ nl}/) + paragraphs := text.split_pattern($Pat"{2+ nl}") if paragraphs.length > 1 return "\n\n".join([unwrap(p, hyphen=hyphen, preserve_paragraphs=no) for p in paragraphs]) @@ -35,7 +37,7 @@ func wrap(text:Text, width:Int, min_split=3, hyphen="-" -> Text) lines : @[Text] line := "" - for word in text.split($/{whitespace}/) + for word in text.split_pattern($Pat"{whitespace}") letters := word.split() skip if letters.length == 0 @@ -94,7 +96,7 @@ func main(files:[Path], width=80, inplace=no, min_split=3, rewrap=yes, hyphen=UN first := yes wrapped_paragraphs : @[Text] - for paragraph in text.split($/{2+ nl}/) + for paragraph in text.split_pattern($Pat"{2+ nl}") wrapped_paragraphs.insert( wrap(paragraph, width=width, min_split=min_split, hyphen=hyphen) ) |
