Skip to content

Commit f3e449e

Browse files
committed
fix js codegen allowing underscore after decimal
1 parent afcd7e9 commit f3e449e

11 files changed

+154
-20
lines changed

compiler-core/src/javascript/decision.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{
22
INDENT, bit_array_segment_int_value_to_bytes,
3-
expression::{self, Generator, Ordering, float},
3+
expression::{self, Generator, Ordering, float, float_from_value},
44
};
55
use crate::{
66
ast::{AssignmentKind, Endianness, SrcSpan, TypedClause, TypedExpr, TypedPattern},
@@ -1024,7 +1024,7 @@ impl<'generator, 'module, 'a> Variables<'generator, 'module, 'a> {
10241024
RuntimeCheck::String { value: expected } => docvec![value, equality, string(expected)],
10251025
RuntimeCheck::Float {
10261026
float_value: expected,
1027-
} => docvec![value, equality, expected.value()],
1027+
} => docvec![value, equality, float_from_value(expected.value())],
10281028
RuntimeCheck::Int {
10291029
int_value: expected,
10301030
} => docvec![value, equality, expected.clone()],

compiler-core/src/javascript/expression.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ impl<'module, 'a> Generator<'module, 'a> {
288288
TypedExpr::String { value, .. } => string(value),
289289

290290
TypedExpr::Int { value, .. } => int(value),
291-
TypedExpr::Float { value, .. } => float(value),
291+
TypedExpr::Float { float_value, .. } => float_from_value(float_value.value()),
292292

293293
TypedExpr::List { elements, tail, .. } => {
294294
self.not_in_tail_position(Some(Ordering::Strict), |this| match tail {
@@ -2353,6 +2353,22 @@ pub fn float(value: &str) -> Document<'_> {
23532353
out.to_doc()
23542354
}
23552355

2356+
pub fn float_from_value(value: f64) -> Document<'static> {
2357+
if value.is_infinite() {
2358+
if value.is_sign_positive() {
2359+
"Infinity".to_doc()
2360+
} else {
2361+
"-Infinity".to_doc()
2362+
}
2363+
} else if value.is_nan() {
2364+
// NOTE: this case is probably unnecessary, as this function is only
2365+
// invoked with `LiteralFloatValue` values, which cannot be nan.
2366+
"NaN".to_doc()
2367+
} else {
2368+
value.to_doc()
2369+
}
2370+
}
2371+
23562372
/// The context where the constant expression is used, it might be inside a
23572373
/// function call, or in the definition of another constant.
23582374
///

compiler-core/src/javascript/tests/numbers.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,56 @@ pub fn main() {
431431
);
432432
}
433433

434+
#[test]
435+
fn underscore_after_decimal_point() {
436+
assert_js!(
437+
"
438+
pub fn main() {
439+
0._1
440+
}
441+
"
442+
);
443+
}
444+
445+
#[test]
446+
fn underscore_after_decimal_point_case_statement() {
447+
assert_js!(
448+
"
449+
pub fn main(x) {
450+
case x {
451+
0._1 -> \"bar\"
452+
_ -> \"foo\"
453+
}
454+
}
455+
"
456+
);
457+
}
458+
459+
#[test]
460+
fn inf_float_case_statement() {
461+
assert_js!(
462+
"
463+
pub fn main(x) {
464+
case x {
465+
100.001e123_456_789 -> \"bar\"
466+
_ -> \"foo\"
467+
}
468+
}
469+
"
470+
);
471+
}
472+
473+
#[test]
474+
fn division_inf_by_inf_float() {
475+
assert_js!(
476+
"
477+
pub fn main(x) {
478+
-100.001e123_456_789 /. 100.001e123_456_789
479+
}
480+
"
481+
);
482+
}
483+
434484
#[test]
435485
fn division_by_zero_float() {
436486
assert_js!(
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
source: compiler-core/src/javascript/tests/numbers.rs
3+
expression: "\npub fn main(x) {\n -100.001e123_456_789 /. 100.001e123_456_789\n}\n"
4+
---
5+
----- SOURCE CODE
6+
7+
pub fn main(x) {
8+
-100.001e123_456_789 /. 100.001e123_456_789
9+
}
10+
11+
12+
----- COMPILED JAVASCRIPT
13+
export function main(x) {
14+
return -Infinity / Infinity;
15+
}

compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__numbers__float_literals.snap

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
---
22
source: compiler-core/src/javascript/tests/numbers.rs
3-
assertion_line: 23
43
expression: "\npub fn go() {\n 1.5\n 2.0\n -0.1\n 1.\n}\n"
5-
snapshot_kind: text
64
---
75
----- SOURCE CODE
86

@@ -19,5 +17,5 @@ export function go() {
1917
1.5;
2018
2.0;
2119
-0.1;
22-
return 1.;
20+
return 1.0;
2321
}
Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
---
22
source: compiler-core/src/javascript/tests/numbers.rs
3-
assertion_line: 37
43
expression: "\npub fn go() {\n 0.01e-1\n 0.01e-0\n -10.01e-1\n -10.01e-0\n 100.001e523\n -100.001e-523\n 100.001e123_456_789\n -100.001e-123_456_789\n}\n"
5-
snapshot_kind: text
64
---
75
----- SOURCE CODE
86

@@ -20,12 +18,12 @@ pub fn go() {
2018

2119
----- COMPILED JAVASCRIPT
2220
export function go() {
23-
0.01e-1;
24-
0.01e-0;
25-
-10.01e-1;
26-
-10.01e-0;
27-
100.001e523;
28-
-100.001e-523;
29-
100.001e123_456_789;
30-
return -100.001e-123_456_789;
21+
0.001;
22+
0.01;
23+
-1.001;
24+
-10.01;
25+
Infinity;
26+
-0.0;
27+
Infinity;
28+
return -0.0;
3129
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
source: compiler-core/src/javascript/tests/numbers.rs
3+
expression: "\npub fn main(x) {\n case x {\n 100.001e123_456_789 -> \"bar\"\n _ -> \"foo\"\n }\n}\n"
4+
---
5+
----- SOURCE CODE
6+
7+
pub fn main(x) {
8+
case x {
9+
100.001e123_456_789 -> "bar"
10+
_ -> "foo"
11+
}
12+
}
13+
14+
15+
----- COMPILED JAVASCRIPT
16+
export function main(x) {
17+
if (x === Infinity) {
18+
return "bar";
19+
} else {
20+
return "foo";
21+
}
22+
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
---
22
source: compiler-core/src/javascript/tests/numbers.rs
3-
assertion_line: 264
43
expression: "\npub fn main() {\n 09_179.1\n}\n"
5-
snapshot_kind: text
64
---
75
----- SOURCE CODE
86

@@ -13,5 +11,5 @@ pub fn main() {
1311

1412
----- COMPILED JAVASCRIPT
1513
export function main() {
16-
return 9_179.1;
14+
return 9179.1;
1715
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
source: compiler-core/src/javascript/tests/numbers.rs
3+
expression: "\npub fn main() {\n 0._1\n}\n"
4+
---
5+
----- SOURCE CODE
6+
7+
pub fn main() {
8+
0._1
9+
}
10+
11+
12+
----- COMPILED JAVASCRIPT
13+
export function main() {
14+
return 0.1;
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
source: compiler-core/src/javascript/tests/numbers.rs
3+
expression: "\npub fn main(x) {\n case x {\n 0._1 -> \"bar\"\n _ -> \"foo\"\n }\n}\n"
4+
---
5+
----- SOURCE CODE
6+
7+
pub fn main(x) {
8+
case x {
9+
0._1 -> "bar"
10+
_ -> "foo"
11+
}
12+
}
13+
14+
15+
----- COMPILED JAVASCRIPT
16+
export function main(x) {
17+
if (x === 0.1) {
18+
return "bar";
19+
} else {
20+
return "foo";
21+
}
22+
}

0 commit comments

Comments
 (0)