1
0

Rewrite io.write_line built-in; Fix memory bug

This commit is contained in:
Jeff 2024-04-27 05:45:39 -04:00
parent 2b546e7b63
commit 70f55c85f4
3 changed files with 15 additions and 5 deletions

View File

@ -1,4 +1,8 @@
use std::{io::stdin, thread, time::Duration};
use std::{
io::{stdin, stdout, Write},
thread,
time::Duration,
};
use crate::{
abstract_tree::{Action, Type},
@ -47,7 +51,9 @@ impl AbstractNode for BuiltInFunctionCall {
stdin().read_line(&mut buffer)?;
Ok(Action::Return(Value::string(buffer)))
Ok(Action::Return(Value::string(
buffer.strip_suffix('\n').unwrap_or(&buffer),
)))
}
BuiltInFunctionCall::Sleep(expression) => {
let action = expression.clone().run(context, _manage_memory)?;
@ -76,7 +82,11 @@ impl AbstractNode for BuiltInFunctionCall {
};
if let ValueInner::String(output) = value.inner().as_ref() {
println!("{output}");
let mut stdout = stdout();
stdout.write_all(output.as_bytes())?;
stdout.write(b"\n")?;
stdout.flush()?;
}
Ok(Action::None)

View File

@ -28,7 +28,7 @@ impl AbstractNode for MapIndex {
(&self.collection, &self.index)
{
let collection =
if let Some(collection) = context.use_value(&collection_identifier.item)? {
if let Some(collection) = context.get_value(&collection_identifier.item)? {
collection
} else {
return Err(ValidationError::VariableNotFound {

View File

@ -62,7 +62,7 @@ pub fn parser<'src>(
Token::Boolean(boolean) => ValueNode::Boolean(boolean),
Token::Float(float) => ValueNode::Float(float),
Token::Integer(integer) => ValueNode::Integer(integer),
Token::String(string) => ValueNode::String(string.to_string()),
Token::String(text) => ValueNode::String(text.to_string()),
}
.map_with(|value, state| Expression::Value(value.with_position(state.span())));