Fix up evaluation baybeeee!!!!!

also write the primes program or w/e
This commit is contained in:
Emi Simpson 2022-04-24 23:03:42 -04:00
parent ac62dc4e2d
commit d9385ed1c3
Signed by: Emi
GPG key ID: A12F2C2FFDC3D847
2 changed files with 50 additions and 18 deletions

View file

@ -1,5 +1,31 @@
main: Int
main = 1 + distance_squared 4 3
main = nth_prime 77
distance_squared: Int, Int -> Int
distance_squared a b = a * a + b * b
nth_prime: Int -> Int
nth_prime n =
if n == 1
then 2
else nth_prime_helper (n - 1) 3
nth_prime_helper: Int, Int -> Int
nth_prime_helper remaining_primes next_to_try =
if is_prime next_to_try
then
if remaining_primes < 2
then next_to_try
else nth_prime_helper (remaining_primes - 1) (next_to_try + 2)
else nth_prime_helper remaining_primes (next_to_try + 2)
is_prime: Int -> Int
is_prime x =
if x % 2 == 0
then 0
else is_prime_helper x 3
is_prime_helper: Int, Int -> Int
is_prime_helper x try =
if try * try > x
then 1
else if x % try == 0
then 0
else is_prime_helper x (try + 2)

View file

@ -252,6 +252,11 @@ pub fn get_last_ident(exprs: &LinkedList<Expr>) -> Option<Identifier> {
pub fn evaluate(exprs: LinkedList<Expr>, bindings: &ValueBindings) -> evaluation::Result {
let bindings = bindings.nested_scope();
if exprs.iter().all(|e| !e.isnt_noop()) {
bindings.lookup(&exprs.back().ok_or(EvaluateError::EvaluatingZeroLengthExpr)?.identifier)
.ok_or_else(|| EvaluateError::UndefinedValue(exprs.back().unwrap().identifier.clone()))
.cloned()
} else {
let (all_bindings, last_ident) = exprs.into_iter()
.filter(Expr::isnt_noop)
.try_fold(
@ -268,3 +273,4 @@ pub fn evaluate(exprs: LinkedList<Expr>, bindings: &ValueBindings) -> evaluation
.ok_or(EvaluateError::UndefinedValue(ident))
)
}
}