temp commit

This commit is contained in:
2025-07-12 00:46:10 +02:00
parent 1868f1a506
commit fe89188c4b
60 changed files with 1536 additions and 709 deletions

View File

@@ -1,67 +1,25 @@
use std::fmt::Debug;
use std::rc::Rc;
use async_once_cell::OnceCell;
use async_std::sync::{Mutex, RwLock};
use async_stream::stream;
use futures::FutureExt;
use futures::future::join_all;
use futures::{FutureExt, StreamExt};
use hashbrown::{HashMap, HashSet};
use hashbrown::HashSet;
use itertools::Itertools;
use orchid_base::error::{OrcRes, mk_errv};
use orchid_base::format::{FmtCtx, FmtUnit, Format, Variants};
use orchid_base::interner::Tok;
use orchid_base::location::{Pos, SrcRange};
use orchid_base::name::{NameLike, Sym};
use orchid_base::location::SrcRange;
use orchid_base::parse::{Comment, Import};
use orchid_base::tl_cache;
use orchid_base::tree::{TokTree, Token, TokenVariant};
use orchid_base::tree::{TokTree, Token};
use crate::api;
use crate::ctx::Ctx;
use crate::dealias::{ChildErrorKind, ChildResult, Tree};
use crate::expr::{Expr, ExprParseCtx, PathSetBuilder};
use crate::expr_store::ExprStore;
use crate::expr::Expr;
use crate::system::System;
pub type ParsTokTree = TokTree<Expr, Expr>;
pub type ParsTok = Token<Expr, Expr>;
impl TokenVariant<api::ExprTicket> for Expr {
type ToApiCtx<'a> = ExprStore;
async fn into_api(self, ctx: &mut Self::ToApiCtx<'_>) -> api::ExprTicket {
ctx.give_expr(self.clone());
self.id()
}
type FromApiCtx<'a> = ExprStore;
async fn from_api(
api: &api::ExprTicket,
ctx: &mut Self::FromApiCtx<'_>,
_: SrcRange,
_: &orchid_base::interner::Interner,
) -> Self {
let expr = ctx.get_expr(*api).expect("Dangling expr");
ctx.take_expr(*api);
expr
}
}
impl TokenVariant<api::Expression> for Expr {
type FromApiCtx<'a> = ExprParseCtx;
async fn from_api(
api: &api::Expression,
ctx: &mut Self::FromApiCtx<'_>,
_: SrcRange,
_: &orchid_base::interner::Interner,
) -> Self {
Expr::from_api(api, PathSetBuilder::new(), ctx).await
}
type ToApiCtx<'a> = ();
async fn into_api(self, (): &mut Self::ToApiCtx<'_>) -> api::Expression {
panic!("Failed to replace NewExpr before returning sublexer value")
}
}
#[derive(Debug)]
pub struct Item {
pub sr: SrcRange,
@@ -72,10 +30,10 @@ pub struct Item {
#[derive(Debug)]
pub enum ItemKind {
Member(ParsedMember),
Export(Tok<String>),
Import(Import),
}
impl ItemKind {
#[must_use]
pub fn at(self, sr: SrcRange) -> Item { Item { comments: vec![], sr, kind: self } }
}
@@ -84,11 +42,13 @@ impl Format for Item {
let comment_text = self.comments.iter().join("\n");
let item_text = match &self.kind {
ItemKind::Import(i) => format!("import {i}").into(),
ItemKind::Export(e) => format!("export {e}").into(),
ItemKind::Member(mem) => match &mem.kind {
ParsedMemberKind::Const =>
tl_cache!(Rc<Variants>: Rc::new(Variants::default().bounded("const {0}")))
.units([mem.name.rc().into()]),
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]),
ParsedMemberKind::Mod(module) =>
tl_cache!(Rc<Variants>: Rc::new(Variants::default().bounded("module {0} {{\n\t{1}\n}}")))
.units([mem.name.rc().into(), module.print(c).boxed_local().await]),
@@ -101,10 +61,11 @@ impl Format for Item {
pub struct ParsedMember {
pub name: Tok<String>,
pub full_name: Sym,
pub exported: bool,
pub kind: ParsedMemberKind,
}
impl ParsedMember {
#[must_use]
pub fn name(&self) -> Tok<String> { self.name.clone() }
}
impl Debug for ParsedMember {
@@ -118,7 +79,8 @@ impl Debug for ParsedMember {
#[derive(Debug)]
pub enum ParsedMemberKind {
Const,
DeferredConst(api::ParsedConstId, System),
ParsedConst(Expr),
Mod(ParsedModule),
}
@@ -131,13 +93,13 @@ pub struct ParsedModule {
pub items: Vec<Item>,
}
impl ParsedModule {
#[must_use]
pub fn new(items: impl IntoIterator<Item = Item>) -> Self {
let items = items.into_iter().collect_vec();
let exports = (items.iter())
.filter_map(|i| match &i.kind {
ItemKind::Export(e) => Some(e.clone()),
_ => None,
})
.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 }
}
@@ -146,32 +108,34 @@ impl ParsedModule {
std::mem::swap(self, &mut swap);
*self = ParsedModule::new(swap.items.into_iter().chain(other.items))
}
#[must_use]
pub fn get_imports(&self) -> impl IntoIterator<Item = &Import> {
(self.items.iter())
.filter_map(|it| if let ItemKind::Import(i) = &it.kind { Some(i) } else { None })
}
}
impl Tree for ParsedModule {
type Ctx = ();
type Ctx<'a> = ();
async fn child(
&self,
key: Tok<String>,
public_only: bool,
ctx: &mut Self::Ctx,
(): &mut Self::Ctx<'_>,
) -> ChildResult<'_, Self> {
let Some(member) = (self.items.iter())
.filter_map(|it| if let ItemKind::Member(m) = &it.kind { Some(m) } else { None })
.find(|m| m.name == key)
else {
return ChildResult::Err(ChildErrorKind::Missing);
};
if public_only && !self.exports.contains(&key) {
return ChildResult::Err(ChildErrorKind::Private);
}
match &member.kind {
ParsedMemberKind::Const => return ChildResult::Err(ChildErrorKind::Constant),
ParsedMemberKind::Mod(m) => return ChildResult::Value(m),
if let Some(member) = (self.items.iter())
.filter_map(|it| if let ItemKind::Member(m) = &it.kind { Some(m) } else { None })
.find(|m| m.name == key)
{
match &member.kind {
ParsedMemberKind::DeferredConst(..) | ParsedMemberKind::ParsedConst(_) =>
return ChildResult::Err(ChildErrorKind::Constant),
ParsedMemberKind::Mod(m) => return ChildResult::Ok(m),
}
}
ChildResult::Err(ChildErrorKind::Missing)
}
fn children(&self, public_only: bool) -> HashSet<Tok<String>> {
let mut public: HashSet<_> = self.exports.iter().cloned().collect();
@@ -214,44 +178,6 @@ pub struct ConstPath {
steps: Tok<Vec<Tok<String>>>,
}
impl ConstPath {
#[must_use]
pub fn to_const(steps: Tok<Vec<Tok<String>>>) -> Self { Self { steps } }
}
#[derive(Clone)]
pub struct Root {
tree: Rc<ParsedModule>,
consts: Rc<RwLock<HashMap<Sym, Expr>>>,
}
impl Root {
pub fn new(module: ParsedModule, consts: HashMap<Sym, Expr>) -> Self {
Self { tree: Rc::new(module), consts: Rc::new(RwLock::new(consts)) }
}
pub async fn get_const_value(&self, name: Sym, pos: Pos, ctx: Ctx) -> OrcRes<Expr> {
if let Some(val) = self.consts.read().await.get(&name) {
return Ok(val.clone());
}
let (cn, mp) = name.split_last();
let module = self.tree.walk(false, mp.iter().cloned(), &mut ()).await.unwrap();
let member = (module.items.iter())
.filter_map(|it| if let ItemKind::Member(m) = &it.kind { Some(m) } else { None })
.find(|m| m.name() == cn);
match member {
None => Err(mk_errv(
ctx.i.i("Constant does not exist").await,
format!("{name} does not refer to a constant"),
[pos.clone().into()],
)),
Some(mem) => match &mem.kind {
ParsedMemberKind::Mod(_) => Err(mk_errv(
ctx.i.i("module used as constant").await,
format!("{name} is a module, not a constant"),
[pos.clone().into()],
)),
ParsedMemberKind::Const => Ok(
(self.consts.read().await.get(&name).cloned())
.expect("Tree says the path is correct but no value was found"),
),
},
}
}
}