Skip to content

Commit

Permalink
Merge pull request #107 from bzy-debug/zhiyuan/update-syntax
Browse files Browse the repository at this point in the history
func -> fn, Float64 -> Double
  • Loading branch information
bzy-debug authored Nov 14, 2023
2 parents 2e2f81c + e29983c commit 2f927fa
Show file tree
Hide file tree
Showing 38 changed files with 195 additions and 195 deletions.
8 changes: 4 additions & 4 deletions examples/assert/lib/assert.mbt
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
pub func assert_eq[T : Compare + Show](left : T, right : T) {
pub fn assert_eq[T : Compare + Show](left : T, right : T) {
if left.compare(right) != 0 {
abort("assertion failed: (left == right)\n" + "left: " + left.to_string() + "\n" + "right: " + right.to_string() + "\n")
}
}

pub func assert(x : Bool) {
pub fn assert(x : Bool) {
if x != true {
abort("assertion failed: " + x.to_string() + "\n")
}
}

pub func assert_ne[T : Compare + Show](left : T, right : T) {
pub fn assert_ne[T : Compare + Show](left : T, right : T) {
if left.compare(right) == 0 {
abort("assertion failed: (left != right)\n" + "left: " + left.to_string() + "\n" + "right: " + right.to_string() + "\n")
}
}

func init {
fn init {
()
}

2 changes: 1 addition & 1 deletion examples/assert/main/main.mbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
func init {
fn init {
@lib.assert_eq(1, 2) // abort
}

24 changes: 12 additions & 12 deletions examples/avl_tree/lib/avl.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub enum T[U] {
/// `height[U](self: T[U])`
///
/// Calculate the height of a tree-like structure.
pub func height[U](self: T[U]) -> Int {
pub fn height[U](self: T[U]) -> Int {
match self {
Empty => 0
Node(_, _, _, h) => h
Expand All @@ -20,7 +20,7 @@ pub func height[U](self: T[U]) -> Int {
/// `create[U](l: T[U], v: U, r: T[U])`
///
/// Create a new node with the given left and right subtrees, along with a value of type `U`.
func create[U](l: T[U], v: U, r: T[U]) -> T[U] {
fn create[U](l: T[U], v: U, r: T[U]) -> T[U] {
let hl = l.height()
let hr = r.height()
Node(l, v, r, if hl >= hr { hl + 1 } else { hr + 1 })
Expand All @@ -33,7 +33,7 @@ func create[U](l: T[U], v: U, r: T[U]) -> T[U] {
/// This function performs a balancing operation on a avl tree node based on the heights
/// of its left and right subtrees. It ensures that the heights of the subtrees are balanced
/// and returns a new avl tree node with appropriate restructuring if necessary.
func bal[U](l: T[U], v: U, r: T[U]) -> T[U] {
fn bal[U](l: T[U], v: U, r: T[U]) -> T[U] {
let hl = l.height()
let hr = r.height()

Expand Down Expand Up @@ -79,7 +79,7 @@ func bal[U](l: T[U], v: U, r: T[U]) -> T[U] {
/// `add[U:Compare](self: T[U], x: U)`
///
/// Add a value to a tree-like structure.
pub func add[U:Compare](self: T[U], x: U) -> T[U] {
pub fn add[U:Compare](self: T[U], x: U) -> T[U] {
match self {
Empty => Node(Empty, x, Empty, 1)
Node(l, v, r, _) as t => {
Expand All @@ -94,7 +94,7 @@ pub func add[U:Compare](self: T[U], x: U) -> T[U] {
/// `min_elt[U](self: T[U], default: U)`
///
/// Find the minimum element in a tree-like data structure.
func min_elt[U](self: T[U], default: U) -> U {
fn min_elt[U](self: T[U], default: U) -> U {
match self {
Empty => default
Node(Empty, v, _, _) => v
Expand All @@ -105,7 +105,7 @@ func min_elt[U](self: T[U], default: U) -> U {
/// `remove_min_elt[U](l: T[U], v: U, r: T[U])`
///
/// Remove the minimum element from a avl tree and rebalance the tree.
func remove_min_elt[U](l: T[U], v: U, r: T[U]) -> T[U] {
fn remove_min_elt[U](l: T[U], v: U, r: T[U]) -> T[U] {
match l {
Empty => r
Node(ll, lv, lr, _) => bal(remove_min_elt(ll, lv, lr), v, r)
Expand All @@ -115,7 +115,7 @@ func remove_min_elt[U](l: T[U], v: U, r: T[U]) -> T[U] {
/// `internal_merge[U](self: T[U], other: T[U])`
///
/// Merge two AVL trees of the same user-defined type `U` into a new AVL tree.
func internal_merge[U](self: T[U], other: T[U]) -> T[U] {
fn internal_merge[U](self: T[U], other: T[U]) -> T[U] {
match (self, other) {
(Empty, t) => t
(t, Empty) => t
Expand All @@ -127,7 +127,7 @@ func internal_merge[U](self: T[U], other: T[U]) -> T[U] {
/// `remove[U:Compare](self: T[U], x: U)`
///
/// Removes a value from the AVL tree while maintaining balance
pub func remove[U:Compare](self: T[U], x: U) -> T[U] {
pub fn remove[U:Compare](self: T[U], x: U) -> T[U] {
match self {
Empty => Empty
Node(l, v, r, _) => {
Expand All @@ -142,7 +142,7 @@ pub func remove[U:Compare](self: T[U], x: U) -> T[U] {
/// `to_string[U:Show](self: T[U]) -> String`
///
/// convert the AVL tree to string.
pub func to_string[U : Show](self : T[U]) -> String {
pub fn to_string[U : Show](self : T[U]) -> String {
match self {
Empty => "()"
Node(Empty, v, Empty, _) => v.to_string()
Expand All @@ -153,7 +153,7 @@ pub func to_string[U : Show](self : T[U]) -> String {
/// `mem[U:Compare](self: T[U], x: U)`
///
/// Check if a given element exists in an AVL tree.
pub func mem[U:Compare](self: T[U], x: U) -> Bool {
pub fn mem[U:Compare](self: T[U], x: U) -> Bool {
match self {
Empty => false
Node(l, v, r, _) => {
Expand All @@ -164,7 +164,7 @@ pub func mem[U:Compare](self: T[U], x: U) -> Bool {
}
}

func repeat_str(s: String, n: Int) -> String {
fn repeat_str(s: String, n: Int) -> String {
var result = ""
var i = 0
while i < n {
Expand All @@ -177,7 +177,7 @@ func repeat_str(s: String, n: Int) -> String {
/// `print_tree[U:Show](self: T[U])`
///
/// Print the AVL tree
pub func print_tree[U:Show](self: T[U]) {
pub fn print_tree[U:Show](self: T[U]) {
fn helper(node: T[U], level: Int) {
match node {
Empty => ()
Expand Down
4 changes: 2 additions & 2 deletions examples/avl_tree/main/main.mbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
func init {
fn init {
var v : @lib.T[Int] = Empty // Create an empty AVL tree with Int type
let iter = 30

Expand Down Expand Up @@ -28,7 +28,7 @@ func init {
while k < iter {
v = v.remove(k)
k = k + 1
}
}

// Tree is empty, removal successful
match v {
Expand Down
24 changes: 12 additions & 12 deletions examples/buffer/lib/buffer.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@ pub struct Buffer[T] {
mut data : Array[T]
}

pub func Buffer::new[T : Default](capacity : Int) -> Buffer[T] {
pub fn Buffer::new[T : Default](capacity : Int) -> Buffer[T] {
{ cap: capacity, len: 0, data: Array::make(capacity, T::default()) }
}

pub func capacity[T](self : Buffer[T]) -> Int {
pub fn capacity[T](self : Buffer[T]) -> Int {
self.cap
}

pub func length[T](self : Buffer[T]) -> Int {
pub fn length[T](self : Buffer[T]) -> Int {
self.len
}

pub func op_get[T](self : Buffer[T], i : Int) -> Option[T] {
pub fn op_get[T](self : Buffer[T], i : Int) -> Option[T] {
if i < self.cap {
Some(self.data[i])
} else {
None
}
}

func expand_size[T : Default](self : Buffer[T]) {
fn expand_size[T : Default](self : Buffer[T]) {
let new_capacity = if self.cap != 0 {
self.cap * 2
} else {
Expand All @@ -40,15 +40,15 @@ func expand_size[T : Default](self : Buffer[T]) {
self.data = new_data
}

pub func append[T : Default](self : Buffer[T], value : T) {
pub fn append[T : Default](self : Buffer[T], value : T) {
if self.len >= self.cap {
self.expand_size()
}
self.data[self.len] = value
self.len = self.len + 1
}

pub func truncate[T : Default](self : Buffer[T], another : Buffer[T]) {
pub fn truncate[T : Default](self : Buffer[T], another : Buffer[T]) {
var index = 0
while index < another.len {
if self.len >= self.cap {
Expand All @@ -60,7 +60,7 @@ pub func truncate[T : Default](self : Buffer[T], another : Buffer[T]) {
}
}

pub func clear[T : Default](self : Buffer[T]) {
pub fn clear[T : Default](self : Buffer[T]) {
var index = 0
while index < self.len {
self.data[index] = T::default()
Expand All @@ -69,13 +69,13 @@ pub func clear[T : Default](self : Buffer[T]) {
self.len = 0
}

pub func reset[T : Default](self : Buffer[T], capacity : Int) {
pub fn reset[T : Default](self : Buffer[T], capacity : Int) {
self.cap = capacity
self.len = 0
self.data = Array::make(capacity, T::default())
}

pub func println[T : Show](self : Buffer[T]) {
pub fn println[T : Show](self : Buffer[T]) {
var index = 0
print('[')
while index < self.len {
Expand All @@ -85,10 +85,10 @@ pub func println[T : Show](self : Buffer[T]) {
print(',')
}
}
println(']')
println(']')
}

func init {
fn init {
()
}

2 changes: 1 addition & 1 deletion examples/buffer/main/main.mbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
func init {
fn init {
let buf : @lib.Buffer[Int] = @lib.Buffer::new(5)
println(buf.capacity()) // 5
var index = 0
Expand Down
24 changes: 12 additions & 12 deletions examples/bytes-buffer/lib/buffer.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@ struct Buffer {
mut len : Int
}

pub func Buffer::new(size : Int) -> Buffer {
pub fn Buffer::new(size : Int) -> Buffer {
{ data: Bytes::make(size, 0), len: 0 }
}

pub func op_get(self : Buffer, i : Int) -> Option[Int] {
pub fn op_get(self : Buffer, i : Int) -> Option[Int] {
if i < self.data.length() {
Some(self.data[i])
} else {
None
}
}

pub func capacity(self : Buffer) -> Int {
pub fn capacity(self : Buffer) -> Int {
self.data.length()
}

pub func length(self : Buffer) -> Int {
pub fn length(self : Buffer) -> Int {
self.len
}

func expand_size(self : Buffer) {
fn expand_size(self : Buffer) {
let new_capacity = if self.data.length() != 0 {
self.data.length() * 2
} else {
Expand All @@ -38,7 +38,7 @@ func expand_size(self : Buffer) {
self.data = new_data
}

pub func append_int(self : Buffer, value : Int) {
pub fn append_int(self : Buffer, value : Int) {
if self.len >= self.data.length() {
self.expand_size()
}
Expand All @@ -54,7 +54,7 @@ pub func append_int(self : Buffer, value : Int) {
}
}

pub func truncate(self : Buffer, another: Buffer) {
pub fn truncate(self : Buffer, another: Buffer) {
var index = 0
while index < another.len {
if self.len >= self.data.length() {
Expand All @@ -66,7 +66,7 @@ pub func truncate(self : Buffer, another: Buffer) {
}
}

pub func clear(self : Buffer) {
pub fn clear(self : Buffer) {
var index = 0
while index < self.len {
self.data[index] = 0
Expand All @@ -75,24 +75,24 @@ pub func clear(self : Buffer) {
self.len = 0
}

pub func reset(self : Buffer, capacity : Int) {
pub fn reset(self : Buffer, capacity : Int) {
self.len = 0
self.data = Bytes::make(capacity, 0)
}

pub func to_bytes(self : Buffer) -> Bytes {
pub fn to_bytes(self : Buffer) -> Bytes {
self.data
}

pub func bytes_to_int(bytes : Bytes, start : Int) -> Int {
pub fn bytes_to_int(bytes : Bytes, start : Int) -> Int {
bytes[start].land(0xff).lsl(24).lor(
bytes[start + 1].land(0xff).lsl(16).lor(
bytes[start + 2].land(0xff).lsl(8).lor(bytes[start + 3].land(0xff)),
),
)
}

func init {
fn init {
()
}

2 changes: 1 addition & 1 deletion examples/bytes-buffer/main/main.mbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
func init {
fn init {
let buf = @lib.Buffer::new(4)
buf.append_int(256)
buf.append_int(255)
Expand Down
2 changes: 1 addition & 1 deletion examples/cf_worker_demo/lib/add100.mbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub func add(a:Int, b:Int) -> Int {
pub fn add(a:Int, b:Int) -> Int {
return a + b + 100
}
2 changes: 1 addition & 1 deletion examples/cf_worker_demo/main/main.mbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
func init {
fn init {

}
Loading

0 comments on commit 2f927fa

Please sign in to comment.