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

(interpreter) Fix integer expansions to bigint #234

Merged
merged 1 commit into from
Nov 22, 2021

Conversation

perlun
Copy link
Collaborator

@perlun perlun commented Nov 22, 2021

While working on #225, I discovered that some semantics here was essentially broken. The exponential operator only worked properly with int + int or bigint + int operands. When one of the operands was a float, it was impossible to assign the result to a variable.

$ perlang
Perlang Interactive REPL Console (0.1.0-dev.181, built from git commit 9480f21)
> var v = 2 ** 3.5
[line unknown] Unsupported target type System.Numerics.BigInteger

@perlun perlun added the bug Something isn't working as expected label Nov 22, 2021
@perlun perlun added this to the 0.1.0 milestone Nov 22, 2021
// TODO: Might need to revisit this to support more types as we implement #70.
_ => throw new IllegalStateException($"Unsupported conversion from {value.GetType().ToTypeKeyword()} to {targetTypeReference.ClrType.ToTypeKeyword()}")
};
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

☝️ This is the essence of the fix.

Also, the TODO comment for #70 is quite important. The easiest way to ensure that it gets fixed is probably to add more test cases, so that we properly test this with values of all types expected to be supported (i.e. expanding this list as we support more types, uint, ulong and potentially decimal - there are BigInteger constructors for all of these that could be used).

public void exponential_integer_literals_as_variable_initializer()
[Theory]
[InlineData("2", "12", "4096")]
[InlineData("10", "3.5", "3162")] // TODO: Fix this once the `dynamic` PR gets merged. This is incorrectly coerced to BigInteger in the current implementation.
Copy link
Collaborator Author

@perlun perlun Nov 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙈 The correct value here would be something like 3162.2776601683795.

@perlun perlun merged commit 94fe2ca into master Nov 22, 2021
@perlun perlun deleted the fix/support-type-inference-of-bigint-values branch November 22, 2021 19:46
@perlun perlun changed the title (interpreter) Fix integer expansions to bigint (interpreter) Fix integer expansions to bigint Jan 6, 2022
@perlun perlun changed the title (interpreter) Fix integer expansions to bigint (interpreter) Fix double expansions to bigint Jan 6, 2022
@perlun perlun changed the title (interpreter) Fix double expansions to bigint (interpreter) Fix integer expansions to bigint Jan 6, 2022
@perlun
Copy link
Collaborator Author

perlun commented Jan 6, 2022

For reference, the semantics after this MR got merged was insane:

Perlang Interactive REPL Console (0.1.0-dev.186, built from git commit 94fe2ca)
> var v1 = 2 ** 3.5
> var v2 = 2 ** 3.1
> v1.get_type()
System.Numerics.BigInteger
> v2.get_type()
System.Numerics.BigInteger
> 2 ** 3.5
11.313708498984761
> 2 ** 3.1
8.574187700290345

(Using a BigInteger type but containing a double value?!?)

This was corrected in #237:

Perlang Interactive REPL Console (0.1.0-dev.190, built from git commit 3dda89d)
> var v1 = 2 ** 3.5
> var v2 = 2 ** 3.1
> v1.get_type()
System.Double
> v2.get_type()
System.Double

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working as expected
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant