implement basic namespacing for types, traits and commands

This commit is contained in:
Schrottkatze 2023-11-16 19:22:26 +01:00
parent 3cca2bc2cc
commit b6b8c5085a
5 changed files with 287 additions and 6 deletions

14
src/builtins/mod.rs Normal file
View file

@ -0,0 +1,14 @@
use self::namespace::{GlobalNamespace, TypeDef};
pub mod namespace;
pub fn initialise_globals() -> GlobalNamespace {
let mut ns = GlobalNamespace::init();
let numeric = ns.register_trait("Numeric").unwrap();
ns.register_type("integer").unwrap().add_trait(&numeric);
ns.register_type("float").unwrap().add_trait(&numeric);
ns
}

224
src/builtins/namespace.rs Normal file
View file

@ -0,0 +1,224 @@
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_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
}
}
}
#[derive(Debug)]
pub enum TypeRegistrationError {
NameAlreadyExists,
}
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,
}
pub struct Trait<'a> {
id: usize,
namespace: &'a GlobalNamespace,
}
struct InternalTrait {
// make resolution easier
types: RefCell<HashSet<usize>>,
name: String,
}
struct Command<'a> {
id: usize,
namespace: &'a GlobalNamespace,
}
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>)>),
}
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(),
),
}
}
}
// what is a type apart from the obvious?
// - it has Traits (integer might have the trait numeric for example)-> also need trait system, should share a namespace tho?
// spec for add
//
// inputs: [ numeric numeric ]
// outputs: numeric
//

View file

@ -1,3 +1,4 @@
use builtins::initialise_globals;
use codespan_reporting::files::SimpleFiles;
use codespan_reporting::term;
use codespan_reporting::term::termcolor::ColorChoice;
@ -6,6 +7,8 @@ use syntax::parse_syntax;
use crate::syntax::check::check;
mod builtins;
mod hir;
mod lexer;
mod syntax;
@ -13,6 +16,11 @@ fn main() {
let mut files = SimpleFiles::new();
let mut out_errs = Vec::new();
let global_ns = initialise_globals();
let int = global_ns.get_type_by_name("integer").unwrap();
let numeric = global_ns.get_trait_by_name("Numeric").unwrap();
assert!(int.has_trait(&numeric));
let invalid_toks = "meow | gay $ error!\\";
let invalid_toks_id = files.add("invalid_toks", invalid_toks);
if let Err(err) = parse_syntax(invalid_toks, invalid_toks_id) {
@ -39,6 +47,16 @@ fn main() {
out_errs.append(&mut errs)
}
let invalid_no_sink = "meow | invert | ";
let invalid_no_sink_id = files.add("invalid_no_sink", invalid_no_sink);
if let Err(mut errs) = check(
parse_syntax(invalid_no_sink, invalid_no_sink_id).unwrap(),
invalid_no_sink,
invalid_no_sink_id,
) {
out_errs.append(&mut errs)
}
// invalid
let writer = StandardStream::stderr(ColorChoice::Always);
let config = term::Config::default();

View file

@ -20,6 +20,10 @@ pub fn check(
))
}
if let Err(e_span) = check_missing_sink(&syntax) {
errs.push(SyntaxError::MissingSink(vec![(file_id, e_span)]));
}
if errs.is_empty() {
Ok(syntax)
} else {
@ -44,11 +48,11 @@ fn check_missing_filters(syntax: &Vec<PipelineElement>) -> Result<(), Vec<logos:
for i in 0..syntax.len() {
if let (
Some(PipelineElement {
Some(&PipelineElement {
kind: PipelineElementKind::Pipe,
ref span,
}),
Some(PipelineElement {
Some(&PipelineElement {
kind: PipelineElementKind::Pipe,
span: ref span1,
}),
@ -64,3 +68,15 @@ fn check_missing_filters(syntax: &Vec<PipelineElement>) -> Result<(), Vec<logos:
Err(missing_filter_locs)
}
}
fn check_missing_sink(syntax: &Vec<PipelineElement>) -> Result<(), logos::Span> {
if let Some(&PipelineElement {
kind: PipelineElementKind::Pipe,
ref span,
}) = syntax.last()
{
Err(span.clone())
} else {
Ok(())
}
}

View file

@ -13,11 +13,11 @@ pub enum SyntaxError {
MissingSink(Vec<(FileId, logos::Span)>),
/// This indicates a missing filter somewhere in the pipeline, meaning that there's 2 pipes after one another
MissingFilter(Vec<(FileId, logos::Span)>),
/// A literal cannot be a sink
/// A literal cannot be a sink, TODO
LiteralAsSink,
/// A literal can't be a filter either
/// A literal can't be a filter either, TODO
LiteralAsFilter,
/// A literal acting as streamer cannot take arguments
/// A literal acting as streamer cannot take arguments, TODO
LiteralWithArgs,
}
@ -51,7 +51,16 @@ impl SyntaxError {
})
.collect(),
),
_ => todo!(),
Self::MissingSink(locs) => Diagnostic::error()
.with_message("pipelines cannot end on a pipe")
.with_labels(
locs.into_iter()
.map(|(file_id, span)| {
Label::primary(*file_id, span.clone()).with_message("no sink")
})
.collect(),
),
_ => unimplemented!(),
}
}
}