Skip to content
Extraits de code Groupes Projets
Non vérifiée Valider 1502e8b0 rédigé par Kubat's avatar Kubat
Parcourir les fichiers

[VN+AST] Begin to add structs to form the script graph to execute!

parent 0910c21f
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
use crate::{error::Error, spell::Spell, state::State};
use derive_more::Display;
#[derive(Debug, Display, Clone, Copy, PartialEq, Eq, Hash)]
pub struct BlockId(u64);
pub struct Block {
internal_id: u64,
name: Option<String>,
spells: Vec<Box<dyn Spell>>,
}
impl Default for Block {
fn default() -> Self {
use std::sync::atomic::{AtomicU64, Ordering};
static INTERNAL_ID: AtomicU64 = AtomicU64::new(1);
Self {
internal_id: INTERNAL_ID.fetch_add(1, Ordering::Relaxed),
name: Default::default(),
spells: Default::default(),
}
}
}
impl Block {
pub fn with_name(self, name: impl Into<String>) -> Self {
let name = name.into();
Self {
name: (!name.is_empty()).then_some(name),
..self
}
}
pub fn with_spells(self, spells: impl IntoIterator<Item = Box<dyn Spell>>) -> Self {
Self {
spells: spells.into_iter().collect(),
..self
}
}
pub fn into_parts(self) -> (BlockId, Self) {
(self.id(), self)
}
pub fn id(&self) -> BlockId {
BlockId(self.internal_id)
}
pub fn iter(&self) -> impl Iterator<Item = &dyn Spell> {
self.spells.iter().map(Box::as_ref)
}
pub fn indexed_iter(&self) -> impl Iterator<Item = &dyn Spell> {
self.spells.iter().map(Box::as_ref)
}
pub fn name(&self) -> Option<&str> {
self.name.as_deref()
}
pub fn try_cast_spells(&self, state: State) -> Result<State, Error> {
self.spells
.iter()
.try_fold(state, |state, spell| spell.cast(state))
}
}
mod block;
mod constant;
mod expr;
mod span;
pub mod operator;
pub use self::{constant::*, expr::*, span::Span as AstSpan};
pub use self::{
block::{Block, BlockId},
constant::Const,
expr::{Expression, VarOrConst},
span::Span as AstSpan,
};
use crate::{
ast::{Block, BlockId, Expression},
error::Error,
};
use std::collections::HashMap;
#[derive(Default)]
pub struct Graph {
nodes: HashMap<BlockId, Block>,
edges: Vec<(BlockId, BlockId, Option<Expression>)>,
id_mapping: HashMap<String, BlockId>,
}
impl Graph {
pub fn add_node(&mut self, block: Block) -> Result<BlockId, Error> {
todo!()
}
pub fn get_node(&self, id: BlockId) -> Result<&Block, Error> {
todo!()
}
pub fn mut_node(&mut self, id: BlockId) -> Result<&mut Block, Error> {
todo!()
}
pub fn add_edge(&mut self, from: BlockId, to: BlockId) -> Result<(), Error> {
todo!()
}
pub fn add_conditional_edge(
&mut self,
from: BlockId,
to: BlockId,
predicate: Expression,
) -> Result<(), Error> {
todo!()
}
}
mod error;
mod graph;
mod state;
mod types;
mod vn;
......@@ -15,6 +16,7 @@ pub mod prelude {
VarOrConst as GrimoireVarOrConst,
},
error::Error as GrimoireError,
graph::Graph as GrimoireGraph,
state::State as GrimoireState,
types::Type as GrimoireType,
};
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter