Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add int16 and uint16 operators #1421

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions builtin/builtin.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,11 @@ impl Byte {
op_sub(Byte, Byte) -> Byte
to_float(Byte) -> Float
to_int(Byte) -> Int
to_int16(Byte) -> Int16
to_int64(Byte) -> Int64
to_string(Byte) -> String
to_uint(Byte) -> UInt
to_uint16(Byte) -> UInt16
}


Expand Down Expand Up @@ -485,16 +487,26 @@ impl Int {
to_byte(Int) -> Byte
to_double(Int) -> Double
to_float(Int) -> Float
to_int16(Int) -> Int16
to_int64(Int) -> Int64
to_json(Int) -> Json
to_string(Int) -> String
to_uint(Int) -> UInt //deprecated
to_uint16(Int) -> UInt16
to_uint64(Int) -> UInt64
until(Int, Int, step~ : Int = .., inclusive~ : Bool = ..) -> Iter[Int]
upto(Int, Int, inclusive~ : Bool = ..) -> Iter[Int] //deprecated
}


impl Int16 {
to_byte(Int16) -> Byte
to_int(Int16) -> Int
to_int64(Int16) -> Int64
to_string(Int16) -> String
}


impl Int64 {
asr(Int64, Int) -> Int64 //deprecated
clz(Int64) -> Int
Expand Down Expand Up @@ -525,8 +537,10 @@ impl Int64 {
to_double(Int64) -> Double
to_float(Int64) -> Float
to_int(Int64) -> Int
to_int16(Int64) -> Int16
to_json(Int64) -> Json
to_string(Int64) -> String
to_uint16(Int64) -> UInt16
to_uint64(Int64) -> UInt64 //deprecated
until(Int64, Int64, step~ : Int64 = .., inclusive~ : Bool = ..) -> Iter[Int64]
upto(Int64, Int64, inclusive~ : Bool = ..) -> Iter[Int64] //deprecated
Expand Down Expand Up @@ -567,6 +581,14 @@ impl UInt {
}


impl UInt16 {
to_byte(UInt16) -> Byte
to_int(UInt16) -> Int
to_int64(UInt16) -> Int64
to_string(UInt16) -> String
}


impl UInt64 {
clz(UInt64) -> Int
compare(UInt64, UInt64) -> Int
Expand Down Expand Up @@ -870,8 +892,10 @@ impl Show for Bool
impl Show for Byte
impl Show for Char
impl Show for Int
impl Show for Int16
impl Show for Int64
impl Show for UInt
impl Show for UInt16
impl Show for UInt64
impl Show for String
impl[X : Show] Show for X?
Expand Down
10 changes: 10 additions & 0 deletions builtin/console.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@ pub fn UInt64::to_string(self : UInt64) -> String {
buf.to_string()
}

///|
pub fn Int16::to_string(self : Int16) -> String {
self.to_int().to_string()
}

///|
pub fn UInt16::to_string(self : UInt16) -> String {
self.to_int().to_string()
}

///|
/// @coverage.skip
pub fn op_lt[T : Compare](self_ : T, other : T) -> Bool {
Expand Down
20 changes: 20 additions & 0 deletions builtin/int64_js.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,16 @@ pub fn Int64::to_byte(self : Int64) -> Byte {
MyInt64::from_int64(self).to_int().to_byte()
}

///|
pub fn Int64::to_int16(self : Int64) -> Int16 {
MyInt64::from_int64(self).to_int().to_int16()
}

///|
pub fn Int64::to_uint16(self : Int64) -> UInt16 {
MyInt64::from_int64(self).to_int().to_uint16()
}

///|
pub fn UInt64::extend_uint(value : UInt) -> UInt64 {
MyInt64::extend_i32_u(value.reinterpret_as_int()).to_uint64()
Expand All @@ -566,6 +576,16 @@ pub fn Int::to_int64(self : Int) -> Int64 {
MyInt64::from_int(self).to_int64()
}

///|
pub fn Int16::to_int64(self : Int16) -> Int64 {
MyInt64::from_int(self.to_int()).to_int64()
}

///|
pub fn UInt16::to_int64(self : UInt16) -> Int64 {
MyInt64::from_int(self.to_int()).to_int64()
}

///|
/// @alert deprecated "Use `reinterpret_as_int64` instead"
/// @coverage.skip
Expand Down
12 changes: 12 additions & 0 deletions builtin/int64_nonjs.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ pub fn UInt64::reinterpret_as_double(self : UInt64) -> Double = "%i64_to_f64_rei
///|
pub fn Int64::to_byte(self : Int64) -> Byte = "%i64_to_byte"

///|
pub fn Int64::to_int16(self : Int64) -> Int16 = "%i64_to_i16"

///|
pub fn Int64::to_uint16(self : Int64) -> UInt16 = "%i64_to_u16"

///|
pub fn UInt64::trunc_double(val : Double) -> UInt64 = "%f64.to_u64"

Expand All @@ -118,6 +124,12 @@ pub fn UInt64::extend_uint(val : UInt) -> UInt64 = "%u32.to_u64"
///|
pub fn Int::to_int64(self : Int) -> Int64 = "%i32_to_i64"

///|
pub fn Int16::to_int64(self : Int16) -> Int64 = "%i16_to_i64"

///|
pub fn UInt16::to_int64(self : UInt16) -> Int64 = "%u16_to_i64"

///|
/// @alert deprecated "Use `reinterpret_as_int64` instead"
/// @coverage.skip
Expand Down
24 changes: 24 additions & 0 deletions builtin/intrinsics.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -462,3 +462,27 @@ pub fn Double::to_float(self : Double) -> Float = "%f64.to_f32"

///|
pub fn UInt::to_float(self : UInt) -> Float = "%u32.to_f32"

///|
pub fn Int16::to_int(self : Int16) -> Int = "%i16_to_i32"

///|
pub fn Int16::to_byte(self : Int16) -> Byte = "%i16_to_byte"

///|
pub fn Int::to_int16(self : Int) -> Int16 = "%i32_to_i16"

///|
pub fn Byte::to_int16(self : Byte) -> Int16 = "%byte_to_i16"

///|
pub fn UInt16::to_int(self : UInt16) -> Int = "%u16_to_i32"

///|
pub fn UInt16::to_byte(self : UInt16) -> Byte = "%u16_to_byte"

///|
pub fn Int::to_uint16(self : Int) -> UInt16 = "%i32_to_u16"

///|
pub fn Byte::to_uint16(self : Byte) -> UInt16 = "%byte_to_u16"
10 changes: 10 additions & 0 deletions builtin/show.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ pub impl Show for Byte with output(self, logger) {
logger.write_string(self.to_string())
}

///|
pub impl Show for Int16 with output(self, logger) {
logger.write_string(self.to_string())
}

///|
pub impl Show for UInt16 with output(self, logger) {
logger.write_string(self.to_string())
}

///|
pub impl Show for Bytes with output(self, logger) {
fn to_hex_digit(i : Int) -> Char {
Expand Down
51 changes: 51 additions & 0 deletions int16/int16.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2024 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

///|
pub fn op_add(self : Int16, that : Int16) -> Int16 {
(self.to_int() + that.to_int()).to_int16()
}

///|
pub fn op_sub(self : Int16, that : Int16) -> Int16 {
(self.to_int() - that.to_int()).to_int16()
}

///|
pub fn op_mul(self : Int16, that : Int16) -> Int16 {
(self.to_int() * that.to_int()).to_int16()
}

///|
pub fn op_div(self : Int16, that : Int16) -> Int16 {
(self.to_int() / that.to_int()).to_int16()
}

///|
pub fn op_equal(self : Int16, that : Int16) -> Bool {
self.to_int() == that.to_int()
}

///|
pub fn compare(self : Int16, that : Int16) -> Int {
self.to_int().compare(that.to_int())
}

///|
pub impl Hash for Int16 with hash(self) { self.to_int() }

///|
pub impl Hash for Int16 with hash_combine(self, hasher) {
hasher.combine_int(self.to_int())
}
20 changes: 20 additions & 0 deletions int16/int16.mbti
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package moonbitlang/core/int16

// Values

// Types and methods


impl Int16 {
compare(Int16, Int16) -> Int
op_add(Int16, Int16) -> Int16
op_div(Int16, Int16) -> Int16
op_equal(Int16, Int16) -> Bool
op_mul(Int16, Int16) -> Int16
op_sub(Int16, Int16) -> Int16
}

// Type aliases

// Traits

Loading
Loading