update built-in examples to use newly introduced syntax

This commit is contained in:
りき萌 2025-09-03 16:57:10 +02:00
parent b5cdfdb1b6
commit 914da923f7
4 changed files with 53 additions and 55 deletions

View file

@ -603,29 +603,19 @@ fn is_prefix_token((kind, spaces): (TokenKind, Spaces)) -> bool {
}
fn prefix(p: &mut Parser) -> Closed {
let (kind, spaces) = p.peek_with_spaces();
match kind {
match p.peek() {
TokenKind::Ident => one(p, NodeKind::Ident),
TokenKind::Tag => one(p, NodeKind::Tag),
TokenKind::Number => one(p, NodeKind::Number),
TokenKind::Color => one(p, NodeKind::Color),
TokenKind::LBrack => list(p),
TokenKind::Minus if spaces.pair() == (true, false) => unary(p),
TokenKind::Minus => unary(p),
TokenKind::Not => unary(p),
TokenKind::LParen => paren(p),
TokenKind::Backslash => lambda(p),
TokenKind::If => if_expr(p),
TokenKind::Minus if spaces.pair() == (false, true) => {
let span = p.span();
p.emit(Diagnostic::error(
span,
"`-` operator must be surrounded by an equal amount of spaces",
));
p.advance_with_error()
}
_ => {
assert!(
!is_prefix_token(p.peek_with_spaces()),

View file

@ -28,8 +28,6 @@ pub enum TokenKind {
Greater,
GreaterEqual,
Not,
Dot,
Pipe,
// Punctuation
Newline,
@ -42,6 +40,8 @@ pub enum TokenKind {
Colon,
Backslash,
RArrow,
Dot,
Pipe,
// Keywords
Underscore,

View file

@ -1,5 +1,15 @@
# Introduction to rakugaki
*Warning: Neither rakugaki, nor this introductory manual is finished.*
While it will always use the most up-to-date and recommended syntax, there are things it does cover, and it will probably be rebuilt to improve coverage of features once the app stabilises a bit.
For now, I recommend cross-referencing it with the following documents:
- [haku language reference](haku.html)
- [System library](system.html)
---
Welcome to rakugaki!
I hope you've been having fun fiddling with the app so far.
@ -28,7 +38,7 @@ In case you edited anything in the input box on the right, paste the following t
-- and see what happens!
withDotter \d ->
stroke 8 #000 (d To)
stroke 8 #000 d.To
```
rakugaki is a drawing program for digital scribbles and other pieces of art.
@ -86,8 +96,8 @@ If you want to draw multiple scribbles, you can wrap them into a list, which we
-- Draw two colorful dots instead of one!
withDotter \d ->
[
stroke 8 #F00 (d To + vec 4 0)
stroke 8 #00F (d To + vec -4 0)
stroke 8 #F00 (d.To + vec 4 0)
stroke 8 #00F (d.To + vec -4 0)
]
```
@ -108,12 +118,12 @@ It'll draw the first inner list, which contains two scribbles, and then it'll dr
withDotter \d ->
[
[
stroke 8 #F00 (d To + vec 4 0)
stroke 8 #00F (d To + vec -4 0)
stroke 8 #F00 (d.To + vec 4 0)
stroke 8 #00F (d.To + vec -4 0)
]
[
stroke 8 #FF0 (d To + vec 0 4)
stroke 8 #0FF (d To + vec 0 -4)
stroke 8 #FF0 (d.To + vec 0 4)
stroke 8 #0FF (d.To + vec 0 -4)
]
]
```
@ -136,7 +146,7 @@ Recall that super simple brush from before...
```haku
withDotter \d ->
stroke 8 #000 (d To)
stroke 8 #000 d.To
```
This reads as "given a dotter, output a stroke that's 8 pixels wide, has the color `#000`, and is drawn at the dotter's `To` coordinates."
@ -169,6 +179,7 @@ If you reorder or remove any one of them, your brush isn't going to work!
You can also specify an alpha channel, for transparent colors---`#RRGGBBAA`, or `#RGBA`.
- The third ingredient is the stroke's _position_.
`d.To` is the position of your mouse cursor.
Positions in haku are represented using mathematical _vectors_, which, when broken down into pieces, are just lists of some numbers.
@ -187,22 +198,22 @@ Likewise, negative X coordinates go leftwards, and negative Y coordinates go upw
---
Vectors in haku are obtained with another function---`vec`---though we don't use it in the basic example, because `d To` already is a vector.
Vectors support all the usual math operators though, so if we wanted to, we could, for example, add a vector to `d To`, thus moving the position of the dot relative to the mouse cursor:
Vectors in haku are obtained with another function---`vec`---though we don't use it in the basic example, because `d.To` already is a vector.
Vectors support all the usual math operators though, so if we wanted to, we could, for example, add a vector to `d.To`, thus moving the position of the dot relative to the mouse cursor:
```haku
withDotter \d ->
stroke 8 #000 (d To + vec 10 0) -- moved 10 pixels rightwards
stroke 8 #000 (d.To + vec 10 0) -- moved 10 pixels rightwards
```
Also note how the `d To + vec 10 0` expression is parenthesized.
Also note how the `d.To + vec 10 0` expression is parenthesized.
This is because otherwise, its individual parts would be interpreted as separate arguments to `stroke`, which is not what we want!
Anyways, with all that, we let haku mix all the ingredients together, and get a black dot under the cursor.
```haku
withDotter \d ->
stroke 8 #000 (d To)
stroke 8 #000 d.To
```
Nice!
@ -221,10 +232,10 @@ Let's fix that by drawing a `line` instead!
```haku
withDotter \d ->
stroke 8 #000 (line (d From) (d To))
stroke 8 #000 (line d.From d.To)
```
We replace the singular position `d To` with a `line`. `line` expects two arguments, which are vectors defining the line's start and end points.
We replace the singular position `d.To` with a `line`. `line` expects two arguments, which are vectors defining the line's start and end points.
For the starting position we use a _different_ property of `d`, which is `From`---this is the _previous_ value of `To`, which allows us to draw a continuous line.
::: aside
@ -239,8 +250,8 @@ haku also supports other kinds of shapes: circles and rectangles.
```haku
withDotter \d ->
[
stroke 8 #F00 (circle (d To + vec -16 0) 16)
stroke 8 #00F (rect (d To + vec 0 -16) (vec 32 32))
stroke 8 #F00 (circle (d.To + vec -16 0) 16)
stroke 8 #00F (rect (d.To + vec 0 -16) (vec 32 32))
]
```
@ -292,13 +303,15 @@ In fact, here's the full order of operations in haku for reference:
1. Tight
1. Function applications: `.`
1. Arithmetic: `+`, `-`, `*`, `/`
1. Comparisons: `==`, `!=`, `<`, `<=`, `>`, `>=`
1. Function calls
1. Loose
1. Arithmetic
1. Function applications: `.`
1. Arithmetic and pipelines `|`
1. Comparisons
1. Variables: `:`, `=`

View file

@ -15,7 +15,7 @@ color: #000
thickness: 8
withDotter \\d ->
stroke thickness color (line (d From) (d To))
stroke thickness color (line d.From d.To)
`.trim(),
},
@ -27,7 +27,7 @@ color: #000
thickness: 48
withDotter \\d ->
stroke thickness color (line (d From) (d To))
stroke thickness color (line d.From d.To)
`.trim(),
},
@ -40,14 +40,10 @@ thickness: 4
length: 5
duty: 0.5
or_: \\a, b ->
if (a) a
else b
withDotter \\d ->
visible? = mod (d Num) length < length * duty
visible? = d.Num |mod length < length * duty
if (visible?)
stroke thickness color (line (d From) (d To))
stroke thickness color (line d.From d.To)
else
()
`.trim(),
@ -61,7 +57,7 @@ color: #0003
thickness: 6
withDotter \\d ->
stroke thickness color (line (d From) (d To))
stroke thickness color (line d.From d.To)
`.trim(),
},
@ -76,10 +72,10 @@ wavelength: 1
withDotter \\d ->
pi = 3.14159265
a = sin (d Num * wavelength / pi) + 1 / 2
a = sin (d.Num * wavelength / pi) + 1 / 2
range = maxThickness - minThickness
thickness = a * range + minThickness
stroke thickness color (line (d From) (d To))
stroke thickness color (line d.From d.To)
`.trim(),
},
@ -93,22 +89,21 @@ amplitude: 50
wavelength: 1
mag: \\v ->
hypot (vecX v) (vecY v)
hypot vecX.v vecY.v
norm: \\u ->
l = mag u
u / vec l l
perpClockwise: \\v ->
vec (vecY v) -(vecX v)
vec vecY.v -(vecX.v)
withDotter \\d ->
pi = 3.14159265
a = sin (d Num * wavelength / pi) * amplitude
direction = (d To) - (d From)
clockwise = norm (perpClockwise direction) * vec a a
from = d From + clockwise
to = d To + clockwise
a = sin (d.Num * wavelength / pi) * amplitude
clockwise = norm (perpClockwise d.To-d.From) * vec a a
from = d.From + clockwise
to = d.To + clockwise
stroke thickness color (line from to)
`.trim(),
},
@ -121,16 +116,16 @@ wavelength: 0.1
thickness: 8
colorCurve: \\n ->
abs (cos n)
n |cos |abs
withDotter \\d ->
pi = 3.14159265
l = wavelength
r = colorCurve (d Num * l)
g = colorCurve (d Num * l + pi/3)
b = colorCurve (d Num * l + 2*pi/3)
r = colorCurve (d.Num * l)
g = colorCurve (d.Num * l + pi/3)
b = colorCurve (d.Num * l + 2*pi/3)
color = rgba r g b 1
stroke thickness color (line (d From) (d To))
stroke thickness color (line d.From d.To)
`.trim(),
},