update built-in examples to use newly introduced syntax
This commit is contained in:
parent
b5cdfdb1b6
commit
914da923f7
4 changed files with 53 additions and 55 deletions
|
@ -603,29 +603,19 @@ fn is_prefix_token((kind, spaces): (TokenKind, Spaces)) -> bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn prefix(p: &mut Parser) -> Closed {
|
fn prefix(p: &mut Parser) -> Closed {
|
||||||
let (kind, spaces) = p.peek_with_spaces();
|
match p.peek() {
|
||||||
match kind {
|
|
||||||
TokenKind::Ident => one(p, NodeKind::Ident),
|
TokenKind::Ident => one(p, NodeKind::Ident),
|
||||||
TokenKind::Tag => one(p, NodeKind::Tag),
|
TokenKind::Tag => one(p, NodeKind::Tag),
|
||||||
TokenKind::Number => one(p, NodeKind::Number),
|
TokenKind::Number => one(p, NodeKind::Number),
|
||||||
TokenKind::Color => one(p, NodeKind::Color),
|
TokenKind::Color => one(p, NodeKind::Color),
|
||||||
TokenKind::LBrack => list(p),
|
TokenKind::LBrack => list(p),
|
||||||
|
|
||||||
TokenKind::Minus if spaces.pair() == (true, false) => unary(p),
|
TokenKind::Minus => unary(p),
|
||||||
TokenKind::Not => unary(p),
|
TokenKind::Not => unary(p),
|
||||||
TokenKind::LParen => paren(p),
|
TokenKind::LParen => paren(p),
|
||||||
TokenKind::Backslash => lambda(p),
|
TokenKind::Backslash => lambda(p),
|
||||||
TokenKind::If => if_expr(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!(
|
assert!(
|
||||||
!is_prefix_token(p.peek_with_spaces()),
|
!is_prefix_token(p.peek_with_spaces()),
|
||||||
|
|
|
@ -28,8 +28,6 @@ pub enum TokenKind {
|
||||||
Greater,
|
Greater,
|
||||||
GreaterEqual,
|
GreaterEqual,
|
||||||
Not,
|
Not,
|
||||||
Dot,
|
|
||||||
Pipe,
|
|
||||||
|
|
||||||
// Punctuation
|
// Punctuation
|
||||||
Newline,
|
Newline,
|
||||||
|
@ -42,6 +40,8 @@ pub enum TokenKind {
|
||||||
Colon,
|
Colon,
|
||||||
Backslash,
|
Backslash,
|
||||||
RArrow,
|
RArrow,
|
||||||
|
Dot,
|
||||||
|
Pipe,
|
||||||
|
|
||||||
// Keywords
|
// Keywords
|
||||||
Underscore,
|
Underscore,
|
||||||
|
|
49
docs/rkgk.dj
49
docs/rkgk.dj
|
@ -1,5 +1,15 @@
|
||||||
# Introduction to rakugaki
|
# 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!
|
Welcome to rakugaki!
|
||||||
|
|
||||||
I hope you've been having fun fiddling with the app so far.
|
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!
|
-- and see what happens!
|
||||||
|
|
||||||
withDotter \d ->
|
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.
|
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!
|
-- Draw two colorful dots instead of one!
|
||||||
withDotter \d ->
|
withDotter \d ->
|
||||||
[
|
[
|
||||||
stroke 8 #F00 (d To + vec 4 0)
|
stroke 8 #F00 (d.To + vec 4 0)
|
||||||
stroke 8 #00F (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 ->
|
withDotter \d ->
|
||||||
[
|
[
|
||||||
[
|
[
|
||||||
stroke 8 #F00 (d To + vec 4 0)
|
stroke 8 #F00 (d.To + vec 4 0)
|
||||||
stroke 8 #00F (d To + vec -4 0)
|
stroke 8 #00F (d.To + vec -4 0)
|
||||||
]
|
]
|
||||||
[
|
[
|
||||||
stroke 8 #FF0 (d To + vec 0 4)
|
stroke 8 #FF0 (d.To + vec 0 4)
|
||||||
stroke 8 #0FF (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
|
```haku
|
||||||
withDotter \d ->
|
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."
|
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`.
|
You can also specify an alpha channel, for transparent colors---`#RRGGBBAA`, or `#RGBA`.
|
||||||
|
|
||||||
- The third ingredient is the stroke's _position_.
|
- 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.
|
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 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 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
|
```haku
|
||||||
withDotter \d ->
|
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!
|
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.
|
Anyways, with all that, we let haku mix all the ingredients together, and get a black dot under the cursor.
|
||||||
|
|
||||||
```haku
|
```haku
|
||||||
withDotter \d ->
|
withDotter \d ->
|
||||||
stroke 8 #000 (d To)
|
stroke 8 #000 d.To
|
||||||
```
|
```
|
||||||
|
|
||||||
Nice!
|
Nice!
|
||||||
|
@ -221,10 +232,10 @@ Let's fix that by drawing a `line` instead!
|
||||||
|
|
||||||
```haku
|
```haku
|
||||||
withDotter \d ->
|
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.
|
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
|
::: aside
|
||||||
|
@ -239,8 +250,8 @@ haku also supports other kinds of shapes: circles and rectangles.
|
||||||
```haku
|
```haku
|
||||||
withDotter \d ->
|
withDotter \d ->
|
||||||
[
|
[
|
||||||
stroke 8 #F00 (circle (d To + vec -16 0) 16)
|
stroke 8 #F00 (circle (d.To + vec -16 0) 16)
|
||||||
stroke 8 #00F (rect (d To + vec 0 -16) (vec 32 32))
|
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. Tight
|
||||||
|
|
||||||
|
1. Function applications: `.`
|
||||||
1. Arithmetic: `+`, `-`, `*`, `/`
|
1. Arithmetic: `+`, `-`, `*`, `/`
|
||||||
1. Comparisons: `==`, `!=`, `<`, `<=`, `>`, `>=`
|
1. Comparisons: `==`, `!=`, `<`, `<=`, `>`, `>=`
|
||||||
|
|
||||||
1. Function calls
|
1. Function calls
|
||||||
1. Loose
|
1. Loose
|
||||||
|
|
||||||
1. Arithmetic
|
1. Function applications: `.`
|
||||||
|
1. Arithmetic and pipelines `|`
|
||||||
1. Comparisons
|
1. Comparisons
|
||||||
1. Variables: `:`, `=`
|
1. Variables: `:`, `=`
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ color: #000
|
||||||
thickness: 8
|
thickness: 8
|
||||||
|
|
||||||
withDotter \\d ->
|
withDotter \\d ->
|
||||||
stroke thickness color (line (d From) (d To))
|
stroke thickness color (line d.From d.To)
|
||||||
`.trim(),
|
`.trim(),
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@ color: #000
|
||||||
thickness: 48
|
thickness: 48
|
||||||
|
|
||||||
withDotter \\d ->
|
withDotter \\d ->
|
||||||
stroke thickness color (line (d From) (d To))
|
stroke thickness color (line d.From d.To)
|
||||||
`.trim(),
|
`.trim(),
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -40,14 +40,10 @@ thickness: 4
|
||||||
length: 5
|
length: 5
|
||||||
duty: 0.5
|
duty: 0.5
|
||||||
|
|
||||||
or_: \\a, b ->
|
|
||||||
if (a) a
|
|
||||||
else b
|
|
||||||
|
|
||||||
withDotter \\d ->
|
withDotter \\d ->
|
||||||
visible? = mod (d Num) length < length * duty
|
visible? = d.Num |mod length < length * duty
|
||||||
if (visible?)
|
if (visible?)
|
||||||
stroke thickness color (line (d From) (d To))
|
stroke thickness color (line d.From d.To)
|
||||||
else
|
else
|
||||||
()
|
()
|
||||||
`.trim(),
|
`.trim(),
|
||||||
|
@ -61,7 +57,7 @@ color: #0003
|
||||||
thickness: 6
|
thickness: 6
|
||||||
|
|
||||||
withDotter \\d ->
|
withDotter \\d ->
|
||||||
stroke thickness color (line (d From) (d To))
|
stroke thickness color (line d.From d.To)
|
||||||
`.trim(),
|
`.trim(),
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -76,10 +72,10 @@ wavelength: 1
|
||||||
|
|
||||||
withDotter \\d ->
|
withDotter \\d ->
|
||||||
pi = 3.14159265
|
pi = 3.14159265
|
||||||
a = sin (d Num * wavelength / pi) + 1 / 2
|
a = sin (d.Num * wavelength / pi) + 1 / 2
|
||||||
range = maxThickness - minThickness
|
range = maxThickness - minThickness
|
||||||
thickness = a * range + minThickness
|
thickness = a * range + minThickness
|
||||||
stroke thickness color (line (d From) (d To))
|
stroke thickness color (line d.From d.To)
|
||||||
`.trim(),
|
`.trim(),
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -93,22 +89,21 @@ amplitude: 50
|
||||||
wavelength: 1
|
wavelength: 1
|
||||||
|
|
||||||
mag: \\v ->
|
mag: \\v ->
|
||||||
hypot (vecX v) (vecY v)
|
hypot vecX.v vecY.v
|
||||||
|
|
||||||
norm: \\u ->
|
norm: \\u ->
|
||||||
l = mag u
|
l = mag u
|
||||||
u / vec l l
|
u / vec l l
|
||||||
|
|
||||||
perpClockwise: \\v ->
|
perpClockwise: \\v ->
|
||||||
vec (vecY v) -(vecX v)
|
vec vecY.v -(vecX.v)
|
||||||
|
|
||||||
withDotter \\d ->
|
withDotter \\d ->
|
||||||
pi = 3.14159265
|
pi = 3.14159265
|
||||||
a = sin (d Num * wavelength / pi) * amplitude
|
a = sin (d.Num * wavelength / pi) * amplitude
|
||||||
direction = (d To) - (d From)
|
clockwise = norm (perpClockwise d.To-d.From) * vec a a
|
||||||
clockwise = norm (perpClockwise direction) * vec a a
|
from = d.From + clockwise
|
||||||
from = d From + clockwise
|
to = d.To + clockwise
|
||||||
to = d To + clockwise
|
|
||||||
stroke thickness color (line from to)
|
stroke thickness color (line from to)
|
||||||
`.trim(),
|
`.trim(),
|
||||||
},
|
},
|
||||||
|
@ -121,16 +116,16 @@ wavelength: 0.1
|
||||||
thickness: 8
|
thickness: 8
|
||||||
|
|
||||||
colorCurve: \\n ->
|
colorCurve: \\n ->
|
||||||
abs (cos n)
|
n |cos |abs
|
||||||
|
|
||||||
withDotter \\d ->
|
withDotter \\d ->
|
||||||
pi = 3.14159265
|
pi = 3.14159265
|
||||||
l = wavelength
|
l = wavelength
|
||||||
r = colorCurve (d Num * l)
|
r = colorCurve (d.Num * l)
|
||||||
g = colorCurve (d Num * l + pi/3)
|
g = colorCurve (d.Num * l + pi/3)
|
||||||
b = colorCurve (d Num * l + 2*pi/3)
|
b = colorCurve (d.Num * l + 2*pi/3)
|
||||||
color = rgba r g b 1
|
color = rgba r g b 1
|
||||||
stroke thickness color (line (d From) (d To))
|
stroke thickness color (line d.From d.To)
|
||||||
`.trim(),
|
`.trim(),
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue