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

Rename standard library functions #639

Merged
merged 9 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions keywords.ab
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
#! /usr/bin/env amber

#!/usr/bin/env amber
import {
replace,
split_words,
starts_with,
words,
} from "std/text"

main (args) {
main(args) {
let keywords = [Text]
let should_append = false
for line in lines("grammar.ebnf") {
if not should_append and not starts_with(line, "KEYWORD"): continue
should_append = true
for token in words(line) {
for token in split_words(line) {
if {
starts_with(token, "'"): keywords += [replace(token, "'", "")]
token == ";": should_append = false
Expand All @@ -22,7 +21,7 @@ main (args) {
let result = $ echo "\$\{{nameof keywords}[@]}" | sort $ failed {
echo "Something went wrong while sorting the keywords"
}
for item in words(result) {
for item in split_words(result) {
echo item
}
}
14 changes: 7 additions & 7 deletions setup/install.ab
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { has_failed, input } from "std/env"
import { chars } from "std/text"
import { includes } from "std/array"
import { file_exist } from "std/fs"
import { has_failed, input_prompt } from "std/env"
import { split_chars } from "std/text"
import { array_contains } from "std/array"
import { file_exists } from "std/fs"
import { get_os, get_arch, get_place, get_bins_folder } from "./shared.ab"

let name = "amber"
Expand All @@ -28,7 +28,7 @@ main(args) {
let os = get_os()
let arch = get_arch()

let user_only_install = includes(args, "--user")
let user_only_install = array_contains(args, "--user")
let place = get_place(user_only_install)
let bins_folder = get_bins_folder(user_only_install)

Expand Down Expand Up @@ -116,7 +116,7 @@ main(args) {
}

// Delete the previous symbolic link
if file_exist("{bins_folder}/{target}") {
if file_exists("{bins_folder}/{target}") {
$ {sudo} rm "{bins_folder}/{target}" $ failed {
echo "Failed to remove the previous amber symbol link."
echo "Please make sure that root user can access {bins_folder} directory."
Expand All @@ -131,7 +131,7 @@ main(args) {
exit 1
}

let nickname = input("Would you like to help improve Amber by sharing your OS info with our developer database? Enter your GitHub nickname (or any nickname) or type `no`:")
let nickname = input_prompt("Would you like to help improve Amber by sharing your OS info with our developer database? Enter your GitHub nickname (or any nickname) or type `no`:")
if (nickname != "no") {
// Send feedback to the server
trust silent $ curl -G --data-urlencode "agent={agent}" --data-urlencode "nickname={nickname}" --data-urlencode "name=download" "https://amber-lang.com/api/visit" $
Expand Down
4 changes: 2 additions & 2 deletions setup/shared.ab
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { has_failed } from "std/env"
import { includes } from "std/array"
import { array_contains } from "std/array"

pub fun get_os(): Text {
// Determine OS type
Expand Down Expand Up @@ -31,7 +31,7 @@ pub fun get_arch(): Text {
exit 1
}

let arch = includes(["arm64", "aarch64"], arch_type)
let arch = array_contains(["arm64", "aarch64"], arch_type)
then "aarch64"
else "x86_64"

Expand Down
4 changes: 2 additions & 2 deletions setup/uninstall.ab
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { includes } from "std/array"
import { array_contains } from "std/array"
import { get_arch, get_place, get_bins_folder } from "./shared.ab"

echo ""

main(args) {
let arch = get_arch()

let user_only_install = includes(args, "--user")
let user_only_install = array_contains(args, "--user")
let place = get_place(user_only_install)
let bins_folder = get_bins_folder(user_only_install)

Expand Down
20 changes: 10 additions & 10 deletions src/std/array.ab
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// Returns index of the first value found in the specified array.
///
/// If the value is not found, the function returns -1.
pub fun array_first_index(array, value): Num {
pub fun array_find(array, value): Num {
for index, element in array {
if value as Text == element as Text {
return index
Expand All @@ -11,7 +11,7 @@ pub fun array_first_index(array, value): Num {
}

/// Searches for a value in an array and returns an array with the index of the various items.
pub fun array_search(array, value): [Num] {
pub fun array_find_all(array, value): [Num] {
let result = [Num]
for index, element in array {
if value as Text == element as Text {
Expand All @@ -22,28 +22,28 @@ pub fun array_search(array, value): [Num] {
}

/// Checks if a value is in the array.
pub fun includes(array, value) {
let result = array_first_index(array, value)
pub fun array_contains(array, value) {
let result = array_find(array, value)
return result >= 0
}

/// Returns the first element in the array; if the array is empty, the return
/// value is undefined.
pub fun first(array) {
pub fun array_first(array) {
return array[0]
}

/// Returns the last element in the array; if the array is empty, the return
/// value is undefined.
pub fun last(array) {
pub fun array_last(array) {
let index = len(array) - 1
return array[index]
}

/// Removes an element at the index from the array; if the index is negative
/// or beyond the end, the return value is undefined, but the array will be
/// unchanged.
pub fun remove_at(ref array: [], index: Num): Null {
pub fun array_remove_at(ref array: [], index: Num): Null {
let offset = index + 1
let length = len(array)
array = array[0..index] + array[offset..length]
Expand All @@ -52,7 +52,7 @@ pub fun remove_at(ref array: [], index: Num): Null {
/// Removes an element at the index from the array, and returns it; if the
/// index is negative or beyond the end, the return value is undefined, but
/// the array will be unchanged.
pub fun extract_at(ref array, index) {
pub fun array_extract_at(ref array, index) {
let element = array[index]
let offset = index + 1
let length = len(array)
Expand All @@ -62,7 +62,7 @@ pub fun extract_at(ref array, index) {

/// Removes the last element from the array, and returns it; if the array
/// is empty, the return value is undefined, but the array will be unchanged.
pub fun pop(ref array) {
pub fun array_pop(ref array) {
let length = len(array)
let index = length - 1
let element = array[index]
Expand All @@ -72,7 +72,7 @@ pub fun pop(ref array) {

/// Removes the first element from the array, and returns it; if the array
/// is empty, the return value is undefined, but the array will be unchanged.
pub fun shift(ref array) {
pub fun array_shift(ref array) {
let length = len(array)
let element = array[0]
array = array[1..length]
Expand Down
2 changes: 1 addition & 1 deletion src/std/date.ab
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub fun date_posix(format: Text = "", date: Text = "", utc: Bool = false): Text?

/// Returns the current timestamp (seconds since the Epoch (1970-01-01 00:00 UTC)).
#[allow_absurd_cast]
pub fun now(): Num {
pub fun date_now(): Num {
return trust $ date +%s $ as Num
}

Expand Down
46 changes: 23 additions & 23 deletions src/std/env.ab
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import * from "std/fs"
import * from "std/text"

/// Retrieves the value of an environment variable, optionally sourcing it from a file if not already set.
pub fun get_env_var(var: Text, file: Text = ".env"): Text {
pub fun env_var_load(var: Text, file: Text = ".env"): Text {
let _var = trust $ echo "\$\{!var}" $
if _var != "" {
return _var
}

if file_exist(file) {
if file_exists(file) {
trust $ source "{file}" $
return trust $ echo "\$\{!var}" $
}
Expand All @@ -17,40 +17,40 @@ pub fun get_env_var(var: Text, file: Text = ".env"): Text {
}

/// Loads the env file in the environment, using `xargs`.
pub fun load_env_file(file: Text = ".env"): Null {
pub fun env_file_load(file: Text = ".env"): Null {
trust $ export "\$(xargs < {file})" > /dev/null $
}

/// Checks if a variable inside the shell session exists.
pub fun shell_isset(name: Text): Bool {
pub fun env_var_test(name: Text): Bool {
$ [[ ! -z \$\{!{nameof name}+z} ]] $ failed {
return false
}
return true
}

/// Sets a constant inside the shell session.
pub fun shell_constant_set(name: Text, val: Text): Null? {
pub fun env_const_set(name: Text, val: Text): Null? {
$ readonly \${nameof name}="\${nameof val}" 2> /dev/null $?
}

/// Gets a constant inside the shell session.
pub fun shell_constant_get(name: Text): Text? {
pub fun env_const_get(name: Text): Text? {
return $ echo \$\{!{nameof name}} $?
}

/// Sets a constant inside the shell session.
pub fun shell_var_set(name: Text, val: Text): Null? {
pub fun env_var_set(name: Text, val: Text): Null? {
$ export \${nameof name}="\${nameof val}" 2> /dev/null $?
}

/// Gets a constant inside the shell session.
pub fun shell_var_get(name: Text): Text? {
pub fun env_var_get(name: Text): Text? {
return $ echo \$\{!{nameof name}} $?
}

/// Removes a variable inside the shell session.
pub fun shell_unset(name: Text): Null? {
pub fun env_var_unset(name: Text): Null? {
$ unset {name} $?
}

Expand All @@ -63,7 +63,7 @@ pub fun is_command(command: Text): Bool {
}

/// Creates a prompt and returns the value.
pub fun input(prompt: Text): Text {
pub fun input_prompt(prompt: Text): Text {
trust $ read -p "\${nameof prompt}" $
return "\$REPLY"
}
Expand All @@ -80,14 +80,14 @@ pub fun input_hidden(prompt: Text): Text {
/// Creates a confirm prompt (Yes/No), and returns true if the choice is Yes.
///
/// "No" is the default choice, set default_yes to true for "Yes" as default choice.
pub fun confirm(prompt: Text, default_yes: Bool = false): Bool {
pub fun input_confirm(prompt: Text, default_yes: Bool = false): Bool {
let choice_default = default_yes then " [\x1b[1mY/\x1b[0mn]" else " [y/\x1b[1mN\x1b[0m]"
trust {
$ printf "\x1b[1m{prompt}\x1b[0m{choice_default}" $
$ read -s -n 1 $
$ printf "\n" $
}
let result = lower(trust $ echo \$REPLY $)
let result = lowercase(trust $ echo \$REPLY $)
return result == "y" or (result == "" and default_yes)
}

Expand All @@ -113,32 +113,32 @@ pub fun printf(format: Text, args: [Text] = [""]): Null {
}

/// Escapes the text to be used with `printf`.
pub fun printf_escape(text: Text): Text {
pub fun escaped(text: Text): Text {
return trust $ echo \${nameof text} | sed -e 's/\\\\/\\\\\\\\/g' -e "s/%/%%/g" $
}

/// Prepares a text with formatting options for `printf`.
pub fun text_shell(message: Text, style: Num, fg: Num, bg: Num): Text {
return "\x1b[{style};{fg};{bg}m{printf_escape(message)}\x1b[0m"
pub fun styled(message: Text, style: Num, fg: Num, bg: Num): Text {
return "\x1b[{style};{fg};{bg}m{escaped(message)}\x1b[0m"
}

/// Returns a text as bold.
pub fun text_bold(message: Text): Text {
return "\x1b[1m{printf_escape(message)}\x1b[0m"
pub fun bold(message: Text): Text {
return "\x1b[1m{escaped(message)}\x1b[0m"
}

/// Returns a text as italic.
pub fun text_italic(message: Text): Text {
return "\x1b[3m{printf_escape(message)}\x1b[0m"
pub fun italic(message: Text): Text {
return "\x1b[3m{escaped(message)}\x1b[0m"
}

/// Returns a text as underlined.
pub fun text_underlined(message: Text): Text {
return "\x1b[4m{printf_escape(message)}\x1b[0m"
pub fun underlined(message: Text): Text {
return "\x1b[4m{escaped(message)}\x1b[0m"
}

/// Prints a text with a specified color.
pub fun color_echo(message: Text, color: Num): Null {
pub fun echo_colored(message: Text, color: Num): Null {
printf("\x1b[{color as Text}m%s\x1b[0m\n", [message])
}

Expand All @@ -158,7 +158,7 @@ pub fun echo_warning(message: Text): Null {
}

/// Prints a text as a error and exits if the status code is greater than 0.
pub fun error(message: Text, exit_code: Num = 1): Null {
pub fun echo_error(message: Text, exit_code: Num = 1): Null {
printf("\x1b[1;3;97;41m%s\x1b[0m\n", [message])
if exit_code > 0 : exit(exit_code)
}
Loading
Loading