Skip to content

Commit dfc8e0e

Browse files
committed
chore(docs): ARK-236, add documentation for ArkScript's builtins
1 parent 61831eb commit dfc8e0e

File tree

5 files changed

+339
-0
lines changed

5 files changed

+339
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
--#
2+
* @name toString
3+
* @brief Convert a value to a string
4+
* @param value anything
5+
* =begin
6+
* (print (toString "abc")) # "abc"
7+
* (print (toString 1)) # "1"
8+
* (print (toString (fun () ()))) # "Function@1"
9+
* (print (toString print)) # "CProcedure"
10+
* (print (toString [])) # "[]"
11+
* (let x 1) (print (toString (fun (&x) ()))) # "(.x=1)"
12+
* (print (toString (dict 1 2 3 4))) # "{1: 2, 3: 4}"
13+
* (print (toString nil)) # "nil"
14+
* (print (toString true)) # "true"
15+
* (print (toString false)) # "false"
16+
* =end
17+
#--
18+
19+
--#
20+
* @name type
21+
* @brief Get the type of a given value as a string
22+
* @param value anything
23+
* =begin
24+
* (print (type "abc")) # "String"
25+
* (print (type 1)) # "Number"
26+
* (print (type (fun () ()))) # "Function"
27+
* (print (type print)) # "CProc"
28+
* (print (type [])) # "List"
29+
* (let x 1) (print (type (fun (&x) ()))) # "Closure"
30+
* (print (type (dict 1 2 3 4))) # "Dict"
31+
* (print (type nil)) # "Nil"
32+
* (print (type true)) # "Bool"
33+
* (print (type false)) # "Bool"
34+
* =end
35+
#--
36+
37+
--#
38+
* @name nil?
39+
* @brief Check if a value is nil
40+
* @param value anything
41+
* =begin
42+
* (print (nil? "abc")) # false
43+
* (print (nil? 1)) # false
44+
* (print (nil? (fun () ()))) # false
45+
* (print (nil? print)) # false
46+
* (print (nil? [])) # false
47+
* (print (nil? (dict 1 2 3 4))) # false
48+
* (print (nil? nil)) # true
49+
* (print (nil? true)) # false
50+
* (print (nil? false)) # false
51+
* =end
52+
#--
53+
54+
--#
55+
* @name comparison
56+
* @brief Compare two values and return true or false
57+
* @details Comparing two values (using `<`, `>`, `<=`, `>=` and `=`) with a different type will always return false
58+
* @param a first value
59+
* @param b second value
60+
* =begin
61+
* (print (< "abc" "def")) # true, string are compared lexicographically
62+
* (print (< 2 1)) # false
63+
* (print (> 3 -5.5) # true
64+
* (print (> "Hello" ""))
65+
* (print (<= [] [1 2])) # true, lists are compared lexicographically
66+
* (print (<= [1 2] [1 0])) # false
67+
* (print (<= [1 2] [10])) # true
68+
* (print (>= 5 5)) # true
69+
* (print (= false 5)) # false
70+
* (print (!= false 5)) # true
71+
* =end
72+
#--
73+
74+
--#
75+
* @name not
76+
* @brief Convert a value to a boolean and invert it
77+
* @param value anything
78+
* =begin
79+
* (print (not "")) # true
80+
* (print (not "a")) # false
81+
* (print (not 0)) # true
82+
* (print (not 1)) # false
83+
* (print (not [])) # true
84+
* (print (not [1 2])) # false
85+
* (print (not nil)) # true
86+
* (print (not true)) # false
87+
* (print (not false)) # true
88+
* (print (not (dict))) # true
89+
* (print (not (dict "a" 1))) # false
90+
* =end
91+
#--
92+
93+
--#
94+
* @name hasField
95+
* @brief Check if a closure has a given field
96+
* @param c closure
97+
* @param field string, field name to look for
98+
* =begin
99+
* (let x 1)
100+
* (let b "hello")
101+
* (let closure (fun (&x &b) ()))
102+
* (print (hasField closure "x")) # true
103+
* (print (hasField closure "b")) # true
104+
* (print (hasField closure "B")) # false, field names are case-sensitive
105+
* =end
106+
#--

src/arkreactor/Builtins/Dict.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--#
2+
* @name empty?
3+
* @brief Check if a dict is empty
4+
* @param a dict
5+
* =begin
6+
* (print (empty? (dict "a" 2))) # false
7+
* (print (empty? (dict))) # true
8+
* =end
9+
#--
10+
11+
--#
12+
* @name len
13+
* @brief Return the length of a dictionary
14+
* @param a dict
15+
* =begin
16+
* (print (len (dict)) # 0
17+
* (print (len (dict "a" 1 "b" 2 "c" 3))) # 3
18+
* =end
19+
#--

src/arkreactor/Builtins/List.txt

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
--#
2+
* @name len
3+
* @brief Return the length of a list
4+
* @param a list
5+
* =begin
6+
* (print (len [])) # 0
7+
* (print (len (list)) # 0
8+
* (print (len [1 2 3])) # 3
9+
* =end
10+
#--
11+
12+
--#
13+
* @name empty?
14+
* @brief Check if a list is empty
15+
* @details `nil` is also considered empty, as it is the void value that can be returned by `head`
16+
* @param a list
17+
* =begin
18+
* (print (empty? [])) # true
19+
* (print (empty? (list)) # true
20+
* (print (empty? nil)) # true
21+
* (print (empty? [1 2 3])) # false
22+
* =end
23+
#--
24+
25+
--#
26+
* @name head
27+
* @brief Return the first element of a list, or nil if empty
28+
* @param a list
29+
* =begin
30+
* (print (head [])) # nil
31+
* (print (head (list)) # nil
32+
* (print (head [1])) # 1
33+
* (print (head [1 2 3])) # 1
34+
* =end
35+
#--
36+
37+
--#
38+
* @name tail
39+
* @brief Return the tail of a list, or empty list if it has one or less element
40+
* @param a list
41+
* =begin
42+
* (print (tail [])) # []
43+
* (print (tail (list)) # []
44+
* (print (tail [1])) # []
45+
* (print (tail [1 2 3])) # [2 3]
46+
* =end
47+
#--
48+
49+
--#
50+
* @name @
51+
* @brief Get an element in a list
52+
* @details Raise an error if the index is out of range
53+
* @param lst list
54+
* @param i index (can be negative to start from the end)
55+
* =begin
56+
* (print (@ [1 2 3] 0)) # 1
57+
* (print (@ [1 2 3] 1)) # 2
58+
* (print (@ [1 2 3] 2)) # 3
59+
* (print (@ [1 2 3] -1)) # 3
60+
* (print (@ [1 2 3] -2)) # 2
61+
* =end
62+
#--
63+
64+
--#
65+
* @name @@
66+
* @brief Get an element in a list of lists, or list of strings
67+
* @details Raise an error if an index is out of range
68+
* @param lst list of collections (lists or strings, can be mixed)
69+
* @param y index (can be negative to start from the end)
70+
* @param x index (can be negative to start from the end)
71+
* =begin
72+
* (print (@@ [[1 2 3] [4 5 6] [7 8 9]] 0 1)) # 2
73+
* (print (@@ [[1 2 3] [4 5 6] "ghi"] -1 1)) # h
74+
* (print (@@ [[1 2 3] [4 5 6] [7 8 9]] 0 -1)) # 3
75+
* (print (@@ ["abc" "def" "ghi"] 0 1)) # b
76+
* (print (@@ ["abc" "def" "ghi"] -1 1)) # h
77+
* (print (@@ ["abc" "def" "ghi"] 0 -1)) # c
78+
* =end
79+
#--

src/arkreactor/Builtins/Math.txt

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
--#
2+
* @name +
3+
* @brief Add two or more numbers together
4+
* @param a number
5+
* @param b... more numbers
6+
* =begin
7+
* (print (+ 1 2.5)) # 3.5
8+
* (print (+ 5 -7.6 12)) # 9.4
9+
* =end
10+
#--
11+
12+
--#
13+
* @name -
14+
* @brief Subtract two or more numbers together
15+
* @param a first number to subtract other numbers to
16+
* @param b... more numbers
17+
* =begin
18+
* (print (- 1 2.5)) # -1.5
19+
* (print (- 5 11 1)) # -7
20+
* =end
21+
#--
22+
23+
--#
24+
* @name *
25+
* @brief Multiply two or more numbers together
26+
* @param a number
27+
* @param b... more numbers
28+
* =begin
29+
* (print (* 2 2.5)) # 5
30+
* (print (* 5 4 3)) # 60
31+
* =end
32+
#--
33+
34+
--#
35+
* @name /
36+
* @brief Divide two or more numbers together
37+
* @details The result is always a floating point number. For an integer division, see **math:floordiv**
38+
* @param a first number to divide by other numbers
39+
* @param b... more numbers
40+
* =begin
41+
* (print (/ 10 3)) # 3.33333...
42+
* (print (/ 99 5 4)) # (99 / 5) / 4 = 4.95
43+
* =end
44+
#--
45+
46+
--#
47+
* @name mod
48+
* @brief Compute the modulus of two numbers
49+
* @details The result is always a floating point number
50+
* @param a number
51+
* @param b number
52+
* =begin
53+
* (print (mod 10 3)) # 1
54+
* (print (mod 12.9 4)) # 0.9000000000000004
55+
* =end
56+
#--

src/arkreactor/Builtins/String.txt

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
--#
2+
* @name +
3+
* @brief Add two or more strings together
4+
* @param a string
5+
* @param b... more strings
6+
* =begin
7+
* (print (+ "a" "b")) # ab
8+
* (print (+ "Hello" ", " "World" "!")) # Hello, World!
9+
* =end
10+
#--
11+
12+
--#
13+
* @name len
14+
* @brief Return the length of a string (in bytes)
15+
* @param a string
16+
* =begin
17+
* (print (len "abc")) # 3
18+
* (print (len "🏳️‍⚧️")) # 16
19+
* =end
20+
#--
21+
22+
--#
23+
* @name empty?
24+
* @brief Check if a string is empty
25+
* @param a string
26+
* =begin
27+
* (print (empty? "abc")) # false
28+
* (print (empty? "")) # true
29+
* =end
30+
#--
31+
32+
--#
33+
* @name head
34+
* @brief Return the first character of a string, or emtpy string if empty
35+
* @param a string
36+
* =begin
37+
* (print (head "abc")) # a
38+
* (print (head "")) # ""
39+
* =end
40+
#--
41+
42+
--#
43+
* @name tail
44+
* @brief Return the tail of a string, or emtpy string if it has one or less character
45+
* @param a string
46+
* =begin
47+
* (print (tail "abc")) # bc
48+
* (print (tail "a")) # ""
49+
* (print (tail "")) # ""
50+
* =end
51+
#--
52+
53+
--#
54+
* @name toNumber
55+
* @brief Convert a string to a number
56+
* @details Return nil if the conversion failed
57+
* @param a string
58+
* =begin
59+
* (print (toNumber "abc")) # nil
60+
* (print (toNumber "1")) # 1
61+
* (print (toNumber "1e3")) # 1000
62+
* (print (toNumber "3.14159")) # 3.14159
63+
* =end
64+
#--
65+
66+
--#
67+
* @name @
68+
* @brief Get a character in a string
69+
* @details Raise an error if the index is out of range
70+
* @param str string
71+
* @param i index (can be negative to start from the end)
72+
* =begin
73+
* (print (@ "abc" 0)) # "a"
74+
* (print (@ "abc" 1)) # "b"
75+
* (print (@ "abc" 2)) # "c"
76+
* (print (@ "abc" -1)) # "c"
77+
* (print (@ "abc" -2)) # "b"
78+
* =end
79+
#--

0 commit comments

Comments
 (0)