moved namespaces to their own module

This commit is contained in:
Schrottkatze 2023-11-18 19:10:28 +01:00
parent 91adcd40f5
commit 03412ce8cd
9 changed files with 326 additions and 304 deletions

View file

@ -1,9 +1,7 @@
use self::namespace::{GlobalNamespace, TypeDef}; use crate::namespace::{typedef::TypeDef, GlobalNamespace};
pub mod namespace;
pub fn initialise_globals() -> GlobalNamespace { pub fn initialise_globals() -> GlobalNamespace {
let mut ns = GlobalNamespace::init(); let ns = GlobalNamespace::init();
let numeric = ns.register_trait("Numeric").unwrap(); let numeric = ns.register_trait("Numeric").unwrap();

View file

@ -1,299 +1 @@
use std::{
cell::RefCell,
collections::{HashMap, HashSet},
rc::Rc,
};
pub struct GlobalNamespace {
// types
// constants
// groups (maybe global data namespace that has groups as subset?)
types: RefCell<Vec<InternalType>>,
traits: RefCell<Vec<InternalTrait>>,
type_namespace: RefCell<HashMap<String, TypeNamespaceId>>,
commands: RefCell<Vec<InternalCommand>>,
data_namespace: RefCell<HashMap<String, DataNamespaceId>>,
}
enum TypeNamespaceId {
Types(usize),
Traits(usize),
}
enum DataNamespaceId {
Commands(usize),
Globals(usize),
}
impl GlobalNamespace {
pub fn init() -> Self {
Self {
types: RefCell::new(Vec::new()),
traits: RefCell::new(Vec::new()),
type_namespace: RefCell::new(HashMap::new()),
commands: RefCell::new(Vec::new()),
data_namespace: RefCell::new(HashMap::new()),
}
}
pub fn register_type(&self, name: &str) -> Result<Type, TypeRegistrationError> {
if self.type_namespace.borrow().contains_key(name) {
Err(TypeRegistrationError::NameAlreadyExists)
} else {
self.types.borrow_mut().push(InternalType {
traits: RefCell::new(HashSet::new()),
name: name.to_owned(),
});
let id = self.types.borrow().len() - 1;
let _ = self
.type_namespace
.borrow_mut()
.insert(name.to_owned(), TypeNamespaceId::Types(id));
Ok(Type {
id,
namespace: self,
})
}
}
pub fn register_trait(&self, name: &str) -> Result<Trait, TypeRegistrationError> {
if self.type_namespace.borrow().contains_key(name) {
Err(TypeRegistrationError::NameAlreadyExists)
} else {
self.traits.borrow_mut().push(InternalTrait {
types: RefCell::new(HashSet::new()),
name: name.to_owned(),
});
let id = self.traits.borrow().len() - 1;
let _ = self
.type_namespace
.borrow_mut()
.insert(name.to_owned(), TypeNamespaceId::Traits(id));
Ok(Trait {
id,
namespace: &*self,
})
}
}
pub fn register_command(
&self,
name: &str,
input: Option<TypeDef>,
output: Option<TypeDef>,
) -> Result<Command, TypeRegistrationError> {
if self.data_namespace.borrow().contains_key(name) {
Err(TypeRegistrationError::NameAlreadyExists)
} else {
self.commands.borrow_mut().push(InternalCommand {
name: name.to_owned(),
input: input.map(|def| def.into()),
output: output.map(|def| def.into()),
});
let id = self.traits.borrow().len() - 1;
let _ = self
.data_namespace
.borrow_mut()
.insert(name.to_owned(), DataNamespaceId::Commands(id));
Ok(Command {
id,
namespace: self,
})
}
}
pub fn get_type(&self, id: usize) -> Option<Type> {
if self.types.borrow().len() > id {
Some(Type {
id,
namespace: self,
})
} else {
None
}
}
pub fn get_trait(&self, id: usize) -> Option<Trait> {
if self.traits.borrow().len() > id {
Some(Trait {
id,
namespace: self,
})
} else {
None
}
}
pub fn get_command(&self, id: usize) -> Option<Command> {
if self.commands.borrow().len() > id {
Some(Command {
id,
namespace: self,
})
} else {
None
}
}
pub fn get_type_by_name(&self, name: &str) -> Option<Type> {
if let Some(TypeNamespaceId::Types(id)) = self.type_namespace.borrow().get(name) {
Some(Type {
id: *id,
namespace: self,
})
} else {
None
}
}
pub fn get_trait_by_name(&self, name: &str) -> Option<Trait> {
if let Some(TypeNamespaceId::Traits(id)) = self.type_namespace.borrow().get(name) {
Some(Trait {
id: *id,
namespace: self,
})
} else {
None
}
}
pub fn get_command_by_name(&self, name: &str) -> Option<Command> {
if let Some(DataNamespaceId::Commands(id)) = self.data_namespace.borrow().get(name) {
Some(Command {
id: *id,
namespace: self,
})
} else {
None
}
}
}
#[derive(Debug)]
pub enum TypeRegistrationError {
NameAlreadyExists,
}
#[derive(Clone, Copy)]
pub struct Type<'a> {
id: usize,
namespace: &'a GlobalNamespace,
}
impl<'a> Type<'a> {
pub fn add_trait(&self, to_add: &'a Trait) {
let internal_self = &self.namespace.types.borrow()[self.id];
internal_self.traits.borrow_mut().insert(to_add.id);
self.namespace.traits.borrow_mut()[to_add.id]
.types
.borrow_mut()
.insert(self.id);
}
pub fn has_trait(&self, to_check: &'a Trait) -> bool {
self.namespace.types.borrow()[self.id]
.traits
.borrow()
.contains(&to_check.id)
}
}
struct InternalType {
traits: RefCell<HashSet<usize>>,
name: String,
}
#[derive(Clone, Copy)]
pub struct Trait<'a> {
id: usize,
namespace: &'a GlobalNamespace,
}
struct InternalTrait {
// make resolution easier
types: RefCell<HashSet<usize>>,
name: String,
}
#[derive(Clone, Copy)]
pub struct Command<'a> {
id: usize,
namespace: &'a GlobalNamespace,
}
impl<'a> Command<'a> {
pub fn get_input_types(&self) -> Option<TypeDef<'a>> {
self.namespace.commands.borrow()[self.id]
.input
.as_ref()
.map(|def| TypeDef::from_internal(self.namespace, def))
}
pub fn get_output_types(&self) -> Option<TypeDef> {
self.namespace.commands.borrow()[self.id]
.output
.as_ref()
.map(|def| TypeDef::from_internal(self.namespace, def))
}
}
struct InternalCommand {
name: String,
// gosh this is hacky
input: Option<InternalTypeDef>,
output: Option<InternalTypeDef>,
}
/// the thing used for type annotations etc
/// stores type ids
pub enum TypeDef<'a> {
/// T
Type(Type<'a>),
Trait(Trait<'a>),
/// [ T ]
List(Vec<TypeDef<'a>>),
/// { name: T } or in group declarations [ name: T ]
Record(Vec<(String, TypeDef<'a>)>),
}
impl<'a> TypeDef<'a> {
fn from_internal(ns: &'a GlobalNamespace, def: &InternalTypeDef) -> TypeDef<'a> {
match def {
InternalTypeDef::Single(id) => match id {
// safe to unwrap because this is only used with internal representations
TypeNamespaceId::Types(id) => TypeDef::Type(ns.get_type(*id).unwrap()),
TypeNamespaceId::Traits(id) => TypeDef::Trait(ns.get_trait(*id).unwrap()),
},
InternalTypeDef::List(list) => TypeDef::List(
list.into_iter()
.map(|def| Self::from_internal(ns, def))
.collect(),
),
InternalTypeDef::Record(rec) => TypeDef::Record(
rec.into_iter()
.map(|(name, def)| (name.clone(), Self::from_internal(ns, def)))
.collect(),
),
}
}
}
enum InternalTypeDef {
Single(TypeNamespaceId),
List(Vec<InternalTypeDef>),
Record(Vec<(String, InternalTypeDef)>),
}
impl From<TypeDef<'_>> for InternalTypeDef {
fn from(value: TypeDef) -> Self {
match value {
TypeDef::Type(val) => Self::Single(TypeNamespaceId::Types(val.id)),
TypeDef::Trait(val) => Self::Single(TypeNamespaceId::Traits(val.id)),
TypeDef::List(list) => Self::List(list.into_iter().map(|def| def.into()).collect()),
TypeDef::Record(rec) => Self::Record(
rec.into_iter()
.map(|(name, typ)| (name, typ.into()))
.collect(),
),
}
}
}

View file

@ -1,5 +1,6 @@
mod builtins; mod builtins;
mod lexer; mod lexer;
mod namespace;
mod syntax; mod syntax;
fn main() {} fn main() {}

31
src/namespace/command.rs Normal file
View file

@ -0,0 +1,31 @@
use super::typedef::{InternalTypeDef, TypeDef};
use super::GlobalNamespace;
#[derive(Clone, Copy)]
pub struct Command<'a> {
pub(super) id: usize,
pub(super) namespace: &'a GlobalNamespace,
}
impl<'a> Command<'a> {
pub fn get_input_types(&self) -> Option<TypeDef<'a>> {
self.namespace.commands.borrow()[self.id]
.input
.as_ref()
.map(|def| TypeDef::from_internal(self.namespace, def))
}
pub fn get_output_types(&self) -> Option<TypeDef> {
self.namespace.commands.borrow()[self.id]
.output
.as_ref()
.map(|def| TypeDef::from_internal(self.namespace, def))
}
}
pub(super) struct InternalCommand {
pub(super) name: String,
// gosh this is hacky
pub(super) input: Option<InternalTypeDef>,
pub(super) output: Option<InternalTypeDef>,
}

182
src/namespace/mod.rs Normal file
View file

@ -0,0 +1,182 @@
use std::{
cell::RefCell,
collections::{HashMap, HashSet},
};
use self::{
command::{Command, InternalCommand},
r#trait::{InternalTrait, Trait},
r#type::InternalType,
typedef::TypeDef,
};
mod command;
mod r#trait;
mod r#type;
pub mod typedef;
pub struct GlobalNamespace {
types: RefCell<Vec<InternalType>>,
traits: RefCell<Vec<InternalTrait>>,
type_namespace: RefCell<HashMap<String, TypeNamespaceId>>,
commands: RefCell<Vec<InternalCommand>>,
data_namespace: RefCell<HashMap<String, DataNamespaceId>>,
}
enum TypeNamespaceId {
Types(usize),
Traits(usize),
}
enum DataNamespaceId {
Commands(usize),
Globals(usize),
}
impl GlobalNamespace {
pub fn init() -> Self {
Self {
types: RefCell::new(Vec::new()),
traits: RefCell::new(Vec::new()),
type_namespace: RefCell::new(HashMap::new()),
commands: RefCell::new(Vec::new()),
data_namespace: RefCell::new(HashMap::new()),
}
}
pub fn register_type(&self, name: &str) -> Result<r#type::Type, NsRegistrationError> {
if self.type_namespace.borrow().contains_key(name) {
Err(NsRegistrationError::NameAlreadyExists)
} else {
self.types.borrow_mut().push(InternalType {
traits: RefCell::new(HashSet::new()),
name: name.to_owned(),
});
let id = self.types.borrow().len() - 1;
let _ = self
.type_namespace
.borrow_mut()
.insert(name.to_owned(), TypeNamespaceId::Types(id));
Ok(r#type::Type {
id,
namespace: self,
})
}
}
pub fn register_trait(&self, name: &str) -> Result<Trait, NsRegistrationError> {
if self.type_namespace.borrow().contains_key(name) {
Err(NsRegistrationError::NameAlreadyExists)
} else {
self.traits.borrow_mut().push(InternalTrait {
types: RefCell::new(HashSet::new()),
name: name.to_owned(),
});
let id = self.traits.borrow().len() - 1;
let _ = self
.type_namespace
.borrow_mut()
.insert(name.to_owned(), TypeNamespaceId::Traits(id));
Ok(Trait {
id,
namespace: self,
})
}
}
pub fn register_command(
&self,
name: &str,
input: Option<TypeDef>,
output: Option<TypeDef>,
) -> Result<Command, NsRegistrationError> {
if self.data_namespace.borrow().contains_key(name) {
Err(NsRegistrationError::NameAlreadyExists)
} else {
self.commands.borrow_mut().push(InternalCommand {
name: name.to_owned(),
input: input.map(|def| def.into()),
output: output.map(|def| def.into()),
});
let id = self.traits.borrow().len() - 1;
let _ = self
.data_namespace
.borrow_mut()
.insert(name.to_owned(), DataNamespaceId::Commands(id));
Ok(Command {
id,
namespace: self,
})
}
}
pub fn get_type(&self, id: usize) -> Option<r#type::Type> {
if self.types.borrow().len() > id {
Some(r#type::Type {
id,
namespace: self,
})
} else {
None
}
}
pub fn get_trait(&self, id: usize) -> Option<Trait> {
if self.traits.borrow().len() > id {
Some(Trait {
id,
namespace: self,
})
} else {
None
}
}
pub fn get_command(&self, id: usize) -> Option<Command> {
if self.commands.borrow().len() > id {
Some(Command {
id,
namespace: self,
})
} else {
None
}
}
pub fn get_type_by_name(&self, name: &str) -> Option<r#type::Type> {
if let Some(TypeNamespaceId::Types(id)) = self.type_namespace.borrow().get(name) {
Some(r#type::Type {
id: *id,
namespace: self,
})
} else {
None
}
}
pub fn get_trait_by_name(&self, name: &str) -> Option<Trait> {
if let Some(TypeNamespaceId::Traits(id)) = self.type_namespace.borrow().get(name) {
Some(Trait {
id: *id,
namespace: self,
})
} else {
None
}
}
pub fn get_command_by_name(&self, name: &str) -> Option<Command> {
if let Some(DataNamespaceId::Commands(id)) = self.data_namespace.borrow().get(name) {
Some(Command {
id: *id,
namespace: self,
})
} else {
None
}
}
}
#[derive(Debug)]
pub enum NsRegistrationError {
NameAlreadyExists,
}

17
src/namespace/trait.rs Normal file
View file

@ -0,0 +1,17 @@
use std::collections::HashSet;
use std::cell::RefCell;
use super::GlobalNamespace;
#[derive(Clone, Copy)]
pub struct Trait<'a> {
pub(super) id: usize,
pub(super) namespace: &'a GlobalNamespace,
}
pub(super) struct InternalTrait {
// make resolution easier
pub(super) types: RefCell<HashSet<usize>>,
pub(super) name: String,
}

37
src/namespace/type.rs Normal file
View file

@ -0,0 +1,37 @@
use std::collections::HashSet;
use std::cell::RefCell;
use super::Trait;
use super::GlobalNamespace;
#[derive(Clone, Copy)]
pub struct Type<'a> {
pub(super) id: usize,
pub(super) namespace: &'a GlobalNamespace,
}
impl<'a> Type<'a> {
pub fn add_trait(&self, to_add: &'a Trait) {
let internal_self = &self.namespace.types.borrow()[self.id];
internal_self.traits.borrow_mut().insert(to_add.id);
self.namespace.traits.borrow_mut()[to_add.id]
.types
.borrow_mut()
.insert(self.id);
}
pub fn has_trait(&self, to_check: &'a Trait) -> bool {
self.namespace.types.borrow()[self.id]
.traits
.borrow()
.contains(&to_check.id)
}
}
pub(super) struct InternalType {
pub(super) traits: RefCell<HashSet<usize>>,
pub(super) name: String,
}

56
src/namespace/typedef.rs Normal file
View file

@ -0,0 +1,56 @@
use super::TypeNamespaceId;
use super::GlobalNamespace;
use super::r#trait::Trait;
use super::r#type::Type;
pub enum TypeDef<'a> {
Type(Type<'a>),
Trait(Trait<'a>),
List(Vec<TypeDef<'a>>),
Record(Vec<(String, TypeDef<'a>)>),
}
impl<'a> TypeDef<'a> {
pub(super) fn from_internal(ns: &'a GlobalNamespace, def: &InternalTypeDef) -> TypeDef<'a> {
match def {
InternalTypeDef::Single(id) => match id {
// safe to unwrap because this is only used with internal representations
TypeNamespaceId::Types(id) => TypeDef::Type(ns.get_type(*id).unwrap()),
TypeNamespaceId::Traits(id) => TypeDef::Trait(ns.get_trait(*id).unwrap()),
},
InternalTypeDef::List(list) => TypeDef::List(
list.into_iter()
.map(|def| Self::from_internal(ns, def))
.collect(),
),
InternalTypeDef::Record(rec) => TypeDef::Record(
rec.into_iter()
.map(|(name, def)| (name.clone(), Self::from_internal(ns, def)))
.collect(),
),
}
}
}
pub(super) enum InternalTypeDef {
Single(TypeNamespaceId),
List(Vec<InternalTypeDef>),
Record(Vec<(String, InternalTypeDef)>),
}
impl From<TypeDef<'_>> for InternalTypeDef {
fn from(value: TypeDef) -> Self {
match value {
TypeDef::Type(val) => Self::Single(TypeNamespaceId::Types(val.id)),
TypeDef::Trait(val) => Self::Single(TypeNamespaceId::Traits(val.id)),
TypeDef::List(list) => Self::List(list.into_iter().map(|def| def.into()).collect()),
TypeDef::Record(rec) => Self::Record(
rec.into_iter()
.map(|(name, typ)| (name, typ.into()))
.collect(),
),
}
}
}

View file

@ -5,8 +5,6 @@ use logos::Span;
use crate::lexer::Token; use crate::lexer::Token;
use self::error::SyntaxError;
pub mod check; pub mod check;
mod error; mod error;