added scientific notation
This commit is contained in:
parent
9b18d38546
commit
0c539af6ac
3
AUTHORS
3
AUTHORS
@ -13,4 +13,5 @@ Raymond Sohn <raymondsohn@gmail.com>
|
||||
Thorbjørn Lindeijer <bjorn@lindeijer.nl>
|
||||
Patricio Mac Adden <patriciomacadden@gmail.com>
|
||||
Evan Hahn <me@evanhahn.com>
|
||||
Starbeamrainbowlabs <contact@starbeamrainbowlabs.com>
|
||||
Starbeamrainbowlabs <contact@starbeamrainbowlabs.com>
|
||||
Alexander Roper <minirop@gmail.com>
|
||||
@ -608,7 +608,6 @@ static void readHexNumber(Parser* parser)
|
||||
// Finishes lexing a number literal.
|
||||
static void readNumber(Parser* parser)
|
||||
{
|
||||
// TODO: scientific, etc.
|
||||
while (isDigit(peekChar(parser))) nextChar(parser);
|
||||
|
||||
// See if it has a floating point. Make sure there is a digit after the "."
|
||||
@ -618,6 +617,22 @@ static void readNumber(Parser* parser)
|
||||
nextChar(parser);
|
||||
while (isDigit(peekChar(parser))) nextChar(parser);
|
||||
}
|
||||
|
||||
// See if the number is in scientific notation
|
||||
if (peekChar(parser) == 'e' || peekChar(parser) == 'E')
|
||||
{
|
||||
nextChar(parser);
|
||||
|
||||
// if the exponant is negative
|
||||
if(peekChar(parser) == '-') nextChar(parser);
|
||||
|
||||
if(!isDigit(peekChar(parser)))
|
||||
{
|
||||
lexError(parser, "Unterminated scientific notation.");
|
||||
}
|
||||
|
||||
while (isDigit(peekChar(parser))) nextChar(parser);
|
||||
}
|
||||
|
||||
makeNumber(parser, false);
|
||||
}
|
||||
|
||||
@ -6,5 +6,4 @@ IO.print(-0) // expect: -0
|
||||
IO.print(123.456) // expect: 123.456
|
||||
IO.print(-0.001) // expect: -0.001
|
||||
|
||||
// TODO: Scientific notation?
|
||||
// TODO: Literals at and beyond numeric limits.
|
||||
|
||||
@ -0,0 +1 @@
|
||||
var x = 1.2e // expect error
|
||||
1
test/language/number/scientific_floating_exponant.wren
Normal file
1
test/language/number/scientific_floating_exponant.wren
Normal file
@ -0,0 +1 @@
|
||||
var x = 1.2e3.4 // expect error
|
||||
9
test/language/number/scientific_literals.wren
Normal file
9
test/language/number/scientific_literals.wren
Normal file
@ -0,0 +1,9 @@
|
||||
var x = 2.55e2
|
||||
|
||||
IO.print(x) // expect: 255
|
||||
IO.print(x + 1) // expect: 256
|
||||
IO.print(x == 255) // expect: true
|
||||
IO.print(2.55e-2 is Num) // expect: true
|
||||
IO.print(x is Num) // expect: true
|
||||
IO.print(-2.55e2) // expect: -255
|
||||
IO.print(-25500e-2) // expect: -255
|
||||
1
test/language/number/scientific_missing_exponent.wren
Normal file
1
test/language/number/scientific_missing_exponent.wren
Normal file
@ -0,0 +1 @@
|
||||
var x = 1e // expect error
|
||||
@ -0,0 +1 @@
|
||||
var x = 1.e // expect runtime error: Num does not implement 'e'.
|
||||
1
test/language/number/scientific_multiple_exponants.wren
Normal file
1
test/language/number/scientific_multiple_exponants.wren
Normal file
@ -0,0 +1 @@
|
||||
var x = 1.2e3e4 // expect error
|
||||
Loading…
Reference in New Issue
Block a user