Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added stdlib documentation and updated builtins #39

Merged
merged 14 commits into from
Sep 26, 2024
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Happy scripting with Amber!
```sh
# Install dependencies
npm install
# Generate the stdlib documentation
./sync-stdlib-doc.ab
# Start the development server
npm run dev
```
50 changes: 50 additions & 0 deletions docs/advanced_syntax/builtins.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Builtins

Builtins are native methods (built in the compiler itself) and they are also reserved keywords in Amber.

Similar to the standard library, they generate valid [Shellcheck](https://www.shellcheck.net/) code (though full support for this in Amber is still in progress).

## Cd

Transpile to `cd` which changes the current directory, requires a `Text` parameter.

```ab
cd "/tmp"
```

## Echo

Transpile to `echo` which print text to the console, requires a `Text` parameter.

```ab
echo "Hello World!"
```

## Mv

If you need to move files you can use the `mv` builtin, requires two `Text` parameters.

```ab
mv "/tmp/a" "/tmp/b"
```

This builtin is `failable`, meaning you can handle errors like this:
```ab
mv "/tmp/a" "/tmp/b" failed {
echo "Error"
}
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about -r? is it supported? if it is, how do i specify it? if it is not, how do i move a directory?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is something we should address in Amber and not in the doc website

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is also something that should be thoroughly explained in the documentation


## Nameof

For more advanced commands, you might need the name of the variable in the compiled script. The `nameof` keyword provides this functionality.

For example, this allows you to perform operations like:

```ab
let variable = null

unsafe ${nameof variable}=12$
// Which is the same as:
let variable = 12
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you sure this is good practice? even though its for demonstration purposes, people will still use it if you put it in docs

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was something written by @Ph0enixKM, I think that is the only way to explain nameof

This comment was marked as resolved.

```
11 changes: 0 additions & 11 deletions docs/advanced_syntax/nameof.md

This file was deleted.

42 changes: 40 additions & 2 deletions docs/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@
"title": "As Cast"
},
{
"path": "advanced_syntax/nameof",
"title": "Nameof"
"path": "advanced_syntax/builtins",
"title": "Builtins"
},
{
"path": "advanced_syntax/type_condition",
Expand All @@ -78,6 +78,44 @@
}
]
},
{
"path": "stdlib/doc",
"title": "Standard Library",
"docs": [
{
"path": "stdlib/doc",
"title": "Documentation"
},
{
"path": "stdlib/doc/array",
"title": "Array"
},
{
"path": "stdlib/doc/date",
"title": "Date"
},
{
"path": "stdlib/doc/env",
"title": "Environment"
},
{
"path": "stdlib/doc/fs",
"title": "FileSystem"
},
{
"path": "stdlib/doc/http",
"title": "HTTP"
},
{
"path": "stdlib/doc/math",
"title": "Math"
},
{
"path": "stdlib/doc/text",
"title": "Text"
}
]
},
{
"path": "by_example/examples",
"title": "Amber by Example",
Expand Down
45 changes: 45 additions & 0 deletions docs/stdlib/doc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
## Builtin VS StdLib
Mte90 marked this conversation as resolved.
Show resolved Hide resolved

[Builtins](/advanced_syntax/builtins) are method that are included in the Amber compiler and don't need to be imported in the code.

In contrast, the standard library (stdlib) is a collection of Amber functions that are embedded in every Amber release. Each version of Amber may include changes to the standard library and you need to import these functions in your code. These functions are more advanced and can accept various parameters.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i fail to see the point of this paragraph

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to explain the difference between those, we discussed internally why something should became a builtin or a stdflib so deserve a mention in the docs

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this section doesn't explain when something should be builtin or stdlib. It just says that you don't need to import builtin and stdlib you have to import. It's basic truth and I too don't see much sense in writing this

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this section is important to explain the difference between the two.
Maybe the wording is not the best one but it's a start.

If you have a suggestion on how to improve it right now I will change it as I already did since the PR started.


## Standard library and Shellcheck
Just like the Amber's compiled Bash output, all standard library functions are built from the ground up to be shellcheck compliant. This means that you can focus more on building the logic and spend less time on keeping the code predictable and valid.

## How to use it

Below is an example of how to use the standard library to generate documentation (using the [script](https://github.com/amber-lang/amber-docs/sync-stdlib-doc.ab) provided on the Amber Documentation repository):

```ab
import { download } from "std/http"
import { split, contains } from "std/text"
import { file_exist } from "std/fs"

unsafe $rm -fr /tmp/amber-git$
if silent download("https://github.com/amber-lang/amber/archive/refs/heads/master.zip", "/tmp/amber-git.zip") {
unsafe $unzip "/tmp/amber-git.zip" -d /tmp/amber-git$

let std = unsafe $/usr/bin/ls "/tmp/amber-git/amber-master/src/std/"$
let stdlib = split(std, "\n")

loop v in stdlib {
if (contains(v, ".ab") and file_exist("/tmp/amber-git/amber-master/src/std/{v}")) {
unsafe $amber --docs "/tmp/amber-git/amber-master/src/std/{v}" "./docs/stdlib/doc"$
echo "\n"
}
}
}
```
Comment on lines +15 to +34
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its a bad idea to hardcode this. if the reader wants to see the code, they should click on the link and open it in github

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now github doens't highlight amber code so it is better to put on the documentation website.


> WARNING: Each Amber release may have a different version of the standard library, so make sure to verify compatibility with the specific release you are using.
Mte90 marked this conversation as resolved.
Show resolved Hide resolved

### Importing a library

You can also import all functions from a module by using the following syntax:

```ab
import { * } from "std/http"
Mte90 marked this conversation as resolved.
Show resolved Hide resolved
```

However, only the functions that are used in the script will be included in the generated Bash code, ensuring efficiency.
26 changes: 26 additions & 0 deletions docs/stdlib/doc/array.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
## `array_first_index`
```ab
pub fun array_first_index(array, value): Num
```

Returns index of the first value found in the specified array.

If the value is not found, the function returns -1.


## `array_search`
```ab
pub fun array_search(array, value): [Num]
```

Searches for a value in an array and returns an array with the index of the various items.


## `includes`
```ab
pub fun includes(array, value)
```

Checks if a value is in the array.


125 changes: 125 additions & 0 deletions docs/stdlib/doc/date.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
## `date_add`
```ab
pub fun date_add(add: Text, date: Text = "", utc: Bool = false): Text ?
```

### EXPERIMENTAL
Mte90 marked this conversation as resolved.
Show resolved Hide resolved

Adds a value to a date.

If no date is specified, the current date is used.
Mte90 marked this conversation as resolved.
Show resolved Hide resolved

Example : `date_add("+3 days")`

You can use (+/-):

- years
- months
- days
- hours
- minutes
- seconds


## `date_compare`
```ab
pub fun date_compare(date_a: Text, date_b: Text = "", utc: Bool = false): Num ?
```

### EXPERIMENTAL
Compares two dates.
Mte90 marked this conversation as resolved.
Show resolved Hide resolved

Returns 1 if date_a is after date_b.

Returns 0 if date_a and date_b is the same.

Returns -1 if date_b is after date_a.

If date_b is not provided, current date will be used.


## `date_posix`
```ab
pub fun date_posix(format: Text = "", date: Text = "", utc: Bool = false): Text ?
```

### EXPERIMENTAL

Formats a date with a special format.

If no date is specified, the current date is used.

If no format is specified, "%FT%T%Z" format is used.

For more info about format type "man date" on your shell or go to <https://www.gnu.org/software/coreutils/date>.

Format :
```
%% a literal %
%a locale's abbreviated weekday name (e.g., Sun)
%A locale's full weekday name (e.g., Sunday)
%b locale's abbreviated month name (e.g., Jan)
%B locale's full month name (e.g., January)
%c locale's date and time (e.g., Thu Mar 3 23:05:25 2005)
%C century; like %Y, except omit last two digits (e.g., 20)
%d day of month (e.g., 01)
%D date; same as %m/%d/%y
%e day of month, space padded; same as %_d
%F full date; like %+4Y-%m-%d
%g last two digits of year of ISO week number (see %G)
%G year of ISO week number (see %V); normally useful only with %V
%h same as %b
%H hour (00..23)
%I hour (01..12)
%j day of year (001..366)
%k hour, space padded ( 0..23); same as %_H
%l hour, space padded ( 1..12); same as %_I
%m month (01..12)
%M minute (00..59)
%n a newline
%N nanoseconds (000000000..999999999)
%p locale's equivalent of either AM or PM; blank if not known
%P like %p, but lower case
%q quarter of year (1..4)
%r locale's 12-hour clock time (e.g., 11:11:04 PM)
%R 24-hour hour and minute; same as %H:%M
%s seconds since the Epoch (1970-01-01 00:00 UTC)
%S second (00..60)
%t a tab
%T time; same as %H:%M:%S
%u day of week (1..7); 1 is Monday
%U week number of year, with Sunday as first day of week (00..53)
%V ISO week number, with Monday as first day of week (01..53)
%w day of week (0..6); 0 is Sunday
%W week number of year, with Monday as first day of week (00..53)
%x locale's date representation (e.g., 12/31/99)
%X locale's time representation (e.g., 23:13:48)
%y last two digits of year (00..99)
%Y year
%z +hhmm numeric time zone (e.g., -0400)
%:z +hh:mm numeric time zone (e.g., -04:00)
%::z +hh:mm:ss numeric time zone (e.g., -04:00:00)
%:::z numeric time zone with : to necessary precision (e.g., -04, +05:30)
%Z alphabetic time zone abbreviation (e.g., EDT)
```

By default, date pads numeric fields with zeroes. The following optional flags may follow '%':

```
- (hyphen) do not pad the field
_ (underscore) pad with spaces
0 (zero) pad with zeros
+ pad with zeros, and put '+' before future years with >4 digits
^ use upper case if possible
# use opposite case if possible
```


## `now`
```ab
pub fun now(): Num
```

Returns the current timestamp (seconds since the Epoch (1970-01-01 00:00 UTC)).


Loading