-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Pull Request Initial implementation of the monkey formatter. ## Description In this PR a basic Monkey formatter is implemented. The formatter itself is really simple and there is no customization. The idea was to have an initial implementation that could be later improved. Some modifications to the structure of the project have also been made. ## Changes Made - Creation of a formatter crate. - Basic formatter - Refactoring of the `precedence_of` function on the `parser` module - Renamed `repl::Cli` to `repl::ReplCli`, to avoid any sort of confusion with `formatter::FormatterCli` - Fixed the `fibonacci_it.monkey` example and the `array_append` bench. - Add more monkey examples. ## Related Issue <!-- Optional --> <!-- Link to the related issue, e.g., "Closes #123" or "Fixes #456" --> Closes #3 ## Checklist - [x] I have self-reviewed my code - [x] Code follows project's style guidelines - [x] Tests added and passing - [x] Documentation updated (if needed)
- Loading branch information
Showing
25 changed files
with
1,528 additions
and
71 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
use criterion::{black_box, criterion_group, criterion_main, Criterion}; | ||
|
||
use formatter::formatter::Formatter; | ||
|
||
pub fn long_formatter_benchmark(c: &mut Criterion) { | ||
let input = r#" | ||
let fibonacci_it = fn (x) { | ||
if (x < 2) { | ||
return x; | ||
} | ||
let iter = fn (i, table) { | ||
if (i > x) { | ||
return last(table); | ||
} else { | ||
let new_table = push(table, table[i - 1] + table[i - 2]); | ||
return iter(i + 1, new_table); | ||
} | ||
}; | ||
return iter(2, [0, 1]); | ||
}; | ||
let fib = fibonacci_it(20); | ||
puts (fib); | ||
let fibonacci = fn (x) { | ||
if (x < 2) { | ||
x | ||
} else { | ||
fibonacci(x - 1) + fibonacci(x - 2) | ||
} | ||
}; | ||
puts(fibonacci(3)); | ||
let filter = fn (arr, f) { | ||
let iter = fn (arr, accumulated) { | ||
if (len(arr) == 0) { | ||
accumulated | ||
} else { | ||
let head = first(arr); | ||
let tail = rest(arr); | ||
if (f(head)) { | ||
iter(tail, push(accumulated, head)) | ||
} else { | ||
iter(tail, accumulated) | ||
} | ||
} | ||
}; | ||
iter(arr, []) | ||
}; | ||
let a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 100]; | ||
let is_even = fn (x) { | ||
x / 2 * 2 == x | ||
}; | ||
filter(a, is_even); | ||
let foldl = fn (arr, initial , f) { | ||
let iter = fn (arr, result) { | ||
if (len(arr) == 0) { | ||
result; | ||
} else { | ||
iter(rest(arr), f(result, first(arr))) | ||
} | ||
}; | ||
iter(arr, initial) | ||
}; | ||
let a = [1, 2, 3, 4]; | ||
let sum = fn (x, y) { | ||
x + y | ||
}; | ||
foldl(a, 0, sum); | ||
let foldr = fn(arr, initial, f) { | ||
let iter = fn(arr, result) { | ||
if (len(arr) == 0) { | ||
result | ||
} else { | ||
iter(rest(arr), f(first(arr), result)); | ||
} | ||
}; | ||
iter(arr, initial); | ||
}; | ||
let a = [1, 2, 3, 4]; | ||
let sum = fn(x, y) { x + y }; | ||
foldr(a, 0, sum); | ||
let input = r; | ||
" | ||
let map = fn(arr, f) { | ||
let iter = fn(arr, accumulated) { | ||
if (len(arr) == 0) { | ||
accumulated | ||
} else { | ||
iter(rest(arr), push(accumulated, f(first(arr)))); | ||
} | ||
}; | ||
iter(arr, []); | ||
}; | ||
let a = [1, 2, 3, 4]; | ||
let double = fn(x) { x * 2 }; | ||
map(a, double); | ||
"; | ||
~/Projects/monkey-rs 3-Formatter *2 !1 ?1 ❯ cat monkey_examples/* 17:00:36 | ||
let fibonacci_it = fn (x) { | ||
if (x < 2) { | ||
return x; | ||
} | ||
let iter = fn (i, table) { | ||
if (i > x) { | ||
return last(table); | ||
} else { | ||
let new_table = push(table, table[i - 1] + table[i - 2]); | ||
return iter(i + 1, new_table); | ||
} | ||
}; | ||
return iter(2, [0, 1]); | ||
}; | ||
let fib = fibonacci_it(20); | ||
puts(fib); | ||
let fibonacci = fn (x) { | ||
if (x < 2) { | ||
x | ||
} else { | ||
fibonacci(x - 1) + fibonacci(x - 2) | ||
} | ||
}; | ||
puts(fibonacci(30)); | ||
let filter = fn (arr, f) { | ||
let iter = fn (arr, accumulated) { | ||
if (len(arr) == 0) { | ||
accumulated | ||
} else { | ||
let head = first(arr); | ||
let tail = rest(arr); | ||
if (f(head)) { | ||
iter(tail, push(accumulated, head)) | ||
} else { | ||
iter(tail, accumulated) | ||
} | ||
} | ||
}; | ||
iter(arr, []) | ||
}; | ||
let a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 100]; | ||
let is_even = fn (x) { | ||
x / 2 * 2 == x | ||
}; | ||
filter(a, is_even); | ||
let foldl = fn (arr, initial, f) { | ||
let iter = fn (arr, result) { | ||
if (len(arr) == 0) { | ||
result | ||
} else { | ||
iter(rest(arr), f(result, first(arr))) | ||
} | ||
}; | ||
iter(arr, initial) | ||
}; | ||
let a = [1, 2, 3, 4]; | ||
let sum = fn (x, y) { | ||
x + y | ||
}; | ||
foldl(a, 0, sum); | ||
let foldr = fn(arr, initial, f) { | ||
let iter = fn(arr, result) { | ||
if (len(arr) == 0) { | ||
result | ||
} else { | ||
iter(rest(arr), f(first(arr), result)); | ||
} | ||
}; | ||
iter(arr, initial); | ||
}; | ||
let a = [1, 2, 3, 4]; | ||
let sum = fn(x, y) { x + y }; | ||
foldr(a, 0, sum); | ||
let map = fn(arr, f) {let iter = fn(arr, accumulated) { | ||
if (len(arr) == 0) { | ||
accumulated | ||
} else { | ||
iter(rest(arr), push(accumulated, f(first(arr)))); | ||
} | ||
}; | ||
iter(arr, []); | ||
}; | ||
let a = [1, 2, 3, 4]; | ||
let double = fn(x) { x * 2 }; | ||
map(a, double); | ||
"#; | ||
|
||
c.bench_function("Long format", |b| { | ||
b.iter(|| Formatter::format(black_box(input))) | ||
}); | ||
} | ||
|
||
criterion_group!(benches, long_formatter_benchmark); | ||
criterion_main!(benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
name = "formatter" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
|
||
lexer = { path = "../lexer" } | ||
parser = { path = "../parser" } | ||
|
||
|
||
# External crates | ||
clap = "4.3.11" | ||
clap_derive = "4.3.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
let filter = fn(arr, f) { | ||
let iter = fn(arr, accumulated) { | ||
if (len(arr)==0) { | ||
accumulated | ||
} else { | ||
let head = first(arr); | ||
let tail =rest(arr); | ||
if (f(head)) { | ||
iter(tail, push(accumulated, head)); | ||
} else { | ||
iter(tail, accumulated); | ||
} | ||
} | ||
}; | ||
iter(arr, []); | ||
}; | ||
let a = [1, 2, 3, 4,5,6,7,8,9,11,100]; | ||
let is_even = fn(x) { (x/2)*2 == x }; | ||
filter(a, is_even); |
Oops, something went wrong.