Skip to content

Conversation

@Rito13
Copy link
Contributor

@Rito13 Rito13 commented Sep 1, 2025

Motivation / Problem

As more and more features in NML require users to use arrays it would be reasonable to expand capabilities of array creation.

Description

It allows for creating arrays with following syntax:
[13] * 200
or for more complex repetitive arrays:
[13,12,11,15] * 19 * 8

and with the "embed for" syntax:

[a, a*a, a - 13 for a in [10, 20, 30, 40]]  ->  [10, 100, -3, 20, 400, 7, 30, 900, 17, 40, 1600, 27]
[palette_switch(a) for a in [0, 5, 6, 10]]  ->  [palette_switch(0), palette_switch(5), palette_switch(6), palette_switch(10)]
[[a, bitmask(a)] for a in [1, 2, 3]]  ->  [[1, 2], [2, 4], [3, 8]]
[a, a+2 for a in [param[param[1]], param[0]]]  ->  [param[param[1]], param[param[1]] + 2, param[0], param[0] + 2]

it also adds support for array addition.

@Rito13 Rito13 changed the title Feature: syntax for creating recurring arrays Feature: operations on arrays Sep 1, 2025
@Rito13 Rito13 force-pushed the syntax_for_creating_recurring_arrays branch from 0a706f5 to 2c46e10 Compare September 2, 2025 05:33
@Rito13 Rito13 force-pushed the syntax_for_creating_recurring_arrays branch from 2c46e10 to 60d0b3e Compare September 17, 2025 12:52
@LordAro
Copy link
Member

LordAro commented Sep 17, 2025

Tests very much needed

@PeterN
Copy link
Member

PeterN commented Sep 17, 2025

Reminder that NML is not a programming language.

What problem does this PR solve?

@FLHerne
Copy link
Contributor

FLHerne commented Sep 17, 2025

The checks have caught some errors which are generally self-explanatory.

I also wonder what the intended use of this is: large arrays with repetitive values aren't something I've seen in NML sources.

@Rito13 Rito13 force-pushed the syntax_for_creating_recurring_arrays branch from 60d0b3e to ca95169 Compare September 17, 2025 16:26
@Rito13 Rito13 force-pushed the syntax_for_creating_recurring_arrays branch from ca95169 to 9455b74 Compare September 17, 2025 18:11
@Rito13
Copy link
Contributor Author

Rito13 commented Sep 17, 2025

Reminder that NML is not a programming language.

Acknowledged

What problem does this PR solve?

Provides ability to recreate feature that You have rejected in #385 in the newgrfs.

I also wonder what the intended use of this is: large arrays with repetitive values aren't something I've seen in NML sources.

It is intended to use this for all lengths of array in order to make code more clean, shorter and easier to maintain. The alternative and currently working solution is to use 3-rd party tools to generate nml code at the compile time, this solution is hard to maintain because it demands knowledge of multiple tools.

Tests very much needed

Is the 042_for.nml sufficient

@Rito13 Rito13 force-pushed the syntax_for_creating_recurring_arrays branch from 9455b74 to 2d32cab Compare September 24, 2025 11:51
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.

4 participants