added command functions to GlobalNamespace

This commit is contained in:
Schrottkatze 2023-11-18 18:11:59 +01:00
parent 414b6c927d
commit d49e0d38e6
2 changed files with 33 additions and 13 deletions

View file

@ -123,6 +123,17 @@ impl GlobalNamespace {
}
}
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 {
@ -144,6 +155,17 @@ impl GlobalNamespace {
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)]
@ -201,18 +223,16 @@ pub struct Command<'a> {
impl<'a> Command<'a> {
pub fn get_input_types(&self) -> Option<TypeDef<'a>> {
self.namespace
.commands
.borrow()
.get(self.id)
.unwrap()
self.namespace.commands.borrow()[self.id]
.input
.map(|def| TypeDef::from_internal(self.namespace, &def))
.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
.map(|def| TypeDef::from_internal(self.namespace, &def))
.as_ref()
.map(|def| TypeDef::from_internal(self.namespace, def))
}
}
@ -236,7 +256,7 @@ pub enum TypeDef<'a> {
}
impl<'a> TypeDef<'a> {
fn from_internal(ns: &'a GlobalNamespace, def: &'a InternalTypeDef) -> 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

View file

@ -19,11 +19,6 @@ 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) {
@ -60,6 +55,11 @@ fn main() {
out_errs.append(&mut errs)
}
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 valid_add_input_pipe = "1 | add 2";
let valid_add_input_pipe_id = files.add("valid_add_input_pipe", valid_add_input_pipe);
let syntax = dbg!(check(