cl-interpret: Builtins! builtins(), chars(str), and panic(msg)
This commit is contained in:
parent
08b5937fc2
commit
124bb2f680
@ -5,7 +5,7 @@ use crate::{
|
|||||||
env::Environment,
|
env::Environment,
|
||||||
error::{Error, IResult},
|
error::{Error, IResult},
|
||||||
};
|
};
|
||||||
use std::io::{stdout, Write};
|
use std::io::{Write, stdout};
|
||||||
|
|
||||||
/// A function built into the interpreter.
|
/// A function built into the interpreter.
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
@ -98,10 +98,10 @@ pub macro builtins($(
|
|||||||
[$(builtin!($(#[$($meta)*])* fn $name ($($args)*) $(@$env)? $body)),*]
|
[$(builtin!($(#[$($meta)*])* fn $name ($($args)*) $(@$env)? $body)),*]
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates an [Error::BuiltinDebug] using interpolation of runtime expressions.
|
/// Creates an [Error::BuiltinError] using interpolation of runtime expressions.
|
||||||
/// See [std::format].
|
/// See [std::format].
|
||||||
pub macro error_format ($($t:tt)*) {
|
pub macro error_format ($($t:tt)*) {
|
||||||
$crate::error::Error::BuiltinDebug(format!($($t)*))
|
$crate::error::Error::BuiltinError(format!($($t)*))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const Builtins: &[Builtin] = &builtins![
|
pub const Builtins: &[Builtin] = &builtins![
|
||||||
@ -143,18 +143,23 @@ pub const Builtins: &[Builtin] = &builtins![
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn panic(message) {
|
||||||
|
Err(error_format!("Panic: {message}"))?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
/// Dumps the environment
|
/// Dumps the environment
|
||||||
fn dump() @env {
|
fn dump() @env {
|
||||||
println!("{env}");
|
println!("{env}");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
// fn builtins() @env {
|
fn builtins() @env {
|
||||||
// for builtin in env.builtins().values().flatten() {
|
for builtin in env.globals().values().flatten().filter(|v| matches!(v, ConValue::Builtin(_))) {
|
||||||
// println!("{builtin}");
|
println!("{builtin}")
|
||||||
// }
|
}
|
||||||
// Ok(())
|
Ok(())
|
||||||
// }
|
}
|
||||||
|
|
||||||
/// Returns the length of the input list as a [ConValue::Int]
|
/// Returns the length of the input list as a [ConValue::Int]
|
||||||
fn len(list) @env {
|
fn len(list) @env {
|
||||||
@ -170,6 +175,16 @@ pub const Builtins: &[Builtin] = &builtins![
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn chars(string) @env {
|
||||||
|
Ok(match string {
|
||||||
|
ConValue::String(s) => ConValue::Array(s.chars().map(Into::into).collect()),
|
||||||
|
ConValue::Ref(r) => {
|
||||||
|
return chars(env, &[env.get_id(*r).ok_or(Error::StackOverflow(*r))?.clone()])
|
||||||
|
}
|
||||||
|
_ => Err(Error::TypeError())?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
fn dump_symbols() {
|
fn dump_symbols() {
|
||||||
println!("{}", cl_structures::intern::string_interner::StringInterner::global());
|
println!("{}", cl_structures::intern::string_interner::StringInterner::global());
|
||||||
Ok(ConValue::Empty)
|
Ok(ConValue::Empty)
|
||||||
|
@ -99,8 +99,8 @@ impl Error {
|
|||||||
Self { kind: ErrorKind::MatchNonexhaustive, span: None }
|
Self { kind: ErrorKind::MatchNonexhaustive, span: None }
|
||||||
}
|
}
|
||||||
/// Error produced by a Builtin
|
/// Error produced by a Builtin
|
||||||
pub fn BuiltinDebug(msg: String) -> Self {
|
pub fn BuiltinError(msg: String) -> Self {
|
||||||
Self { kind: ErrorKind::BuiltinDebug(msg), span: None }
|
Self { kind: ErrorKind::BuiltinError(msg), span: None }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -156,7 +156,7 @@ pub enum ErrorKind {
|
|||||||
/// Fell through a non-exhaustive match
|
/// Fell through a non-exhaustive match
|
||||||
MatchNonexhaustive,
|
MatchNonexhaustive,
|
||||||
/// Error produced by a Builtin
|
/// Error produced by a Builtin
|
||||||
BuiltinDebug(String),
|
BuiltinError(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::error::Error for ErrorKind {}
|
impl std::error::Error for ErrorKind {}
|
||||||
@ -205,7 +205,7 @@ impl std::fmt::Display for ErrorKind {
|
|||||||
ErrorKind::MatchNonexhaustive => {
|
ErrorKind::MatchNonexhaustive => {
|
||||||
write!(f, "Fell through a non-exhaustive match expression!")
|
write!(f, "Fell through a non-exhaustive match expression!")
|
||||||
}
|
}
|
||||||
ErrorKind::BuiltinDebug(s) => write!(f, "DEBUG: {s}"),
|
ErrorKind::BuiltinError(s) => write!(f, "{s}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user