From a6334070aee8a896e767de6999d11f39bac6ff21 Mon Sep 17 00:00:00 2001 From: Jeff Date: Tue, 20 Aug 2024 15:16:06 -0400 Subject: [PATCH] Get fizzbuzz working again --- dust-lang/src/ast/expression.rs | 1 - dust-lang/src/built_in_function.rs | 5 ++++- dust-lang/src/core_library.rs | 9 +++++++++ dust-lang/src/value.rs | 18 ++++++++++++++---- dust-lang/src/vm.rs | 20 +++++++++----------- examples/fizzbuzz.ds | 16 ++++++++-------- 6 files changed, 44 insertions(+), 25 deletions(-) diff --git a/dust-lang/src/ast/expression.rs b/dust-lang/src/ast/expression.rs index 60c65d4..1d5a62c 100644 --- a/dust-lang/src/ast/expression.rs +++ b/dust-lang/src/ast/expression.rs @@ -2,7 +2,6 @@ use std::{ cmp::Ordering, collections::HashMap, fmt::{self, Display, Formatter}, - rc::Weak, }; use serde::{Deserialize, Serialize}; diff --git a/dust-lang/src/built_in_function.rs b/dust-lang/src/built_in_function.rs index 22f418f..d025fc9 100644 --- a/dust-lang/src/built_in_function.rs +++ b/dust-lang/src/built_in_function.rs @@ -128,7 +128,10 @@ impl BuiltInFunction { } BuiltInFunction::WriteLine => { 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) } else { diff --git a/dust-lang/src/core_library.rs b/dust-lang/src/core_library.rs index 8b687db..90dc28e 100644 --- a/dust-lang/src/core_library.rs +++ b/dust-lang/src/core_library.rs @@ -43,6 +43,15 @@ pub fn core_library<'a>() -> &'a Context { (0, 0), ), ), + ( + Identifier::new("write_line"), + ( + ContextData::VariableValue(Value::Function(Function::BuiltIn( + BuiltInFunction::WriteLine, + ))), + (0, 0), + ), + ), ])) }) } diff --git a/dust-lang/src/value.rs b/dust-lang/src/value.rs index 6818f82..2cf7626 100644 --- a/dust-lang/src/value.rs +++ b/dust-lang/src/value.rs @@ -792,15 +792,25 @@ impl Value { } pub fn and(&self, other: &Value) -> Result { - match (self.as_boolean(), other.as_boolean()) { - (Some(left), Some(right)) => Ok(Value::Boolean(left && right)), + match (self, other) { + (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())), } } pub fn or(&self, other: &Value) -> Result { - match (self.as_boolean(), other.as_boolean()) { - (Some(left), Some(right)) => Ok(Value::Boolean(left || right)), + match (self, other) { + (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())), } } diff --git a/dust-lang/src/vm.rs b/dust-lang/src/vm.rs index 3492d59..f4dfee9 100644 --- a/dust-lang/src/vm.rs +++ b/dust-lang/src/vm.rs @@ -965,19 +965,17 @@ impl Vm { .ok_or(RuntimeError::ExpectedBoolean { position })?; if boolean { - let evaluation = self.run_block(if_block.inner, collect_garbage)?; - - if let Evaluation::Break(_) = evaluation { - return Ok(evaluation); + self.run_block(if_block.inner, collect_garbage) + } else { + match r#else { + ElseExpression::If(if_expression) => { + self.run_if(*if_expression.inner, collect_garbage) + } + ElseExpression::Block(block) => { + self.run_block(block.inner, collect_garbage) + } } } - - match r#else { - ElseExpression::If(if_expression) => { - self.run_if(*if_expression.inner, collect_garbage) - } - ElseExpression::Block(block) => self.run_block(block.inner, collect_garbage), - } } } } diff --git a/examples/fizzbuzz.ds b/examples/fizzbuzz.ds index 3eda4a7..eb9bd42 100644 --- a/examples/fizzbuzz.ds +++ b/examples/fizzbuzz.ds @@ -1,18 +1,18 @@ -count = 1 +let mut count = 1; while count <= 15 { - divides_by_3 = count % 3 == 0 - divides_by_5 = count % 5 == 0 + let divides_by_3 = count % 3 == 0; + let divides_by_5 = count % 5 == 0; - output = if divides_by_3 && divides_by_5 { - 'fizzbuzz' + let output = if divides_by_3 && divides_by_5 { + "fizzbuzz" } else if divides_by_3 { - 'fizz' + "fizz" } else if divides_by_5 { - 'buzz' + "buzz" } else { count.to_string() - } + }; write_line(output)