Builtin function

Home of the ABS programming language: the joy of shell scripting.


Builtin function

There are many builtin functions in ABS. Take type, for example:

type(1) # NUMBER
type([]) # ARRAY

We’ll reveal you a secret now: all string, array, number & hash functions are actually “generic”, but the syntax you see makes you think those are specific to the string, number, etc object.

The trick is very simple; whenever the ABS’ interpreter finds a method call such as object.func(arg) it will actually translate it to func(object, arg).

Don’t believe us? Try with these examples:

map(["1"], int) # [1]
sort([3, 2, 1]) # [1, 2, 3]
len("abc") # 3

At the same time, there are some builtin functions that doesn’t really make sense to call with the method notation, so we’ve kept them in a “special” location in the documentation. exit(99), for example, exits the program with the status code 99, but it would definitely look strange to see something such as 99.exit().

Generic builtin functions

echo(var)

Prints the given variable:

echo("hello world")

You can use use placeholders in your strings:

echo("hello %s", "world")

stdin()

Reads from the stdin:

echo("What do you like?")
echo("Oh, you like %s!", stdin()) # This line will block until user enters some text

Worth to note that you can read the stdin indefinitely with:

# Will read all input to the
# stdin and output it back
for input in stdin {
    echo(input)
}

# Or from the REPL:for input in stdin { echo((input.int() / 2).str() + "...try again:")  }
10
5...try again:
5
2.5...try again:

...

exit(code)

Exists the script with status code:

exit(99)

rand(max)

Returns a random integer number between 0 and max:

rand(10) # 7

env(str)

Returns the str environment variable:

env("PATH") # "/go/bin:/usr/local/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

arg(n)

Returns the nth argument to the current script:

arg(0) # /usr/bin/abs

type(var)

Returns the type if the given variable:

type("") # "STRING"
type({}) # "HASH"

pwd()

Returns the working directory where the script was started for – equivalent to env("PWD"):

pwd() # /go/src/github.com/abs-lang/abs

flag(str)

Returns the value of a command-line flag. Both the --flag and -flag form are accepted, and you can specify values with --flag=x as well as --flag x:

$ abs --test --test2 2 --test3=3 --test4 -test5
Hello user, welcome to the ABS programming language!
Type 'quit' when you're done, 'help' if you get lost!
⧐  flag("test")
true
⧐  flag("test2")
2
⧐  flag("test3")
3
⧐  flag("test4")
true
⧐  flag("test5")
true
⧐  flag("test6")
⧐  

If a flag value is not set, it will default to true. The value of a flag that does not exist is NULL.

In all other cases flag(...) returns the literal string value of the flag:

$ abs --number 10
Hello user, welcome to the ABS programming language!
Type 'quit' when you're done, 'help' if you get lost!
⧐  n = flag("number")
⧐  n
10
⧐  type(n)
STRING

Next

That’s about it for this section!

You can now head over to read a little bit about errors.