From 9617498442e7961c4a0b62b296527be16782ec1b Mon Sep 17 00:00:00 2001 From: Edgar Luque Date: Mon, 15 Jan 2024 17:08:01 +0100 Subject: [PATCH] add fib and update readme --- README.md | 26 +++++++++++++----------- crates/concrete_driver/tests/programs.rs | 25 +++++++++++++++++++++++ examples/fib_if.con | 13 ++++++++++++ 3 files changed, 52 insertions(+), 12 deletions(-) create mode 100644 examples/fib_if.con diff --git a/README.md b/README.md index 6791844..122a5c3 100644 --- a/README.md +++ b/README.md @@ -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 { None, Some(T), diff --git a/crates/concrete_driver/tests/programs.rs b/crates/concrete_driver/tests/programs.rs index 442b01f..e10a08a 100644 --- a/crates/concrete_driver/tests/programs.rs +++ b/crates/concrete_driver/tests/programs.rs @@ -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#" diff --git a/examples/fib_if.con b/examples/fib_if.con new file mode 100644 index 0000000..4863382 --- /dev/null +++ b/examples/fib_if.con @@ -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); + } +}