new Runtime object

This commit is contained in:
2024-10-15 00:47:20 -04:00
parent e0e33c868b
commit f9f5cb40e9
3 changed files with 35 additions and 5 deletions

View File

@@ -1,7 +1,9 @@
use std::io::{self, BufReader};
fn main() {
for value in lamm::evaluate(BufReader::new(io::stdin())) {
let runtime = lamm::Runtime::new(BufReader::new(io::stdin()));
for value in runtime.values() {
match value {
Ok(v) => println!("{v}"),
Err(e) => eprintln!("{e}"),

View File

@@ -326,7 +326,7 @@ where
ParseTree::StringCast(x) => Ok(Value::String(format!("{}", self.exec(x, locals)?))),
ParseTree::Print(x) => match self.exec(x, locals)? {
x => {
writeln!(self.stdout, "{x}").map_err(|e| RuntimeError::IO(e));
writeln!(self.stdout, "{x}").map_err(|e| RuntimeError::IO(e))?;
Ok(Value::Nil)
}
}

View File

@@ -3,8 +3,12 @@ mod tokenizer;
mod parser;
mod executor;
use executor::{Executor, RuntimeError};
use parser::Parser;
use tokenizer::Tokenizer;
use std::fmt::Display;
use std::io::BufRead;
use std::io::{Write, Read, BufRead};
#[derive(Clone, Debug)]
pub enum Type {
@@ -72,6 +76,30 @@ pub(crate) struct FunctionDeclaration {
args: Vec<(String, Type)>,
}
pub fn evaluate<R: BufRead>(r: R) -> impl Iterator<Item = Result<Value, executor::RuntimeError>> {
executor::Executor::new(parser::Parser::new(tokenizer::Tokenizer::new(r)))
pub struct Runtime<'a, R: BufRead> {
inner: executor::Executor<'a, parser::Parser<tokenizer::Tokenizer<R>>>
}
impl<'a, R: BufRead> Runtime<'a, R> {
pub fn new(reader: R) -> Self {
Self {
inner: Executor::new(Parser::new(Tokenizer::new(reader)))
}
}
pub fn stdout(self, stdout: impl Write + 'a) -> Self {
Self {
inner: self.inner.stdout(stdout)
}
}
pub fn stdin(self, stdin: impl Read + 'a) -> Self {
Self {
inner: self.inner.stdin(stdin)
}
}
pub fn values(self) -> impl Iterator<Item = Result<Value, RuntimeError>> + use<'a, R> {
self.inner
}
}