From d49e0d38e6dd7d6e074bddc84bb5e9dfb5cfa026 Mon Sep 17 00:00:00 2001 From: Schrottkatze Date: Sat, 18 Nov 2023 18:11:59 +0100 Subject: [PATCH] added command functions to GlobalNamespace --- src/builtins/namespace.rs | 36 ++++++++++++++++++++++++++++-------- src/main.rs | 10 +++++----- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/builtins/namespace.rs b/src/builtins/namespace.rs index 26a6af3..36ff066 100644 --- a/src/builtins/namespace.rs +++ b/src/builtins/namespace.rs @@ -123,6 +123,17 @@ impl GlobalNamespace { } } + pub fn get_command(&self, id: usize) -> Option { + if self.commands.borrow().len() > id { + Some(Command { + id, + namespace: self, + }) + } else { + None + } + } + pub fn get_type_by_name(&self, name: &str) -> Option { 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 { + 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> { - 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 { 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 diff --git a/src/main.rs b/src/main.rs index 467130e..c019514 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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(