diff options
| -rw-r--r-- | examples/data_structures/stack.zc | 8 | ||||
| -rw-r--r-- | examples/graphics/mandelbrot.zc | 74 | ||||
| -rw-r--r-- | examples/networking/echo_server.zc | 10 | ||||
| -rw-r--r-- | examples/tools/mini_grep.zc | 14 | ||||
| -rw-r--r-- | src/codegen/codegen_utils.c | 4 |
5 files changed, 58 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/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; diff --git a/src/codegen/codegen_utils.c b/src/codegen/codegen_utils.c index 8de3cf6..391ebd3 100644 --- a/src/codegen/codegen_utils.c +++ b/src/codegen/codegen_utils.c @@ -411,6 +411,10 @@ char *infer_type(ParserContext *ctx, ASTNode *node) char *inner = infer_type(ctx, node->unary.operand); if (inner) { + if (strcmp(inner, "string") == 0) + { + return xstrdup("char"); + } char *ptr = strchr(inner, '*'); if (ptr) { |
