rudus/src/value.rs

48 lines
1.0 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,
Boolean(bool),
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-15 22:54:40 +00:00
List(Box<Vector<Value>>),
Dict(Box<HashMap<&'static str, Value>>),
Box(Rc<LBox>),
Fn(Rc<RefCell<LFn>>),
}
2024-12-15 22:54:40 +00:00
impl Value {
fn show(&self, ctx: &Chunk) -> String {
use Value::*;
match &self {
Nil => format!("nil"),
2024-12-05 00:07:03 +00:00
}
}
}