Skip to content

Commit

Permalink
add fib and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
edg-l committed Jan 15, 2024
1 parent fffd75f commit 9617498
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
26 changes: 14 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,22 +124,24 @@ But we want to take a different path with respect to:
- No marker traits like Send, Sync for concurrency. The runtime will take care of that.

## Syntax
```
mod FibonacciModule {
pub fib(x: u64) -> u64 {
match x {
// we can match literal values
0 | 1 -> x,
n -> fib(n-1) + fib(n-2)
}
}
```rust
mod Fibonacci {
fn main() -> i64 {
return fib(10);
}

pub fn fib(n: u64) -> u64 {
if n < 2 {
return n;
}

return fib(n - 1) + fib(n - 2);
}
}
```

```
```rust
mod Option {
pub enum Option<T> {
None,
Some(T),
Expand Down
25 changes: 25 additions & 0 deletions crates/concrete_driver/tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,31 @@ fn test_factorial_with_if() {
assert_eq!(code, 24);
}

#[test]
fn test_fib_with_if() {
let source = r#"
mod Fibonacci {
fn main() -> i64 {
return fib(10);
}
pub fn fib(n: u64) -> u64 {
if n < 2 {
return n;
}
return fib(n - 1) + fib(n - 2);
}
}
"#;

let result = compile_program(source, "fib", false).expect("failed to compile");

let output = run_program(&result.binary_file).expect("failed to run");
let code = output.status.code().unwrap();
assert_eq!(code, 55);
}

#[test]
fn test_simple_add() {
let source = r#"
Expand Down
13 changes: 13 additions & 0 deletions examples/fib_if.con
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
mod Fibonacci {
fn main() -> i64 {
return fib(10);
}

pub fn fib(n: u64) -> u64 {
if n < 2 {
return n;
}

return fib(n - 1) + fib(n - 2);
}
}

0 comments on commit 9617498

Please sign in to comment.