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

Local multi-dimensional arrays #90

Open
magneto538 opened this issue Feb 21, 2018 · 9 comments
Open

Local multi-dimensional arrays #90

magneto538 opened this issue Feb 21, 2018 · 9 comments

Comments

@magneto538
Copy link
Contributor

magneto538 commented Feb 21, 2018

function some_func
	declare arr[8, 9]
	{...}
end function

This wouldn't compile. "Syntax error" is returned. This happens regardless of the type of function (with return value, with parameters etc.). It happens even if the function is not called anywhere in the script.

@mkruselj
Copy link
Collaborator

There's a comment in preprocessor_plugins.py, at line 522 that says "Multidimensional arrays are only allowed in the init callback" :/

@ghost
Copy link

ghost commented Sep 14, 2019

It also won't compile if you import a script that has an "on_init_[something]" function and inline that function in "on init". Should work there really as code compiled that way results in anything in "on_init_[something]" being inlined in "on init" as described in Nil's original documentation.

@eitherys
Copy link
Collaborator

If your in-line doesn’t work, it’s because there’s a part of code inside it that doesn’t work in the init callback. The easiest example is if you in-line a function that uses “call [function]” inside of it.

@ghost
Copy link

ghost commented Sep 14, 2019

imported_script.ksp:
function on_init_imported_script
declare array[10, 10]
end function
slot0.ksp:
import "imported_script.ksp"
on init
on_init_imported_script
end on
Won't compile even though array would end up in on init if it did.

@eitherys
Copy link
Collaborator

eitherys commented Sep 14, 2019

You can not declare multi dim arrays in functions, which is why it doesn’t work. It doesn’t matter if you only use it inside of a function you happen to use in-line.

The function has to be understood by the compiler first before it can be used in-line. It can’t be understood by the compiler if it doesn’t follow legally what a function can do.

For this purpose, you simply just use a macro instead, which has no restrictions as the purpose is in-line text replacement.

@ghost
Copy link

ghost commented Sep 14, 2019

Functions starting with "on_init" behave differently to normal functions. It doesn't work, but it should because the array would be compiled into on init.

@eitherys
Copy link
Collaborator

eitherys commented Sep 14, 2019

There's nothing in the compiler to suggest the existence of this feature nor can I find anything in any Nils documentation suggesting that he did this besides a small note that if you write "on_init" in front of your function name, it will treat variable declarations as global. Even that, however, I can not find any actual code that does what he states.

If you want init callback text replacements, you only need to use a macro. It's very easy and much more powerful.

@eitherys
Copy link
Collaborator

Multi-dim arrays were an appended feature created after Nils was working on the compiler, and so whoever created it did not treat it the same way as the rest of the code compilation process handles variables and arrays. That's why it is more limited than normal variables and arrays, since it is a "preprocessor" meaning it is checked for legal syntax before any functions are expanded.

@ghost
Copy link

ghost commented Sep 14, 2019

I've used the "on_init" thing for ages, it definitely works. Without it it won't compile. However, I'll try turning them into macros instead though I'm sure I had a problem with that when there were macros inside the imported function, can't test at the moment.

I had no idea whether it was going to be an easy fix or a major pain so thought I'd mention it. Anyway, thanks a lot for your help.

@mkruselj mkruselj changed the title Multi-dim local arrays Multi-dimensional local arrays Dec 28, 2022
@mkruselj mkruselj changed the title Multi-dimensional local arrays Local multi-dimensional arrays Dec 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants