-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add hello world, loops, values and variables examples
- Loading branch information
Showing
5 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
This example demonstrates how to print a message to the terminal using the Umbra language. | ||
|
||
### Code | ||
|
||
```u title="hello-world.u" hl_lines="3" | ||
import "io" | ||
io::printLn("Hello world") | ||
``` | ||
|
||
### How to Run | ||
|
||
To run the code above, save it in a file with the `.u` extension, such as `hello-world.u`. Then, execute the command: | ||
|
||
```sh | ||
umbra hello-world.u | ||
``` | ||
|
||
This command will display `Hello world` in the terminal, showcasing the basic output functionality in Umbra. | ||
|
||
Next example: [Values](/examples/values) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
In Umbra, for is the only looping construct, offering two distinct forms: the initialized `for` statement and the conditional for statement. | ||
|
||
### Initialized | ||
|
||
The initialized `for` loop has an initializer, stop condition, and an optional step value, structured as | ||
|
||
```u title="loops.u" | ||
for mut i num = 0, 10, 2 { | ||
io::printLn(i) | ||
} | ||
``` | ||
where `i` starts at `0`, runs until `10`, and increments by `2` on each iteration, resulting in | ||
|
||
``` | ||
0 | ||
2 | ||
4 | ||
6 | ||
8 | ||
10 | ||
``` | ||
### Conditional | ||
|
||
The conditional `for` loop, on the other hand, is structured with a condition, executing as long as the condition remains true. | ||
|
||
```u title="loops.u" | ||
if i < 10 { | ||
} | ||
``` | ||
|
||
If no condition is specified, the loop becomes infinite, running until explicitly broken with `break`. | ||
|
||
```u title="loops.u" | ||
mut i num = 0 | ||
for { | ||
if i > 10 { | ||
break | ||
} | ||
i = i + 1 | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
Umbra has various value types including strings (`str`), numbers (`num`), booleans (`bool`), etc. Here are a few basic examples. | ||
|
||
Strings, which can be added together with +. | ||
|
||
```u title="values.go" | ||
io::printLn("umbra" + "lang") | ||
``` | ||
Numbers. | ||
|
||
```u title="values.go" | ||
io::printLn("1+1 =", 1+1) | ||
io::printLn("7/3 =", 7/3) | ||
``` | ||
|
||
Booleans, with boolean operators as you’d expect. | ||
|
||
```u title="values.go" | ||
io::printLn(true and false) | ||
io::printLn(true or false) | ||
io::printLn(!true) | ||
``` | ||
|
||
```sh | ||
$ go run values.go | ||
# golang | ||
# 1+1 = 2 | ||
# 7.0/3.0 = 2.3333333333333335 | ||
# false | ||
# true | ||
# false | ||
``` | ||
|
||
Next example: [Variables](/examples/variables) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
In Umbra, variables must always be explicitly typed, with the option to declare them as either `const` or `mut`, providing control over immutability and mutability in code. | ||
|
||
### Mutable and constants | ||
|
||
```u title="types.u" | ||
const name str = "Peam" | ||
mut age num = 24 | ||
const fool bool = true | ||
mut person hashmap = { | ||
name: name, | ||
age: age, | ||
} | ||
const people arr = [person] | ||
``` | ||
|
||
### Nullable | ||
|
||
Additionally, by adding a `?` after the type, a variable can be made nullable, allowing it to hold either a value of the specified type or `null`. | ||
|
||
```u title="types.u" | ||
mut name str? = "Peam" | ||
name = null | ||
``` | ||
|
||
!!! info "Not initialized" | ||
Variables not initialized should be set as nullable when declaring | ||
```u title="types.u" | ||
mut name str? | ||
``` | ||
|
||
Next example: [Loops](/examples/loops) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters