nojanath / SublimeKSP

Fork of Nils Liberg's SublimeKSP plugin. See README for details.
GNU General Public License v3.0
82 stars 18 forks source link

Local multi-dimensional arrays #90

Open magneto538 opened 6 years ago

magneto538 commented 6 years ago
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 commented 5 years ago

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

ghost commented 4 years ago

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

eitherys commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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.