Finally have things printing to the screen

Nothing fancy yet. This commit adds a bunch of stuff (oops):

* compiling code
* VM with instructions
* remove eval.rs and just eval in the Machine struct itself
* squash most warnings now that we're using stuff here

And probably more. But that's all for now folks

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2022-01-12 17:35:48 -08:00
parent a190157eeb
commit 1c669decc4
14 changed files with 561 additions and 93 deletions

View File

@@ -4,8 +4,13 @@
#![allow(dead_code)]
use crate::syn::ast::SpExpr;
use crate::{
syn::{span::Span, words::Scope},
vm::inst::Inst,
};
use std::cell::RefCell;
use std::collections::{BTreeMap, HashMap};
use std::fmt::{self, Display};
use std::rc::{Rc, Weak};
pub type Str = String;
@@ -13,22 +18,106 @@ pub type Int = i64;
pub type Float = f64;
pub type VTable = HashMap<String, Value>;
// /////////////////////////////////////////////////////////////////////////////
// Quote
// /////////////////////////////////////////////////////////////////////////////
/// A handle to a quote pointing to an element in a `QuoteTable`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Quote(usize);
impl Quote {
pub fn index(&self) -> usize {
self.0
}
}
/// A table of compiled quotes, their expression trees, and their spans.
#[derive(Debug, Clone, Default)]
pub struct QuoteTable {
table: Vec<(Span, Scope, Vec<SpExpr>, Vec<Inst>)>,
}
impl QuoteTable {
pub fn new() -> Self {
Default::default()
}
pub fn insert(
&mut self,
span: Span,
scope: Scope,
quote: Vec<SpExpr>,
compiled: Vec<Inst>,
) -> Quote {
let next = Quote(self.table.len());
self.table.push((span, scope, quote, compiled));
next
}
pub fn get(&self, quote: Quote) -> &(Span, Scope, Vec<SpExpr>, Vec<Inst>) {
&self.table[quote.0]
}
}
// /////////////////////////////////////////////////////////////////////////////
// Value
// /////////////////////////////////////////////////////////////////////////////
#[derive(Debug, Clone)]
pub enum Value {
Array(Vec<Value>),
Float(Float),
Int(Int),
Str(Str),
Quote(Vec<SpExpr>),
Quote(Quote),
ObjPtr(ObjPtr),
}
impl Value {
pub fn name(&self) -> &str {
use Value::*;
match self {
Array(_) => "array",
Float(_) => "float",
Int(_) => "int",
Str(_) => "str",
Quote(_) => "quote",
ObjPtr(_) => "object",
}
}
}
impl Display for Value {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
use Value::*;
match self {
Array(_) => write!(fmt, "[array]"),
Float(f) => write!(fmt, "{}", f),
Int(i) => write!(fmt, "{}", i),
Str(s) => write!(fmt, "{}", s),
Quote(q) => write!(fmt, "[quoted value #{}]", q.index()),
ObjPtr(o) => write!(fmt, "[object #{}]", o.slot()),
}
}
}
// /////////////////////////////////////////////////////////////////////////////
// Obj
// /////////////////////////////////////////////////////////////////////////////
#[derive(Debug, Clone)]
pub struct ObjPtr {
arena: Weak<RefCell<Arena>>,
slot: usize,
}
impl ObjPtr {
pub fn slot(&self) -> usize {
self.slot
}
}
#[derive(Debug, Default)]
pub struct Obj {
vtable: VTable,
@@ -48,6 +137,10 @@ impl Obj {
}
}
// /////////////////////////////////////////////////////////////////////////////
// Arena
// /////////////////////////////////////////////////////////////////////////////
#[derive(Debug)]
pub struct Arena {
slots: Vec<SlotRange>,
@@ -170,6 +263,10 @@ impl Arena {
}
}
// /////////////////////////////////////////////////////////////////////////////
// Slots
// /////////////////////////////////////////////////////////////////////////////
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SlotRange {
/// A list of slots that are in a range, exclusive.