Break out obj module into individual files by type
object.rs was getting a little big and the implementations are only going to get bigger. Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
84
src/obj/quote.rs
Normal file
84
src/obj/quote.rs
Normal file
@@ -0,0 +1,84 @@
|
||||
use crate::obj::{Obj, VTable};
|
||||
use crate::syn::ast::SpStmt;
|
||||
use crate::vm::{error::*, machine::Machine};
|
||||
use crate::{scope::Scope, syn::span::Span, vm::inst::SpInst};
|
||||
use gc::{unsafe_empty_trace, Finalize, Trace};
|
||||
use std::fmt::Debug;
|
||||
use std::rc::Rc;
|
||||
|
||||
/// A handle to a quote pointing to an element in a `QuoteTable`.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Finalize)]
|
||||
pub struct Quote(usize);
|
||||
|
||||
impl Quote {
|
||||
pub fn index(&self) -> usize {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl Trace for Quote {
|
||||
unsafe_empty_trace!();
|
||||
}
|
||||
|
||||
#[derive(Debug, Trace, Finalize)]
|
||||
pub struct QuoteObj {
|
||||
value: Quote,
|
||||
vtable: VTable,
|
||||
}
|
||||
|
||||
impl QuoteObj {
|
||||
pub fn new(value: Quote) -> Self {
|
||||
QuoteObj {
|
||||
value,
|
||||
vtable: vtable! {},
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn value(&self) -> Quote {
|
||||
self.value
|
||||
}
|
||||
}
|
||||
|
||||
impl Obj for QuoteObj {
|
||||
fn vtable(&self) -> &VTable {
|
||||
&self.vtable
|
||||
}
|
||||
|
||||
fn vtable_mut(&mut self) -> &mut VTable {
|
||||
&mut self.vtable
|
||||
}
|
||||
|
||||
fn call(&self, _call_site: Option<Span>, machine: &mut Machine) -> Result<()> {
|
||||
machine.call_quote(self.value);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn as_any(&self) -> &(dyn std::any::Any + 'static) {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// A table of compiled quotes, their expression trees, and their spans.
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct QuoteTable {
|
||||
table: Vec<(Span, Scope, Vec<SpStmt>, Rc<Vec<SpInst>>)>,
|
||||
}
|
||||
|
||||
impl QuoteTable {
|
||||
pub fn insert(
|
||||
&mut self,
|
||||
span: Span,
|
||||
scope: Scope,
|
||||
quote: Vec<SpStmt>,
|
||||
compiled: Rc<Vec<SpInst>>,
|
||||
) -> Quote {
|
||||
let next = Quote(self.table.len());
|
||||
self.table.push((span, scope, quote, compiled));
|
||||
next
|
||||
}
|
||||
|
||||
pub fn get(&self, quote: Quote) -> &(Span, Scope, Vec<SpStmt>, Rc<Vec<SpInst>>) {
|
||||
&self.table[quote.index()]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user