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

Support positive sign in scientific notation #706

Merged
merged 3 commits into from Jun 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/site/values.markdown
Expand Up @@ -19,13 +19,17 @@ Like other scripting languages, Wren has a single numeric type:
double-precision floating point. Number literals look like you expect coming
from other languages:


<pre class="snippet">
0
1234
-5678
3.14159
1.0
-12.34
0.0314159e02
0.0314159e+02
314.159e-02
</pre>

Numbers are instances of the [Num][] class.
Expand Down
13 changes: 8 additions & 5 deletions src/vm/wren_compiler.c
Expand Up @@ -758,18 +758,21 @@ static void readNumber(Parser* parser)
nextChar(parser);
while (isDigit(peekChar(parser))) nextChar(parser);
}

// See if the number is in scientific notation.
if (matchChar(parser, 'e') || matchChar(parser, 'E'))
{
// Allow a negative exponent.
matchChar(parser, '-');

// Allow a single positive/negative exponent symbol.
if(!matchChar(parser, '+'))
{
matchChar(parser, '-');
}

if (!isDigit(peekChar(parser)))
{
lexError(parser, "Unterminated scientific notation.");
}

while (isDigit(peekChar(parser))) nextChar(parser);
}

Expand Down
1 change: 1 addition & 0 deletions test/language/number/scientific_literals.wren
Expand Up @@ -7,3 +7,4 @@ System.print(2.55e-2 is Num) // expect: true
System.print(x is Num) // expect: true
System.print(-2.55e2) // expect: -255
System.print(-25500e-2) // expect: -255
System.print(2.55e+2) // expect: 255
@@ -0,0 +1 @@
var x = 1e+-01 // expect error