diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/commands/commands.c | 56 | ||||
| -rw-r--r-- | examples/http-server/http-server.tm | 2 | ||||
| -rw-r--r-- | examples/learnxiny.tm | 24 | ||||
| -rw-r--r-- | examples/patterns/README.md | 10 | ||||
| -rw-r--r-- | examples/patterns/match_type.h | 2 | ||||
| -rw-r--r-- | examples/patterns/patterns.c | 58 | ||||
| -rw-r--r-- | examples/random/README.md | 8 | ||||
| -rw-r--r-- | examples/random/random.tm | 6 |
8 files changed, 83 insertions, 83 deletions
diff --git a/examples/commands/commands.c b/examples/commands/commands.c index f823eeb0..973fc0c5 100644 --- a/examples/commands/commands.c +++ b/examples/commands/commands.c @@ -15,8 +15,8 @@ #define READ_END 0 #define WRITE_END 1 -int run_command(Text_t exe, Array_t arg_array, Table_t env_table, - OptionalArray_t input_bytes, Array_t *output_bytes, Array_t *error_bytes) +int run_command(Text_t exe, List_t arg_list, Table_t env_table, + OptionalList_t input_bytes, List_t *output_bytes, List_t *error_bytes) { pthread_testcancel(); @@ -57,28 +57,28 @@ int run_command(Text_t exe, Array_t arg_array, Table_t env_table, const char *exe_str = Text$as_c_string(exe); - Array_t arg_strs = {}; - Array$insert_value(&arg_strs, exe_str, I(0), sizeof(char*)); - for (int64_t i = 0; i < arg_array.length; i++) - Array$insert_value(&arg_strs, Text$as_c_string(*(Text_t*)(arg_array.data + i*arg_array.stride)), I(0), sizeof(char*)); - Array$insert_value(&arg_strs, NULL, I(0), sizeof(char*)); + List_t arg_strs = {}; + List$insert_value(&arg_strs, exe_str, I(0), sizeof(char*)); + for (int64_t i = 0; i < arg_list.length; i++) + List$insert_value(&arg_strs, Text$as_c_string(*(Text_t*)(arg_list.data + i*arg_list.stride)), I(0), sizeof(char*)); + List$insert_value(&arg_strs, NULL, I(0), sizeof(char*)); char **args = arg_strs.data; extern char **environ; char **env = environ; if (env_table.entries.length > 0) { - Array_t env_array = {}; // Array of const char* + List_t env_list = {}; // List of const char* for (char **e = environ; *e; e++) - Array$insert(&env_array, e, I(0), sizeof(char*)); + List$insert(&env_list, e, I(0), sizeof(char*)); for (int64_t i = 0; i < env_table.entries.length; i++) { struct { Text_t key, value; } *entry = env_table.entries.data + env_table.entries.stride*i; const char *env_entry = String(entry->key, "=", entry->value); - Array$insert(&env_array, &env_entry, I(0), sizeof(char*)); + List$insert(&env_list, &env_entry, I(0), sizeof(char*)); } - Array$insert_value(&env_array, NULL, I(0), sizeof(char*)); - assert(env_array.stride == sizeof(char*)); - env = env_array.data; + List$insert_value(&env_list, NULL, I(0), sizeof(char*)); + assert(env_list.stride == sizeof(char*)); + env = env_list.data; } pid_t pid; @@ -101,11 +101,11 @@ int run_command(Text_t exe, Array_t arg_array, Table_t env_table, if (error_bytes) pollfds[2] = (struct pollfd){.fd=child_errpipe[WRITE_END], .events=POLLIN}; if (input_bytes.length > 0 && input_bytes.stride != 1) - Array$compact(&input_bytes, sizeof(char)); + List$compact(&input_bytes, sizeof(char)); if (output_bytes) - *output_bytes = (Array_t){.atomic=1, .stride=1, .length=0}; + *output_bytes = (List_t){.atomic=1, .stride=1, .length=0}; if (error_bytes) - *error_bytes = (Array_t){.atomic=1, .stride=1, .length=0}; + *error_bytes = (List_t){.atomic=1, .stride=1, .length=0}; while (input_bytes.length > 0 || output_bytes || error_bytes) { (void)poll(pollfds, sizeof(pollfds)/sizeof(pollfds[0]), -1); // Wait for data or readiness @@ -223,7 +223,7 @@ static Text_t _next_line(child_info_t *child) return line_text; } -OptionalClosure_t command_by_line(Text_t exe, Array_t arg_array, Table_t env_table) +OptionalClosure_t command_by_line(Text_t exe, List_t arg_list, Table_t env_table) { posix_spawnattr_t attr; posix_spawnattr_init(&attr); @@ -238,28 +238,28 @@ OptionalClosure_t command_by_line(Text_t exe, Array_t arg_array, Table_t env_tab const char *exe_str = Text$as_c_string(exe); - Array_t arg_strs = {}; - Array$insert_value(&arg_strs, exe_str, I(0), sizeof(char*)); - for (int64_t i = 0; i < arg_array.length; i++) - Array$insert_value(&arg_strs, Text$as_c_string(*(Text_t*)(arg_array.data + i*arg_array.stride)), I(0), sizeof(char*)); - Array$insert_value(&arg_strs, NULL, I(0), sizeof(char*)); + List_t arg_strs = {}; + List$insert_value(&arg_strs, exe_str, I(0), sizeof(char*)); + for (int64_t i = 0; i < arg_list.length; i++) + List$insert_value(&arg_strs, Text$as_c_string(*(Text_t*)(arg_list.data + i*arg_list.stride)), I(0), sizeof(char*)); + List$insert_value(&arg_strs, NULL, I(0), sizeof(char*)); char **args = arg_strs.data; extern char **environ; char **env = environ; if (env_table.entries.length > 0) { - Array_t env_array = {}; // Array of const char* + List_t env_list = {}; // List of const char* for (char **e = environ; *e; e++) - Array$insert(&env_array, e, I(0), sizeof(char*)); + List$insert(&env_list, e, I(0), sizeof(char*)); for (int64_t i = 0; i < env_table.entries.length; i++) { struct { Text_t key, value; } *entry = env_table.entries.data + env_table.entries.stride*i; const char *env_entry = String(entry->key, "=", entry->value); - Array$insert(&env_array, &env_entry, I(0), sizeof(char*)); + List$insert(&env_list, &env_entry, I(0), sizeof(char*)); } - Array$insert_value(&env_array, NULL, I(0), sizeof(char*)); - assert(env_array.stride == sizeof(char*)); - env = env_array.data; + List$insert_value(&env_list, NULL, I(0), sizeof(char*)); + assert(env_list.stride == sizeof(char*)); + env = env_list.data; } pid_t pid; diff --git a/examples/http-server/http-server.tm b/examples/http-server/http-server.tm index 5b73d1af..80774bff 100644 --- a/examples/http-server/http-server.tm +++ b/examples/http-server/http-server.tm @@ -38,7 +38,7 @@ func serve(port:Int32, handler:func(request:HTTPRequest -> HTTPResponse), num_th response := handler(request).bytes() C_code { if (@response.stride != 1) - Array$compact(&@response, 1); + List$compact(&@response, 1); write(@connection, @response.data, @response.length); close(@connection); } diff --git a/examples/learnxiny.tm b/examples/learnxiny.tm index b8eaa77e..9b9f1017 100644 --- a/examples/learnxiny.tm +++ b/examples/learnxiny.tm @@ -52,23 +52,23 @@ func main() else say("else") - # Arrays: + # Lists: my_numbers := [10, 20, 30] - # Empty arrays require specifying the type: - empty_array : [Int] - >> empty_array.length + # Empty lists require specifying the type: + empty_list : [Int] + >> empty_list.length = 0 - # Arrays are 1-indexed, so the first element is at index 1: + # Lists are 1-indexed, so the first element is at index 1: >> my_numbers[1] = 10 - # Negative indices can be used to get items from the back of the array: + # Negative indices can be used to get items from the back of the list: >> my_numbers[-1] = 30 - # If an invalid index outside the array's bounds is used (e.g. + # If an invalid index outside the list's bounds is used (e.g. # my_numbers[999]), an error message will be printed and the program will # exit. @@ -80,7 +80,7 @@ func main() for index, num in my_numbers pass # Pass means "do nothing" - # Arrays can be created with array comprehensions, which are loops: + # Lists can be created with list comprehensions, which are loops: >> [x*10 for x in my_numbers] = [100, 200, 300] >> [x*10 for x in my_numbers if x != 20] @@ -132,7 +132,7 @@ func main() pass # Tables also have ".keys" and ".values" fields to explicitly access the - # array of keys or values in the table. + # list of keys or values in the table. >> table.keys = ["one", "two"] >> table.values @@ -153,7 +153,7 @@ func main() # If no default is provided and a missing key is looked up, the program # will print an error message and halt. - # Any types can be used in tables, for example, a table mapping arrays to + # Any types can be used in tables, for example, a table mapping lists to # strings: table3 := {[10, 20]="one", [30, 40, 50]="two"} >> table3[[10, 20]]! @@ -241,10 +241,10 @@ func takes_many_types( integer:Int, floating_point_number:Num, text_aka_string:Text, - array_of_ints:[Int], + list_of_ints:[Int], table_of_text_to_bools:{Text=Bool}, set_of_ints:|Int|, - pointer_to_mutable_array_of_ints:@[Int], + pointer_to_mutable_list_of_ints:@[Int], optional_int:Int?, function_from_int_to_text:func(x:Int -> Text), ) diff --git a/examples/patterns/README.md b/examples/patterns/README.md index 8e2a1ff8..faf2854e 100644 --- a/examples/patterns/README.md +++ b/examples/patterns/README.md @@ -35,7 +35,7 @@ Pattern matching functions work with a type called `PatternMatch` that has three - `text`: The full text of the match. - `index`: The index in the text where the match was found. -- `captures`: An array containing the matching text of each non-literal pattern group. +- `captures`: A list containing the matching text of each non-literal pattern group. See [Text Functions](text.md#Text-Functions) for the full API documentation. @@ -229,7 +229,7 @@ func find_patterns(text:Text, pattern:Pat -> [PatternMatch]) - `pattern`: The pattern to match. **Returns:** -An array of `PatternMatch` objects. +A list of `PatternMatch` objects. **Example:** ```tomo @@ -314,7 +314,7 @@ func matches_pattern(text:Text, pattern:Pat -> Bool) --- ### `pattern_captures` -Returns an array of pattern captures for the given pattern. +Returns a list of pattern captures for the given pattern. ```tomo func pattern_captures(text:Text, pattern:Pat -> [Text]?) @@ -324,7 +324,7 @@ func pattern_captures(text:Text, pattern:Pat -> [Text]?) - `pattern`: The pattern to match. **Returns:** -An optional array of matched pattern captures. Returns `none` if the text does +An optional list of matched pattern captures. Returns `none` if the text does not match the pattern. **Example:** @@ -384,7 +384,7 @@ func split_pattern(text:Text, pattern:Pat -> [Text]) - `pattern`: The pattern to use as a separator. **Returns:** -An array of text segments. +A list of text segments. **Example:** ```tomo diff --git a/examples/patterns/match_type.h b/examples/patterns/match_type.h index 5d063431..abbc4fce 100644 --- a/examples/patterns/match_type.h +++ b/examples/patterns/match_type.h @@ -3,6 +3,6 @@ typedef struct { Text_t text; Int_t index; - Array_t captures; + List_t captures; } XMatch; diff --git a/examples/patterns/patterns.c b/examples/patterns/patterns.c index 180ab431..ee27e4e3 100644 --- a/examples/patterns/patterns.c +++ b/examples/patterns/patterns.c @@ -12,13 +12,13 @@ typedef struct { Text_t text; Int_t index; - Array_t captures; + List_t captures; } PatternMatch; typedef struct { Text_t text; Int_t index; - Array_t captures; + List_t captures; bool is_none:1; } OptionalPatternMatch; @@ -42,7 +42,7 @@ typedef struct { }; } pat_t; -static Text_t replace_array(Text_t text, Array_t replacements, Text_t backref_pat, bool recursive); +static Text_t replace_list(Text_t text, List_t replacements, Text_t backref_pat, bool recursive); static INLINE void skip_whitespace(TextIter_t *state, int64_t *i) { @@ -821,15 +821,15 @@ static OptionalPatternMatch find(Text_t text, Text_t pattern, Int_t from_index) if (found == -1) return NONE_MATCH; - Array_t capture_array = {}; + List_t capture_list = {}; for (int i = 0; captures[i].occupied; i++) { Text_t capture = Text$slice(text, I(captures[i].index+1), I(captures[i].index+captures[i].length)); - Array$insert(&capture_array, &capture, I(0), sizeof(Text_t)); + List$insert(&capture_list, &capture, I(0), sizeof(Text_t)); } return (OptionalPatternMatch){ .text=Text$slice(text, I(found+1), I(found+len)), .index=I(found+1), - .captures=capture_array, + .captures=capture_list, }; } @@ -858,33 +858,33 @@ static bool Pattern$matches(Text_t text, Text_t pattern) return (match_len == text.length); } -static OptionalArray_t Pattern$captures(Text_t text, Text_t pattern) +static OptionalList_t Pattern$captures(Text_t text, Text_t pattern) { capture_t captures[MAX_BACKREFS] = {}; int64_t match_len = match(text, 0, pattern, 0, captures, 0); if (match_len != text.length) - return NONE_ARRAY; + return NONE_LIST; - Array_t capture_array = {}; + List_t capture_list = {}; for (int i = 0; captures[i].occupied; i++) { Text_t capture = Text$slice(text, I(captures[i].index+1), I(captures[i].index+captures[i].length)); - Array$insert(&capture_array, &capture, I(0), sizeof(Text_t)); + List$insert(&capture_list, &capture, I(0), sizeof(Text_t)); } - return capture_array; + return capture_list; } -static Array_t Pattern$find_all(Text_t text, Text_t pattern) +static List_t Pattern$find_all(Text_t text, Text_t pattern) { if (pattern.length == 0) // special case - return (Array_t){.length=0}; + return (List_t){.length=0}; - Array_t matches = {}; + List_t matches = {}; for (int64_t i = 1; ; ) { OptionalPatternMatch m = find(text, pattern, I(i)); if (m.is_none) break; i = Int64$from_int(m.index, false) + m.text.length; - Array$insert(&matches, &m, I_small(0), sizeof(PatternMatch)); + List$insert(&matches, &m, I_small(0), sizeof(PatternMatch)); } return matches; } @@ -916,7 +916,7 @@ static Closure_t Pattern$by_match(Text_t text, Text_t pattern) }; } -static Text_t apply_backrefs(Text_t text, Array_t recursive_replacements, Text_t replacement, Text_t backref_pat, capture_t *captures) +static Text_t apply_backrefs(Text_t text, List_t recursive_replacements, Text_t replacement, Text_t backref_pat, capture_t *captures) { if (backref_pat.length == 0) return replacement; @@ -960,7 +960,7 @@ static Text_t apply_backrefs(Text_t text, Array_t recursive_replacements, Text_t Text_t backref_text = Text$slice(text, I(captures[backref].index+1), I(captures[backref].index + captures[backref].length)); if (captures[backref].recursive && recursive_replacements.length > 0) - backref_text = replace_array(backref_text, recursive_replacements, backref_pat, true); + backref_text = replace_list(backref_text, recursive_replacements, backref_pat, true); if (pos > nonmatching_pos) { Text_t before_slice = Text$slice(replacement, I(nonmatching_pos+1), I(pos)); @@ -989,7 +989,7 @@ static Text_t Pattern$replace(Text_t text, Text_t pattern, Text_t replacement, T && !uc_is_property((ucs4_t)first_grapheme, UC_PROPERTY_PAIRED_PUNCTUATION)); Text_t entries[2] = {pattern, replacement}; - Array_t replacements = { + List_t replacements = { .data=entries, .length=1, .stride=sizeof(entries), @@ -1015,7 +1015,7 @@ static Text_t Pattern$replace(Text_t text, Text_t pattern, Text_t replacement, T .occupied = true, .recursive = false, }; - Text_t replacement_text = apply_backrefs(text, recursive ? replacements : (Array_t){}, replacement, backref_pat, captures); + Text_t replacement_text = apply_backrefs(text, recursive ? replacements : (List_t){}, replacement, backref_pat, captures); if (pos > nonmatching_pos) { Text_t before_slice = Text$slice(text, I(nonmatching_pos+1), I(pos)); ret = Text$concat(ret, before_slice, replacement_text); @@ -1084,7 +1084,7 @@ static Text_t Pattern$map(Text_t text, Text_t pattern, Closure_t fn, bool recurs Text_t capture = Text$slice(text, I(captures[i].index+1), I(captures[i].index+captures[i].length)); if (recursive) capture = Pattern$map(capture, pattern, fn, recursive); - Array$insert(&m.captures, &capture, I(0), sizeof(Text_t)); + List$insert(&m.captures, &capture, I(0), sizeof(Text_t)); } Text_t replacement = text_mapper(m, fn.userdata); @@ -1133,7 +1133,7 @@ static void Pattern$each(Text_t text, Text_t pattern, Closure_t fn, bool recursi Text_t capture = Text$slice(text, I(captures[i].index+1), I(captures[i].index+captures[i].length)); if (recursive) Pattern$each(capture, pattern, fn, recursive); - Array$insert(&m.captures, &capture, I(0), sizeof(Text_t)); + List$insert(&m.captures, &capture, I(0), sizeof(Text_t)); } action(m, fn.userdata); @@ -1141,7 +1141,7 @@ static void Pattern$each(Text_t text, Text_t pattern, Closure_t fn, bool recursi } } -Text_t replace_array(Text_t text, Array_t replacements, Text_t backref_pat, bool recursive) +Text_t replace_list(Text_t text, List_t replacements, Text_t backref_pat, bool recursive) { if (replacements.length == 0) return text; @@ -1166,7 +1166,7 @@ Text_t replace_array(Text_t text, Array_t replacements, Text_t backref_pat, bool // Concatenate the replacement: Text_t replacement = *(Text_t*)(replacements.data + i*replacements.stride + sizeof(Text_t)); - Text_t replacement_text = apply_backrefs(text, recursive ? replacements : (Array_t){}, replacement, backref_pat, captures); + Text_t replacement_text = apply_backrefs(text, recursive ? replacements : (List_t){}, replacement, backref_pat, captures); ret = Text$concat(ret, replacement_text); pos += MAX(len, 1); nonmatch_pos = pos; @@ -1187,18 +1187,18 @@ Text_t replace_array(Text_t text, Array_t replacements, Text_t backref_pat, bool static Text_t Pattern$replace_all(Text_t text, Table_t replacements, Text_t backref_pat, bool recursive) { - return replace_array(text, replacements.entries, backref_pat, recursive); + return replace_list(text, replacements.entries, backref_pat, recursive); } -static Array_t Pattern$split(Text_t text, Text_t pattern) +static List_t Pattern$split(Text_t text, Text_t pattern) { if (text.length == 0) // special case - return (Array_t){.length=0}; + return (List_t){.length=0}; if (pattern.length == 0) // special case return Text$clusters(text); - Array_t chunks = {}; + List_t chunks = {}; int64_t i = 0; for (;;) { @@ -1208,12 +1208,12 @@ static Array_t Pattern$split(Text_t text, Text_t pattern) found = _find(text, pattern, i + 1, text.length-1, &len, NULL); if (found < 0) break; Text_t chunk = Text$slice(text, I(i+1), I(found)); - Array$insert(&chunks, &chunk, I_small(0), sizeof(Text_t)); + List$insert(&chunks, &chunk, I_small(0), sizeof(Text_t)); i = MAX(found + len, i + 1); } Text_t last_chunk = Text$slice(text, I(i+1), I(text.length)); - Array$insert(&chunks, &last_chunk, I_small(0), sizeof(Text_t)); + List$insert(&chunks, &last_chunk, I_small(0), sizeof(Text_t)); return chunks; } diff --git a/examples/random/README.md b/examples/random/README.md index d9983ab7..183b9d0b 100644 --- a/examples/random/README.md +++ b/examples/random/README.md @@ -16,8 +16,8 @@ a Tomo program launches. ## RNG Functions This documentation provides details on RNG functions available in the API. -Arrays also have some methods which use RNG values: -`array.shuffle()`, `array.shuffled()`, `array.random()`, and `array.sample()`. +Lists also have some methods which use RNG values: +`list.shuffle()`, `list.shuffled()`, `list.random()`, and `list.sample()`. - [`func bool(rng: RNG, p: Num = 0.5 -> Bool)`](#bool) - [`func byte(rng: RNG -> Byte)`](#byte) @@ -75,7 +75,7 @@ A random byte (0-255). --- ### `bytes` -Generate an array of uniformly random bytes with the given length. +Generate a list of uniformly random bytes with the given length. ```tomo func bytes(rng: RNG, count: Int -> [Byte]) @@ -85,7 +85,7 @@ func bytes(rng: RNG, count: Int -> [Byte]) - `count`: The number of random bytes to return. **Returns:** -An array of length `count` random bytes with uniform random distribution (0-255). +A list of length `count` random bytes with uniform random distribution (0-255). **Example:** ```tomo diff --git a/examples/random/random.tm b/examples/random/random.tm index 13600b47..14b68d7f 100644 --- a/examples/random/random.tm +++ b/examples/random/random.tm @@ -21,7 +21,7 @@ func _os_random_bytes(count:Int64 -> [Byte]) return C_code:[Byte]( uint8_t *random_bytes = GC_MALLOC_ATOMIC(@count); getrandom(random_bytes, @count, 0); - (Array_t){.length=@count, .data=random_bytes, .stride=1, .atomic=1} + (List_t){.length=@count, .data=random_bytes, .stride=1, .atomic=1} ) struct RandomNumberGenerator(_chacha:chacha_ctx, _random_bytes:[Byte]=[]; secret) func new(seed:[Byte]?=none, -> @RandomNumberGenerator) @@ -36,7 +36,7 @@ struct RandomNumberGenerator(_chacha:chacha_ctx, _random_bytes:[Byte]=[]; secret // Immediately reinitialize for backtracking resistance chacha_keysetup(&@rng->_chacha, new_keystream); chacha_ivsetup(&@rng->_chacha, new_keystream + KEYSZ); - Array_t new_bytes = (Array_t){.data=GC_MALLOC_ATOMIC(1024), .length=1024, .stride=1, .atomic=1}; + List_t new_bytes = (List_t){.data=GC_MALLOC_ATOMIC(1024), .length=1024, .stride=1, .atomic=1}; memset(new_bytes.data, 0, new_bytes.length); chacha_encrypt_bytes(&@rng->_chacha, new_bytes.data, new_bytes.data, new_bytes.length); new_bytes @@ -65,7 +65,7 @@ struct RandomNumberGenerator(_chacha:chacha_ctx, _random_bytes:[Byte]=[]; secret count64 := Int64(count) buf := C_code:@Memory(GC_MALLOC_ATOMIC(@count64)) rng._fill_bytes(buf, count64) - return C_code:[Byte]((Array_t){.data=@buf, .stride=1, .atomic=1, .length=@count64}) + return C_code:[Byte]((List_t){.data=@buf, .stride=1, .atomic=1, .length=@count64}) func byte(rng:&RandomNumberGenerator -> Byte) byte : &Byte |
