Various progress, doesnt compile

Added prelude, made lambdas a single-token prefix like NS, made progress on implementations, removed const line type
This commit is contained in:
2025-07-31 00:30:41 +02:00
parent 19f2c6426a
commit 769c6cfc9f
31 changed files with 450 additions and 250 deletions

View File

@@ -1,8 +1,8 @@
use std::fmt::Debug;
use std::fmt::{self, Debug};
use std::rc::Rc;
use futures::FutureExt;
use futures::future::join_all;
use futures::future::{LocalBoxFuture, join_all};
use hashbrown::HashSet;
use itertools::Itertools;
use orchid_base::format::{FmtCtx, FmtUnit, Format, Variants};
@@ -26,6 +26,11 @@ pub struct Item {
pub comments: Vec<Comment>,
pub kind: ItemKind,
}
impl Item {
pub fn new(sr: SrcRange, kind: impl Into<ItemKind>) -> Self {
Self { sr, comments: Vec::new(), kind: kind.into() }
}
}
#[derive(Debug)]
pub enum ItemKind {
@@ -36,6 +41,12 @@ impl ItemKind {
#[must_use]
pub fn at(self, sr: SrcRange) -> Item { Item { comments: vec![], sr, kind: self } }
}
impl From<ParsedMember> for ItemKind {
fn from(value: ParsedMember) -> Self { Self::Member(value) }
}
impl From<Import> for ItemKind {
fn from(value: Import) -> Self { Self::Import(value) }
}
impl Format for Item {
async fn print<'a>(&'a self, c: &'a (impl FmtCtx + ?Sized + 'a)) -> FmtUnit {
@@ -43,9 +54,6 @@ impl Format for Item {
let item_text = match &self.kind {
ItemKind::Import(i) => format!("import {i}").into(),
ItemKind::Member(mem) => match &mem.kind {
ParsedMemberKind::ParsedConst(expr) =>
tl_cache!(Rc<Variants>: Rc::new(Variants::default().bounded("const {0} = {1l}")))
.units([mem.name.rc().into(), expr.print(c).await]),
ParsedMemberKind::DeferredConst(_, sys) =>
tl_cache!(Rc<Variants>: Rc::new(Variants::default().bounded("const {0} via {1}")))
.units([mem.name.rc().into(), sys.print(c).await]),
@@ -67,6 +75,9 @@ pub struct ParsedMember {
impl ParsedMember {
#[must_use]
pub fn name(&self) -> Tok<String> { self.name.clone() }
pub fn new(exported: bool, name: Tok<String>, kind: impl Into<ParsedMemberKind>) -> Self {
Self { exported, name, kind: kind.into() }
}
}
impl Debug for ParsedMember {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@@ -77,36 +88,53 @@ impl Debug for ParsedMember {
}
}
pub(crate) type ParsedExprCallback =
Rc<dyn for<'a> Fn(&'a [Tok<String>]) -> LocalBoxFuture<'a, Expr>>;
pub struct ParsedExpr {
pub(crate) debug: String,
pub(crate) callback: ParsedExprCallback,
}
impl ParsedExpr {
pub async fn run(self, imported_names: &[Tok<String>]) -> Expr {
(self.callback)(imported_names).await
}
}
impl fmt::Debug for ParsedExpr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.debug) }
}
#[derive(Debug)]
pub enum ParsedMemberKind {
DeferredConst(api::ParsedConstId, System),
ParsedConst(Expr),
Mod(ParsedModule),
}
// TODO: cannot determine alias origin at this stage; parsed tree is never
// walkable!
impl From<ParsedModule> for ParsedMemberKind {
fn from(value: ParsedModule) -> Self { Self::Mod(value) }
}
#[derive(Debug, Default)]
pub struct ParsedModule {
pub exports: Vec<Tok<String>>,
pub items: Vec<Item>,
pub use_prelude: bool,
}
impl ParsedModule {
#[must_use]
pub fn new(items: impl IntoIterator<Item = Item>) -> Self {
pub fn new(use_prelude: bool, items: impl IntoIterator<Item = Item>) -> Self {
let items = items.into_iter().collect_vec();
let exports = (items.iter())
.filter_map(|i| if let ItemKind::Member(m) = &i.kind { Some(m) } else { None })
.filter(|m| m.exported)
.map(|m| m.name.clone())
.collect_vec();
Self { exports, items }
Self { exports, items, use_prelude }
}
pub fn merge(&mut self, other: ParsedModule) {
let mut swap = ParsedModule::default();
std::mem::swap(self, &mut swap);
*self = ParsedModule::new(swap.items.into_iter().chain(other.items))
assert_eq!(self.use_prelude, other.use_prelude, "merging modules that disagree on prelude");
*self = ParsedModule::new(self.use_prelude, swap.items.into_iter().chain(other.items))
}
#[must_use]
pub fn get_imports(&self) -> impl IntoIterator<Item = &Import> {
@@ -134,8 +162,7 @@ impl Tree for ParsedModule {
.find(|m| m.name == key)
{
match &member.kind {
ParsedMemberKind::DeferredConst(..) | ParsedMemberKind::ParsedConst(_) =>
return ChildResult::Err(ChildErrorKind::Constant),
ParsedMemberKind::DeferredConst(..) => return ChildResult::Err(ChildErrorKind::Constant),
ParsedMemberKind::Mod(m) => return ChildResult::Ok(m),
}
}