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
use super::variables::VariableMutability;
use crate::{context::facts::Facts, TypeId, VariableId};

use source_map::Span;

#[derive(Debug)]
pub struct NamePair<'a> {
	pub value: &'a str,
	pub r#as: &'a str,
	pub position: Span,
}

pub enum ImportKind<'a, T: Iterator<Item = NamePair<'a>>> {
	Parts(T),
	All {
		under: &'a str,
		position: Span,
	},
	/// From `export * from ...`
	Everything,
}

pub struct SynthesisedModule<M> {
	pub content: M,
	pub exported: Exported,
	/// TODO ...
	pub facts: Facts,
}

/// TODO tidy
#[derive(Clone, Debug, Default, binary_serialize_derive::BinarySerializable)]
pub struct Exported {
	pub default: Option<TypeId>,
	pub named: Vec<(String, (VariableId, VariableMutability))>,
	pub named_types: Vec<(String, TypeId)>,
}

pub enum TypeOrVariable {
	ExportedVariable((VariableId, VariableMutability)),
	Type(TypeId),
}

impl Exported {
	pub(crate) fn get_export(&self, want: &str) -> Option<TypeOrVariable> {
		self.named
			.iter()
			.find_map(|(export, value)| {
				(export == want).then_some(TypeOrVariable::ExportedVariable((value.0, value.1)))
			})
			.or_else(|| {
				self.named_types.iter().find_map(|(export, value)| {
					(export == want).then_some(TypeOrVariable::Type(*value))
				})
			})
	}
}

/// After a syntax error
pub struct InvalidModule;

pub type FinalModule<M> = Result<SynthesisedModule<M>, InvalidModule>;