Skip to content

Commit

Permalink
add new examples
Browse files Browse the repository at this point in the history
  • Loading branch information
tahadostifam committed Feb 11, 2025
1 parent ef1ccbd commit 9ea2fe1
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 11 deletions.
15 changes: 11 additions & 4 deletions compiler/src/funcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,12 @@ impl Compiler {
));
}
} else {
if !self.block_is_terminated(block) {
unsafe { gcc_jit_block_end_with_void_return(block, null_mut()) };
let guard = self.block_func_ref.lock().unwrap();
if let Some(block) = guard.block {
drop(guard);
if !self.block_is_terminated(block) {
unsafe { gcc_jit_block_end_with_void_return(block, null_mut()) };
}
}
}

Expand Down Expand Up @@ -246,8 +250,11 @@ impl Compiler {
}
};

let mut args =
self.compile_func_arguments(Rc::clone(&scope), Some(metadata.params.list.clone()), func_call.arguments);
let mut args = self.compile_func_arguments(
Rc::clone(&scope),
Some(metadata.params.list.clone()),
func_call.arguments,
);

let rvalue = unsafe {
gcc_jit_context_new_call(
Expand Down
31 changes: 31 additions & 0 deletions examples/display_square_of_numbers.cy
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import io;

fn display_double_square(square_size: i32) {
for (#i = 1; i <= square_size; i++) { // col
for (#j = 1; j <= square_size; j++) { // row
IO.printf("%d ", j);
}

IO.printf(" ");

for (#j = 1; j <= square_size; j++) { // row
IO.printf("%d ", j);
}

IO.printf("\n");
}
}

pub fn main() {
#square_size: i32 = 4;

for (#sizer = 0; sizer < 3; sizer++) {
display_double_square(square_size);
IO.printf("\n");
display_double_square(square_size);
square_size = square_size + 4;

IO.printf("\n");
IO.printf("\n");
}
}
6 changes: 3 additions & 3 deletions examples/main.cy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import io;
import io;

pub fn main() {
IO.printf("Hi %s.\n", "Taha");
}
io.printf("Hello World\n");
}
8 changes: 5 additions & 3 deletions examples/unconditional_for_loop1.cy
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import io;

pub fn main(): i32 {
cprintf("Program started.\n");
IO.printf("Program started.\n");

#i: i32 = 10;

for {
cprintf("i->%d\n", i);
IO.printf("i->%d\n", i);

if (i == 5) {
break;
Expand All @@ -13,7 +15,7 @@ pub fn main(): i32 {
i--;
}

cprintf("Program finished.\n");
IO.printf("Program finished.\n");

return 0;
}
2 changes: 1 addition & 1 deletion stdlib/io.cy
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extern fn printf(fmt: string, ...): void as _printf;

pub struct IO {
pub struct io {
pub fn printf(fmt: string, ...) {
_printf(fmt);
}
Expand Down

0 comments on commit 9ea2fe1

Please sign in to comment.