Start adding eval and machine implementations, remove Inst for now

We're not compiling yet and mostly want to get the name stuff figured
out. Also may switch to using anyhow error handling.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2022-01-08 10:13:27 -08:00
parent 9e20dcf59c
commit cbe22bb89e
6 changed files with 15 additions and 37 deletions

View File

@@ -7,6 +7,7 @@ use std::io::Read;
use std::path::PathBuf;
use structopt::StructOpt;
use syn::parser::Parser;
use vm::eval::Eval;
use vm::machine::MachineBuilder;
#[derive(Debug, StructOpt)]
@@ -40,10 +41,12 @@ fn main() -> Result {
exprs.extend(parser.next_expr_list()?);
}
let machine = MachineBuilder::default()
let mut machine = MachineBuilder::default()
.max_stack_size(opt.max_stack_size)
.max_arena_objects(opt.max_arena_objects)
.finish();
let mut eval = Eval::new(&mut machine);
eval.eval_expr_list(&exprs)?;
Ok(())
}

View File

@@ -1,4 +1,4 @@
use crate::object::{Float, Int, Str, Value};
use crate::object::{Float, Int, Str};
use crate::syn::span::*;
#[derive(Debug, Clone, PartialEq)]

View File

@@ -4,12 +4,8 @@ use thiserror::Error;
pub enum RuntimeError {
#[error("stack overflow")]
StackOverflow,
#[error("stack underflow")]
StackUnderflow,
//#[error("unexpected {0}")]
//Unexpected(String),
//#[error("expected {expected}, but got {got}")]
//ExpectedGot { expected: String, got: String },
//#[error("stack underflow")]
//StackUnderflow,
}
pub type Result<T, E = RuntimeError> = std::result::Result<T, E>;

View File

@@ -1,6 +1,6 @@
use crate::object::*;
use crate::syn::ast::*;
use crate::vm::{error::*, inst::Inst, machine::Machine};
use crate::vm::{error::*, machine::Machine};
/// An evaluation context for the VM.
pub struct Eval<'m> {
@@ -12,6 +12,13 @@ impl<'m> Eval<'m> {
Self { machine }
}
pub fn eval_expr_list(&mut self, exprs: &Vec<SpExpr>) -> Result<()> {
for expr in exprs {
self.eval_expr(expr)?;
}
Ok(())
}
pub fn eval_expr(&mut self, expr: &SpExpr) -> Result<()> {
match expr.inner() {
Expr::Atom(atom) => match atom.inner() {
@@ -26,22 +33,6 @@ impl<'m> Eval<'m> {
}
Ok(())
}
pub fn eval_inst(&mut self, inst: Inst) -> Result<()> {
match inst {
Inst::Push(v) => self.machine.stack_push(v)?,
Inst::Pop => {
self.machine
.stack_pop()
.ok_or(RuntimeError::StackUnderflow)?;
}
Inst::When => todo!(),
Inst::If => todo!(),
Inst::Eval => todo!(),
Inst::Print => todo!(),
}
Ok(())
}
}
// IDEA: Eval chain

View File

@@ -1,11 +0,0 @@
use crate::object::Value;
#[derive(Debug, Clone)]
pub enum Inst {
Push(Value),
Pop,
When,
If,
Eval,
Print,
}

View File

@@ -1,4 +1,3 @@
pub mod error;
pub mod eval;
pub mod inst;
pub mod machine;