rudus/src/value.rs

85 lines
2.3 KiB
Rust
Raw Normal View History

2024-12-15 22:54:40 +00:00
use crate::compiler::Chunk;
use crate::parser::Ast;
use crate::spans::Spanned;
use imbl::{HashMap, Vector};
use std::cell::RefCell;
use std::rc::Rc;
2024-10-31 20:59:26 +00:00
2024-12-15 22:54:40 +00:00
#[derive(Clone, Debug, PartialEq)]
pub struct LBox {
pub name: usize,
pub cell: RefCell<Value>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct LFn {
pub name: &'static str,
pub body: Vec<Spanned<Ast>>,
pub doc: Option<&'static str>,
pub enclosing: Vec<(usize, Value)>,
2024-12-13 00:01:51 +00:00
pub has_run: bool,
pub input: &'static str,
pub src: &'static str,
2024-10-31 20:59:26 +00:00
}
2024-12-15 22:54:40 +00:00
#[derive(Clone, Debug, PartialEq)]
pub enum Value {
2024-10-31 20:59:26 +00:00
Nil,
2024-12-16 04:28:57 +00:00
True,
False,
2024-12-15 22:54:40 +00:00
Keyword(usize), // use an idx, rather than a raw index
Interned(usize),
FnDecl(usize),
String(Rc<String>),
2024-10-31 20:59:26 +00:00
Number(f64),
2024-12-16 04:28:57 +00:00
Tuple(Rc<Vec<Value>>),
TupleStart { len: u8, size: u16 },
TupleEnd { len: u8, size: u16 },
2024-12-15 22:54:40 +00:00
List(Box<Vector<Value>>),
2024-12-16 04:28:57 +00:00
Dict(Box<HashMap<usize, Value>>),
2024-12-15 22:54:40 +00:00
Box(Rc<LBox>),
Fn(Rc<RefCell<LFn>>),
}
2024-12-15 22:54:40 +00:00
impl Value {
2024-12-16 04:28:57 +00:00
pub fn show(&self, ctx: &Chunk) -> String {
2024-12-15 22:54:40 +00:00
use Value::*;
match &self {
2024-12-16 04:28:57 +00:00
Nil => "nil".to_string(),
True => "true".to_string(),
False => "false".to_string(),
Number(n) => format!("{n}"),
Interned(i) => {
let str_str = ctx.strings[*i];
format!("\"{str_str}\"")
}
Keyword(i) => {
let kw_str = ctx.keywords[*i];
format!(":{kw_str}")
}
Tuple(t) => {
let members = t.iter().map(|e| e.show(ctx)).collect::<Vec<_>>().join(", ");
format!("({members})")
}
List(l) => {
let members = l.iter().map(|e| e.show(ctx)).collect::<Vec<_>>().join(", ");
format!("[{members}]")
}
Dict(d) => {
let members = d
.iter()
.map(|(k, v)| {
let key_show = Value::Keyword(*k).show(ctx);
let value_show = v.show(ctx);
format!("{key_show} {value_show}")
})
.collect::<Vec<_>>()
.join(", ");
format!("#{{{members}}}")
}
String(s) => s.as_ref().clone(),
_ => todo!(),
2024-12-05 00:07:03 +00:00
}
}
}