Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
refactor: remove some constructs
Browse files Browse the repository at this point in the history
  • Loading branch information
aripiprazole committed Nov 23, 2023
1 parent 1f5d544 commit 0c6718d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 115 deletions.
2 changes: 1 addition & 1 deletion build.soft → soft/build.lsp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(require '[stdlib.soft])
(require 'stdlib)

(println (-> stdlib/args
(map (fun [x] (str "Hello " x)))))
25 changes: 25 additions & 0 deletions soft/stdlib.lsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(declare defmacro* fun*)

(defmacro* map* (fun* [f coll]
(if (cons? coll)
(cons (f (car coll)) (map* f (cdr coll)))
coll)))

(defmacro* quasi-quote (fun* [form]
(if (cons? form)
(if (= 'unquote (car form))
(cdr form)
(cons 'list (map* quasi-quote form)))
(list 'quote form))))

(defmacro* defmacro (fun* [name args & body]
(let [body `(begin ~body)]
`(defmacro* ~name (fun* ~name ~args ~body)))))

(defmacro fun [args & body]
(let [body `(begin ~body)]
`(fun* local ~args ~body)))

(defmacro defun [name args & body]
(let [body `(begin ~body)]
`(def* ~name (fun* ~name ~args ~body))))
36 changes: 0 additions & 36 deletions src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,10 @@ pub enum Value {
name: Keyword,
value: Box<Value>,
},
Set {
target: Box<Value>,
value: Box<Value>,
},
Deref {
value: Box<Value>,
},
Recur {
arguments: Vec<Value>,
},
Quote(Expr),
Atomic(Arc<RwLock<Value>>),
Ptr(*mut ()),
Nil,
}
Expand Down Expand Up @@ -247,18 +239,6 @@ impl Expr {
.map(|expr| expr.expand(environment))
.collect::<Result<Vec<_>, _>>()?,
}),
Expr::Deref(deref) => Ok(Value::Deref {
value: deref.value()?.expand(environment)?.into(),
}),
Expr::Atomic(atomic) => {
let value = atomic.value()?.expand(environment)?;
let atomic = Arc::new(RwLock::new(value));
Ok(Value::Atomic(atomic))
}
Expr::Set(set) => Ok(Value::Set {
target: set.target()?.expand(environment)?.into(),
value: set.value()?.expand(environment)?.into(),
}),
Expr::DefMacro(def_macro) => Ok(Value::DefMacro {
name: def_macro.name()?.expand(environment)?.try_into()?,
value: def_macro.value()?.expand(environment)?.into(),
Expand Down Expand Up @@ -316,22 +296,6 @@ impl Value {
/// Evaluate the expression into a value.
pub fn eval(self, environment: &Environment) -> Trampoline<Value> {
match self {
Value::Deref { box value, .. } => {
let Value::Atomic(atomic) = value else {
bail!(EvalError::ExpectedAtomic)
};
let guard = atomic.read().expect("poisoned atomic");

Done(guard.clone())
}
Value::Set { box target, value } => {
let Value::Atomic(atomic) = target else {
bail!(EvalError::ExpectedAtomic)
};
let mut guard = atomic.write().expect("poisoned atomic");
*guard = value.eval(environment)?.clone();
Done(guard.clone())
}
Value::Keyword(keyword) if !keyword.is_atom => {
match environment.find_definition(keyword.clone()) {
Some(Definition { value, .. }) => Done(value),
Expand Down
65 changes: 2 additions & 63 deletions src/semantic.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
use crate::Term;

define_ast!(Expr, {
Fun, // (fun* (a b) (+ a b))
Fun, // (fun* [a b] (+ a b))
List, // [a b c] or (list a b c)
Apply, // (a b c) or (apply a b c)
Def, // (def a 123)
Def, // (def* a 123)
Recur, // (recur a)
Deref, // (deref* 123)
Atomic, // (atomic* 123)
Set, // (set* a 123)
DefMacro, // (defmacro* a (fun (a b) (+ a b))
Quote, // '(fun* (a b) (+ a b))
Literal // 123 | "bla" | :bla | bla
Expand All @@ -17,9 +14,6 @@ define_ast!(Expr, {
define_builtin!(DefMacro, "defmacro*", 2);
define_builtin!(Def, "def*", 2);
define_builtin!(Recur, "recur");
define_builtin!(Atomic, "atomic*", 1);
define_builtin!(Deref, "deref*", 1);
define_builtin!(Set, "set*", 2);
define_builtin!(Fun, "fun*", 2);
define_builtin!(Quote, "'", 2);
define_builtin!(Apply, "apply");
Expand Down Expand Up @@ -123,58 +117,6 @@ pub mod recur {
}
}

/// Set expression construct, it's a definition of a value.
pub mod set {
pub use super::*;

impl Set {
/// Returns the name of the definition.
pub fn target(&self) -> Result<Expr> {
self.0
.at(1)
.ok_or(SemanticError::InvalidExpression)?
.try_into()
}

/// Returns the value of the definition.
pub fn value(&self) -> Result<Expr> {
self.0
.at(2)
.ok_or(SemanticError::InvalidExpression)?
.try_into()
}
}
}

/// Deref expression construct, it's a mutable value.
pub mod deref {
pub use super::*;

impl Deref {
/// Returns the value of the definition.
pub fn value(&self) -> Result<Expr> {
self.0
.at(1)
.ok_or(SemanticError::InvalidExpression)?
.try_into()
}
}
}

/// Atomic expression construct, it's a mutable value.
pub mod atomic {
pub use super::*;

impl Atomic {
/// Returns the value of the definition.
pub fn value(&self) -> Result<Expr> {
self.0
.at(1)
.ok_or(SemanticError::InvalidExpression)?
.try_into()
}
}
}

/// Define expression construct, it's a definition of a value.
pub mod def {
Expand Down Expand Up @@ -343,11 +285,8 @@ impl TryFrom<Term> for Expr {

fn try_from(value: Term) -> Result<Self, Self::Error> {
DefMacro::try_new(value.clone())
.or_else(|_| Atomic::try_new(value.clone()))
.or_else(|_| Deref::try_new(value.clone()))
.or_else(|_| Recur::try_new(value.clone()))
.or_else(|_| Def::try_new(value.clone()))
.or_else(|_| Set::try_new(value.clone()))
.or_else(|_| Fun::try_new(value.clone()))
.or_else(|_| Quote::try_new(value.clone()))
.or_else(|_| Apply::try_new(value.clone()))
Expand Down
15 changes: 0 additions & 15 deletions stdlib.soft

This file was deleted.

0 comments on commit 0c6718d

Please sign in to comment.