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

Editor: @for loop custom step size #401

Merged

Conversation

netux
Copy link
Contributor

@netux netux commented Jan 28, 2024

This PR adds an optional step size to @for loops (instead of the default: 1), active when using the in steps of operator.

Example

@for (0 through 4 in steps of 2) {
  For.i;
}

Renders:

  0;
  2;
  4;

netux added 3 commits January 28, 2024 18:19
…loops

Allows for the following syntax:
```
@for (0 through 4 in steps of 2) {
  For.i;
}
```
Printing:
```
  0;
  2;
  4;
```
Since this operator uses spaces, I also had to change `customOperators` from a word set to a regular expression.
…ng a @for loop with a custom step size

Use the "to"/"through" as a delimiter for where we should check. If to the left there is no "from", then warn the user.
Copy link
Owner

@Mitcheljager Mitcheljager left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! It's a bit overkill since you can just do For.i * x, but it's also kinda fun, so why not! I did have a couple of nitpicks though

@@ -58,6 +58,7 @@ function tokenBase(stream, state) {
if (stream.match(hexadecimal)) return "number"
if (stream.match(decimal)) return "number"
if (stream.match(customKeywords)) return "atom"
if (stream.match(customOperators)) return "atom"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully it wasn't down below for a reason :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it was just leeching off the identifier regex.

app/javascript/src/lib/OWLanguageLinter.js Outdated Show resolved Hide resolved
app/javascript/src/utils/compiler/for.js Outdated Show resolved Hide resolved
@netux
Copy link
Contributor Author

netux commented Jan 28, 2024

It's a bit overkill since you can just do For.i * x

Yes, but that means outputting (whatever value For.i was) * x, which means more elements in the Workshop. So this should help with (micro-)optimization.

For simple cases I also found you can do a mixin like the one below, though its less readable/more verbose.

@mixin map01To(value, ifZero, ifOne) { @if (Mixin.value == 0) { Mixin.ifZero } @else { Mixin.ifOne } }

// This is meant to go through 0 to 2, skipping 1
@for (0 through 1) {
  @include map01To(For.i, 0, 2)
}

@Mitcheljager Mitcheljager merged commit 11f6162 into Mitcheljager:master Feb 1, 2024
6 checks passed
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