Skip to content

Commit

Permalink
Merge branch 'main' into iter-map-option
Browse files Browse the repository at this point in the history
  • Loading branch information
bobzhang authored Oct 22, 2024
2 parents d775564 + 13d85b0 commit 4e14af0
Show file tree
Hide file tree
Showing 61 changed files with 513 additions and 113 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,10 @@ jobs:
bleeding-check:
continue-on-error: true
runs-on: macos-latest
strategy:
matrix:
os: [macos-latest, ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4

Expand All @@ -226,7 +229,7 @@ jobs:
run: |
ulimit -s 8176
moon test --target native
moon test --release --target native
moon test --target native --release
- name: moon bundle
run: moon bundle --all
Expand Down
13 changes: 12 additions & 1 deletion buffer/buffer.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub fn write_bytes(self : Buffer, value : Bytes) -> Unit {
}

/// Write a sub-string into buffer.
pub fn write_sub_string(
pub fn write_substring(
self : Buffer,
value : String,
start : Int,
Expand All @@ -92,6 +92,17 @@ pub fn write_sub_string(
self.len += len * 2
}

/// Write a sub-string into buffer.
/// @alert deprecated "Use `Buffer::write_substring` instead"
pub fn write_sub_string(
self : Buffer,
value : String,
start : Int,
len : Int
) -> Unit {
self.write_substring(value, start, len)
}

/// Write a char into buffer.
pub fn write_char(self : Buffer, value : Char) -> Unit {
self.grow_if_necessary(self.len + 4)
Expand Down
3 changes: 2 additions & 1 deletion buffer/buffer.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ impl Buffer {
write_char(Self, Char) -> Unit
write_object(Self, Show) -> Unit
write_string(Self, String) -> Unit
write_sub_string(Self, String, Int, Int) -> Unit
write_sub_string(Self, String, Int, Int) -> Unit //deprecated
write_substring(Self, String, Int, Int) -> Unit
}

// Type aliases
Expand Down
12 changes: 6 additions & 6 deletions buffer/buffer_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ test "grow_if_necessary method" {
assert_true!(buf.to_bytes().length() >= 60)
}

test "write_sub_string method" {
test "write_substring method" {
let buf = @buffer.new(size_hint=10)
buf.write_sub_string("Hello, World!", 7, 5)
buf.write_substring("Hello, World!", 7, 5)
inspect!(buf, content="World")
}

Expand Down Expand Up @@ -66,9 +66,9 @@ test "grow_if_necessary method" {
assert_true!(buf.to_bytes().length() >= 60)
}

test "write_sub_string method" {
test "write_substring method" {
let buf = @buffer.new(size_hint=10)
buf.write_sub_string("Hello, World!", 7, 5)
buf.write_substring("Hello, World!", 7, 5)
inspect!(buf, content="World")
}

Expand Down Expand Up @@ -106,9 +106,9 @@ test "grow_if_necessary method" {
assert_true!(buf.to_bytes().length() >= 60)
}

test "write_sub_string method" {
test "write_substring method" {
let buf = @buffer.new(size_hint=10)
buf.write_sub_string("Hello, World!", 7, 5)
buf.write_substring("Hello, World!", 7, 5)
inspect!(buf, content="World")
}

Expand Down
4 changes: 4 additions & 0 deletions builtin/array.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -982,3 +982,7 @@ pub fn iter2[A](self : Array[A]) -> Iter2[Int, A] {
},
)
}

pub fn Array::default[T]() -> Array[T] {
[]
}
8 changes: 8 additions & 0 deletions builtin/array_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -566,3 +566,11 @@ test "array_dedup - edge cases" {
array.dedup()
inspect!(array, content="[1, 2, 3, 4, 5]")
}

struct MX {
mm_num : Array[Int]
} derive(Default, ToJson)

test {
@json.inspect!(MX::default(), content={ "mm_num": [] })
}
13 changes: 12 additions & 1 deletion builtin/buffer.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub fn write_bytes(self : Buffer, value : Bytes) -> Unit {
}

/// Write a sub-string into buffer.
pub fn write_sub_string(
pub fn write_substring(
self : Buffer,
value : String,
start : Int,
Expand All @@ -93,6 +93,17 @@ pub fn write_sub_string(
self.len += len * 2
}

/// Write a sub-string into buffer.
/// @alert deprecated "Use `Buffer::write_substring` instead"
pub fn write_sub_string(
self : Buffer,
value : String,
start : Int,
len : Int
) -> Unit {
self.write_substring(value, start, len)
}

/// Write a char into buffer.
pub fn write_char(self : Buffer, value : Char) -> Unit {
self.grow_if_necessary(self.len + 4)
Expand Down
10 changes: 8 additions & 2 deletions builtin/builtin.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ impl Array {
compare[T : Compare + Eq](Self[T], Self[T]) -> Int
contains[T : Eq](Self[T], T) -> Bool
dedup[T : Eq](Self[T]) -> Unit
default[T]() -> Self[T]
drain[T](Self[T], Int, Int) -> Self[T]
each[T](Self[T], (T) -> Unit) -> Unit
eachi[T](Self[T], (Int, T) -> Unit) -> Unit
Expand Down Expand Up @@ -175,7 +176,8 @@ impl Buffer {
write_char(Self, Char) -> Unit
write_object(Self, Show) -> Unit
write_string(Self, String) -> Unit
write_sub_string(Self, String, Int, Int) -> Unit
write_sub_string(Self, String, Int, Int) -> Unit //deprecated
write_substring(Self, String, Int, Int) -> Unit
}

pub type! Failure String
Expand Down Expand Up @@ -351,7 +353,7 @@ impl StringBuilder {
write_char(Self, Char) -> Unit
write_object[T : Show](Self, T) -> Unit
write_string(Self, String) -> Unit
write_sub_string(Self, String, Int, Int) -> Unit
write_substring(Self, String, Int, Int) -> Unit
}

pub type UnsafeMaybeUninit
Expand Down Expand Up @@ -494,6 +496,7 @@ impl UInt {
op_shr(UInt, Int) -> UInt
op_sub(UInt, UInt) -> UInt
popcnt(UInt) -> Int
reinterpret_as_float(UInt) -> Float
reinterpret_as_int(UInt) -> Int
shl(UInt, Int) -> UInt //deprecated
shr(UInt, Int) -> UInt //deprecated
Expand Down Expand Up @@ -723,6 +726,7 @@ pub trait Hash {

pub trait Logger {
write_string(Self, String) -> Unit
write_substring(Self, String, Int, Int) -> Unit
write_sub_string(Self, String, Int, Int) -> Unit
write_char(Self, Char) -> Unit
}
Expand Down Expand Up @@ -765,6 +769,8 @@ impl Hash for Tuple(6)

impl Hash for Tuple(7)

impl Logger::write_sub_string

impl Show for Unit

impl Show for Bool
Expand Down
2 changes: 2 additions & 0 deletions builtin/intrinsics.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,8 @@ pub fn Int::to_float(self : Int) -> Float = "%i32.to_f32"

pub fn Int::reinterpret_as_float(self : Int) -> Float = "%i32.to_f32_reinterpret"

pub fn UInt::reinterpret_as_float(self : UInt) -> Float = "%i32.to_f32_reinterpret"

pub fn Byte::to_float(self : Byte) -> Float = "%byte.to_f32"

pub fn Double::to_float(self : Double) -> Float = "%f64.to_f32"
Expand Down
21 changes: 21 additions & 0 deletions builtin/intrinsics_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -412,3 +412,24 @@ test "Int::to_uint64 - Random Values" {
inspect!(Int::to_uint64(1234567890), content="1234567890")
inspect!(Int::to_uint64(-1234567890), content="18446744072474983726")
}

test "UInt::reinterpret_as_float roundtrip" {
let values = [
0x00000000U, // zero
0x80000000U, // negative zero
0x3F800000U, // 1.0
0xBF800000U, // -1.0
0x7F800000U, // positive infinity
0xFF800000U, // negative infinity
0x7FC00000U, // NaN
0x00800000U, // smallest positive normal float
0x7F7FFFFFU, // largest positive normal float
0x00000001U, // smallest positive subnormal float
0x007FFFFFU, // largest positive subnormal float
]
for value in values {
let float_value = value.reinterpret_as_float()
let roundtrip_value = Float::reinterpret_as_uint(float_value)
assert_eq!(roundtrip_value, value)
}
}
2 changes: 1 addition & 1 deletion builtin/show.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub impl Show for String with output(self, logger) {
let mut segment_start = 0
fn flush_segment(i : Int) {
if i > segment_start {
logger.write_sub_string(self, segment_start, i - segment_start)
logger.write_substring(self, segment_start, i - segment_start)
}
segment_start = i + 1
}
Expand Down
15 changes: 1 addition & 14 deletions builtin/string.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,6 @@ pub fn substring(
~start : Int = 0,
~end : Int = self.length()
) -> String {
if start < 0 {
abort("String::substring: start index is negative")
}
if end < 0 {
abort("String::substring: end index is negative")
}
if start > end {
abort("String::substring: start index is greater than end index")
}
if end > self.length() {
abort(
"String::substring: end index is greater than the length of the string",
)
}
guard start >= 0 && start <= end && end <= self.length()
unsafe_substring(self, start, end)
}
2 changes: 1 addition & 1 deletion builtin/stringbuilder_buffer.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub fn StringBuilder::write_char(self : StringBuilder, ch : Char) -> Unit {
self.len += inc
}

pub fn StringBuilder::write_sub_string(
pub fn StringBuilder::write_substring(
self : StringBuilder,
str : String,
start : Int,
Expand Down
2 changes: 1 addition & 1 deletion builtin/stringbuilder_concat.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn StringBuilder::write_char(self : StringBuilder, ch : Char) -> Unit {
self.val += Char::to_string(ch)
}

pub fn StringBuilder::write_sub_string(
pub fn StringBuilder::write_substring(
self : StringBuilder,
str : String,
start : Int,
Expand Down
16 changes: 15 additions & 1 deletion builtin/stringbuilder_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,24 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// when we write the string into the buffer
// we assume the string is utf16 and blit
// we assume the bytes (in valid region [0, len)) is utf16
// so that
fn id(s : String) -> String {
StringBuilder::new()..write_string(s).to_string()
}

test "stringbuilder" {
let buf = StringBuilder::new()
buf.write_string("hello")
buf.write_char(' ')
buf.write_sub_string("world", 0, 3)
buf.write_substring("world", 0, 3)
inspect!(buf.to_string(), content="hello wor")
}

test {
let data = ["a", "b", "c", "hello world"]
@json.inspect!(data.map(id), content=["a", "b", "c", "hello world"])
// assert_eq!(StringBuilder::new()..write_string("hello").to_string(), "hello")
}
6 changes: 6 additions & 0 deletions builtin/traits.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,16 @@ pub trait Default {
/// Trait for a logger, where debug logs can be written into
pub trait Logger {
write_string(Self, String) -> Unit
write_substring(Self, String, Int, Int) -> Unit
/// @alert deprecated "use `Logger::write_substring` instead"
write_sub_string(Self, String, Int, Int) -> Unit
write_char(Self, Char) -> Unit
}

impl Logger with write_sub_string(self, value, start, len) {
self.write_substring(value, start, len)
}

/// Trait for types that can be converted to `String`
pub trait Show {
// `output` is used for composition of aggregate structure.
Expand Down
2 changes: 1 addition & 1 deletion bytes/bytes.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn Bytes::from_iter(iter : Iter[Byte]) -> Bytes {
/// ```moonbit
/// let b = @bytes.of([b'\x41', b'\x00', b'\x42'])
/// ```
///
/// TODO: marked as intrinsic, inline if it is constant
pub fn Bytes::of(arr : FixedArray[Byte]) -> Bytes {
let rv = Bytes::new(arr.length())
for i = 0; i < arr.length(); i = i + 1 {
Expand Down
23 changes: 23 additions & 0 deletions bytes/bytes_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,29 @@ test "from_array" {
)
}

test "from_array literal" {
let b = @bytes.of([65, 0, 66, 0, 67, 0])
inspect!(
b,
content=
#|b"\x41\x00\x42\x00\x43\x00"
,
)
}

test "from array literal" {
let b1 = @bytes.of(
[
b'\x41', b'\x00', b'\x42', b'\x00', b'\x43', b'\x00', b'\x44', b'\x00', b'\x45',
b'\x00', b'\x46', b'\x00', b'\x47', b'\x00', b'\x48', b'\x00', b'\x49', b'\x00',
],
)
let b2 = @bytes.of(
[65, 0, 66, 0, 67, 0, 68, 0, 69, 0, 70, 0, 71, 0, 72, 0, 73, 0],
)
assert_eq!(b1, b2)
}

test "hash" {
let b1 = @bytes.of(
[
Expand Down
1 change: 0 additions & 1 deletion char/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"import": [
"moonbitlang/core/builtin",
"moonbitlang/core/test",
"moonbitlang/core/coverage"
]
}
4 changes: 2 additions & 2 deletions coverage/coverage.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ pub fn end() -> Unit {
let mut start = 0
for j = 0; j < str.length(); j = j + 1 {
if str[j] == '\n' {
line_buf.write_sub_string(str, start, j)
line_buf.write_substring(str, start, j)
println(line_buf.to_string())
line_buf.reset()
start = j + 1
}
}
if start < str.length() {
line_buf.write_sub_string(str, start, str.length())
line_buf.write_substring(str, start, str.length())
}
}
let trailing_line = line_buf.to_string()
Expand Down
4 changes: 1 addition & 3 deletions deque/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{
"import": [
"moonbitlang/core/builtin",
"moonbitlang/core/test",
"moonbitlang/core/coverage",
"moonbitlang/core/array"
"moonbitlang/core/coverage"
],
"targets": {
"panic_test.mbt": ["not", "native"]
Expand Down
Loading

0 comments on commit 4e14af0

Please sign in to comment.