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

expression blocks #89

Closed
lizelive opened this issue Aug 18, 2021 · 2 comments
Closed

expression blocks #89

lizelive opened this issue Aug 18, 2021 · 2 comments
Labels
enhancement New feature or request

Comments

@lizelive
Copy link

Is your feature request related to a problem? Please describe.
allow temporary variables inside expression-like block

Describe the solution you'd like

macro math_atan(y) expr
	sign = math_sign(y)
	flip=1-2*(abs(y)>1)
	didFlip = flip<0
	90*didFlip*sign+flip*215.814*(y^flip)/(3.8+y^(2*flip))
end
:cool=2*math_atan(:beans)

is equvilent to

math_atan(ans,:beans)
:cool=2*ans

finally

b=:beans c=b>=0-b<=0 d=1-2*(abs b>1) e=d<0
f=90*e*c+d*215.814*b^d/(3.8+b^(2*d)) :cool=2*f goto1

Describe alternatives you've considered
macro math_atan2(out, y, x) block is ugly

Additional context
Add any other context or screenshots about the feature request here.

@lizelive lizelive added the enhancement New feature or request label Aug 18, 2021
@dbaumgarten
Copy link
Owner

Allowing only temporary variables and nothing else there seems a little odd and would probably be very confusing.
I guess what you really want is the ability for block-macros to return a value.

When implementing macros, I initially tried to have them as close to "usual" functions as possible: A bunch of statements that can return a value. While this did work somehow, the generated code was usually pretty bad and it led quite often to unexpected results. Thats why I decided to have multiple types (expr, line, block) of macros. This way it is always relatively clear to users of macros how to compiler will handle the code. No magic, no suprises.

I am not saying it can't be done properly. But I didn't get it working in a satisfying way.

However, I thing there are some other (planned) features that can help here:
For readability you could put flip into another macro.
To reduce the amount of repeated sub-expressions there is issue #42, which would automatically move them to temporary variables whenever possible. And #78 would move the constants out of the way, further shorting the code.

So in the end it would look like this:

macro flip(x) expr
1-2*(abs(x)>1)
end

macro math_atan(y) expr
	90*(flip(y)<0)* math_sign(y)+flip(y)*215.814*(y^flip(y))/(3.8+y^(2*flip(y)))
end
:cool=2*math_atan(:beans)

And the result should look pretty similar to what you described.

Also: I don't think having to use a block-macro for something that complicated as a proper atan is too bad. It's only minimally more inconvenient and at least makes it clear to users that the macro they are using will result in a lot of code and should not be used light-heartedly inside some expression.

@dbaumgarten
Copy link
Owner

Closed as won't/can't fix.
If someobody comes up with a way to implement something like this without causing confusion and/or bad generated code, I would be happy to hear about it.
Until then, hopefully the upcoming automatic optimizations help a little here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants