Skip to content

Commit

Permalink
Merge pull request #1867 from RickBarretto/update-documentation
Browse files Browse the repository at this point in the history
Update documentation
  • Loading branch information
drkameleon authored Feb 1, 2025
2 parents 1feabea + 6c8e7f8 commit 6f10987
Show file tree
Hide file tree
Showing 9 changed files with 231 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/library/Arithmetic.nim
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ proc defineLibrary*() =
returns = {Floating,Rational,Quantity,Object,Nothing},
example = """
print fdiv 5 2 ; 2.5
print 5 // 2 ; 2.5
..........
a: 6
fdiv 'a 3 ; a: 2.0
Expand Down
66 changes: 63 additions & 3 deletions src/library/Collections.nim
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,14 @@ proc defineLibrary*() =
AGE: 35
]
; e: [name:John, surname:Doe, age:35]
..........
entity: "EU"
location: dictionary.with: [entity][
country: "Spain"
]
print location\entity ; => EU
""":
#=======================================================
var dict: ValueDict
Expand Down Expand Up @@ -1403,6 +1411,34 @@ proc defineLibrary*() =
},
returns = {Range},
example = """
; range of :integers
range 0 5 ; 0..5
0..5 ; 0..5
@0..5 ; [0 1 2 3 4 5]
..........
; range of :chars
'a'..'e' ; 'a'..'e'
@'a'..'e' ; [a b c d e]
..........
; range with steps
@range.step: 2 1 5 ; [1 3 5]
..........
; iterate a range
0..5 | loop 'i -> print ~"|i|. hello"
; 0. hello
; 1. hello
; 2. hello
; 3. hello
; 4. hello
; 5. hello
..........
; check bounds
in? 5 0..10 ; => true
""":
#=======================================================
var limX: int
Expand Down Expand Up @@ -1466,19 +1502,26 @@ proc defineLibrary*() =
example = """
remove "hello" "l" ; => "heo"
print "hello" -- "l" ; heo
remove [1 2 3 4] 4 ; => [1 2 3]
..........
str: "mystring"
remove 'str "str"
print str ; mying
..........
remove.key #[name: "John" surname: "Doe"] "surname" ; => #[name: "John"]
..........
print remove.once "hello" "l"
; helo
; Remove each element of given block from collection once
remove.once [1 2 [1 2] 3 4 1 2 [1 2] 3 4] [1 2]
; [[1 2] 3 4 1 2 [1 2] 3 4]
..........
remove [1 2 3 4] 4 ; => [1 2 3]
remove.index: 2 "Ruby" "u" ; => Rby
remove.index: 2 "Ruby" "a" ; => Ruby
..........
remove.prefix "--empty --flag" "--" ; => "empty --flag"
remove.suffix "test.txt file.txt" ".txt" ; => "test.txt file"
..........
remove.instance [1 [6 2] 5 3 [6 2] 4 5 6] [6 2] ; => [1 5 3 4 5 6]
remove.instance.once [1 [6 2] 5 3 [6 2] 4 5 6] [6 2] ; => [1 5 3 [6 2] 4 5 6]
Expand Down Expand Up @@ -2013,6 +2056,16 @@ proc defineLibrary*() =
; 1 : Urgent!
; 2 : Important
; 3 : Low priority
..........
spanishWords: ["uno","dos","tres","Uno","perversión","ábaco","abismo", "aberración"]
sort.as: 'es spanishWords
; => ["ábaco" "aberración" "abismo" "dos" "perversión" "tres" "uno" "Uno"]
..........
sort.sensitive ["c" "C" "CoffeeScript" "nim" "Arturo" "coffeescript" "arturo" "Nim"]
; => ["Arturo" "C" "CoffeeScript" "Nim" "arturo" "c" "coffeescript" "nim"]
..........
sort.values #[ name: "John" surname: "Doe" age: 35 income: 5000]
; => #[age: 35 income: 5000 surname: "Doe" name: "John" ]
""":
#=======================================================
var sortOrdering = SortOrder.Ascending
Expand Down Expand Up @@ -2180,6 +2233,12 @@ proc defineLibrary*() =
split "hello" ; => [`h` `e` `l` `l` `o`]
..........
split.words "hello world" ; => ["hello" "world"]
split.by: "," "hello,world" ; => ["hello" "world"]
split.lines "hello\nworld" ; => ["hello" "world"]
split.path "/usr/bin" ; => ["usr" "bin"]
; windows only:
split.path "\\usr\\bin" ; => ["usr" "bin"]
..........
split.every: 2 "helloworld"
; => ["he" "ll" "ow" "or" "ld"]
Expand Down Expand Up @@ -2519,6 +2578,8 @@ proc defineLibrary*() =
arr: [1 2 4 1 3 2]
unique 'arr
print arr ; 1 2 4 3
..........
unique.id "user-" ; => user-67915b7a409e222b2f9a6bed
""":
#=======================================================
if (hadAttr("id")):
Expand Down Expand Up @@ -2554,8 +2615,7 @@ proc defineLibrary*() =
surname: "Doe"
]
values user
=> ["John" "Doe"]
values user ; => ["John" "Doe"]
""":
#=======================================================
if xKind == Block:
Expand Down
2 changes: 2 additions & 0 deletions src/library/Colors.nim
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ proc defineLibrary*() =
palette.triad #red ; => [#FF0000 #00FF00 #0000FF]
palette.tetrad #red ; => [#FF0000 #80FF00 #00FFFF #7F00FF]
..........
palette.split #red ; => [#FF0000 #CCFF00 #0066FF]
..........
palette.monochrome #red
; => [#FF0000 #D40000 #AA0000 #7F0000 #550000 #2A0000]
Expand Down
19 changes: 19 additions & 0 deletions src/library/Converters.nim
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,25 @@ proc defineLibrary*() =
print as.binary 123 ; 1111011
print as.octal 123 ; 173
print as.hex 123 ; 7b
..........
inspect as.agnostic [hello world]
; [ :block
; hello :literal
; world :literal
; ]
..........
example: "Hello, world"
example ; => Hello, world
as.code example ; => "Hello, world"
..........
as.code #[name: "John" surname: "Doe"]
; => #[name: "John" surname: "Doe" ]
as.code.pretty #[name: "John" surname: "Doe"]
; => #[
; name: "John"
; surname: "Doe"
; ]
""":
#=======================================================
if (hadAttr("binary")):
Expand Down
101 changes: 99 additions & 2 deletions src/library/Core.nim
Original file line number Diff line number Diff line change
Expand Up @@ -619,9 +619,55 @@ proc defineLibrary*() =
"all" : ({Logical},"export everything, regardless of whether it's been marked as public (makes sense only for modules)")
},
returns = {Nothing},
# TODO(Core\export) add documentation example
# labels: library, documentation, easy
example = """
greeting: module [
greet: method.public [user :string][
print ~"Hello, |user|!"
]
]
export greeting!
greet "Anonymous" ; Hello, Anonymous!
..........
; You can't use private methods
greeting: module [
greet: method [user :string][
print ~"Bye, bye, |user|!"
]
]
export greeting!
greet "Anonymous"
; Cannot resolve requested value
;
; Identifier not found:
; greet
;
; ┃ File: example.art
; ┃ Line: 9
; ┃
; ┃ 7 ║ ]
; ┃ 8 ║
; ┃ 9 ║► export greeting!
; ┃ 10 ║
; ┃ 11 ║ greet "Anonymous"
;
; Hint: Perhaps you meant... greeting ?
; or... repeat ?
; or... greater? ?
..........
; You can export private functions using the `.all` attribute
greeting: module [
greet: method [user :string][
print ~"Bye, bye, |user|!"
]
]
export.all greeting!
greet "Anonymous" ; Bye, bye, Anonymous!
""":
#=======================================================
let exportAll = hadAttr("all")
Expand Down Expand Up @@ -1175,6 +1221,57 @@ proc defineLibrary*() =
# TODO(Core\module) add documentation example
# labels: library, documentation, easy
example = """
ui: module [
namedRule: method [title :string width :integer][
title: ~" |title| "
pad.center.with: '=' title width
]
section: method.public [title :string content :string width :integer][
~{
|\namedRule title width|
|content|
|\namedRule title width|
}
]
]
export ui!
print section "Hello" "World" 50
; ===================== Hello ======================
; World
; ===================== Hello ======================
print set? 'ui ; true
print set? 'namedRule ; false
print set? 'section ; true
..........
ui: [
init: method [symbol :char][
\symbol: symbol
]
namedRule: method [title :string width :integer][
title: ~" |title| "
pad.center.with: \symbol title width
]
section: method.public [title :string content :string width :integer][
~{
|\namedRule title width|
|content|
|\namedRule title width|
}
]
]
export module.with: ['~'] ui!
print section "Example" "This is an example" 40
; ~~~~~~~~~~~~~~~ Example ~~~~~~~~~~~~~~~~
; This is an example
; ~~~~~~~~~~~~~~~ Example ~~~~~~~~~~~~~~~~
""":
#=======================================================
var definitions: ValueDict = newOrderedTable[string,Value]()
Expand Down
2 changes: 1 addition & 1 deletion src/library/Net.nim
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ proc defineLibrary*() =
"routes" : {Block, Function}
},
attrs = {
"port" : ({Integer},"use given port"),
"port" : ({Integer},"use given port. Default: 18966"),
"silent" : ({Logical},"don't print info log"),
"chrome" : ({Logical},"open in Chrome windows as an app")
},
Expand Down
12 changes: 6 additions & 6 deletions src/library/Strings.nim
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,9 @@ proc defineLibrary*() =
str: "hello World, 你好!"
lower 'str ; str: "hello world, 你好!"
..........
ch: `A`
ch: 'A'
lower ch
; => `a`
; => 'a'
""":
#=======================================================
if xKind==String: push(newString(x.s.toLower()))
Expand Down Expand Up @@ -904,9 +904,9 @@ proc defineLibrary*() =
str: "hello World, 你好!"
upper 'str ; str: "HELLO WORLD, 你好!"
..........
ch: `a`
ch: 'a'
upper ch
; => `A`
; => 'A'
""":
#=======================================================
if xKind==String: push(newString(x.s.toUpper()))
Expand Down Expand Up @@ -974,9 +974,9 @@ proc defineLibrary*() =
attrs = NoAttrs,
returns = {Logical},
example = """
ascii? `d` ; true
ascii? 'd' ; true
..........
ascii? `😀` ; false
ascii? '😀' ; false
ascii? "hello world" ; true
ascii? "Hællø wœrld" ; false
Expand Down
37 changes: 36 additions & 1 deletion src/library/System.nim
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,41 @@ proc defineLibrary*() =
split.lines execute "ls"
; => ["tests" "var" "data.txt"]
......................
execute.args: ["-s"] "ls"
; => total 15
; 0 aoc
; 0 architectures
; 4 bundle
; 0 cave
; 4 cmd.c
; 1 expr.art
; 0 galilee
; 4 generic_klist.c
; 1 jquery.js
; 0 shell
; 1 test.art
......................
execute.code "ls"
; => [output:aoc
; architectures
; bundle
; cave
; cmd.c
; expr.art
; galilee
; generic_klist.c
; jquery.js
; shell
; test.art
; code:0]
......................
; This prints the output directly
; And only returns the exit code.
execute.code.directly "ls"
aoc bundle cmd.c galilee jquery.js test.art
architectures cave expr.art generic_klist.c shell
=> 0
""":
#=======================================================
when defined(SAFE): Error_OperationNotPermitted("execute")
Expand Down Expand Up @@ -340,7 +375,7 @@ proc defineLibrary*() =
print "done. let's continue..."
..........
print "waiting for 2 seconds
print "waiting for 2 seconds"
pause 2:s ; let's sleep for a while
Expand Down
Loading

0 comments on commit 6f10987

Please sign in to comment.