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

Unary minus operation with variables #3405

Closed
vitoyucepi opened this issue Sep 13, 2023 · 2 comments · Fixed by #3498
Closed

Unary minus operation with variables #3405

vitoyucepi opened this issue Sep 13, 2023 · 2 comments · Fixed by #3498

Comments

@vitoyucepi
Copy link
Collaborator

Describe the bug
Liquidsoap doesn't support unary minus with variables.

To Reproduce

a = 1
b = -a
if a == -b then
  print(a+-b)
end

Expected behavior
Unary minus supported.

Version details

  • OS: Debian in docker
  • Version: 2.2.1, b2a6924

Install method
savonet/liquidsoap:rolling-release-v2.2.x

Common issues
N/A

@vitoyucepi
Copy link
Collaborator Author

Also -nan and -infinity.

@toots
Copy link
Member

toots commented Sep 18, 2023

Thanks for reporting this. I revisited the way we do it in #3399 and it's not satisfactory.

The problem we have is that there is ambiguity in the language. We accept implicit term separation, so for instance you can write:

x + 1
(1, 2, 3)

Without a ; separating them like most language do.

(It is worth noting that we also support:

x + 1;
(1, 2, 3)

but, I guess, nobody every uses that..)

Morally, what we really should mean is that the line return is the term separator. However, for technical reasons, this is not how it's implemented: our current parser does not see line returns at all.

This creates ambiguity, for instance:

# Just x alone
x
# The number -3.14
-3.14

is seen the same way as:

# x minus 3.14
x - 3.14

The current work around is to distinguish between:

  1. <integer, float, variable name or an expression terminated by )> - <anything>
  2. <anything else> - <integer or float or (expression)>.

The meaning for each is then:

  1. <integer, float or an expression terminated by )> <minus> <anything>
  2. <anything else> <negative integer or float or (expression)>.

Examples:

# 1 minus 2
1 - 2

# x minus 2
x - 2

# application function minus 2
f(x) - 2

# y equals the number -1
y = -1

# This fails because we don't know how to parse it. There is an ambiguity in the grammar
y = -x

# This is the workaround:
y = -(x)

This is clearly a hack and we need to do better. We could keep adjusting the workaround but, the best is to add support for line return as term separator, which I will have a look at soon I hope.

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 a pull request may close this issue.

2 participants