refactor to common id types

This commit is contained in:
Schrottkatze 2023-11-24 09:39:32 +01:00
parent c3db966765
commit 1acb5ef3cf
6 changed files with 29 additions and 25 deletions

View file

@ -30,12 +30,14 @@ impl EvalConfig {
} }
} }
pub type FileId = usize;
// this is also bad // this is also bad
// need a better architecture for this // need a better architecture for this
pub struct Evaluator<'a> { pub struct Evaluator<'a> {
curr_phase: EvalPhase, curr_phase: EvalPhase,
files: SimpleFiles<&'a str, String>, files: SimpleFiles<&'a str, String>,
errors: HashMap<usize, Vec<Errors>>, errors: HashMap<FileId, Vec<Errors>>,
cfg: EvalConfig, cfg: EvalConfig,
} }
@ -75,7 +77,7 @@ impl<'a> Evaluator<'a> {
pub fn next(mut self) { pub fn next(mut self) {
match self.curr_phase { match self.curr_phase {
EvalPhase::Lex => { EvalPhase::Lex => {
todo!() unreachable!()
} }
EvalPhase::Check(file_id, syntax) => { EvalPhase::Check(file_id, syntax) => {
let r = check(&syntax); let r = check(&syntax);
@ -129,7 +131,7 @@ impl<'a> Evaluator<'a> {
enum EvalPhase { enum EvalPhase {
Lex, Lex,
Check(usize, Vec<PipelineElement>), Check(FileId, Vec<PipelineElement>),
BareTyped(usize, Vec<PipelineElement>), BareTyped(FileId, Vec<PipelineElement>),
Failed, Failed,
} }

View file

@ -2,12 +2,13 @@ use std::fmt::Debug;
use std::fmt::Display; use std::fmt::Display;
use super::typedef::{InternalTypeDef, TypeDef}; use super::typedef::{InternalTypeDef, TypeDef};
use super::CommandId;
use super::GlobalNamespace; use super::GlobalNamespace;
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub struct Command<'a> { pub struct Command<'a> {
pub(super) id: usize, pub(super) id: CommandId,
pub(super) namespace: &'a GlobalNamespace, pub(super) namespace: &'a GlobalNamespace,
} }
@ -67,4 +68,5 @@ pub(super) struct InternalCommand {
// gosh this is hacky // gosh this is hacky
pub(super) input: Option<InternalTypeDef>, pub(super) input: Option<InternalTypeDef>,
pub(super) output: Option<InternalTypeDef>, pub(super) output: Option<InternalTypeDef>,
// generic_ns: HashMap<String, Vec<usize>>,
} }

View file

@ -23,15 +23,17 @@ pub struct GlobalNamespace {
data_namespace: RefCell<HashMap<String, DataNamespaceId>>, data_namespace: RefCell<HashMap<String, DataNamespaceId>>,
} }
pub type TypeId = usize;
pub type TraitId = usize;
pub type CommandId = usize;
enum TypeNamespaceId { enum TypeNamespaceId {
Types(usize), Types(TypeId),
Traits(usize), Traits(TraitId),
} }
enum DataNamespaceId { enum DataNamespaceId {
Commands(usize), Commands(CommandId),
#[allow(dead_code, reason = "will be used later")]
Globals(usize),
} }
impl GlobalNamespace { impl GlobalNamespace {
@ -126,21 +128,21 @@ impl GlobalNamespace {
} }
} }
pub fn get_type(&self, id: usize) -> Option<Type> { pub fn get_type(&self, id: TypeId) -> Option<Type> {
(self.types.borrow().len() > id).then_some(Type { (self.types.borrow().len() > id).then_some(Type {
id, id,
namespace: self, namespace: self,
}) })
} }
pub fn get_trait(&self, id: usize) -> Option<Trait> { pub fn get_trait(&self, id: TraitId) -> Option<Trait> {
(self.traits.borrow().len() > id).then_some(Trait { (self.traits.borrow().len() > id).then_some(Trait {
id, id,
namespace: self, namespace: self,
}) })
} }
pub fn get_command(&self, id: usize) -> Option<Command> { pub fn get_command(&self, id: CommandId) -> Option<Command> {
(self.commands.borrow().len() > id).then_some(Command { (self.commands.borrow().len() > id).then_some(Command {
id, id,
namespace: self, namespace: self,

View file

@ -5,10 +5,12 @@ use std::fmt::Debug;
use std::fmt::Display; use std::fmt::Display;
use super::GlobalNamespace; use super::GlobalNamespace;
use super::TraitId;
use super::TypeId;
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub struct Trait<'a> { pub struct Trait<'a> {
pub(super) id: usize, pub(super) id: TraitId,
pub(super) namespace: &'a GlobalNamespace, pub(super) namespace: &'a GlobalNamespace,
} }
@ -28,6 +30,6 @@ impl Debug for Trait<'_> {
pub(super) struct InternalTrait { pub(super) struct InternalTrait {
// make resolution easier // make resolution easier
pub(super) types: RefCell<HashSet<usize>>, pub(super) types: RefCell<HashSet<TypeId>>,
pub(super) name: String, pub(super) name: String,
} }

View file

@ -7,10 +7,12 @@ use std::fmt::Display;
use super::Trait; use super::Trait;
use super::GlobalNamespace; use super::GlobalNamespace;
use super::TraitId;
use super::TypeId;
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub struct Type<'a> { pub struct Type<'a> {
pub(super) id: usize, pub(super) id: TypeId,
pub(super) namespace: &'a GlobalNamespace, pub(super) namespace: &'a GlobalNamespace,
} }
@ -48,6 +50,6 @@ impl Debug for Type<'_> {
} }
pub(super) struct InternalType { pub(super) struct InternalType {
pub(super) traits: RefCell<HashSet<usize>>, pub(super) traits: RefCell<HashSet<TraitId>>,
pub(super) name: String, pub(super) name: String,
} }

View file

@ -1,15 +1,9 @@
use core::panic; use core::panic;
use std::process::CommandEnvs;
use crate::{ use crate::{
builtins::{TYPE_FLOAT, TYPE_INTEGER, TYPE_STRING}, builtins::{TYPE_FLOAT, TYPE_INTEGER, TYPE_STRING},
error::Errors, error::Errors,
namespace::{ namespace::{command::Command, r#type::Type, typedef::TypeDef, GlobalNamespace},
command::{self, Command},
r#type::Type,
typedef::TypeDef,
GlobalNamespace,
},
syntax::{CommandPart, CommandPartKind, PipelineElement, PipelineElementKind}, syntax::{CommandPart, CommandPartKind, PipelineElement, PipelineElementKind},
Span, Span,
}; };
@ -51,7 +45,7 @@ impl<'a> ExprKind<'a> {
} }
#[derive(Debug)] #[derive(Debug)]
struct CommandExpr<'a> { pub struct CommandExpr<'a> {
command: Command<'a>, command: Command<'a>,
args: Vec<Expr<'a>>, args: Vec<Expr<'a>>,
} }