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:
2022-01-12 18:00:11 -08:00
parent 1c669decc4
commit 907c8ce1af
8 changed files with 25 additions and 11 deletions

View File

@@ -1,5 +1,6 @@
use crate::object::{QuoteTable, Value}; use crate::object::{QuoteTable, Value};
use crate::syn::{ast::*, words::*}; use crate::scope::*;
use crate::syn::ast::*;
use crate::vm::inst::*; use crate::vm::inst::*;
pub struct Compile<'s> { pub struct Compile<'s> {

View File

@@ -1,5 +1,6 @@
mod compile; mod compile;
mod object; mod object;
mod scope;
mod syn; mod syn;
mod vm; mod vm;

View File

@@ -4,13 +4,11 @@
#![allow(dead_code)] #![allow(dead_code)]
use crate::syn::ast::SpExpr; use crate::syn::ast::SpExpr;
use crate::{ use crate::vm::machine::Machine;
syn::{span::Span, words::Scope}, use crate::{scope::Scope, syn::span::Span, vm::inst::Inst};
vm::inst::Inst,
};
use std::cell::RefCell; use std::cell::RefCell;
use std::collections::{BTreeMap, HashMap}; use std::collections::{BTreeMap, HashMap};
use std::fmt::{self, Display}; use std::fmt::{self, Debug, Display};
use std::rc::{Rc, Weak}; use std::rc::{Rc, Weak};
pub type Str = String; pub type Str = String;
@@ -72,6 +70,7 @@ pub enum Value {
Str(Str), Str(Str),
Quote(Quote), Quote(Quote),
ObjPtr(ObjPtr), ObjPtr(ObjPtr),
BuiltinFn(BuiltinFn),
} }
impl Value { impl Value {
@@ -84,6 +83,7 @@ impl Value {
Str(_) => "str", Str(_) => "str",
Quote(_) => "quote", Quote(_) => "quote",
ObjPtr(_) => "object", ObjPtr(_) => "object",
BuiltinFn(_) => "builtin function",
} }
} }
} }
@@ -98,10 +98,24 @@ impl Display for Value {
Str(s) => write!(fmt, "{}", s), Str(s) => write!(fmt, "{}", s),
Quote(q) => write!(fmt, "[quoted value #{}]", q.index()), Quote(q) => write!(fmt, "[quoted value #{}]", q.index()),
ObjPtr(o) => write!(fmt, "[object #{}]", o.slot()), 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 // Obj
// ///////////////////////////////////////////////////////////////////////////// // /////////////////////////////////////////////////////////////////////////////

View File

@@ -4,4 +4,3 @@ pub mod lexer;
pub mod parser; pub mod parser;
pub mod span; pub mod span;
pub mod token; pub mod token;
pub mod words;

View File

@@ -13,8 +13,6 @@ pub enum RuntimeError {
#[error("cannot call non-quote value '{0}'")] #[error("cannot call non-quote value '{0}'")]
CannotCall(String), CannotCall(String),
//#[error("stack underflow")]
//StackUnderflow,
} }
pub type Result<T, E = RuntimeError> = std::result::Result<T, E>; pub type Result<T, E = RuntimeError> = std::result::Result<T, E>;

View File

@@ -1,5 +1,5 @@
use crate::object::Value; use crate::object::Value;
use crate::syn::words::Word; use crate::scope::Word;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum Inst { pub enum Inst {

View File

@@ -1,6 +1,7 @@
use crate::compile::Compile; use crate::compile::Compile;
use crate::object::*; use crate::object::*;
use crate::syn::{ast::SpExpr, words::*}; use crate::scope::*;
use crate::syn::ast::SpExpr;
use crate::vm::{error::*, inst::*}; use crate::vm::{error::*, inst::*};
use std::cell::RefCell; use std::cell::RefCell;
use std::collections::BTreeMap; use std::collections::BTreeMap;