Get fizzbuzz working again

This commit is contained in:
Jeff 2024-08-20 15:16:06 -04:00
parent f9480ddc24
commit a6334070ae
6 changed files with 44 additions and 25 deletions

View File

@ -2,7 +2,6 @@ use std::{
cmp::Ordering, cmp::Ordering,
collections::HashMap, collections::HashMap,
fmt::{self, Display, Formatter}, fmt::{self, Display, Formatter},
rc::Weak,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};

View File

@ -128,7 +128,10 @@ impl BuiltInFunction {
} }
BuiltInFunction::WriteLine => { BuiltInFunction::WriteLine => {
if let Value::String(string) = &value_arguments[0] { if let Value::String(string) = &value_arguments[0] {
stdout().write_all(string.as_bytes())?; let mut stdout = stdout();
stdout.write_all(string.as_bytes())?;
stdout.write_all(b"\n")?;
Ok(None) Ok(None)
} else { } else {

View File

@ -43,6 +43,15 @@ pub fn core_library<'a>() -> &'a Context {
(0, 0), (0, 0),
), ),
), ),
(
Identifier::new("write_line"),
(
ContextData::VariableValue(Value::Function(Function::BuiltIn(
BuiltInFunction::WriteLine,
))),
(0, 0),
),
),
])) ]))
}) })
} }

View File

@ -792,15 +792,25 @@ impl Value {
} }
pub fn and(&self, other: &Value) -> Result<Value, ValueError> { pub fn and(&self, other: &Value) -> Result<Value, ValueError> {
match (self.as_boolean(), other.as_boolean()) { match (self, other) {
(Some(left), Some(right)) => Ok(Value::Boolean(left && right)), (Value::Boolean(left), Value::Boolean(right)) => Ok(Value::Boolean(*left && *right)),
(Value::Mutable(locked), value) | (value, Value::Mutable(locked)) => {
let locked = locked.read().unwrap();
locked.and(value)
}
_ => Err(ValueError::CannotAnd(self.clone(), other.clone())), _ => Err(ValueError::CannotAnd(self.clone(), other.clone())),
} }
} }
pub fn or(&self, other: &Value) -> Result<Value, ValueError> { pub fn or(&self, other: &Value) -> Result<Value, ValueError> {
match (self.as_boolean(), other.as_boolean()) { match (self, other) {
(Some(left), Some(right)) => Ok(Value::Boolean(left || right)), (Value::Boolean(left), Value::Boolean(right)) => Ok(Value::Boolean(*left || *right)),
(Value::Mutable(locked), value) | (value, Value::Mutable(locked)) => {
let locked = locked.read().unwrap();
locked.or(value)
}
_ => Err(ValueError::CannotOr(self.clone(), other.clone())), _ => Err(ValueError::CannotOr(self.clone(), other.clone())),
} }
} }

View File

@ -965,18 +965,16 @@ impl Vm {
.ok_or(RuntimeError::ExpectedBoolean { position })?; .ok_or(RuntimeError::ExpectedBoolean { position })?;
if boolean { if boolean {
let evaluation = self.run_block(if_block.inner, collect_garbage)?; self.run_block(if_block.inner, collect_garbage)
} else {
if let Evaluation::Break(_) = evaluation {
return Ok(evaluation);
}
}
match r#else { match r#else {
ElseExpression::If(if_expression) => { ElseExpression::If(if_expression) => {
self.run_if(*if_expression.inner, collect_garbage) self.run_if(*if_expression.inner, collect_garbage)
} }
ElseExpression::Block(block) => self.run_block(block.inner, collect_garbage), ElseExpression::Block(block) => {
self.run_block(block.inner, collect_garbage)
}
}
} }
} }
} }

View File

@ -1,18 +1,18 @@
count = 1 let mut count = 1;
while count <= 15 { while count <= 15 {
divides_by_3 = count % 3 == 0 let divides_by_3 = count % 3 == 0;
divides_by_5 = count % 5 == 0 let divides_by_5 = count % 5 == 0;
output = if divides_by_3 && divides_by_5 { let output = if divides_by_3 && divides_by_5 {
'fizzbuzz' "fizzbuzz"
} else if divides_by_3 { } else if divides_by_3 {
'fizz' "fizz"
} else if divides_by_5 { } else if divides_by_5 {
'buzz' "buzz"
} else { } else {
count.to_string() count.to_string()
} };
write_line(output) write_line(output)