-
-
Notifications
You must be signed in to change notification settings - Fork 171
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
[Feature request] User-defined functions #201
Comments
Interesting! So, if I'm right in saying, the main things that distinguish this from existing macros are:
I do like that you're going for a functional-style feel, but it bothers me that it's a very different syntax for something that's intuitively similar to macros. Since I think the latter two features would be also good for macros to have, we could potentially unify their syntaxes, perhaps in a manner similar to this:
The syntax to use them would probably still have to differ, due to the existing difference between macros and built-in functions. This approach makes functions look procedural, so I don't know if we should do so (and add a Alternately, you could think of it as something more similar to But, in order to add a little consistency, I'd consider using
You can probably tell that I'm just thinking around the issue, and haven't really made any decisions on what I like best. However we implement it though, I do think this would be a useful feature. Thoughts? |
I prefer the {} version because they are less ambiguous: "\foo1" vs "{foo}1" (see #63 ). In principle it doesn't sound bad. In practice, this is a lot of work. The code that handles macros is quite annoying because it simply cannot be "just parsed", you have to inject the expanded code of the macro... EDIT: Also, more things like macros, equs, etc would make this even worse: #64 |
I'd say the functional style is simpler, although I can imagine a few cases in which procedural would be more powerful. That being said, procedural-style functions would require you to actually execute them, so that might increase the burden on your side. I'm also assuming lazy evaluation here (see the linearaddress example above, which wouldn't work without it because BANK($4444) is invalid), which might be harder to achieve in a procedural way. That being said, I agree that named parameters and overloads (and even varargs) could be useful for macros, so a unified syntax sounds like a good idea. The function/endf syntax looks a bit long for functional-style one-liners, but the "= replaces func" syntax looks good. I'm not really biased towards any particular syntax, though. #64 and the numerical lexer abuse in #63 are downright silly, and the assembler should probably just fail and exit if it detects weird behavior like that with functions. I can't see something like that appearing in real code. By the way, I'm well aware that this is a big feature, so I don't expect it to be finished soon. |
Can named args be used on call as well? And can it be multiline? MyFunction (
foo = 2,
bar = 1
) |
One of the things for sure: such functions will be "pure". No side effects, only produce an expression.
Should this:
|
I don't think it should be an error. As for 0 or 1, both approaches are valid, but I'd say 0 is more useful, but 1 is more natural to people used to macros — I'd prefer 0. |
If so, I'll go with 0 for a first implementation, and maybe extend with a "capture" syntax later so 1 can be produced. |
If I'm understanding correctly, the behavior "output 1" could create dependency cycles... |
No, because the right-hand is always evaluated before the left-hand. |
For comparison, |
Making these work in a useful way would require #619, if only because recursive functions would require the same kind of expression system rewrite that short-circuiting operators would. Therefore, and because I think 0.4.2 has been delayed enough, this feature will be postponed (again) to 0.4.3. |
Lazy evaluation of symbols is important. Eager evaluation can be forced using naked braces (since #634), but lazy evaluation cannot otherwise be done. |
Sjasm has something similar: "text macros with arguments". (Although these are expanded as statements, not expressions, so more like macros with named parameters.) |
Some implementations for common math functions that aren't built into rgbasm:
|
Functions could allow specifying their arguments by name, in case the given order doesn't match the declaration order.
|
This would be especially helpful to clarify intent, as it's neither obvious nor widely agreed upon whether X or Y goes first (typical usage is |
Various useful functions:
|
I'm sure that this has been mentioned before, but I've been coming up with a full feature proposal and I'd like to know how feasible it is. What follows is a proposed way of implementing functions, at least in a way I'd consider usable.
The idea is to allow the user to define functions, that would be evaluated in-line when called, similar to the various built-ins. Functions can have one or more arguments, which define variables that are local to the function; they are substituted in the expression. An example of the syntax I have in mind would be:
Functions can be defined with the same name as long as they have a different number of arguments; the proper version of the function would be used when called. For simplicity, all arguments are required.
Declaring functions with the same name and number of arguments is not allowed; such declarations would either replace previous ones or cause an error (either works).
Calling built-in functions should obviously be allowed from user-defined functions:
In order to simplify functions like the average one above, varargs functions could be defined. Such functions would be used when there is no suitable non-varargs function (i.e., with the correct number of arguments) to call. For instance:
The special symbol
{...}
is replaced by the list of variable arguments. Defining multiple varargs functions with the same name is not allowed; however, they can coexist with non-varargs functions.As a silly example just to show how they would coexist:
I hope that this is doable, as it would certainly reduce some repetition and extremely macro-heavy code in some cases. As a simple realistic example, take this macro from Prism (might be slightly different because I'm typing it from memory):
Which using functions would be a lot cleaner:
I await your responses; thanks for reading.
The text was updated successfully, but these errors were encountered: