1
0

Get hello world working again

This commit is contained in:
Jeff 2024-08-20 15:43:50 -04:00
parent a6334070ae
commit 2eff51815a
4 changed files with 24 additions and 11 deletions

View File

@ -94,26 +94,26 @@ impl BuiltInFunction {
(None, None) => {} (None, None) => {}
} }
let value_arguments = value_arguments.unwrap();
match self { 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 => { 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))) Ok(Some(Value::Boolean(integer % 2 == 0)))
} else { } else {
Err(BuiltInFunctionError::ExpectedInteger) Err(BuiltInFunctionError::ExpectedInteger)
} }
} }
BuiltInFunction::IsOdd => { 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))) Ok(Some(Value::Boolean(integer % 2 != 0)))
} else { } else {
Err(BuiltInFunctionError::ExpectedInteger) Err(BuiltInFunctionError::ExpectedInteger)
} }
} }
BuiltInFunction::Length => { 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))) Ok(Some(Value::Integer(list.len() as i64)))
} else { } else {
Err(BuiltInFunctionError::ExpectedList) Err(BuiltInFunctionError::ExpectedList)
@ -127,7 +127,7 @@ impl BuiltInFunction {
Ok(Some(Value::string(input.trim_end_matches('\n')))) Ok(Some(Value::string(input.trim_end_matches('\n'))))
} }
BuiltInFunction::WriteLine => { BuiltInFunction::WriteLine => {
if let Value::String(string) = &value_arguments[0] { if let Value::String(string) = &value_arguments.unwrap()[0] {
let mut stdout = stdout(); let mut stdout = stdout();
stdout.write_all(string.as_bytes())?; stdout.write_all(string.as_bytes())?;

View File

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

View File

@ -781,7 +781,7 @@ impl Vm {
}); });
}; };
let mut value_arguments = Vec::new(); let mut value_arguments: Option<Vec<Value>> = None;
for argument in arguments { for argument in arguments {
let position = argument.position(); let position = argument.position();
@ -789,13 +789,17 @@ impl Vm {
.run_expression(argument, collect_garbage)? .run_expression(argument, collect_garbage)?
.expect_value(position)?; .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(); let context = Context::new();
function function
.call(None, Some(value_arguments), &context) .call(None, value_arguments, &context)
.map(Evaluation::Return) .map(Evaluation::Return)
} }
_ => Err(RuntimeError::ExpectedValueOrConstructor { _ => Err(RuntimeError::ExpectedValueOrConstructor {

View File

@ -1,6 +1,6 @@
write_line("Hello, world!") write_line("Hello, world!")
write_line("Enter your name...") write_line("Enter your name...")
name = read_line() let name = read_line();
write_line("Hello " + name + "!") write_line("Hello " + name + "!")