From 1acb5ef3cfb60129c8691d2e9a7947f4bfc241a0 Mon Sep 17 00:00:00 2001 From: Schrottkatze Date: Fri, 24 Nov 2023 09:39:32 +0100 Subject: [PATCH] refactor to common id types --- src/evaluator.rs | 10 ++++++---- src/namespace/command.rs | 4 +++- src/namespace/mod.rs | 18 ++++++++++-------- src/namespace/trait.rs | 6 ++++-- src/namespace/type.rs | 6 ++++-- src/typed.rs | 10 ++-------- 6 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/evaluator.rs b/src/evaluator.rs index ba0e002..9b11f3e 100644 --- a/src/evaluator.rs +++ b/src/evaluator.rs @@ -30,12 +30,14 @@ impl EvalConfig { } } +pub type FileId = usize; + // this is also bad // need a better architecture for this pub struct Evaluator<'a> { curr_phase: EvalPhase, files: SimpleFiles<&'a str, String>, - errors: HashMap>, + errors: HashMap>, cfg: EvalConfig, } @@ -75,7 +77,7 @@ impl<'a> Evaluator<'a> { pub fn next(mut self) { match self.curr_phase { EvalPhase::Lex => { - todo!() + unreachable!() } EvalPhase::Check(file_id, syntax) => { let r = check(&syntax); @@ -129,7 +131,7 @@ impl<'a> Evaluator<'a> { enum EvalPhase { Lex, - Check(usize, Vec), - BareTyped(usize, Vec), + Check(FileId, Vec), + BareTyped(FileId, Vec), Failed, } diff --git a/src/namespace/command.rs b/src/namespace/command.rs index 3402dc3..0b6fac0 100644 --- a/src/namespace/command.rs +++ b/src/namespace/command.rs @@ -2,12 +2,13 @@ use std::fmt::Debug; use std::fmt::Display; use super::typedef::{InternalTypeDef, TypeDef}; +use super::CommandId; use super::GlobalNamespace; #[derive(Clone, Copy)] pub struct Command<'a> { - pub(super) id: usize, + pub(super) id: CommandId, pub(super) namespace: &'a GlobalNamespace, } @@ -67,4 +68,5 @@ pub(super) struct InternalCommand { // gosh this is hacky pub(super) input: Option, pub(super) output: Option, + // generic_ns: HashMap>, } diff --git a/src/namespace/mod.rs b/src/namespace/mod.rs index c8017e7..bc553ce 100644 --- a/src/namespace/mod.rs +++ b/src/namespace/mod.rs @@ -23,15 +23,17 @@ pub struct GlobalNamespace { data_namespace: RefCell>, } +pub type TypeId = usize; +pub type TraitId = usize; +pub type CommandId = usize; + enum TypeNamespaceId { - Types(usize), - Traits(usize), + Types(TypeId), + Traits(TraitId), } enum DataNamespaceId { - Commands(usize), - #[allow(dead_code, reason = "will be used later")] - Globals(usize), + Commands(CommandId), } impl GlobalNamespace { @@ -126,21 +128,21 @@ impl GlobalNamespace { } } - pub fn get_type(&self, id: usize) -> Option { + pub fn get_type(&self, id: TypeId) -> Option { (self.types.borrow().len() > id).then_some(Type { id, namespace: self, }) } - pub fn get_trait(&self, id: usize) -> Option { + pub fn get_trait(&self, id: TraitId) -> Option { (self.traits.borrow().len() > id).then_some(Trait { id, namespace: self, }) } - pub fn get_command(&self, id: usize) -> Option { + pub fn get_command(&self, id: CommandId) -> Option { (self.commands.borrow().len() > id).then_some(Command { id, namespace: self, diff --git a/src/namespace/trait.rs b/src/namespace/trait.rs index fe84732..9cb3f74 100644 --- a/src/namespace/trait.rs +++ b/src/namespace/trait.rs @@ -5,10 +5,12 @@ use std::fmt::Debug; use std::fmt::Display; use super::GlobalNamespace; +use super::TraitId; +use super::TypeId; #[derive(Clone, Copy)] pub struct Trait<'a> { - pub(super) id: usize, + pub(super) id: TraitId, pub(super) namespace: &'a GlobalNamespace, } @@ -28,6 +30,6 @@ impl Debug for Trait<'_> { pub(super) struct InternalTrait { // make resolution easier - pub(super) types: RefCell>, + pub(super) types: RefCell>, pub(super) name: String, } diff --git a/src/namespace/type.rs b/src/namespace/type.rs index 010e6e7..18a1bad 100644 --- a/src/namespace/type.rs +++ b/src/namespace/type.rs @@ -7,10 +7,12 @@ use std::fmt::Display; use super::Trait; use super::GlobalNamespace; +use super::TraitId; +use super::TypeId; #[derive(Clone, Copy)] pub struct Type<'a> { - pub(super) id: usize, + pub(super) id: TypeId, pub(super) namespace: &'a GlobalNamespace, } @@ -48,6 +50,6 @@ impl Debug for Type<'_> { } pub(super) struct InternalType { - pub(super) traits: RefCell>, + pub(super) traits: RefCell>, pub(super) name: String, } diff --git a/src/typed.rs b/src/typed.rs index 6bf77bb..0f07b7e 100644 --- a/src/typed.rs +++ b/src/typed.rs @@ -1,15 +1,9 @@ use core::panic; -use std::process::CommandEnvs; use crate::{ builtins::{TYPE_FLOAT, TYPE_INTEGER, TYPE_STRING}, error::Errors, - namespace::{ - command::{self, Command}, - r#type::Type, - typedef::TypeDef, - GlobalNamespace, - }, + namespace::{command::Command, r#type::Type, typedef::TypeDef, GlobalNamespace}, syntax::{CommandPart, CommandPartKind, PipelineElement, PipelineElementKind}, Span, }; @@ -51,7 +45,7 @@ impl<'a> ExprKind<'a> { } #[derive(Debug)] -struct CommandExpr<'a> { +pub struct CommandExpr<'a> { command: Command<'a>, args: Vec>, }