Skip to content

Commit

Permalink
tests: add operator overloading
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Feb 28, 2024
1 parent 2110cc9 commit e58691d
Showing 1 changed file with 195 additions and 0 deletions.
195 changes: 195 additions & 0 deletions tests/operator_overloading/main.jule
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
// Copyright 2024 The Jule Programming Language.
// Use of this source code is governed by a BSD 3-Clause
// license that can be found in the LICENSE file.

struct Int {
x: int
}

impl Int {
pub fn add(self, y: Int): Int {
ret Int{self.x + y.x}
}

pub fn add_assign(mut self, y: Int) {
self.x += y.x
}

pub fn sub(self, y: Int): Int {
ret Int{self.x - y.x}
}

pub fn sub_assign(mut self, y: Int) {
self.x -= y.x
}

pub fn mul(self, y: Int): Int {
ret Int{self.x * y.x}
}

pub fn mul_assign(mut self, y: Int) {
self.x *= y.x
}

pub fn div(self, y: Int): Int {
ret Int{self.x / y.x}
}

pub fn div_assign(mut self, y: Int) {
self.x /= y.x
}

pub fn mod(self, y: Int): Int {
ret Int{self.x % y.x}
}

pub fn mod_assign(mut self, y: Int) {
self.x %= y.x
}

pub fn shl(self, y: Int): Int {
ret Int{self.x << y.x}
}

pub fn shl_assign(mut self, y: Int) {
self.x <<= y.x
}

pub fn shr(self, y: Int): Int {
ret Int{self.x >> y.x}
}

pub fn shr_assign(mut self, y: Int) {
self.x >>= y.x
}

pub fn bit_or(self, y: Int): Int {
ret Int{self.x | y.x}
}

pub fn bit_or_assign(mut self, y: Int) {
self.x |= y.x
}

pub fn bit_and(self, y: Int): Int {
ret Int{self.x & y.x}
}

pub fn bit_and_assign(mut self, y: Int) {
self.x &= y.x
}

pub fn bit_xor(self, y: Int): Int {
ret Int{self.x ^ y.x}
}

pub fn bit_xor_assign(mut self, y: Int) {
self.x ^= y.x
}

pub fn bit_not(self): Int {
ret Int{^self.x}
}

pub fn neg(self): Int {
ret Int{-self.x}
}

pub fn pos(self): Int {
ret Int{+self.x}
}
}

struct Number[T] {
x: T
}

impl Number {
pub fn add(self, y: Number[T]): Number[T] {
ret Number[T]{self.x + y.x}
}

pub fn add_assign(mut self, y: Number[T]) {
self.x += y.x
}

pub fn sub(self, y: Number[T]): Number[T] {
ret Number[T]{self.x - y.x}
}

pub fn sub_assign(mut self, y: Number[T]) {
self.x -= y.x
}

pub fn mul(self, y: Number[T]): Number[T] {
ret Number[T]{self.x * y.x}
}

pub fn mul_assign(mut self, y: Number[T]) {
self.x *= y.x
}

pub fn div(self, y: Number[T]): Number[T] {
ret Number[T]{self.x / y.x}
}

pub fn div_assign(mut self, y: Number[T]) {
self.x /= y.x
}

pub fn neg(self): Number[T] {
ret Number[T]{-self.x}
}

pub fn pos(self): Number[T] {
ret Number[T]{+self.x}
}
}

fn test_int() {
let mut x = Int{10}
let y = Int{20}
_ = x + y
x += y
_ = x - y
x -= y
_ = x * y
x *= y
_ = x / y
x /= y
_ = x % y
x %= y
_ = x << y
x <<= y
_ = x >> y
x >>= y
_ = x | y
x |= y
_ = x & y
x &= y
_ = x ^ y
x ^= y
_ = ^x
_ = -x
_ = +x
}

fn test_number() {
let mut x = Number[f64]{10}
let y = Number[f64]{20}
_ = x + y
x += y
_ = x - y
x -= y
_ = x * y
x *= y
_ = x / y
x /= y
_ = -x
_ = +x
}

fn main() {
test_int()
test_number()
}

0 comments on commit e58691d

Please sign in to comment.