forked from Orchid/orchid
New plans for macros
About to move them completely to stdlib
This commit is contained in:
@@ -1,42 +1,39 @@
|
||||
use orchid_base::error::{mk_err, OrcErr, OrcRes};
|
||||
use orchid_base::intern;
|
||||
use orchid_base::location::Pos;
|
||||
|
||||
use crate::atom::{AtomicFeatures, TypAtom};
|
||||
use crate::error::{ProjectError, ProjectResult};
|
||||
use crate::expr::{atom, bot_obj, ExprHandle, GenExpr, OwnedExpr};
|
||||
use crate::atom::{AtomicFeatures, ToAtom, TypAtom};
|
||||
use crate::expr::{atom, botv, ExprHandle, GenExpr, OwnedExpr};
|
||||
use crate::system::downcast_atom;
|
||||
|
||||
pub trait TryFromExpr: Sized {
|
||||
fn try_from_expr(expr: ExprHandle) -> ProjectResult<Self>;
|
||||
fn try_from_expr(expr: ExprHandle) -> OrcRes<Self>;
|
||||
}
|
||||
|
||||
impl TryFromExpr for OwnedExpr {
|
||||
fn try_from_expr(expr: ExprHandle) -> ProjectResult<Self> { Ok(OwnedExpr::new(expr)) }
|
||||
fn try_from_expr(expr: ExprHandle) -> OrcRes<Self> { Ok(OwnedExpr::new(expr)) }
|
||||
}
|
||||
|
||||
impl<T: TryFromExpr, U: TryFromExpr> TryFromExpr for (T, U) {
|
||||
fn try_from_expr(expr: ExprHandle) -> ProjectResult<Self> {
|
||||
fn try_from_expr(expr: ExprHandle) -> OrcRes<Self> {
|
||||
Ok((T::try_from_expr(expr.clone())?, U::try_from_expr(expr)?))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ErrorNotAtom(Pos);
|
||||
impl ProjectError for ErrorNotAtom {
|
||||
const DESCRIPTION: &'static str = "Expected an atom";
|
||||
fn one_position(&self) -> Pos { self.0.clone() }
|
||||
fn err_not_atom(pos: Pos) -> OrcErr {
|
||||
mk_err(intern!(str: "Expected an atom"), "This expression is not an atom", [pos.into()])
|
||||
}
|
||||
|
||||
pub struct ErrorUnexpectedType(Pos);
|
||||
impl ProjectError for ErrorUnexpectedType {
|
||||
const DESCRIPTION: &'static str = "Type error";
|
||||
fn one_position(&self) -> Pos { self.0.clone() }
|
||||
fn err_type(pos: Pos) -> OrcErr {
|
||||
mk_err(intern!(str: "Type error"), "The atom is a different type than expected", [pos.into()])
|
||||
}
|
||||
|
||||
impl<A: AtomicFeatures> TryFromExpr for TypAtom<A> {
|
||||
fn try_from_expr(expr: ExprHandle) -> ProjectResult<Self> {
|
||||
impl<'a, A: AtomicFeatures> TryFromExpr for TypAtom<'a, A> {
|
||||
fn try_from_expr(expr: ExprHandle) -> OrcRes<Self> {
|
||||
OwnedExpr::new(expr)
|
||||
.foreign_atom()
|
||||
.map_err(|ex| ErrorNotAtom(ex.pos.clone()).pack())
|
||||
.and_then(|f| downcast_atom(f).map_err(|f| ErrorUnexpectedType(f.pos).pack()))
|
||||
.map_err(|ex| vec![err_not_atom(ex.pos.clone())])
|
||||
.and_then(|f| downcast_atom(f).map_err(|f| vec![err_type(f.pos)]))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,15 +41,19 @@ pub trait ToExpr {
|
||||
fn to_expr(self) -> GenExpr;
|
||||
}
|
||||
|
||||
impl<T: ToExpr> ToExpr for ProjectResult<T> {
|
||||
impl ToExpr for GenExpr {
|
||||
fn to_expr(self) -> GenExpr { self }
|
||||
}
|
||||
|
||||
impl<T: ToExpr> ToExpr for OrcRes<T> {
|
||||
fn to_expr(self) -> GenExpr {
|
||||
match self {
|
||||
Err(e) => bot_obj(e),
|
||||
Err(e) => botv(e),
|
||||
Ok(t) => t.to_expr(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: AtomicFeatures> ToExpr for A {
|
||||
impl<A: ToAtom> ToExpr for A {
|
||||
fn to_expr(self) -> GenExpr { atom(self) }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user