summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorZuhaitz <zuhaitz.zechhub@gmail.com>2026-01-31 17:22:17 +0000
committerGitHub <noreply@github.com>2026-01-31 17:22:17 +0000
commit962d659c61212b1a23acfe56dda7cb92b721feda (patch)
treeba1637d3885213095b312f81a477c33b1ebca6aa /examples
parente521ee7d175393ef37579ebd61ccb7e8d56a397f (diff)
parent91ed9fdd65e09bd6cd32e44dd07c390f2cf79c22 (diff)
Merge branch 'main' into main
Diffstat (limited to 'examples')
-rw-r--r--examples/data_structures/stack.zc8
-rw-r--r--examples/graphics/mandelbrot.zc74
-rw-r--r--examples/networking/echo_server.zc10
-rw-r--r--examples/objc_interop.zc42
-rw-r--r--examples/process/exec.zc20
-rw-r--r--examples/tools/mini_grep.zc14
6 files changed, 116 insertions, 52 deletions
diff --git a/examples/data_structures/stack.zc b/examples/data_structures/stack.zc
index 4a8593a..ea6a20a 100644
--- a/examples/data_structures/stack.zc
+++ b/examples/data_structures/stack.zc
@@ -1,11 +1,11 @@
import "std.zc"
-struct Stack<T> {
+struct MyStack<T> {
data: Vec<T>;
}
-impl Stack<T> {
+impl MyStack<T> {
fn new() -> Self {
return Self { data: Vec<T>::new() };
}
@@ -43,7 +43,7 @@ impl Stack<T> {
fn main() {
"[Integer Stack]";
- let int_stack = Stack<int>::new();
+ let int_stack = MyStack<int>::new();
defer int_stack.free();
"Pushing: 10, 20, 30";
@@ -64,7 +64,7 @@ fn main() {
"";
"[String Stack]";
- let str_stack = Stack<String>::new();
+ let str_stack = MyStack<String>::new();
defer str_stack.free();
str_stack.push(String::new("First"));
diff --git a/examples/graphics/mandelbrot.zc b/examples/graphics/mandelbrot.zc
index 04f61b9..3a0662e 100644
--- a/examples/graphics/mandelbrot.zc
+++ b/examples/graphics/mandelbrot.zc
@@ -5,35 +5,37 @@ struct Complex {
im: float,
}
-fn complex_make(re: float, im: float) -> Complex {
- return Complex { re: re, im: im };
-}
+impl Complex {
+ fn new(re: float, im: float) -> Complex {
+ return Complex { re: re, im: im };
+ }
-fn complex_add(a: Complex, b: Complex) -> Complex {
- return Complex { re: a.re + b.re, im: a.im + b.im };
-}
+ fn add(self, b: Complex) -> Complex {
+ return Complex { re: self.re + b.re, im: self.im + b.im };
+ }
-fn complex_mul(a: Complex, b: Complex) -> Complex {
- return Complex {
- re: a.re * b.re - a.im * b.im,
- im: a.re * b.im + a.im * b.re
- };
-}
+ fn mul(self, b: Complex) -> Complex {
+ return Complex {
+ re: self.re * b.re - self.im * b.im,
+ im: self.re * b.im + self.im * b.re
+ };
+ }
-fn complex_abs2(z: Complex) -> float {
- return z.re * z.re + z.im * z.im;
-}
+ fn abs2(self) -> float {
+ return self.re * self.re + self.im * self.im;
+ }
-fn complex_print(z: Complex) {
- println "{z.re}{z.im}i";
+ fn print(self) {
+ println "{self.re}{self.im}i";
+ }
}
fn pick_char(iter: int, max_iter: int, edge_chars: char[], edge_count: int) -> char {
if (iter >= max_iter) { return ' '; }
if (iter <= 0) { return edge_chars[0]; }
- let t: float = ((float)iter) / ((float)max_iter);
- let idx: int = (int)(t * ((float)(edge_count - 1)));
+ let t = (float)iter / (float)max_iter;
+ let idx = (int)(t * (float)(edge_count - 1));
if (idx < 0) { idx = 0; }
if (idx >= edge_count) { idx = edge_count - 1; }
@@ -42,32 +44,30 @@ fn pick_char(iter: int, max_iter: int, edge_chars: char[], edge_count: int) -> c
}
fn main() {
- let width: int = 120;
- let height: int = 40;
- let max_iter: int = 200;
+ let width = 120;
+ let height = 40;
+ let max_iter = 200;
- let edge_chars: char[12] = [ '#', '@', '%', '8', '&', '*', '+', '=', '-', ':', '.', ',' ];
- let edge_count: int = 12;
+ let edge_chars: char[] = [ '#', '@', '%', '8', '&', '*', '+', '=', '-', ':', '.', ',' ];
+ let edge_count = 12;
- let min_re: float = -2.2;
- let max_re: float = 1.0;
- let min_im: float = -1.2;
- let max_im: float = 1.2;
+ let min_re = -2.2;
+ let max_re = 1.0;
+ let min_im = -1.2;
+ let max_im = 1.2;
for y in 0..height {
- let im: float =
- max_im - (max_im - min_im) * ( ((float)y) / ((float)(height - 1)) );
+ let im = max_im - (max_im - min_im) * ((float)y / (float)(height - 1));
for x in 0..width {
- let re: float =
- min_re + (max_re - min_re) * ( ((float)x) / ((float)(width - 1)) );
+ let re = min_re + (max_re - min_re) * ((float)x / (float)(width - 1));
- let c: Complex = complex_make(re, im);
- let z: Complex = complex_make(0.0, 0.0);
+ let c = Complex::new(re, im);
+ let z = Complex::new(0.0, 0.0);
- let iter: int = 0;
- while (iter < max_iter and complex_abs2(z) <= 4.0) {
- z = complex_add(complex_mul(z, z), c);
+ let iter = 0;
+ while (iter < max_iter and z.abs2() <= 4.0) {
+ z = z.mul(z).add(c);
iter += 1;
}
diff --git a/examples/networking/echo_server.zc b/examples/networking/echo_server.zc
index 072c3a2..2934923 100644
--- a/examples/networking/echo_server.zc
+++ b/examples/networking/echo_server.zc
@@ -1,11 +1,13 @@
import "std/net.zc"
+def SIZE = 1024;
+
fn main() {
"Starting Echo Server on 127.0.0.1:8080...";
let listener_res = TcpListener::bind("127.0.0.1", 8080);
- if listener_res.is_err() {
+ guard listener_res.is_ok() else {
!"Failed to bind: {listener_res.err}";
return 1;
}
@@ -19,9 +21,9 @@ fn main() {
let stream = client_res.unwrap();
defer stream.close();
- let buf: char[1024];
-
- let read_res = stream.read(buf, 1024);
+ let buf = (char*)malloc(SIZE);
+ defer free(buf);
+ let read_res = stream.read(buf, SIZE);
if read_res.is_ok() {
let bytes = read_res.unwrap();
diff --git a/examples/objc_interop.zc b/examples/objc_interop.zc
new file mode 100644
index 0000000..c33fd0d
--- /dev/null
+++ b/examples/objc_interop.zc
@@ -0,0 +1,42 @@
+
+//> macos: framework: Foundation
+//> linux: cflags: -fconstant-string-class=NSConstantString -D_NATIVE_OBJC_EXCEPTIONS -I/usr/include/x86_64-linux-gnu/GNUstep -std=gnu99
+//> linux: link: -lgnustep-base -lobjc
+
+include <Foundation/Foundation.h>
+
+raw {
+ id make_nsstring(const char* s) {
+ return [NSString stringWithUTF8String:s];
+ }
+
+ void print_description(id obj) {
+ NSLog(@"[ObjC Log] Description: %@", obj);
+ }
+}
+
+fn main() {
+
+ raw {
+ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+ }
+
+ "=> Zen C + Objective-C interop demo.";
+ "=> (Make sure to run with: zc run --objc examples/objc_interop.zc)";
+
+ // Call ObjC helper to create an NSString (returned as id)
+ let s = make_nsstring("Hello from Objective-C!");
+
+ // Pass it back to ObjC
+ print_description(s);
+
+ "Zen C interpolated string: {s}";
+
+ raw {
+ // You can also raw ObjC syntax anywhere.
+ NSArray *arr = [NSArray arrayWithObjects: @"Zen", @"Bit", @"C", nil];
+ NSLog(@"[ObjC Raw] Array: %@", arr);
+
+ [pool drain];
+ }
+}
diff --git a/examples/process/exec.zc b/examples/process/exec.zc
new file mode 100644
index 0000000..712a356
--- /dev/null
+++ b/examples/process/exec.zc
@@ -0,0 +1,20 @@
+
+import "std/process.zc";
+import "std/string.zc";
+
+fn main() {
+ "Executing 'ls -la' using std/process...";
+
+ let output = Command::new("ls")
+ .arg("-l")
+ .arg("-a")
+ .output();
+
+ if output.exit_code == 0 {
+ "--- Output ---";
+ output.stdout.print();
+ "--------------";
+ } else {
+ !"Command failed with exit code: {output.exit_code}";
+ }
+}
diff --git a/examples/tools/mini_grep.zc b/examples/tools/mini_grep.zc
index 39fec07..54338bf 100644
--- a/examples/tools/mini_grep.zc
+++ b/examples/tools/mini_grep.zc
@@ -30,7 +30,7 @@ fn str_find_case(haystack: string, needle: string, ignore_case: bool) -> Option<
let n = needle;
let n_len = strlen(needle);
- while (*h != 0)
+ while (*h != '\0')
{
let is_match: bool = true;
for i in 0..n_len
@@ -52,7 +52,7 @@ fn str_find_case(haystack: string, needle: string, ignore_case: bool) -> Option<
return Option<string>::None();
}
-fn print_highlight(line: string, match_ptr: string, match_len: usize, config: GrepConfig) {
+fn print_highlight(line: string, match_ptr: string, match_len: usize, config: GrepConfig*) {
if (!config.color)
{
"{line}";
@@ -84,7 +84,7 @@ fn print_highlight(line: string, match_ptr: string, match_len: usize, config: Gr
"{current}";
}
-fn grep_file(path: string, config: GrepConfig) -> Result<int> {
+fn grep_file(path: string, config: GrepConfig*) -> Result<int> {
let content_str: String = File::read_all(path)?;
defer content_str.destroy();
let content = content_str.c_str();
@@ -95,7 +95,7 @@ fn grep_file(path: string, config: GrepConfig) -> Result<int> {
let q_len = strlen(config.query);
- while (*ptr != 0)
+ while (*ptr != '\0')
{
if (*ptr == '\n')
{
@@ -163,7 +163,7 @@ fn grep_file(path: string, config: GrepConfig) -> Result<int> {
return Result<int>::Ok(0);
}
-fn visit_dir(path: string, config: GrepConfig) {
+fn visit_dir(path: string, config: GrepConfig*) {
let entries_res = File::read_dir(path);
guard entries_res.is_ok() else return;
@@ -287,7 +287,7 @@ fn main(argc: int, argv: string*)
{
if (config.recursive)
{
- visit_dir(config.path, config);
+ visit_dir(config.path, &config);
}
else
{
@@ -296,7 +296,7 @@ fn main(argc: int, argv: string*)
}
else
{
- let res = grep_file(config.path, config);
+ let res = grep_file(config.path, &config);
if (res.is_err()) {
!"Error: {res.err}";
return 1;