1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use super::matcher::{FuncMatcher, Match, MatchError};
use super::vm::{Callable, Func, Script};
use alloc::boxed::Box;
use alloc::vec::Vec;
use core::marker::PhantomData;
pub type ModuleList<'a, C> = Box<[FuncMatcher<'a, C>]>;
pub struct Cons<H, T> {
head: PhantomData<H>,
tail: PhantomData<T>,
}
pub struct Nil;
pub trait ModuleType<'a, C> {
type Error;
fn compile_line(ctx: &mut C, string: &'a str) -> Result<Func<'a>, Self::Error>;
fn compile(ctx: &mut C, string: &'a str) -> Result<Script<'a>, (usize, Self::Error)> {
let mut script = Vec::new();
for (line_num, line) in string
.lines()
.enumerate()
.map(|(i, s)| (i, s.trim()))
.filter(|(_, s)| !s.is_empty())
{
let func = Self::compile_line(ctx, line).map_err(|e| (line_num, e))?;
script.push(func);
}
Ok(script.into())
}
}
pub trait Module<'a, C> {
type Error;
fn compile_line(&self, ctx: &mut C, string: &'a str) -> Result<Func<'a>, Self::Error>;
fn compile(&self, ctx: &mut C, string: &'a str) -> Result<Script<'a>, (usize, Self::Error)> {
let mut script = Vec::new();
for (line_num, line) in string
.lines()
.enumerate()
.map(|(i, s)| (i, s.trim()))
.filter(|(_, s)| !s.is_empty())
{
let func = self.compile_line(ctx, line).map_err(|e| (line_num, e))?;
script.push(func);
}
Ok(script.into())
}
}
impl<'a, H, T, C> ModuleType<'a, C> for Cons<H, T>
where
H: 'a + Match<'a, C> + Callable,
T: ModuleType<'a, C, Error = MatchError>,
<T as ModuleType<'a, C>>::Error: Into<MatchError>,
{
type Error = MatchError;
fn compile_line(ctx: &mut C, string: &'a str) -> Result<Box<dyn Callable + 'a>, Self::Error> {
match H::match_str(ctx, string) {
Ok(matched) => Ok(Box::new(matched)),
Err(_) => T::compile_line(ctx, string),
}
}
}
impl<'a, C> ModuleType<'a, C> for Nil {
type Error = MatchError;
fn compile_line(_: &mut C, _: &'a str) -> Result<Box<dyn Callable + 'a>, Self::Error> {
Err(MatchError::UnexpectedEof)
}
}
impl<'a, C, T> Module<'a, C> for T
where
T: AsRef<[FuncMatcher<'a, C>]>,
{
type Error = MatchError;
fn compile_line(&self, ctx: &mut C, string: &'a str) -> Result<Func<'a>, Self::Error> {
for match_str in self.as_ref().into_iter() {
if let Ok(func) = match_str(ctx, string) {
return Ok(func);
}
}
Err(MatchError::UnexpectedEof)
}
}
#[macro_export]
macro_rules! mod_type {
() => {
$crate::module::Nil
};
($head:ty) => {
$crate::module::Cons<$head, $crate::module::Nil>
};
($head:ty, $($tail:ty),*) => {
$crate::module::Cons<$head, $crate::mod_type!($($tail),*)>
}
}
#[cfg(feature = "std")]
#[macro_export]
macro_rules! mod_list {
() => {
::std::boxed::Box::new([])
};
($ctx:ty => $($item:ty),*) => {
::std::boxed::Box::new([$(<$item as $crate::matcher::MatchFunc<$ctx>>::match_func),*])
}
}
#[cfg(not(feature = "std"))]
#[macro_export]
macro_rules! mod_list {
() => {
::alloc::boxed::Box::new([])
};
($ctx:ty => $($item:ty),*) => {
::alloc::boxed::Box::new([$(<$item as $crate::matcher::MatchFunc<$ctx>>::match_func),*])
}
}