compiler and basic analyzing ast

This commit is contained in:
Gregory Bednov 2025-12-26 14:07:16 +03:00
commit 96a783ae5a
4 changed files with 151 additions and 28 deletions

View file

@ -1,5 +1,4 @@
use std::sync::Arc;
use std::thread;
use crate::support::{run_parallel, Arc};
// Обобщённое выражение: I -> Out
pub trait Expr<I> {
@ -7,6 +6,18 @@ pub trait Expr<I> {
fn eval(&self, x: I) -> Self::Out;
}
// Позволяет использовать Arc<T> там, где требуется Expr.
impl<I, T> Expr<I> for Arc<T>
where
T: Expr<I> + ?Sized,
{
type Out = T::Out;
fn eval(&self, x: I) -> Self::Out {
(**self).eval(x)
}
}
// атомарная функция
pub struct Function<I, O> {
pub f: fn(I) -> O,
@ -39,7 +50,7 @@ where
}
// junction — две ветки параллельно
pub struct Junction<F1, F2> {
pub struct Junction<F1: ?Sized, F2: ?Sized> {
pub left: Arc<F1>,
pub right: Arc<F2>,
}
@ -47,8 +58,8 @@ pub struct Junction<F1, F2> {
impl<I, F1, F2> Expr<I> for Junction<F1, F2>
where
I: Copy + Send + 'static,
F1: Expr<I> + Send + Sync + 'static,
F2: Expr<I> + Send + Sync + 'static,
F1: Expr<I> + Send + Sync + 'static + ?Sized,
F2: Expr<I> + Send + Sync + 'static + ?Sized,
F1::Out: Send + 'static,
F2::Out: Send + 'static,
{
@ -61,9 +72,6 @@ where
let x1 = x;
let x2 = x;
let h1 = thread::spawn(move || l.eval(x1));
let h2 = thread::spawn(move || r.eval(x2));
(h1.join().unwrap(), h2.join().unwrap())
run_parallel(move || l.eval(x1), move || r.eval(x2))
}
}