updated a bunch of stuff

This commit is contained in:
Moritz Gmeiner 2024-09-01 19:16:30 +02:00
commit 67bb5fe8fd
24 changed files with 683 additions and 702 deletions

View file

@ -1,5 +1,7 @@
use std::cell::RefCell;
use std::fmt::Display;
use std::io::{stdin, stdout, Read, Write};
use std::rc::Rc;
use rustc_hash::FxHashMap;
@ -9,19 +11,24 @@ use super::lox_std::init_std;
use super::Value;
pub struct Runtime {
globals: FxHashMap<String, Value>,
globals: FxHashMap<Box<str>, Value>,
in_stream: Box<dyn std::io::BufRead>,
out_stream: Box<dyn std::io::Write>,
in_stream: Rc<RefCell<dyn std::io::Read>>,
out_stream: Rc<RefCell<dyn std::io::Write>>,
debug: bool,
}
impl Runtime {
pub fn new(in_stream: Box<dyn std::io::Read>, out_stream: Box<dyn std::io::Write>) -> Self {
let in_stream = Box::new(std::io::BufReader::new(in_stream));
pub fn new(
in_stream: Rc<RefCell<dyn std::io::Read>>,
out_stream: Rc<RefCell<dyn std::io::Write>>,
) -> Self {
let mut runtime = Runtime {
globals: FxHashMap::default(),
in_stream,
out_stream,
debug: false,
};
init_std(&mut runtime);
@ -29,7 +36,24 @@ impl Runtime {
runtime
}
pub fn globals(&self) -> &FxHashMap<String, Value> {
pub fn set_debug(&mut self, debug: bool) {
self.debug = debug;
}
pub fn debug(&self) -> bool {
self.debug
}
pub fn in_stream(&mut self) -> std::cell::RefMut<dyn std::io::Read> {
self.in_stream.as_ref().borrow_mut()
}
pub fn out_stream(&mut self) -> std::cell::RefMut<dyn std::io::Write> {
// &mut self.out_stream
self.out_stream.as_ref().borrow_mut()
}
pub fn globals(&self) -> &FxHashMap<Box<str>, Value> {
&self.globals
}
@ -37,10 +61,12 @@ impl Runtime {
self.globals
.get(name)
.cloned()
.ok_or_else(|| RuntimeError::GlobalNotDefined { name: name.to_owned() })
.ok_or_else(|| RuntimeError::GlobalNotDefined {
name: name.to_owned(),
})
}
pub fn define_global(&mut self, name: impl Into<String>, value: Value) {
pub fn define_global(&mut self, name: impl Into<Box<str>>, value: Value) {
let name = name.into();
self.globals.insert(name, value);
}
@ -50,20 +76,30 @@ impl Runtime {
*old_value = value;
Ok(())
} else {
Err(RuntimeError::GlobalNotDefined { name: name.to_owned() })
Err(RuntimeError::GlobalNotDefined {
name: name.to_owned(),
})
}
}
}
impl Default for Runtime {
fn default() -> Self {
Runtime::new(Box::new(std::io::BufReader::new(stdin())), Box::new(stdout()))
// let buf_reader = std::io::BufReader::new(stdin());
// let buf_reader_boxed = Box::new(&mut buf_reader);
let in_stream = Rc::new(RefCell::new(stdin()));
let out_stream = Rc::new(RefCell::new(stdout()));
Runtime::new(in_stream, out_stream)
}
}
impl std::fmt::Debug for Runtime {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Runtime").field("globals", &self.globals).finish()
f.debug_struct("Runtime")
.field("globals", &self.globals)
.finish()
}
}
@ -81,22 +117,22 @@ impl Display for Runtime {
impl Drop for Runtime {
fn drop(&mut self) {
self.out_stream.flush().unwrap();
self.out_stream().flush().unwrap();
}
}
impl Read for Runtime {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
self.in_stream.read(buf)
self.in_stream().read(buf)
}
}
impl Write for Runtime {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.out_stream.write(buf)
self.out_stream().write(buf)
}
fn flush(&mut self) -> std::io::Result<()> {
self.out_stream.flush()
self.out_stream().flush()
}
}