diff options
| -rw-r--r-- | examples/algorithms/quicksort.zc | 57 | ||||
| -rw-r--r-- | examples/data_structures/binary_tree.zc | 91 | ||||
| -rw-r--r-- | examples/data_structures/linked_list.zc | 85 | ||||
| -rw-r--r-- | examples/data_structures/stack.zc | 81 | ||||
| -rw-r--r-- | examples/raylib_demo.zc | 40 |
5 files changed, 354 insertions, 0 deletions
diff --git a/examples/algorithms/quicksort.zc b/examples/algorithms/quicksort.zc new file mode 100644 index 0000000..adef038 --- /dev/null +++ b/examples/algorithms/quicksort.zc @@ -0,0 +1,57 @@ + +import "std.zc" + +fn swap(arr: int*, i: isize, j: isize) { + var temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; +} + +fn partition(arr: int*, low: isize, high: isize) -> isize { + var pivot = arr[high]; + var i = low - 1; + + for j in low..high { + if arr[j] < pivot { + i++; + swap(arr, i, j); + } + } + swap(arr, i + 1, high); + return i + 1; +} + +fn quicksort(arr: int*, low: isize, high: isize) { + if low < high { + var pi = partition(arr, low, high); + quicksort(arr, low, pi - 1); + quicksort(arr, pi + 1, high); + } +} + +fn print_array(arr: int*, size: isize) { + "["..; + for i in 0..size { + "{arr[i]}"..; + if i < size - 1 { ", "..; } + } + "]"; +} + +fn main() { + var v = Vec<int>::new(); + defer v.free(); + + var values = [10, 7, 8, 9, 1, 5, 3, 12, 2, 6]; + for i in 0..10 { + v.push(values[i]); + } + + "Original: "..; + print_array(v.data, (int)v.len); + + quicksort(v.data, 0, (isize)v.len - 1); + + "Sorted: "..; + print_array(v.data, (int)v.len); +} diff --git a/examples/data_structures/binary_tree.zc b/examples/data_structures/binary_tree.zc new file mode 100644 index 0000000..14e7b3d --- /dev/null +++ b/examples/data_structures/binary_tree.zc @@ -0,0 +1,91 @@ +import "std.zc" + +struct Node { + value: int; + left: Node*; + right: Node*; +} + +impl Node { + fn new(v: int) -> Self* { + var n = alloc<Self>(); + n.value = v; + n.left = NULL; + n.right = NULL; + return n; + } +} + +struct BST { + root: Node*; +} + +impl BST { + fn new() -> Self { + return Self { root: NULL }; + } + + fn _insert(node: Node*, v: int) -> Node* { + guard node != NULL else { + return Node::new(v); + } + + if v < node.value { + node.left = BST::_insert(node.left, v); + } else if v > node.value { + node.right = BST::_insert(node.right, v); + } + + return node; + } + + fn insert(self, v: int) { + self.root = BST::_insert(self.root, v); + } + + fn _print_in_order(node: Node*) { + if node != NULL { + BST::_print_in_order(node.left); + "{node.value} "..; + BST::_print_in_order(node.right); + } + } + + fn print_all(self) { + BST::_print_in_order(self.root); + ""; + } + + fn _free(node: Node*) { + if node != NULL { + BST::_free(node.left); + BST::_free(node.right); + free(node); + } + } + + fn free(self) { + BST::_free(self.root); + self.root = NULL; + } +} + +fn main() { + var tree = BST::new(); + defer tree.free(); + + "Inserting: 50, 30, 20, 40, 70, 60, 80"; + tree.insert(50); + tree.insert(30); + tree.insert(20); + tree.insert(40); + tree.insert(70); + tree.insert(60); + tree.insert(80); + + "In-Order Traversal: "..; + tree.print_all(); + + "Expected: 20 30 40 50 60 70 80"; + +} diff --git a/examples/data_structures/linked_list.zc b/examples/data_structures/linked_list.zc new file mode 100644 index 0000000..cb85554 --- /dev/null +++ b/examples/data_structures/linked_list.zc @@ -0,0 +1,85 @@ + +import "std/mem.zc" + +struct Node { + value: int; + next: Node*; +} + +impl Node { + fn new(v: int) -> Self* { + var n = alloc<Node>(); + guard n != NULL else { + "Out of memory!"; + return NULL; + } + n.value = v; + n.next = NULL; + return n; + } +} + +struct LinkedList { + head: Node*; +} + +impl LinkedList { + fn new() -> Self { + return Self { head: NULL }; + } + + fn push(self, v: int) { + var new_node = Node::new(v); + guard new_node != NULL else { return; } + + new_node.next = self.head; + self.head = new_node; + } + + fn print_list(self) { + var current: Node* = self.head; + "["..; + while current != NULL { + "{current.value}"..; + current = current.next; + if current != NULL { + " -> "..; + } + } + "]"; + } + + fn free(self) { + var current: Node* = self.head; + while current != NULL { + autofree var temp = current; + current = current.next; + } + self.head = NULL; + } + + fn len(self) -> int { + var count = 0; + var current: Node* = self.head; + while current != NULL { + count++; + current = current.next; + } + return count; + } +} + +fn main() { + var list = LinkedList::new(); + defer list.free(); + + "Pushing: 10, 20, 30..."; + list.push(10); + list.push(20); + list.push(30); + + "List length: {list.len()}"; + + "Current List: "..; + list.print_list(); +} diff --git a/examples/data_structures/stack.zc b/examples/data_structures/stack.zc new file mode 100644 index 0000000..8f1fea5 --- /dev/null +++ b/examples/data_structures/stack.zc @@ -0,0 +1,81 @@ + +import "std.zc" + +struct Stack<T> { + data: Vec<T>; +} + +impl Stack<T> { + fn new() -> Self { + return Self { data: Vec<T>::new() }; + } + + fn push(self, item: T) { + self.data.push(item); + } + + fn pop(self) -> Option<T> { + if self.data.length() == 0 { + return Option<T>::None(); + } + return Option<T>::Some(self.data.pop()); + } + + fn peek(self) -> Option<T> { + if self.data.length() == 0 { + return Option<T>::None(); + } + return Option<T>::Some(self.data.last()); + } + + fn is_empty(self) -> bool { + return self.data.length() == 0; + } + + fn size(self) -> usize { + return self.data.length(); + } + + fn free(self) { + self.data.free(); + } +} + +fn main() { + "[Integer Stack]"; + var int_stack = Stack<int>::new(); + defer int_stack.free(); + + "Pushing: 10, 20, 30"; + int_stack.push(10); + int_stack.push(20); + int_stack.push(30); + + "Size: {int_stack.size()}"; + var p = int_stack.peek(); + "Peek: {p.unwrap()}"; + + "Popping: "..; + while !int_stack.is_empty() { + var opt = int_stack.pop(); + "{opt.unwrap()} "..; + } + ""; + + ""; + "[String Stack]"; + var str_stack = Stack<String>::new(); + defer str_stack.free(); + + str_stack.push(String::new("First")); + str_stack.push(String::new("Second")); + str_stack.push(String::new("Third")); + + "Popping: "..; + while !str_stack.is_empty() { + var opt_s = str_stack.pop(); + var s: String = opt_s.unwrap(); + "{s.c_str()} "..; + } + ""; +} diff --git a/examples/raylib_demo.zc b/examples/raylib_demo.zc new file mode 100644 index 0000000..77b661e --- /dev/null +++ b/examples/raylib_demo.zc @@ -0,0 +1,40 @@ +//> link: -lraylib -lm + +import "raylib.h" as raylib; + +fn main() { + raylib::InitWindow(800, 600, "Zen C + Raylib"); + defer raylib::CloseWindow(); + + raylib::SetTargetFPS(60); + + var x = 400; + var y = 300; + var dx = 5; + var dy = 4; + var radius = 30; + + while !raylib::WindowShouldClose() + { + x += dx; + y += dy; + + if x - radius < 0 || x + radius > 800 { + dx = -dx; + } + if y - radius < 0 || y + radius > 600 { + dy = -dy; + } + + raylib::BeginDrawing(); + raylib::ClearBackground(RAYWHITE); + + raylib::DrawText("Zen C + Raylib Demo!", 250, 20, 30, DARKGRAY); + + raylib::DrawCircle(x, y, (float)radius, RED); + + raylib::DrawFPS(10, 10); + + raylib::EndDrawing(); + } +} |
