Move src/syn/word.rs to src/scope.rs; add BuiltinFn
* crate::scope instead of crate::syn::word, which makes more sense because scopes are not really used in syntax at all * Reference-counted BuiltinFn values are available for creation now Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
use crate::object::{QuoteTable, Value};
|
||||
use crate::syn::{ast::*, words::*};
|
||||
use crate::scope::*;
|
||||
use crate::syn::ast::*;
|
||||
use crate::vm::inst::*;
|
||||
|
||||
pub struct Compile<'s> {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
mod compile;
|
||||
mod object;
|
||||
mod scope;
|
||||
mod syn;
|
||||
mod vm;
|
||||
|
||||
|
||||
@@ -4,13 +4,11 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
use crate::syn::ast::SpExpr;
|
||||
use crate::{
|
||||
syn::{span::Span, words::Scope},
|
||||
vm::inst::Inst,
|
||||
};
|
||||
use crate::vm::machine::Machine;
|
||||
use crate::{scope::Scope, syn::span::Span, vm::inst::Inst};
|
||||
use std::cell::RefCell;
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
use std::fmt::{self, Display};
|
||||
use std::fmt::{self, Debug, Display};
|
||||
use std::rc::{Rc, Weak};
|
||||
|
||||
pub type Str = String;
|
||||
@@ -72,6 +70,7 @@ pub enum Value {
|
||||
Str(Str),
|
||||
Quote(Quote),
|
||||
ObjPtr(ObjPtr),
|
||||
BuiltinFn(BuiltinFn),
|
||||
}
|
||||
|
||||
impl Value {
|
||||
@@ -84,6 +83,7 @@ impl Value {
|
||||
Str(_) => "str",
|
||||
Quote(_) => "quote",
|
||||
ObjPtr(_) => "object",
|
||||
BuiltinFn(_) => "builtin function",
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -98,10 +98,24 @@ impl Display for Value {
|
||||
Str(s) => write!(fmt, "{}", s),
|
||||
Quote(q) => write!(fmt, "[quoted value #{}]", q.index()),
|
||||
ObjPtr(o) => write!(fmt, "[object #{}]", o.slot()),
|
||||
BuiltinFn(b) => write!(fmt, "[{:?}]", b),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct BuiltinFn(Rc<dyn FnMut(&mut Machine)>);
|
||||
|
||||
impl Debug for BuiltinFn {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(
|
||||
fmt,
|
||||
"builtin function at {:#x}",
|
||||
(&self.0 as *const _ as usize)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// /////////////////////////////////////////////////////////////////////////////
|
||||
// Obj
|
||||
// /////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@@ -4,4 +4,3 @@ pub mod lexer;
|
||||
pub mod parser;
|
||||
pub mod span;
|
||||
pub mod token;
|
||||
pub mod words;
|
||||
|
||||
@@ -13,8 +13,6 @@ pub enum RuntimeError {
|
||||
|
||||
#[error("cannot call non-quote value '{0}'")]
|
||||
CannotCall(String),
|
||||
//#[error("stack underflow")]
|
||||
//StackUnderflow,
|
||||
}
|
||||
|
||||
pub type Result<T, E = RuntimeError> = std::result::Result<T, E>;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::object::Value;
|
||||
use crate::syn::words::Word;
|
||||
use crate::scope::Word;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Inst {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use crate::compile::Compile;
|
||||
use crate::object::*;
|
||||
use crate::syn::{ast::SpExpr, words::*};
|
||||
use crate::scope::*;
|
||||
use crate::syn::ast::SpExpr;
|
||||
use crate::vm::{error::*, inst::*};
|
||||
use std::cell::RefCell;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
Reference in New Issue
Block a user