Skip to content

Commit 90d9ba2

Browse files
committed
Merge upstream into master
2 parents 0cd2df7 + 58b3a5e commit 90d9ba2

File tree

540 files changed

+128678
-19141
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

540 files changed

+128678
-19141
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ site
33
janet_modules
44
build
55
/jpm/
6+
7+
.vscode

README.md

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# janet-lang.org
22

3-
[![Gitter](https://badges.gitter.im/janet-language/website.svg)](https://gitter.im/janet-language/website?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
3+
[![Zulip](https://img.shields.io/badge/zulip-join_chat-brightgreen.svg)](https://janet.zulipchat.com)
44

55
This is the source code for the website of the [Janet](https://janet-lang.org) programming
66
language. It is a static website built with [mendoza](https://github.com/bakpakin/mendoza), a
@@ -42,21 +42,19 @@ documentation tool, but of course is written in and is a dialect of Janet. See
4242

4343
## Adding Examples
4444

45-
Simply add a file with the name of the binding you are giving examples for to the examples
46-
directory, with the `.janet` suffix. If the binding includes the `/` character, replace it with
47-
an underscore - this works because no bindings in the core use an underscore. For example, the
48-
binding `array/new` has examples in the `examples/array_new.janet` file.
45+
Simply add a file with the name of the binding you are giving examples
46+
for to the `examples` directory, with the `.janet` suffix.
4947

5048
To cope with some of Janet's symbols having names with characters that
5149
are not-so-friendly to certain filesystem and/or operating system
5250
combinations, an escaping scheme is used.
5351

54-
For a given symbol, use the `content/examples.janet` script to
52+
For a given symbol, use the `content/api/examples.janet` script to
5553
generate an appropriate filename. For example, for `array/new`,
5654
invoking:
5755

5856
```
59-
$ janet content/examples.janet array/new
57+
$ janet content/api/examples.janet array/new
6058
```
6159

6260
should give the output:
@@ -65,12 +63,10 @@ should give the output:
6563
array_47new.janet
6664
```
6765

68-
If such a file already exists, you can simply append your example code
69-
to the existing file.
66+
If such a file already exists, you can simply append your example code the existing file.
7067

71-
When building the site, the new examples will be included in the
72-
generated documentation. Make sure that your example has correct janet
73-
syntax, as syntax errors will cause the entire site to not build. If
74-
the example has valid syntax (has a 0 exit code when loaded with
75-
`janet -k example/my-fn.janet`), there may be a bug in the mendoza
76-
janet syntax highlighter in which case please open a bug in mendoza.
68+
When building the site, the new examples will be included in the generated documentation. Make
69+
sure that your example has correct janet syntax, as syntax errors will cause the entire site
70+
to not build. If the example has valid syntax (has a 0 exit code when loaded with
71+
`janet -k example/my-fn.janet`), there may be a bug in the mendoza janet syntax
72+
highlighter and you open a bug in mendoza.

content/api/examples.janet

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
(def replacer
2+
(peg/compile
3+
~(% (any (+ (/ '(set "%*/:<>?")
4+
,|(string "_" (0 $))) '1)))))
5+
6+
(defn sym-to-filename
7+
``
8+
Convert a symbol to a filename. Certain filenames are not allowed on
9+
various operating systems.
10+
``
11+
[fname]
12+
(string "examples/" ((peg/match replacer fname) 0) ".janet"))
13+
14+
(defn main
15+
[& args]
16+
(def symbol-name (get args 1))
17+
(assert symbol-name "please specify a symbol name")
18+
(print (string/slice (sym-to-filename symbol-name)
19+
(length "examples/"))))
20+

content/docs/data_structures/tuples.mdz

Lines changed: 81 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,87 @@ by the contents of another array.
7575

7676
## Bracketed tuples
7777

78-
Under the hood, there are two kinds of tuples: bracketed and non-bracketed. We
79-
have seen above that bracket tuples are used to create a tuple with @code`[]`
80-
characters (ie: a tuple literal). The way a tuple literal is interpreted by
81-
the compiler is one of the few ways in which bracketed tuples and non-bracketed
82-
tuples differ:
83-
84-
@ul{@li{Bracket tuples are interpreted as a tuple constructor rather than a
85-
function call by the compiler.}
86-
@li{When printed via @code`pp`, bracket tuples are printed with square
87-
brackets instead of parentheses.}
88-
@li{When passed as an argument to @code`tuple/type`, bracket tuples will
89-
return @code`:brackets` instead of @code`:parens`.}}
90-
91-
In all other ways, bracketed tuples behave identically to normal tuples. It is
92-
not recommended to use bracketed tuples for anything outside of macros and
93-
tuple constructors (ie: tuple literals).
78+
Under the hood, there are two kinds of tuples:
79+
normal (aka paren or non-bracketed), and bracketed ones.
80+
81+
@codeblock[janet]```
82+
# bracket tuples
83+
(def b-1 (tuple/brackets 1 2 3))
84+
(def b-2 '[1 2 3])
85+
86+
# paren tuples
87+
(def p-1 (tuple 1 2 3))
88+
(def p-2 '(1 2 3))
89+
90+
# tuple/type can distinguish between tuples
91+
(tuple/type b-1) # -> :brackets
92+
(tuple/type b-2) # -> :brackets
93+
94+
(tuple/type p-1) # -> :parens
95+
(tuple/type p-2) # -> :parens
96+
```
97+
98+
The compiler interprets bracket tuples as a tuple constructor rather than
99+
a function call.
100+
101+
@codeblock[janet]```
102+
# create a function object that returns a tuple
103+
(def c-1 (compile (tuple/brackets 1 2 3)))
104+
105+
# create and return a (paren) tuple with elements 1, 2 and 3
106+
(c-2) # -> (1 2 3)
107+
108+
# create a function object with a call to +
109+
(def c-2 (compile (tuple '+ 1 2 3)))
110+
111+
# call the function named by + with 1, 2 and 3 as arguments
112+
(c-2) # -> 6
113+
```
114+
115+
Considering that functions evaluate their arguments and the tuple literal (@code`[]`)
116+
evaluates to a normal (paren) tuple, the behavior of @code`tuple/type` can be
117+
surprising. It's best to use it with quoted values.
118+
119+
@codeblock[janet]```
120+
(tuple/type [1 2 3]) # -> :parens
121+
122+
(tuple/type '(1 2 3)) # -> :parens
123+
(tuple/type '[1 2 3]) # -> :brackets
124+
125+
(eval-string "[1 2 3]") # -> (1 2 3)
126+
(eval-string "'[1 2 3]") # -> [1 2 3]
127+
```
128+
129+
When printed via @code`pp`, bracket tuples are printed with square brackets
130+
instead of parentheses.
131+
132+
@codeblock[janet]```
133+
(pp (tuple/brackets 1 2 3))
134+
# [1 2 3]
135+
136+
(pp (tuple 1 2 3))
137+
# (1 2 3)
138+
139+
# this can be surprising
140+
(pp [1 2 3])
141+
# (1 2 3)
142+
```
143+
144+
Bracket and paren tuples with the same contents are not @code`=`,
145+
but are @code`deep=`.
146+
147+
@codeblock[janet]```
148+
(= '() '[]) # -> false
149+
(deep= '() '[]) # -> true
150+
151+
(= '(1 2 3) '[1 2 3]) # -> false
152+
(deep= '(1 2 3) '[1 2 3]) # -> true
153+
```
154+
155+
In all other ways, bracketed tuples behave identically to normal tuples.
156+
157+
You should use bracketed tuples only in code that will be evaluated later
158+
(for example in macros).
94159

95160
## More functions
96161

content/docs/documentation.mdz

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ Janet's built-in documentation viewer, @code`(doc)`, understands a subset of
9999
Markdown and will indent docstrings that use this subset in an intelligent way.
100100
The subset of Markdown includes:
101101

102-
@ul{@li{numbered and unnumbered lists}
103-
@li{codeblocks}
104-
@li{blockquotes}}
102+
@ul{@li{top-level paragraphs}
103+
@li{single-level ordered and unordered lists}
104+
@li{code blocks}}
105105

106106
Other Markdown-formatted text (e.g. code spans) are simply treated as ordinary
107107
text.

content/docs/fibers/index.mdz

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,20 @@ an error clause is evaluated.
102102
([err] (print "oops")))
103103
# Evaluates to 6 - no error thrown
104104
```)
105+
106+
The @code[signal] function can be used to raise a particular signal.
107+
The function's first argument, @code[what], specifies the signal,
108+
while the second argument, @code[x], is an arbitrary payload value.
109+
110+
@codeblock[janet](```
111+
(try
112+
(signal :error 1)
113+
([err] (print "got error: " err)))
114+
# evaluates to nil and prints "got error: 1"
115+
116+
(defer (pp :hey)
117+
(signal :user4 :hello)
118+
(pp :unreached))
119+
# only prints :hey
120+
```)
121+

content/docs/functions.mdz

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,38 @@ The first expression creates an anonymous function that adds twice the first
6464
argument to the second, and then calls that function with arguments 10 and 20.
6565
This will return (10 + 10 + 20) = 40.
6666

67+
Another way an anonymous function can be created is via the @code`|`
68+
reader macro (or the equivalent @code`short-fn` macro):
69+
70+
@codeblock[janet](```
71+
# These are functionally equivalent
72+
((fn [x] (+ x x)) 1) # -> 2
73+
(|(+ $ $) 1) # -> 2
74+
75+
# Multiple arguments may be referenced
76+
(|(+ $0 $1 $2) 1 2 3) # -> 6
77+
78+
# All arguments can be accessed as a tuple
79+
(|(map inc $&) -1 0 1 2) # -> @[0 1 2 3]
80+
81+
# | is shorthand for (short-fn ...)
82+
((short-fn (+ $ $)) 1) # -> 2
83+
84+
# Other constructs can be used after |
85+
(|[:x $] :y) # -> [:x :y]
86+
(|{:a $0 :b $1} 1 2) # -> {:a 1 :b 2}
87+
88+
# Will throw an error about the wrong arity
89+
(|(inc $) 1 2)
90+
# Will not throw an error about the wrong arity
91+
(|(get $& 0) 1 2)
92+
```)
93+
94+
Within the above sorts of contexts the symbol @code`$` refers to the
95+
first (or zero-th) argument. Similarly, @code`$0`, @code`$1`,
96+
etc. refer to the arguments at index 0, 1, etc. and @code`$&` refers
97+
to all remaining arguments as a tuple.
98+
6799
There is a common macro @code[defn] that can be used for creating functions and
68100
immediately binding them to a name. @code[defn] works as expected at both the
69101
top level and inside another form. There is also the corresponding macro

content/docs/index.mdz

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,9 @@ gmake test CC=gcc
104104

105105
### Windows
106106

107-
@ol{@li{Install @link[https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=Community&rel=15#][Visual Studio]
108-
or @link[https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=BuildTools&rel=15#][Visual Studio Build Tools]}
107+
@ol{@li{Install @link[https://visualstudio.microsoft.com/downloads/#visual-studio-community-2022][Visual Studio]
108+
or @link[https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2022][Visual Studio Build Tools]
109+
\(you will need the latest MSVC build tools and the Windows SDK components)}
109110
@li{Run a Visual Studio Command Prompt (@code`cl.exe` and @code`link.exe`
110111
need to be on the PATH)}
111112
@li{@code[cd] to the directory with Janet}
@@ -141,7 +142,7 @@ cd janet
141142
meson setup SmallBuild
142143
cd SmallBuild
143144
meson configure -Dsingle_threaded=true -Dassembler=false -Ddocstrings=false \
144-
-Dreduced_os=true -Dtyped_array=false -Dsourcemaps=false -Dpeg=false \
145+
-Dreduced_os=true -Dsourcemaps=false -Dpeg=false \
145146
-Dint_types=false --optimization=s -Ddebug=false
146147
ninja
147148
# ./janet should be about 40% smaller than the default build as of 2019-10-13
@@ -224,9 +225,11 @@ If at any time you want to see short documentation on a binding, use the
224225

225226
@codeblock[janet](```(doc defn) # -> Prints the documentation for "defn"```)
226227

227-
To see a list of all global functions in the REPL, type the command
228+
To see a list of all global bindings in the REPL, use the @code[doc] macro
229+
without arguments to print them out.
228230

229-
@codeblock[janet](```(all-bindings)```)
231+
@codeblock[janet](```(doc)```)
230232

231-
Which will print out every global binding in the Janet REPL. You can also browse
232-
the @link[/doc.html][core library API] on the website.
233+
Note: see the @code[all-bindings] function for related functionality.
234+
235+
You can also browse the @link[/doc.html][core library API] on the website.

content/docs/modules.mdz

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ more natural way.
6262
(p/join (os/cwd) "temp")
6363
```
6464

65+
By default, the following files will be searched for in order for
66+
@code`(import foo)`:
67+
68+
* @code`foo.jimage`
69+
* @code`foo.janet`
70+
* @code`foo/init.janet`
71+
* @code`foo.<native-extension>`
72+
73+
where @code`<native-extension>` is the file extension for shared
74+
objects / libraries for the current platform (e.g. `dll` for Windows,
75+
`dylib` for macos, and `so` for other unix-likes). This is a
76+
"first-match wins" arrangement so at most one file will be imported.
77+
6578
## Custom loaders (@code`module/paths` and @code`module/loaders`)
6679

6780
The @code`module/paths` and @code`module/loaders` data structures

content/docs/peg.mdz

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,13 @@ compiled to bytecode).
178178
@tr{@td{@code`(! patt)` }
179179
@td{ Alias for @code`(not patt)` }}
180180

181-
@tr{@td{@code`(look offset patt)` }
182-
@td{ Matches only if patt matches at a fixed offset. offset can be any
183-
integer. patt will not produce captures and the peg will not advance any
184-
characters. }}
181+
@tr{@td{@code`(look ?offset patt)` }
182+
@td{ Matches only if patt matches at a fixed offset. offset can be
183+
any integer and defaults to 0. The peg will not advance any
184+
characters. }}
185185

186-
@tr{@td{@code`(> offset patt)` }
187-
@td{ Alias for @code`(look offset patt)` }}
186+
@tr{@td{@code`(> offset ?patt)` }
187+
@td{ Alias for @code`(look offset ?patt)` }}
188188

189189
@tr{@td{@code`(to patt)` }
190190
@td{ Match up to patt (but not including it). If the end of the input
@@ -217,7 +217,17 @@ compiled to bytecode).
217217
@code`patt` cannot match more than @code`window-patt`; it will
218218
see end-of-input at the end of the substring matched by
219219
@code`window-patt`. If @code`patt` also succeeds, @code`sub` will
220-
advance to the end of what @code`window-patt` matched. }}}
220+
advance to the end of what @code`window-patt` matched. }}
221+
222+
@tr{@td{@code`(split separator-patt patt)` }
223+
@td{ Split the remaining input by @code`separator-patt`, and execute
224+
@code`patt` on each substring. @code`patt` will execute with its
225+
input constrained to the next instance of @code`separator-patt`,
226+
as if narrowed by @code`(sub (to separator-patt) ...)`.
227+
@code`split` will continue to match separators and patterns until
228+
it reaches the end of the input; if you don't want to match to the
229+
end of the input you should first narrow it with
230+
@code`(sub ... (split ...))`. }}}
221231

222232
PEGs try to match an input text with a pattern in a greedy manner. This means
223233
that if a rule fails to match, that rule will fail and not try again. The only
@@ -337,6 +347,14 @@ grammars simpler.
337347
@tr{@td{@code`(drop patt)` }
338348
@td{ Ignores (drops) all captures from patt. }}
339349

350+
@tr{@td{@code`(only-tags patt)` }
351+
@td{ Ignores all captures from @code`patt`, while making tagged
352+
captures within @code`patt` available for future back-referencing. }}
353+
354+
@tr{@td{@code`(nth index patt ?tag)` }
355+
@td{ Capture one of the captures in @code`patt` at @code`index`.
356+
If no such capture exists, then the match fails. }}
357+
340358
@tr{@td{@code`(uint num-bytes ?tag)` }
341359
@td{ Capture a little endian, unsigned, two's complement integer from
342360
@code`num-bytes`. Works for 1 to 8 byte integers. }}

0 commit comments

Comments
 (0)