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) => {}
}
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())?;

View File

@ -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"),
(

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 {
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 {

View File

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