Skip to content

Commit b5e9e06

Browse files
authored
Merge branch 'main' into pragma
2 parents ee6bc00 + f0d1b1b commit b5e9e06

31 files changed

+379
-242
lines changed

bool/bool.mbt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,23 @@ pub trait Boolean {
1919
pub fn Bool::to_bool(self : Bool) -> Bool {
2020
self
2121
}
22+
23+
pub fn to_int(self : Bool) -> Int {
24+
if self {
25+
1
26+
} else {
27+
0
28+
}
29+
}
30+
31+
pub fn to_int64(self : Bool) -> Int64 {
32+
if self {
33+
1L
34+
} else {
35+
0L
36+
}
37+
}
38+
39+
pub fn hash(self : Bool) -> Int {
40+
self.to_int()
41+
}

builtin/console.mbt

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -85,43 +85,6 @@ pub fn to_string(self : Int) -> String {
8585
buf.to_string()
8686
}
8787

88-
pub fn to_int(self : Bool) -> Int {
89-
if self {
90-
1
91-
} else {
92-
0
93-
}
94-
}
95-
96-
pub fn to_int64(self : Bool) -> Int64 {
97-
if self {
98-
1L
99-
} else {
100-
0L
101-
}
102-
}
103-
104-
fn to_bool(self : Int) -> Bool {
105-
if self == 0 {
106-
false
107-
} else {
108-
true
109-
}
110-
}
111-
112-
fn to_bool(self : Int64) -> Bool {
113-
if self == 0L {
114-
false
115-
} else {
116-
true
117-
}
118-
}
119-
120-
/// @intrinsic %f64.to_string
121-
pub fn to_string(self : Double) -> String {
122-
double_to_string(self)
123-
}
124-
12588
pub fn op_lt[T : Compare](self_ : T, other : T) -> Bool {
12689
self_.compare(other).is_neg()
12790
}
@@ -171,13 +134,6 @@ pub fn String::make(length : Int, value : Char) -> String {
171134
}
172135
}
173136
174-
/// Convert Char to String
175-
pub fn to_string(self : Char) -> String {
176-
let bytes = Bytes::make(4, 0)
177-
let len = bytes.set_utf16_char(0, self)
178-
bytes.sub_string(0, len)
179-
}
180-
181137
pub fn inspect(
182138
obj : Show,
183139
~content : String = "",

builtin/debug.mbt

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ pub fn debug_write(self : Int64, buf : Buffer) -> Unit {
4141
buf.write_string(self.to_string())
4242
}
4343

44-
pub fn debug_write(self : Double, buf : Buffer) -> Unit {
45-
buf.write_string(self.to_string())
46-
}
47-
4844
fn should_escape(ch : Char) -> Bool {
4945
match ch {
5046
'"' | '\'' | '\\' | '\n' | '\r' | '\b' | '\t' => true
@@ -85,12 +81,6 @@ fn escape_write_char(ch : Char, buf : Buffer) -> Unit {
8581
}
8682
}
8783

88-
pub fn debug_write(self : Char, buf : Buffer) -> Unit {
89-
buf.write_char('\'')
90-
escape_write_char(self, buf)
91-
buf.write_char('\'')
92-
}
93-
9484
pub fn debug_write[X : Debug](self : Option[X], buf : Buffer) -> Unit {
9585
match self {
9686
None => buf.write_string("None")
@@ -157,56 +147,3 @@ pub fn debug_write(self : String, buf : Buffer) -> Unit {
157147
flush_segment(self.length())
158148
buf.write_char('"')
159149
}
160-
161-
pub fn debug_write[X : Debug, Y : Debug](self : (X, Y), buf : Buffer) -> Unit {
162-
buf.write_char('(')
163-
self.0.debug_write(buf)
164-
buf.write_string(", ")
165-
self.1.debug_write(buf)
166-
buf.write_char(')')
167-
}
168-
169-
pub fn debug_write[X : Debug, Y : Debug, Z : Debug](
170-
self : (X, Y, Z),
171-
buf : Buffer
172-
) -> Unit {
173-
buf.write_char('(')
174-
self.0.debug_write(buf)
175-
buf.write_string(", ")
176-
self.1.debug_write(buf)
177-
buf.write_string(", ")
178-
self.2.debug_write(buf)
179-
buf.write_char(')')
180-
}
181-
182-
pub fn debug_write[X : Debug, Y : Debug, Z : Debug, W : Debug](
183-
self : (X, Y, Z, W),
184-
buf : Buffer
185-
) -> Unit {
186-
buf.write_char('(')
187-
self.0.debug_write(buf)
188-
buf.write_string(", ")
189-
self.1.debug_write(buf)
190-
buf.write_string(", ")
191-
self.2.debug_write(buf)
192-
buf.write_string(", ")
193-
self.3.debug_write(buf)
194-
buf.write_char(')')
195-
}
196-
197-
pub fn debug_write[X1 : Debug, X2 : Debug, X3 : Debug, X4 : Debug, X5 : Debug](
198-
self : (X1, X2, X3, X4, X5),
199-
buf : Buffer
200-
) -> Unit {
201-
buf.write_char('(')
202-
self.0.debug_write(buf)
203-
buf.write_string(", ")
204-
self.1.debug_write(buf)
205-
buf.write_string(", ")
206-
self.2.debug_write(buf)
207-
buf.write_string(", ")
208-
self.3.debug_write(buf)
209-
buf.write_string(", ")
210-
self.4.debug_write(buf)
211-
buf.write_char(')')
212-
}

builtin/list.mbt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
pub fn List::default[X]() -> List[X] {
16-
Nil
17-
}
18-
1915
pub fn List::op_equal[X : Eq](xs : List[X], ys : List[X]) -> Bool {
2016
match (xs, ys) {
2117
(Nil, Nil) => true

builtin/option.mbt

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
pub fn Option::default[X]() -> Option[X] {
16-
None
17-
}
18-
1915
pub fn op_equal[X : Eq](self : Option[X], other : Option[X]) -> Bool {
2016
match (self, other) {
2117
(None, None) => true
@@ -24,28 +20,6 @@ pub fn op_equal[X : Eq](self : Option[X], other : Option[X]) -> Bool {
2420
}
2521
}
2622

27-
pub fn compare[X : Compare](self : Option[X], other : Option[X]) -> Int {
28-
match self {
29-
None =>
30-
match other {
31-
None => 0
32-
Some(_) => -1
33-
}
34-
Some(x) =>
35-
match other {
36-
None => 1
37-
Some(y) => x.compare(y)
38-
}
39-
}
40-
}
41-
42-
pub fn unwrap[X](self : Option[X]) -> X {
43-
match self {
44-
None => abort("called `Option::unwrap()` on a `None` value")
45-
Some(x) => x
46-
}
47-
}
48-
4923
pub fn to_string[X : Show](self : Option[X]) -> String {
5024
match self {
5125
None => "None"

builtin/result.mbt

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,3 @@ pub fn op_equal[T : Eq, E : Eq](
2222
_ => false
2323
}
2424
}
25-
26-
pub fn compare[T : Compare, E : Compare](
27-
self : Result[T, E],
28-
other : Result[T, E]
29-
) -> Int {
30-
match (self, other) {
31-
(Ok(x), Ok(y)) => x.compare(y)
32-
(Ok(_), Err(_)) => -1
33-
(Err(_), Ok(_)) => 1
34-
(Err(x), Err(y)) => x.compare(y)
35-
}
36-
}
37-
38-
pub fn unwrap[T, E](self : Result[T, E]) -> T {
39-
match self {
40-
Ok(x) => x
41-
Err(_) => abort("called `Result::unwrap()` on an `Err` value")
42-
}
43-
}

builtin/string.mbt

Lines changed: 0 additions & 33 deletions
This file was deleted.

builtin/xxhash.mbt renamed to bytes/xxhash.mbt

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -29,41 +29,6 @@ fn xxhash32(input : Bytes, seed : Int) -> Int {
2929
finalize(h, input, len.land(-16), len.land(0xF))
3030
}
3131

32-
pub fn to_bytes(self : String) -> Bytes {
33-
let len = self.length()
34-
let buf = Bytes::make(len, 0)
35-
for i = 0; i < len; i = i + 1 {
36-
buf[i] = self[i].to_int()
37-
}
38-
buf
39-
}
40-
41-
pub fn hash(self : Bool) -> Int {
42-
self.to_int()
43-
}
44-
45-
pub fn hash(self : Char) -> Int {
46-
self.to_int()
47-
}
48-
49-
pub fn hash(self : Int) -> Int {
50-
self
51-
}
52-
53-
pub fn hash(self : Int64) -> Int {
54-
let lo = self.to_int()
55-
let hi = self.lsr(32).to_int()
56-
lo.lxor(hi)
57-
}
58-
59-
pub fn hash(self : Double) -> Int {
60-
self.reinterpret_as_i64().hash()
61-
}
62-
63-
pub fn hash(self : String) -> Int {
64-
xxhash32(self.to_bytes(), 0)
65-
}
66-
6732
pub fn hash(self : Bytes) -> Int {
6833
xxhash32(self, 0)
6934
}

char/char.mbt

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,70 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14+
15+
/// Convert Char to String
16+
pub fn to_string(self : Char) -> String {
17+
let bytes = Bytes::make(4, 0)
18+
let len = bytes.set_utf16_char(0, self)
19+
bytes.sub_string(0, len)
20+
}
21+
22+
test "to_string" {
23+
@assertion.assert_eq('a'.to_string(), "a")?
24+
}
25+
26+
/// Write a valid MoonBit syntax representation of a character to a buffer,
27+
/// with quotes added and special characters escaped.
28+
pub fn debug_write(self : Char, buf : Buffer) -> Unit {
29+
fn to_hex_digit(i : Int) -> Char {
30+
if i < 10 {
31+
Char::from_int('0'.to_int() + i)
32+
} else {
33+
Char::from_int('a'.to_int() + (i - 10))
34+
}
35+
}
36+
buf.write_char('\'')
37+
match self {
38+
'\'' | '\\' => {
39+
buf.write_char('\\')
40+
buf.write_char(self)
41+
}
42+
'\n' => buf.write_string("\\n")
43+
'\r' => buf.write_string("\\r")
44+
'\b' => buf.write_string("\\b")
45+
'\t' => buf.write_string("\\t")
46+
_ => {
47+
let code = self.to_int()
48+
if code < 0x20 {
49+
buf.write_char('\\')
50+
buf.write_char('x')
51+
buf.write_char(to_hex_digit(code / 16))
52+
buf.write_char(to_hex_digit(code % 16))
53+
} else {
54+
buf.write_char(self)
55+
}
56+
}
57+
}
58+
buf.write_char('\'')
59+
}
60+
61+
test "debug_write" {
62+
fn repr(chr) {
63+
let buf = Buffer::make(0)
64+
debug_write(chr, buf)
65+
buf.to_string()
66+
}
67+
@assertion.assert_eq(repr('a'), "'a'")?
68+
@assertion.assert_eq(repr('\''), "'\\''")?
69+
@assertion.assert_eq(repr('"'), "'\"'")?
70+
@assertion.assert_eq(repr('\\'), "'\\\\'")?
71+
@assertion.assert_eq(repr('\n'), "'\\n'")?
72+
@assertion.assert_eq(repr('\r'), "'\\r'")?
73+
@assertion.assert_eq(repr('\b'), "'\\b'")?
74+
@assertion.assert_eq(repr('\t'), "'\\t'")?
75+
@assertion.assert_eq(repr(Char::from_int(0)), "'\\x00'")?
76+
}
77+
78+
pub fn hash(self : Char) -> Int {
79+
self.to_int()
80+
}

char/moon.pkg.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"import": [
33
"moonbitlang/core/builtin",
4+
"moonbitlang/core/assertion",
45
"moonbitlang/core/coverage"
56
]
67
}

0 commit comments

Comments
 (0)