Skip to content

Basic support for for (var/k, v in X) syntax#431

Merged
SpaceManiac merged 8 commits intoSpaceManiac:masterfrom
ShiftyRail:kv_pair
Jul 19, 2025
Merged

Basic support for for (var/k, v in X) syntax#431
SpaceManiac merged 8 commits intoSpaceManiac:masterfrom
ShiftyRail:kv_pair

Conversation

@ShiftyRail
Copy link
Copy Markdown
Contributor

@ShiftyRail ShiftyRail commented Jun 4, 2025

What this does

This add a new arm to the parser for the for (var/k, v in X) dict-like syntax that was added in 516.

How this works

I have hijacked the arm that checks for this type of expressions :

for (init, test, inc) or for (init; test; inc)

I have seperated it between init; test; inc (left untouched) and init, test, inc.
If inc is not None, I then check if test in a BinaryOp::In (something like for(init, [x in y]). If not I just assume it's a for (init, test) which is valid DM syntax.

I then check if init is a var/k type of statement, using copypaste. DM will not compile for (k, v) nor will it compile for (var/k, var/v).
I check if v is an ident.

If we're all good, I create a new type of structure ForKeyValueStatement and the lib.rs / findreferences.rs knows what to do with it.

Cases tested :

See unit tests in the Files Changed
for (var/k, v in L)
     world.log << k
     world.log << v

for (k, v in L)
     world.log << k
     world.log << v

❌ "for (var/key, value) requires a 'var' keyword"

for (var/k = 1, v in L)
     world.log << k
     world.log << v

❌ "cannot assigned a value in var/key for(var/key, value) statement"

for (var/0, v in L)
     world.log << k
     world.log << v

❌ "path has no effect" (was already covered)

for (var/k, 0 in L)
     world.log << k
     world.log << v

❌ "value must be a variable in for (var/key, value) statement"

Regression checks

Both for (i = 0, i < 10, i++), for (i = 0, i < 10) still pass.

@ZeWaka
Copy link
Copy Markdown
Contributor

ZeWaka commented Jun 4, 2025

Wouldn't the type of k (if it's not a primitive) be derived from the ss13 syntax of var/list/datum/foo/my_list ? Aka, the var type definition of the rhs? I don't see how there's any relation to v.

Copy link
Copy Markdown
Contributor

@ZeWaka ZeWaka left a comment

Choose a reason for hiding this comment

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

Also, don't we have tests? Why put your test cases in the description if you're not going to put in as tests?

@ShiftyRail
Copy link
Copy Markdown
Contributor Author

I agree with the var_type thing - however DM will not let you give a var type to v in the for(var/k, v.

Thinking about it I should create a var_type corresponding to an empty typepath and use that instead but I am not really sure how I can actually do that.

I didnt notice the parser had unit tests. I am more than happy to include the for(var/k, v cases. I included them because I wanted to show maintainers the cases that I had tested in the event that they had questions about it :)

@ShiftyRail
Copy link
Copy Markdown
Contributor Author

ShiftyRail commented Jun 4, 2025

I've added a basic unit test that shows a for(var/k, v in A) is parsed. I can add more specific error messages checking with some help -- I have no idea how to make rust check exactly that a specific error message got printed.

I've also added a block :

                // the "v" in a DM for (var/k, v) statement is essentially typeless.
                // There is currently no way to change that.
                let var_type_value = VarType {
                    flags: VarTypeFlags::from_bits_truncate(0),
                    type_path: Box::new([]),
                    input_type: InputType::from_bits_truncate(0),
                };

DM lets you type k but values are always essentially typeless and there's no way to change it as the moment. This has all the cases I lint covered in unit tests now.

@ShiftyRail
Copy link
Copy Markdown
Contributor Author

this is fine for now

@SpaceManiac SpaceManiac merged commit 936a5ee into SpaceManiac:master Jul 19, 2025
1 check passed
@SpaceManiac SpaceManiac added this to the suite v1.11 milestone Aug 30, 2025
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.

3 participants