Skip to content

Syntax Simplification #7841

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

Open
wants to merge 10 commits into
base: dev/feature
Choose a base branch
from

Conversation

sovdeeth
Copy link
Member

@sovdeeth sovdeeth commented Apr 30, 2025

Description

Skript has always had this little method on Expression called simplify(), which has never been implemented. It's intended to take expressions and, if possible, simplify them to Literals or similarly simplify the code tree.
This PR attempts to properly implement that idea via a Simplifiable interface. A config option is added to allow users to opt out if issues arise.

Every element is simplified immediately after init() in SkriptParser. In addition, it may be called at any time by parent elements, though it should be ensured that no other objects maintain critical references to the element, as simplify may return new objects.

ExprArithmetic is a special case that can only be simplified once all elements in the chain have been parsed and ordered, so it requires checking the ParsingStack to see if it's the top-level element in the chain. If so, it can then call simplify recursively on all elements in the chain.

Todo:

  • Properly simplify ConvertedExpressions. The existing method was stripping the converting part off a lot of converted expressions.
  • Implement simplify on many more expressions that could support it.

Target Minecraft Versions: any
Requirements: none
Related Issues: none

@sovdeeth sovdeeth added the enhancement Feature request, an issue about something that could be improved, or a PR improving something. label Apr 30, 2025
@sovdeeth sovdeeth requested review from UnderscoreTud and a team as code owners April 30, 2025 21:12
@sovdeeth sovdeeth requested review from Romitou and removed request for a team April 30, 2025 21:12
@sovdeeth sovdeeth mentioned this pull request Apr 30, 2025
@sovdeeth sovdeeth requested a review from APickledWalrus April 30, 2025 21:24
Copy link
Member

@APickledWalrus APickledWalrus left a comment

Choose a reason for hiding this comment

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

Looking good so far. Do you think Arithmetic tests that verify the SyntaxElement is a Literal would be possible?

@sovdeeth sovdeeth requested a review from APickledWalrus April 30, 2025 22:45
@sovdeeth sovdeeth added the 2.12 Targeting a 2.12.X version release label May 2, 2025
Copy link
Member

@APickledWalrus APickledWalrus left a comment

Choose a reason for hiding this comment

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

Looking really good. I might prefer to see the classes of the new simplification package under ch.njol. Mainly, given that rewritten classes are possible in the future, it'll make compatibility easier to handle

* Gets the original expression this literal was created from.
* @return the original expression
*/
public Expression<T> getPresimplifiedExpr() {
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this just the same as Expression#getSource? Could you override that?

@@ -375,6 +375,9 @@ private static void userDisableHooks(Class<? extends Hook<?>> hookClass, boolean
public static final Option<Integer> variableChangesUntilSave = new Option<>("variable changes until save", 1000)
.setter(FlatFileStorage::setRequiredChangesForResave);

public static final Option<Boolean> simplifySyntaxesOnParse = new Option<>("simplify syntax on parse", true)
Copy link
Member

Choose a reason for hiding this comment

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

i would include a comment that this option is intentionally omitted from the config. that is, it has to be added manually


@Override
public Expression<? extends Number> simplify() {
// as of INSERT VERSION, there are no literal locations but this is implemented for when pure functions can
Copy link
Member

Choose a reason for hiding this comment

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

just want to note that the insert version script won't cover cases like this

@Override
@Nullable
public Class<?>[] acceptChange(final ChangeMode mode) {
if ((mode == ChangeMode.SET || mode == ChangeMode.ADD || mode == ChangeMode.REMOVE) && getExpr().isSingle() && ChangerUtils.acceptsChange(getExpr(), ChangeMode.SET, Location.class))
public Class<?>[] acceptChange(final Changer.ChangeMode mode) {
Copy link
Member

Choose a reason for hiding this comment

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

Should keep ChangeMode directly imported

Expression<? extends T> convertedExpression = source.simplify().getConvertedExpression(to);
if (convertedExpression != null)
return convertedExpression;
source = source.simplify();
Copy link
Member

Choose a reason for hiding this comment

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

Why this change? Calling getConvertedExpression again could potentially result in a Literal of the desired (conversion) type, which seems like something we'd want. Or is the concern over losing the source information?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.12 Targeting a 2.12.X version release enhancement Feature request, an issue about something that could be improved, or a PR improving something.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants