Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patterned expressions #36

Merged
merged 5 commits into from
Feb 25, 2025
Merged

Patterned expressions #36

merged 5 commits into from
Feb 25, 2025

Conversation

unlessgames
Copy link
Collaborator

I've implemented patterns on the right side of expression * / and %.

While the same thing for Bjorklund parameters still has to be done, these seem to work fine.

Repository owner deleted a comment from github-actions bot Oct 27, 2024
Repository owner deleted a comment from github-actions bot Oct 27, 2024
Copy link

Benchmark for 59aa142

Click to view benchmark
Test Base PR %
Cycle/Generate 58.5±0.80µs 62.7±0.43µs +7.18%
Cycle/Parse 330.2±6.55µs 325.0±4.94µs -1.57%
Rust Phrase/Clone 432.4±3.56ns 432.9±6.85ns +0.12%
Rust Phrase/Create 67.9±1.25µs 66.0±1.09µs -2.80%
Rust Phrase/Run 682.8±4.27µs 684.4±7.64µs +0.23%
Rust Phrase/Seek 161.0±304.47µs 153.6±290.00µs -4.60%
Scripted Phrase/Clone 669.4±14.06ns 658.1±4.12ns -1.69%
Scripted Phrase/Create 901.2±11.50µs 893.5±14.02µs -0.85%
Scripted Phrase/Run 1853.6±17.12µs 1848.5±21.57µs -0.28%
Scripted Phrase/Seek 365.6±708.67µs 252.4±513.49µs -30.96%

Copy link

Benchmark for e66ef7d

Click to view benchmark
Test Base PR %
Cycle/Generate 58.9±0.86µs 57.9±0.71µs -1.70%
Cycle/Parse 323.2±11.59µs 332.1±7.26µs +2.75%
Rust Phrase/Clone 434.2±19.86ns 454.4±5.40ns +4.65%
Rust Phrase/Create 66.0±0.72µs 67.1±1.07µs +1.67%
Rust Phrase/Run 685.8±10.17µs 670.3±6.60µs -2.26%
Rust Phrase/Seek 164.7±340.90µs 159.7±301.55µs -3.04%
Scripted Phrase/Clone 686.5±7.42ns 651.1±9.53ns -5.16%
Scripted Phrase/Create 895.5±39.98µs 888.6±22.10µs -0.77%
Scripted Phrase/Run 1857.3±24.24µs 1875.4±58.30µs +0.97%
Scripted Phrase/Seek 366.9±711.55µs 250.8±508.47µs -31.64%

@emuell
Copy link
Owner

emuell commented Nov 1, 2024

Looks good to me. Given this a quick try and things works fine so far.

While testing, I've noticed that negative numbers are allowed in the right side expressions for * and /. I can't make things crash when using them, but they likely will do weird things?

Example:

  emit = cycle("c8*<0 -1>")

Let me know if you want to merge this now or if you want to add support for bjorklunds as well.

@unlessgames
Copy link
Collaborator Author

unlessgames commented Nov 4, 2024

I was thinking of adding support for bjorklund but the fact that there are three arguments there that would need to be combined in multiple ways got me a bit stuck. We'd need a more general function here that could merge event streams into each other.

Something like (a: Vec<(Span, Vec<Value>)>, b: Vec<Vec<Event>>) -> Vec<(Span, Vec<Value>)> that would fold events from b into a (creating new events when the stream is denser) and appending values. This could be applied any number of times to get like a (Span, [Value, Value, Value]) for each step (in case of the bjorklund).

In strudel, negative numbers will silently apply zero basically, resulting in empty chunks. I wonder how hard it could be to implement a reverse functionality that could be used when the multiplier is negative.

@emuell
Copy link
Owner

emuell commented Feb 23, 2025

Just wanted to report this issue here:

Right side expressions of * / and % currently can't be padded with white-space.

This works:
cycle("[c4]*2")

This fails to parse:
cycle("[c4]* 2")

Seems to be fixed in this branch too.

The changes are useful already. Let's clear and merge them, and deal with the Bjorklund parameters in a new PR?

@unlessgames unlessgames marked this pull request as ready for review February 23, 2025 12:27
@unlessgames
Copy link
Collaborator Author

Let's do that!

@emuell emuell force-pushed the feature/cycle-patterned-expressions branch from 7209353 to e0da30a Compare February 23, 2025 13:02
Copy link

Benchmark for 4639dbe

Click to view benchmark
Test Base PR %
Cycle/Generate 59.2±1.14µs 58.0±0.56µs -2.03%
Cycle/Parse 323.5±9.13µs 326.0±9.74µs +0.77%
Rust Phrase/Clone 445.3±7.79ns 433.7±4.28ns -2.60%
Rust Phrase/Create 67.2±1.27µs 67.1±1.37µs -0.15%
Rust Phrase/Run 677.4±6.12µs 676.5±6.28µs -0.13%
Rust Phrase/Seek 164.1±311.05µs 160.6±304.04µs -2.13%
Scripted Phrase/Clone 625.2±9.68ns 627.7±5.87ns +0.40%
Scripted Phrase/Create 935.5±8.77µs 945.2±14.96µs +1.04%
Scripted Phrase/Run 1840.5±13.26µs 1805.1±49.19µs -1.92%
Scripted Phrase/Seek 230.4±466.41µs 229.1±463.98µs -0.56%

@emuell
Copy link
Owner

emuell commented Feb 23, 2025

Wanted to update the limitations in the API docs, but am a bit unsure here:

bd(3, 8)*2 or bd(3, 8)*<2, 2> does not work, so:

Does that make sense?

REMOVE
* Operators currently only accept numbers on the right side (`a3*2` is valid, `a3*<1 2>` is not)

ADD
* Operators on the right side of bjorklund expressions are not yet supported (e.g. `bd(3, 8)*2` is not)

@unlessgames
Copy link
Collaborator Author

That doesn't sound right. Operators should work for Bjorklund, what is not supported is patterned parameters for it.

These should be fine:

bd(3,8)*2
bd(3,8)*<1 2>

This is not supported:

bd(<3 6>, 8)

@emuell
Copy link
Owner

emuell commented Feb 23, 2025

These should be fine

Currently doesn't work.

Also fine with me to postpone a fix for this. decide you...

@unlessgames
Copy link
Collaborator Author

unlessgames commented Feb 23, 2025

Oh true, I've missed that. Unfortunately operators are read right recursively (to satisfy the peg parser spec) and that's also how they are applied so stacking operators is actually not working properly. Note, this is not specific to Bjorklund, things like [a b]*2/2 don't work as they should either. This will not be an easy/fast fix though, so maybe we should merge this for now.

@emuell emuell merged commit 4470b95 into master Feb 25, 2025
2 checks passed
@emuell emuell deleted the feature/cycle-patterned-expressions branch February 25, 2025 14:12
Copy link

Benchmark for 89b4997

Click to view benchmark
Test Base PR %
Cycle/Generate 58.8±0.92µs 58.0±0.73µs -1.36%
Cycle/Parse 322.3±7.49µs 327.6±3.75µs +1.64%
Rust Phrase/Clone 439.5±35.66ns 436.5±5.23ns -0.68%
Rust Phrase/Create 65.6±1.58µs 66.0±1.47µs +0.61%
Rust Phrase/Run 685.9±11.03µs 682.0±10.28µs -0.57%
Rust Phrase/Seek 164.7±295.60µs 162.8±309.35µs -1.15%
Scripted Phrase/Clone 651.6±6.31ns 623.6±11.23ns -4.30%
Scripted Phrase/Create 954.9±7.61µs 960.8±26.46µs +0.62%
Scripted Phrase/Run 1798.7±11.65µs 1802.0±23.01µs +0.18%
Scripted Phrase/Seek 229.7±463.72µs 227.7±463.62µs -0.87%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants