You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/0.5.0-alpha/basic_syntax/commands.md
+39-8Lines changed: 39 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,9 @@
1
1
The only way to access the bash shell is through Amber's commands. Commands can be used in the form of a statement or an expression.
2
2
3
-
Commands can sometimes _fail_, so it’s important for whoever uses them to be ready to handle what happens next. There are different ways to deal with failures, each with its own pros and cons:
3
+
Commands (as well as *failable functions*) can sometimes _fail_, so it’s important for whoever uses them to be ready to handle what happens next. There are different ways to deal with failures, each with its own pros and cons:
4
4
-`failed` - the recommended way to handle failing that enables you to write some specific logic to run when a command fails
5
+
-`succeeded` - allows you to write specific logic to run when a command completes successfully
6
+
-`exited` - allows you to write logic that runs regardless of whether the command failed or succeeded
5
7
-`?` - this shorthand for propagating the failure to the caller. This operator can only be used in a `main` block or inside of a function.
6
8
-`trust` - the discouraged way to handle failing. This modifier will treat commands as if they have completed successfully and will allow them to be parsed without any further steps.
7
9
@@ -22,7 +24,7 @@ echo result
22
24
23
25
> DETAILS: Command expression result is sent to the variable instead of _standard output_.
24
26
25
-
Command expression can also be interpolated with other expressions and variables
27
+
Command can also be interpolated with other expressions and variables
26
28
27
29
```ab
28
30
let file_path = "/path/to/file"
@@ -31,16 +33,45 @@ $ cat {file_path} $ failed {
31
33
}
32
34
```
33
35
34
-
##Getting the Exit Code
36
+
### Failed
35
37
36
-
In order to get the exit code, you can use the `status` keyword. It will always return you the exit code of the last bash command or *failable function*.
38
+
The `failed` modifier allows you to write specific logic that runs only when a command fails. This is useful when you want to handle errors gracefully or perform recovery operations. Note that `failed` can optionally accept an exit code parameter, like `failed(code)`, to access the command's exit code.
37
39
38
40
```ab
39
-
let file_path = "/path/to/file"
40
-
$ cat {file_path} $ failed {
41
-
echo "Error! Exit code: {status}"
41
+
$ cat file.txt $ failed(code) {
42
+
echo "Exited with code {code}."
43
+
}
44
+
```
45
+
46
+
### Succeeded
47
+
48
+
Just like `failed` allows you to handle command failures, `succeeded` lets you write specific logic that runs only when a command completes successfully. This can be useful when you want to perform additional operations that should only happen if the command succeeds.
49
+
50
+
```ab
51
+
$ cat file.txt $ succeeded {
52
+
echo "File was read successfully"
53
+
}
54
+
```
55
+
56
+
### Exited
57
+
58
+
The `exited` modifier allows you to write logic that runs regardless of whether the command failed or succeeded. This is useful when you need to perform cleanup or logging operations that should always happen. Note that `exited` can only be used when an exit code parameter is provided, like `exited(code)`.
59
+
60
+
```ab
61
+
$ cat file.txt $ exited(code) {
62
+
echo "Command finished with exit code {code}"
42
63
}
43
-
echo "The status code is: {status}"
64
+
```
65
+
66
+
67
+
## Status
68
+
69
+
The `status` keyword allows you to access the exit code of a command. This is the old school and Bash way of handling failures. Its trait is that it holds the exit code of only the previous command or *failable function* call.
Copy file name to clipboardExpand all lines: docs/0.5.0-alpha/basic_syntax/data_types.md
+15-1Lines changed: 15 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
In Bash there is only one primitive data type, string, which internal implementation is represented by an array of characters `char*`. Amber extends on this data type to introduce a few more.
2
2
3
-
Amber supports five data types:
3
+
Amber supports six data types:
4
4
-`Text` - The textual data type. In other programming languages it can also be called "string".
5
5
-`Int` - Integer data type.
6
6
-`Num` - The numeric data type. It's basically any number.
@@ -21,6 +21,20 @@ Text literal in Amber is contained between double quotes. Amber makes sure to pr
21
21
22
22
Just like in other programming languages, characters in `Text` literals can be escaped.
23
23
24
+
| Escape Sequence | Description |
25
+
| :-------------- | :---------- |
26
+
|`\n`| Newline |
27
+
|`\t`| Tab |
28
+
|`\r`| Carriage return |
29
+
|`\0`| Null byte |
30
+
|`\{`| Literal `{`|
31
+
|`\$`| Literal `$`|
32
+
|`\'`| Literal `'`|
33
+
|`\"`| Literal `"`|
34
+
|`\\`| Literal `\`|
35
+
36
+
Any other escape sequence not listed above will be treated as a literal escape sequence, similar to how Bash handles them. For instance, escaping `\c` will result in the literal `\c` being output.
37
+
24
38
## Integer
25
39
26
40
Under the hood its value is stored as a string of characters - the same way as it's done in Bash. However when performing operations the values are treated as 64-bit signed integers.
Copy file name to clipboardExpand all lines: docs/0.5.0-alpha/basic_syntax/expressions.md
+9Lines changed: 9 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -99,3 +99,12 @@ let age = 18
99
99
echo "Hi, I'm {name}. I'm {age} years old."
100
100
// Outputs: Hi, I'm John. I'm 18 years old
101
101
```
102
+
## Lexical Operations
103
+
104
+
Lexical operations allow you to compare sequences element by element (or character by character). These operations work with `Text`, `[Text]`, and `[Int]` data types.
Copy file name to clipboardExpand all lines: docs/0.5.0-alpha/basic_syntax/functions.md
+6-9Lines changed: 6 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,7 +17,7 @@ Function declared in the example above has name `myFunction` and can take two ar
17
17
If you want to declare a function that takes arguments of certain type - you are encouraged to do this. However, for consistency you are required to specify the return type as well
18
18
19
19
```ab
20
-
fun myFunction(arg1: Num, arg2: Num): Num {
20
+
fun myFunction(arg1: Int, arg2: Int): Int {
21
21
let result = arg1 + arg2
22
22
return result
23
23
}
@@ -28,7 +28,7 @@ An interesting fact about functions is that they are not parsed unless they are
28
28
On the condition that you specify an argument's type, you can also specify its default value — it will be used if none other is provided when the function is called:
29
29
30
30
```ab
31
-
fun addition(a: Num, b: Num = 100): Num {
31
+
fun addition(a: Int, b: Int = 100): Int {
32
32
return a + b
33
33
}
34
34
@@ -65,7 +65,7 @@ Notice that using `?` operator is automatically failing with the `status` code o
65
65
If you specify the return type of a failable function, you must also append the `?` to the type name.
66
66
67
67
```ab
68
-
fun failable(): Num? {
68
+
fun failable(): Int? {
69
69
if 0 > 5 {
70
70
fail 1
71
71
}
@@ -98,13 +98,10 @@ echo "{result}, {status}"
98
98
This was a happy ending. Now let's see what happens when we divide by zero:
Copy file name to clipboardExpand all lines: docs/0.5.0-alpha/basic_syntax/importing.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ In Amber, functions can be imported from other files. To make a function accessi
5
5
To declare a function as public we can use a `pub` keyword. Let's keep in mind that `pub` keyword has to be used before the `fun` keyword that declares our function:
Copy file name to clipboardExpand all lines: docs/0.5.0-alpha/basic_syntax/loops.md
-4Lines changed: 0 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -53,8 +53,6 @@ The above example will iterate through all the files in the array and index them
53
53
54
54
The `while` loop is used to repeat a block of code as long as a condition is true. It's most powerful in situations where the number of iterations is not easily known beforehand.
55
55
56
-
Consider an example where we want to find all powers of two less than 1000. While this could be calculated with a `for` loop, a `while` loop expresses the goal more directly: "keep doubling the number as long as it's less than 1000."
57
-
58
56
```ab
59
57
let number = 1
60
58
@@ -71,5 +69,3 @@ while number < 100 {
71
69
// 32
72
70
// 64
73
71
```
74
-
75
-
Like other loops, `while` also supports `break` and `continue` to control the flow of execution.
0 commit comments