new Runtime object
This commit is contained in:
@@ -1,7 +1,9 @@
|
|||||||
use std::io::{self, BufReader};
|
use std::io::{self, BufReader};
|
||||||
|
|
||||||
fn main() {
|
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 {
|
match value {
|
||||||
Ok(v) => println!("{v}"),
|
Ok(v) => println!("{v}"),
|
||||||
Err(e) => eprintln!("{e}"),
|
Err(e) => eprintln!("{e}"),
|
||||||
|
|||||||
@@ -326,7 +326,7 @@ where
|
|||||||
ParseTree::StringCast(x) => Ok(Value::String(format!("{}", self.exec(x, locals)?))),
|
ParseTree::StringCast(x) => Ok(Value::String(format!("{}", self.exec(x, locals)?))),
|
||||||
ParseTree::Print(x) => match self.exec(x, locals)? {
|
ParseTree::Print(x) => match self.exec(x, locals)? {
|
||||||
x => {
|
x => {
|
||||||
writeln!(self.stdout, "{x}").map_err(|e| RuntimeError::IO(e));
|
writeln!(self.stdout, "{x}").map_err(|e| RuntimeError::IO(e))?;
|
||||||
Ok(Value::Nil)
|
Ok(Value::Nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
34
src/lib.rs
34
src/lib.rs
@@ -3,8 +3,12 @@ mod tokenizer;
|
|||||||
mod parser;
|
mod parser;
|
||||||
mod executor;
|
mod executor;
|
||||||
|
|
||||||
|
use executor::{Executor, RuntimeError};
|
||||||
|
use parser::Parser;
|
||||||
|
use tokenizer::Tokenizer;
|
||||||
|
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
use std::io::BufRead;
|
use std::io::{Write, Read, BufRead};
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub enum Type {
|
pub enum Type {
|
||||||
@@ -72,6 +76,30 @@ pub(crate) struct FunctionDeclaration {
|
|||||||
args: Vec<(String, Type)>,
|
args: Vec<(String, Type)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn evaluate<R: BufRead>(r: R) -> impl Iterator<Item = Result<Value, executor::RuntimeError>> {
|
pub struct Runtime<'a, R: BufRead> {
|
||||||
executor::Executor::new(parser::Parser::new(tokenizer::Tokenizer::new(r)))
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user