From 2eff51815aeaa00c639280705431cdbc2c6a3d2f Mon Sep 17 00:00:00 2001 From: Jeff Date: Tue, 20 Aug 2024 15:43:50 -0400 Subject: [PATCH] Get hello world working again --- dust-lang/src/built_in_function.rs | 14 +++++++------- dust-lang/src/core_library.rs | 9 +++++++++ dust-lang/src/vm.rs | 10 +++++++--- examples/hello_world.ds | 2 +- 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/dust-lang/src/built_in_function.rs b/dust-lang/src/built_in_function.rs index d025fc9..bb839a1 100644 --- a/dust-lang/src/built_in_function.rs +++ b/dust-lang/src/built_in_function.rs @@ -94,26 +94,26 @@ impl BuiltInFunction { (None, None) => {} } - let value_arguments = value_arguments.unwrap(); - match self { - BuiltInFunction::ToString => Ok(Some(Value::String(value_arguments[0].to_string()))), + BuiltInFunction::ToString => { + Ok(Some(Value::String(value_arguments.unwrap()[0].to_string()))) + } BuiltInFunction::IsEven => { - if let Some(integer) = value_arguments[0].as_integer() { + if let Value::Integer(integer) = value_arguments.unwrap()[0] { Ok(Some(Value::Boolean(integer % 2 == 0))) } else { Err(BuiltInFunctionError::ExpectedInteger) } } BuiltInFunction::IsOdd => { - if let Some(integer) = value_arguments[0].as_integer() { + if let Value::Integer(integer) = value_arguments.unwrap()[0] { Ok(Some(Value::Boolean(integer % 2 != 0))) } else { Err(BuiltInFunctionError::ExpectedInteger) } } BuiltInFunction::Length => { - if let Value::List(list) = &value_arguments[0] { + if let Value::List(list) = &value_arguments.unwrap()[0] { Ok(Some(Value::Integer(list.len() as i64))) } else { Err(BuiltInFunctionError::ExpectedList) @@ -127,7 +127,7 @@ impl BuiltInFunction { Ok(Some(Value::string(input.trim_end_matches('\n')))) } BuiltInFunction::WriteLine => { - if let Value::String(string) = &value_arguments[0] { + if let Value::String(string) = &value_arguments.unwrap()[0] { let mut stdout = stdout(); stdout.write_all(string.as_bytes())?; diff --git a/dust-lang/src/core_library.rs b/dust-lang/src/core_library.rs index 90dc28e..8df3180 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("read_line"), + ( + ContextData::VariableValue(Value::Function(Function::BuiltIn( + BuiltInFunction::ReadLine, + ))), + (0, 0), + ), + ), ( Identifier::new("write_line"), ( diff --git a/dust-lang/src/vm.rs b/dust-lang/src/vm.rs index f4dfee9..c4852fa 100644 --- a/dust-lang/src/vm.rs +++ b/dust-lang/src/vm.rs @@ -781,7 +781,7 @@ impl Vm { }); }; - let mut value_arguments = Vec::new(); + let mut value_arguments: Option> = None; for argument in arguments { let position = argument.position(); @@ -789,13 +789,17 @@ impl Vm { .run_expression(argument, collect_garbage)? .expect_value(position)?; - value_arguments.push(value); + if let Some(value_arguments) = &mut value_arguments { + value_arguments.push(value); + } else { + value_arguments = Some(vec![value]); + } } let context = Context::new(); function - .call(None, Some(value_arguments), &context) + .call(None, value_arguments, &context) .map(Evaluation::Return) } _ => Err(RuntimeError::ExpectedValueOrConstructor { diff --git a/examples/hello_world.ds b/examples/hello_world.ds index 85167e5..d6bbd05 100644 --- a/examples/hello_world.ds +++ b/examples/hello_world.ds @@ -1,6 +1,6 @@ write_line("Hello, world!") write_line("Enter your name...") -name = read_line() +let name = read_line(); write_line("Hello " + name + "!")