Skip to content

Commit

Permalink
Add tests, cleanup generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
GearsDatapacks committed Mar 9, 2025
1 parent c8c45f0 commit b1758e2
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 19 deletions.
54 changes: 35 additions & 19 deletions compiler-core/src/javascript/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,17 +395,24 @@ impl<'module> Generator<'module> {
.unwrap_or(1);

let (size_value, size) = match size {
Some(Opt::Size { value: size, .. }) => {
let size_value = match *size.clone() {
TypedExpr::Int { int_value, .. } => Some(int_value * unit as usize),
_ => None,
};
Some(Opt::Size { value: size, .. }) => match *size.clone() {
TypedExpr::Int { int_value, .. } => {
let size_value = int_value * unit as usize;
let size = eco_format!("{}", size_value).to_doc();

(
size_value,
self.not_in_tail_position(|r#gen| r#gen.wrap_expression(size))?,
)
}
(Some(size_value), size)
}
_ => {
let mut size =
self.not_in_tail_position(|r#gen| r#gen.wrap_expression(size))?;

if unit != 1 {
size = size.group().append(" * ".to_doc().append(unit.to_doc()));
}

(None, size)
}
},
_ => {
let size_value = if segment.type_ == crate::type_::int() {
8usize
Expand All @@ -418,7 +425,7 @@ impl<'module> Generator<'module> {
};

Ok(SizedBitArraySegmentDetails {
size: size.group().append(" * ".to_doc().append(unit.to_doc())),
size,
size_value,
endianness,
})
Expand Down Expand Up @@ -1713,14 +1720,23 @@ fn sized_bit_array_segment_details<'a>(
.find(|x| matches!(x, Opt::Size { .. }));

let (size_value, size) = match size {
Some(Opt::Size { value: size, .. }) => {
let size_value = match *size.clone() {
Constant::Int { int_value, .. } => Some(int_value * unit as usize),
_ => None,
};
Some(Opt::Size { value: size, .. }) => match *size.clone() {
Constant::Int { int_value, .. } => {
let size_value = int_value * unit as usize;
let size = eco_format!("{}", size_value).to_doc();

(size_value, constant_expr_fun(tracker, size)?)
}
(Some(size_value), size)
}
_ => {
let mut size = constant_expr_fun(tracker, size)?;

if unit != 1 {
size = size.group().append(" * ".to_doc().append(unit.to_doc()));
}

(None, size)
}
},
_ => {
let size_value = if segment.type_ == crate::type_::int() {
8usize
Expand All @@ -1733,7 +1749,7 @@ fn sized_bit_array_segment_details<'a>(
};

Ok(SizedBitArraySegmentDetails {
size: size.group().append(" * ".to_doc().append(unit.to_doc())),
size,
size_value,
endianness,
})
Expand Down
46 changes: 46 additions & 0 deletions compiler-core/src/javascript/tests/bit_arrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1048,3 +1048,49 @@ pub fn main() {
}"#
);
}

#[test]
fn with_unit() {
assert_js!(
r#"
fn main() {
<<1:size(2)-unit(2), 2:size(3)-unit(4)>>
}
"#,
);
}

#[test]
fn dynamic_size_with_unit() {
assert_js!(
r#"
fn main() {
let size = 3
<<1:size(size)-unit(2)>>
}
"#,
);
}

#[test]
fn pattern_with_unit() {
assert_js!(
r#"
fn go(x) {
let assert <<1:size(2)-unit(2), 2:size(3)-unit(4)>> = x
}
"#,
);
}

#[test]
fn dynamic_size_pattern_with_unit() {
assert_js!(
r#"
fn go(x) {
let size = 3
let assert <<1:size(size)-unit(2)>> = x
}
"#,
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
source: compiler-core/src/javascript/tests/bit_arrays.rs
expression: "\nfn go(x) {\n let size = 3\n let assert <<1:size(size)-unit(2)>> = x\n}\n"
---
----- SOURCE CODE

fn go(x) {
let size = 3
let assert <<1:size(size)-unit(2)>> = x
}


----- COMPILED JAVASCRIPT
import { makeError, bitArraySliceToInt } from "../gleam.mjs";

function go(x) {
let size = 3;
if (
bitArraySliceToInt(x, 0, size * 2, true, false) !== 1 ||
!(x.bitSize == size * 2)
) {
throw makeError(
"let_assert",
"my/mod",
4,
"go",
"Pattern match failed, no pattern matched the value.",
{ value: x }
)
}
return x;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
source: compiler-core/src/javascript/tests/bit_arrays.rs
expression: "\nfn main() {\n let size = 3\n <<1:size(size)-unit(2)>>\n}\n"
---
----- SOURCE CODE

fn main() {
let size = 3
<<1:size(size)-unit(2)>>
}


----- COMPILED JAVASCRIPT
import { toBitArray, sizedInt } from "../gleam.mjs";

function main() {
let size = 3;
return toBitArray([sizedInt(1, size * 2, true)]);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
source: compiler-core/src/javascript/tests/bit_arrays.rs
expression: "\nfn go(x) {\n let assert <<1:size(2)-unit(2), 2:size(3)-unit(4)>> = x\n}\n"
---
----- SOURCE CODE

fn go(x) {
let assert <<1:size(2)-unit(2), 2:size(3)-unit(4)>> = x
}


----- COMPILED JAVASCRIPT
import { makeError, bitArraySliceToInt } from "../gleam.mjs";

function go(x) {
if (
bitArraySliceToInt(x, 0, 4, true, false) !== 1 ||
bitArraySliceToInt(x, 4, 16, true, false) !== 2 ||
!(x.bitSize == 16)
) {
throw makeError(
"let_assert",
"my/mod",
3,
"go",
"Pattern match failed, no pattern matched the value.",
{ value: x }
)
}
return x;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
source: compiler-core/src/javascript/tests/bit_arrays.rs
expression: "\nfn main() {\n <<1:size(2)-unit(2), 2:size(3)-unit(4)>>\n}\n"
---
----- SOURCE CODE

fn main() {
<<1:size(2)-unit(2), 2:size(3)-unit(4)>>
}


----- COMPILED JAVASCRIPT
import { toBitArray, sizedInt } from "../gleam.mjs";

function main() {
return toBitArray([sizedInt(1, 4, true), sizedInt(2, 12, true)]);
}
13 changes: 13 additions & 0 deletions test/language/test/language_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,19 @@ fn sized_bit_array_tests() -> List(Test) {
<<i:size(64)>> == <<0, 31, 255, 255, 255, 255, 255, 255>>,
)
}),
"let i = 9_007_199_254_740_991\n<<i:size(4)-unit(16)>> == <<0, 31, 255, 255, 255, 255, 255, 255>>"
|> example(fn() {
let i = 9_007_199_254_740_991
assert_equal(
True,
<<i:size(4)-unit(16)>> == <<0, 31, 255, 255, 255, 255, 255, 255>>,
)
}),
"let size = 5\n<<405:size(size)-unit(2)>> == <<101, 1:2>>"
|> example(fn() {
let size = 5
assert_equal(True, <<405:size(size)-unit(2)>> == <<101, 1:2>>)
}),
]
}

Expand Down

0 comments on commit b1758e2

Please sign in to comment.