aboutsummaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-04-06 23:37:05 -0400
committerBruce Hill <bruce@bruce-hill.com>2025-04-06 23:37:05 -0400
commit1a196aa8f724971e531487f9cdd541f7957cfd92 (patch)
tree52a36701065ab0e3f7012765c909c3b2a3fd2e49 /docs
parent4a3db447ce820617a72bdd9fc6217c84c3799bea (diff)
Update syntax in docs
Diffstat (limited to 'docs')
-rw-r--r--docs/command-line-parsing.md8
-rw-r--r--docs/enums.md18
-rw-r--r--docs/functions.md26
-rw-r--r--docs/integers.md2
-rw-r--r--docs/iterators.md10
-rw-r--r--docs/langs.md22
-rw-r--r--docs/libraries.md4
-rw-r--r--docs/lists.md4
-rw-r--r--docs/namespacing.md4
-rw-r--r--docs/nums.md2
-rw-r--r--docs/operators.md38
-rw-r--r--docs/optionals.md20
-rw-r--r--docs/paths.md8
-rw-r--r--docs/pointers.md8
-rw-r--r--docs/sets.md4
-rw-r--r--docs/structs.md6
-rw-r--r--docs/tables.md4
-rw-r--r--docs/text.md6
18 files changed, 97 insertions, 97 deletions
diff --git a/docs/command-line-parsing.md b/docs/command-line-parsing.md
index bd71ba41..2c1af0e6 100644
--- a/docs/command-line-parsing.md
+++ b/docs/command-line-parsing.md
@@ -5,10 +5,10 @@ Here's a simple example:
```tomo
# greet.tm
-func main(name:Text, be_excited=no):
+func main(name:Text, be_excited=no)
if be_excited
say("Hello $name!!!")
- else:
+ else
say("Hi $name.")
```
@@ -85,7 +85,7 @@ Parsing is case-insensitive:
```
# foo.tm
enum Foo(One, Two, Three)
-func main(foo:Foo):
+func main(foo:Foo)
>> foo
# Signature:
@@ -104,7 +104,7 @@ List-of-text arguments can be passed like this:
```tomo
# many-texts.tm
-func main(args:[Text]):
+func main(args:[Text])
>> args
```
diff --git a/docs/enums.md b/docs/enums.md
index 45321d25..0a927831 100644
--- a/docs/enums.md
+++ b/docs/enums.md
@@ -15,14 +15,14 @@ c := VariousThings.Nothing
## Pattern Matching
-The values inside an enum can be accessed with pattern matching:
+The values inside an enum can be accessed with pattern matching
```tomo
-when x is AnInteger(i):
+when x is AnInteger(i)
say("It was $i")
-is TwoWords(x, y):
+is TwoWords(x, y)
say("It was $x and $y")
-is Nothing:
+is Nothing
say("It was nothing")
```
@@ -54,10 +54,10 @@ from a function with an explicit return type:
enum ArgumentType(AnInt(x:Int), SomeText(text:Text))
enum ReturnType(Nothing, AnInt(x:Int))
-func increment(arg:ArgumentType -> ReturnType):
- when arg is AnInt(x):
+func increment(arg:ArgumentType -> ReturnType)
+ when arg is AnInt(x)
return AnInt(x + 1)
- is SomeText:
+ is SomeText
return Nothing
...
@@ -78,9 +78,9 @@ known.
Enums can also define their own methods and variables inside their namespace:
```tomo
-enum VariousThings(AnInteger(i:Int), TwoWords(word1, word2:Text), Nothing):
+enum VariousThings(AnInteger(i:Int), TwoWords(word1, word2:Text), Nothing)
meaningful_thing := AnInteger(42)
- func doop(v:VariousThings):
+ func doop(v:VariousThings)
say("$v")
```
diff --git a/docs/functions.md b/docs/functions.md
index ba5e86b3..d68d72d0 100644
--- a/docs/functions.md
+++ b/docs/functions.md
@@ -3,7 +3,7 @@
In Tomo, you can define functions with the `func` keyword:
```tomo
-func add(x:Int, y:Int -> Int):
+func add(x:Int, y:Int -> Int)
return x + y
```
@@ -19,7 +19,7 @@ Instead of giving a type, you can provide a default argument and the type
checker will infer the type of the argument from that value:
```tomo
-func increment(x:Int, amount=1 -> Int):
+func increment(x:Int, amount=1 -> Int)
return x + amount
```
@@ -47,7 +47,7 @@ and are bound to arguments first, followed by binding positional arguments to
any unbound arguments, in order:
```tomo
-func foo(x:Int, y:Text, z:Num):
+func foo(x:Int, y:Text, z:Num)
return "x=$x y=$y z=$z"
>> foo(x=1, y="hi", z=2.5)
@@ -68,7 +68,7 @@ Tomo supports automatic function caching using the `cached` or `cache_size=N`
attributes on a function definition:
```tomo
-func add(x, y:Int -> Int; cached):
+func add(x, y:Int -> Int; cached)
return x + y
```
@@ -78,15 +78,15 @@ return value for those arguments. The above example is functionally similar to
the following code:
```tomo
-func _add(x, y:Int -> Int):
+func _add(x, y:Int -> Int)
return x + y
struct add_args(x,y:Int)
add_cache : @{add_args=Int} = @{}
-func add(x, y:Int -> Int):
+func add(x, y:Int -> Int)
args := add_args(x, y)
- if cached := add_cache[args]:
+ if cached := add_cache[args]
return cached
ret := _add(x, y)
add_cache[args] = ret
@@ -98,7 +98,7 @@ evicted if the cache has reached the maximum size and needs to insert a new
entry:
```tomo
-func doop(x:Int, y:Text, z:[Int]; cache_size=100 -> Text):
+func doop(x:Int, y:Text, z:[Int]; cache_size=100 -> Text)
return "x=$x y=$y z=$z"
```
@@ -108,7 +108,7 @@ Functions can also be given an `inline` attribute, which encourages the
compiler to inline the function when possible:
```tomo
-func add(x, y:Int -> Int; inline):
+func add(x, y:Int -> Int; inline)
return x + y
```
@@ -128,8 +128,8 @@ The normal form of a lambda is to give a return expression after the colon,
but you can also use a block that includes statements:
```tomo
-fn := func(x,y:Int):
- if x == 0:
+fn := func(x,y:Int)
+ if x == 0
return y
return x + y
```
@@ -151,8 +151,8 @@ values. **Captured values are copied to a new location at the moment the lambda
is created and will not reflect changes to local variables.**
```tomo
-func create_adder(n:Int -> func(i:Int -> Int)):
- adder := func(i:Int):
+func create_adder(n:Int -> func(i:Int -> Int))
+ adder := func(i:Int)
return n + i
n = -1 // This does not affect the adder
diff --git a/docs/integers.md b/docs/integers.md
index bdef827d..84a2d1a2 100644
--- a/docs/integers.md
+++ b/docs/integers.md
@@ -362,7 +362,7 @@ An iterator function that counts onward from the starting integer.
**Example:**
```tomo
nums : &[Int] = &[]
-for i in (5).onward():
+for i in (5).onward()
nums.insert(i)
stop if i == 10
>> nums[]
diff --git a/docs/iterators.md b/docs/iterators.md
index 8d585ccb..9337d859 100644
--- a/docs/iterators.md
+++ b/docs/iterators.md
@@ -24,7 +24,7 @@ successively gets one line from a file at a time until the file is exhausted:
>> iter()
= none : Text?
-for line in (./test.txt).each_line():
+for line in (./test.txt).each_line()
pass
```
@@ -32,13 +32,13 @@ You can write your own iterator methods this way. For example, this iterator
iterates over prime numbers up to a given limit:
```tomo
-func primes_up_to(limit:Int):
+func primes_up_to(limit:Int)
n := 2
- return func():
- if n > limit:
+ return func()
+ if n > limit
return !Int
- while not n.is_prime():
+ while not n.is_prime()
n += 1
n += 1
diff --git a/docs/langs.md b/docs/langs.md
index aec9cfa9..8f440932 100644
--- a/docs/langs.md
+++ b/docs/langs.md
@@ -9,8 +9,8 @@ values and give type checking errors if you attempt to use one type of string
where a different type of string is needed.
```tomo
-lang HTML:
- convert(t:Text -> HTML):
+lang HTML
+ convert(t:Text -> HTML)
t = t.translate({
"&" = "&amp;",
"<" = "&lt;",
@@ -20,7 +20,7 @@ lang HTML:
})
return HTML.from_text(t)
- func paragraph(content:HTML -> HTML):
+ func paragraph(content:HTML -> HTML)
return $HTML"<p>$content</p>"
```
@@ -73,11 +73,11 @@ instead of building a global function called `execute()` that takes a
`ShellScript` argument, you could instead build something like this:
```tomo
-lang Sh:
- convert(text:Text -> Sh):
+lang Sh
+ convert(text:Text -> Sh)
return Sh.from_text("'" ++ text.replace("'", "''") ++ "'")
- func execute(sh:Sh -> Text):
+ func execute(sh:Sh -> Text)
...
dir := ask("List which dir? ")
@@ -92,14 +92,14 @@ keyword. Conversions can be defined either inside of the language's block,
another type's block or at the top level.
```tomo
-lang Sh:
- convert(text:Text -> Sh):
+lang Sh
+ convert(text:Text -> Sh)
return Sh.from_text("'" ++ text.replace("'", "''") ++ "'")
-struct Foo(x,y:Int):
- convert(f:Foo -> Sh):
+struct Foo(x,y:Int)
+ convert(f:Foo -> Sh)
return Sh.from_text("$(f.x),$(f.y)")
-convert(texts:[Text] -> Sh):
+convert(texts:[Text] -> Sh)
return $Sh" ".join([Sh(t) for t in texts])
```
diff --git a/docs/libraries.md b/docs/libraries.md
index 551b011a..1409bca5 100644
--- a/docs/libraries.md
+++ b/docs/libraries.md
@@ -50,10 +50,10 @@ Now, what happens if we want to _use_ the compiled object file?
// File: baz.tm
foo := use ./foo.tm
-func say_stuff():
+func say_stuff()
say("I got $(foo.my_variable) from foo")
-func main():
+func main()
say_stuff()
```
diff --git a/docs/lists.md b/docs/lists.md
index e605eea1..1cbc2b45 100644
--- a/docs/lists.md
+++ b/docs/lists.md
@@ -86,10 +86,10 @@ list access.
You can iterate over the items in a list like this:
```tomo
-for item in list:
+for item in list
...
-for i, item in list:
+for i, item in list
...
```
diff --git a/docs/namespacing.md b/docs/namespacing.md
index e8b13305..e9dc428d 100644
--- a/docs/namespacing.md
+++ b/docs/namespacing.md
@@ -15,9 +15,9 @@ collide with a user-chosen name like `FooBaz`.
// File: foo.tm
my_var := 123
-struct Baz(x:Int):
+struct Baz(x:Int)
member := 5
- func frob(b:Baz -> Int):
+ func frob(b:Baz -> Int)
return b.x
```
diff --git a/docs/nums.md b/docs/nums.md
index 34e513ef..1cc3782a 100644
--- a/docs/nums.md
+++ b/docs/nums.md
@@ -71,7 +71,7 @@ y := 1.0
# implicit `!`:
x = x/y
-func doop(x:Num -> Num):
+func doop(x:Num -> Num)
# If a function's return type is non-optional and an optional value is
# used in a return statement, an implicit none check will be inserted and
# will error if the value is none:
diff --git a/docs/operators.md b/docs/operators.md
index d76d625d..fdc2aee5 100644
--- a/docs/operators.md
+++ b/docs/operators.md
@@ -98,8 +98,8 @@ This simplifies things if you want to do a reduction without writing a full
comprehension:
```tomo
-struct Foo(x,y:Int):
- func is_even(f:Foo)->Bool:
+struct Foo(x,y:Int)
+ func is_even(f:Foo -> Bool)
return (f.x + f.y) mod 2 == 0
>> foos := [Foo(1, 2), Foo(-10, 20)]
@@ -217,7 +217,7 @@ will be raised.
#### Addition
```
-func plus(T, T)->T
+func plus(T, T -> T)
```
In an addition expression `a + b` between two objects of the same type, the
@@ -226,7 +226,7 @@ method `a.plus(b)` will be invoked, which returns a new value of the same type.
#### Subtraction
```
-func minus(T, T)->T
+func minus(T, T -> T)
```
In a subtraction expression `a - b` between two objects of the same type, the
@@ -235,8 +235,8 @@ method `a.minus(b)` will be invoked, which returns a new value of the same type.
#### Multiplication
```
-func times(T, T)->T
-func scaled_by(T, N)->T
+func times(T, T -> T)
+func scaled_by(T, N -> T)
```
The multiplication expression `a * b` invokes either the `a.times(b)` method,
@@ -247,7 +247,7 @@ non-numeric and `b` is numeric, or `b.scaled_by(a)` if `b` is non-numeric and
#### Division
```
-func divided_by(T, N)->T
+func divided_by(T, N -> T)
```
In a division expression `a / b` the method `a.divided_by(b)` will be invoked
@@ -256,7 +256,7 @@ if `a` has type `T` and `b` has a numeric type `N`.
#### Exponentiation
```
-func power(T, N)->T
+func power(T, N -> T)
```
In an exponentiation expression, `a ^ b`, if `a` has type `T` and `b` has a
@@ -265,8 +265,8 @@ numeric type `N`, then the method `a.power(b)` will be invoked.
#### Modulus
```
-func mod(T, N)->T
-func mod1(T, N)->T
+func mod(T, N -> T)
+func mod1(T, N -> T)
```
In a modulus expression, `a mod b` or `a mod1 b`, if `a` has type `T` and `b`
@@ -275,7 +275,7 @@ has a numeric type `N`, then the method `mod()` or `mod1()` will be invoked.
#### Negative
```
-func negative(T)->T
+func negative(T -> T)
```
In a unary negative expression `-x`, the method `negative()` will be invoked
@@ -284,13 +284,13 @@ and will return a value of the same type.
#### Bit Operations
```
-func left_shifted(T, Int)->T
-func right_shifted(T, Int)->T
-func unsigned_left_shifted(T, Int)->T
-func unsigned_right_shifted(T, Int)->T
-func bit_and(T, T)->T
-func bit_or(T, T)->T
-func bit_xor(T, T)->T
+func left_shifted(T, Int -> T)
+func right_shifted(T, Int -> T)
+func unsigned_left_shifted(T, Int -> T)
+func unsigned_right_shifted(T, Int -> T)
+func bit_and(T, T -> T)
+func bit_or(T, T -> T)
+func bit_xor(T, T -> T)
```
In a bit shifting expression, `a >> b` or `a << b`, if `a` has type `T` and `b`
@@ -305,7 +305,7 @@ method `bit_and()`, `bit_or()`, or `bit_xor()` will be invoked, assuming that
#### Bitwise Negation
```
-func negated(T)->T
+func negated(T -> T)
```
In a unary bitwise negation expression `not x`, the method `negated()` will be
diff --git a/docs/optionals.md b/docs/optionals.md
index 48e48875..bfebd94e 100644
--- a/docs/optionals.md
+++ b/docs/optionals.md
@@ -6,10 +6,10 @@ represent this case using enums like so:
```tomo
enum MaybeInt(AnInt(x:Int), NoInt)
-func maybe_takes_int(maybe_x:MaybeInt):
- when maybe_x is AnInt(x):
+func maybe_takes_int(maybe_x:MaybeInt)
+ when maybe_x is AnInt(x)
say("Got an int: $x")
- else:
+ else
say("Got nothing")
```
@@ -18,10 +18,10 @@ situation where you might want to not have a value. Instead, Tomo has
built-in support for optional types:
```
-func maybe_takes_int(x:Int?):
- if x:
+func maybe_takes_int(x:Int?)
+ if x
say("Got an int: $x")
- else:
+ else
say("Got nothing")
```
@@ -76,7 +76,7 @@ Here are some examples:
x := 5?
x = none
-func doop(arg:Int?)->Text?:
+func doop(arg:Int? -> Text?)
return none
doop(none)
@@ -89,7 +89,7 @@ the need for an explicit `?` operator in the cases listed above:
x : Int? = none
x = 5
-func doop(arg:Int?)->Text?:
+func doop(arg:Int? -> Text?)
return "okay"
doop(123)
@@ -115,10 +115,10 @@ maybe_x = none
>> maybe_x or fail("No value!")
# Failure!
-func do_stuff(matches:[Text]):
+func do_stuff(matches:[Text])
pass
-for line in lines:
+for line in lines
matches := line.matches($/{..},{..}/) or skip
# The `or skip` above means that if we're here, `matches` is non-none:
do_stuff(matches)
diff --git a/docs/paths.md b/docs/paths.md
index cde7f74d..99891671 100644
--- a/docs/paths.md
+++ b/docs/paths.md
@@ -181,14 +181,14 @@ value if the file couldn't be read.
**Example:**
```tomo
# Safely handle file not being readable:
-if lines := (./file.txt).by_line():
- for line in lines:
+if lines := (./file.txt).by_line()
+ for line in lines
say(line.upper())
-else:
+else
say("Couldn't read file!")
# Assume the file is readable and error if that's not the case:
-for line in (/dev/stdin).by_line()!:
+for line in (/dev/stdin).by_line()!
say(line.upper())
```
diff --git a/docs/pointers.md b/docs/pointers.md
index 0644a6ab..254db07e 100644
--- a/docs/pointers.md
+++ b/docs/pointers.md
@@ -12,7 +12,7 @@ a new, different value and assigning it to a pointer's memory location to
replace the value that previously resided there.
```tomo
-func no_mutation_possible(nums:[Int]):
+func no_mutation_possible(nums:[Int])
nums[1] = 10 // This performs a copy-on-write and creates a new list
// The new list is only accessible as a local variable here
...
@@ -21,7 +21,7 @@ no_mutation_possible(my_nums)
>> my_nums
= [0, 1, 2]
-func do_mutation(nums:@[Int]):
+func do_mutation(nums:@[Int])
nums[1] = 10 // The mutates the value at the given pointer's location
...
my_nums := @[0, 1, 2]
@@ -82,9 +82,9 @@ this, and everywhere inside the truthy block will allow you to use the pointer
as a non-null pointer:
```
-if optional:
+if optional
ok := optional[]
-else:
+else
say("Oh, it was null")
```
diff --git a/docs/sets.md b/docs/sets.md
index 1aaba59d..d40b825c 100644
--- a/docs/sets.md
+++ b/docs/sets.md
@@ -62,10 +62,10 @@ Set length can be accessed by the `.length` field:
You can iterate over the items in a table like this:
```tomo
-for item in set:
+for item in set
...
-for i, item in set:
+for i, item in set
...
```
diff --git a/docs/structs.md b/docs/structs.md
index f075f037..1dfa49c9 100644
--- a/docs/structs.md
+++ b/docs/structs.md
@@ -21,13 +21,13 @@ Structs can define their own methods that can be called with a `:` or different
values that are stored on the type itself.
```tomo
-struct Foo(name:Text, age:Int):
+struct Foo(name:Text, age:Int)
oldest := Foo("Methuselah", 969)
- func greet(f:Foo):
+ func greet(f:Foo)
say("Hi my name is $f.name and I am $f.age years old!")
- func get_older(f:@Foo):
+ func get_older(f:@Foo)
f.age += 1
...
my_foo := @Foo("Alice", 28)
diff --git a/docs/tables.md b/docs/tables.md
index 605a1486..c0376f38 100644
--- a/docs/tables.md
+++ b/docs/tables.md
@@ -139,10 +139,10 @@ t := {"A"=10, "B"=20}
You can iterate over the key/value pairs in a table like this:
```tomo
-for key, value in table:
+for key, value in table
...
-for key in table:
+for key in table
...
```
diff --git a/docs/text.md b/docs/text.md
index d3063443..35196d25 100644
--- a/docs/text.md
+++ b/docs/text.md
@@ -370,7 +370,7 @@ text := "
line one
line two
"
-for line in text.by_line():
+for line in text.by_line()
# Prints: "line one" then "line two":
say(line)
```
@@ -398,7 +398,7 @@ delimiter (the default) will iterate over single grapheme clusters in the text.
**Example:**
```tomo
text := "one,two,three"
-for chunk in text.by_split(","):
+for chunk in text.by_split(",")
# Prints: "one" then "two" then "three":
say(chunk)
```
@@ -425,7 +425,7 @@ given delimiter characters, until it runs out and returns `none`.
**Example:**
```tomo
text := "one,two,;,three"
-for chunk in text.by_split_any(",;"):
+for chunk in text.by_split_any(",;")
# Prints: "one" then "two" then "three":
say(chunk)
```