basic bloc structure

This commit is contained in:
WanderingPenwing 2024-12-13 17:09:35 +01:00
parent 371cbb6db1
commit 27fba9c4bf
4 changed files with 58 additions and 29 deletions

View file

@ -1 +1 @@
{"categories":[{"name":"todo","content":[{"name":"multiple compilation error","description":"// Hello there","id":1},{"name":"if","description":"implémente if avec des guillemets de délimitation","id":1},{"name":"scope variable","description":"// Hello there","id":6},{"name":"while","description":"implémente un while","id":2},{"name":"else","description":"// Hello there","id":7},{"name":"break","description":"// Hello there","id":8},{"name":"continue","description":"// Hello there","id":9},{"name":"compilation","description":"// Hello there","id":3}]},{"name":"in progress","content":[]},{"name":"done","content":[{"name":"comparaison","description":"// Hello there","id":2},{"name":"booleen type","description":"// Hello there","id":4},{"name":"soixante-dix","description":"// Hello there","id":1},{"name":"parenthese comparaison","description":"// Hello there","id":1},{"name":"test comparaison","description":"// Hello there","id":4},{"name":"error ligne compilation","description":"// Hello there","id":2},{"name":"commentaires","description":"// Hello there","id":3},{"name":"compile time verification","description":"odre des termes rpn","id":1},{"name":"test multiple space in texte","description":"// Hello there","id":3},{"name":"test puis in texte","description":"// Hello there","id":2},{"name":"enchainement puis","description":"// Hello there","id":5},{"name":"guillemet mal ferme","description":"// Hello there","id":4},{"name":"display element","description":"for better print","id":6},{"name":"puis à la ligne, alinéa","description":"// Hello there","id":1},{"name":"teste double tiret","description":"// Hello there","id":2},{"name":"teste puis seul","description":"tout seul, seul devant, seul fin","id":3},{"name":"erreur calcul bool/nombre","description":"affiche element precedent lorsque mauvais enchainement","id":1}]},{"name":"bug","content":[]},{"name":"to test","content":[{"name":"tests mod","description":"// Hello there","id":3},{"name":"test variable","description":"// Hello there","id":2}]},{"name":"bonus","content":[{"name":"stop cheating with \"-\"","description":"// Hello there","id":5},{"name":"affiche ligne et position erreur","description":"// Hello there","id":1},{"name":"pour numero de ligne execution","description":"sauvegarde numero de ligne dans la commande\n\ncommande.ligne(12)","id":3},{"name":"standardizer erreur","description":"regarder les texte répétés","id":1}]},{"name":"+","content":[]}]} {"categories":[{"name":"todo","content":[{"name":"if","description":"implémente if avec des guillemets de délimitation","id":1},{"name":"scope variable","description":"// Hello there","id":6},{"name":"while","description":"implémente un while","id":2},{"name":"else","description":"// Hello there","id":7},{"name":"break","description":"// Hello there","id":8},{"name":"continue","description":"// Hello there","id":9},{"name":"compilation","description":"// Hello there","id":3}]},{"name":"in progress","content":[]},{"name":"done","content":[{"name":"comparaison","description":"// Hello there","id":2},{"name":"booleen type","description":"// Hello there","id":4},{"name":"soixante-dix","description":"// Hello there","id":1},{"name":"parenthese comparaison","description":"// Hello there","id":1},{"name":"test comparaison","description":"// Hello there","id":4},{"name":"error ligne compilation","description":"// Hello there","id":2},{"name":"commentaires","description":"// Hello there","id":3},{"name":"compile time verification","description":"odre des termes rpn","id":1},{"name":"test multiple space in texte","description":"// Hello there","id":3},{"name":"test puis in texte","description":"// Hello there","id":2},{"name":"enchainement puis","description":"// Hello there","id":5},{"name":"guillemet mal ferme","description":"// Hello there","id":4},{"name":"display element","description":"for better print","id":6},{"name":"puis à la ligne, alinéa","description":"// Hello there","id":1},{"name":"teste double tiret","description":"// Hello there","id":2},{"name":"teste puis seul","description":"tout seul, seul devant, seul fin","id":3},{"name":"erreur calcul bool/nombre","description":"affiche element precedent lorsque mauvais enchainement","id":1},{"name":"multiple compilation error","description":"// Hello there","id":1},{"name":"tests mod","description":"// Hello there","id":3}]},{"name":"bug","content":[]},{"name":"to test","content":[{"name":"test variable","description":"// Hello there","id":2}]},{"name":"bonus","content":[{"name":"stop cheating with \"-\"","description":"// Hello there","id":5},{"name":"affiche ligne et position erreur","description":"// Hello there","id":1},{"name":"pour numero de ligne execution","description":"sauvegarde numero de ligne dans la commande\n\ncommande.ligne(12)","id":3},{"name":"standardizer erreur","description":"regarder les texte répétés","id":1}]},{"name":"+","content":[]}]}

View file

@ -187,9 +187,18 @@ impl fmt::Display for Operateur {
impl fmt::Display for Programme { impl fmt::Display for Programme {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {//' fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {//'
let mut texte: String = format!("variables : {:?}", self.variables); let mut texte: String = format!("variables : {:?}", self.variables);
for (index, commande) in self.commandes.iter().enumerate() { for (index, phrase) in self.contenu.iter().enumerate() {
texte += &format!("\n#{:2}-{}", index+1, commande); texte += &format!("\n#{:2}-{}", index+1, phrase);
} }
write!(f, "{}", texte) write!(f, "{}", texte)
} }
}
impl fmt::Display for Phrase {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {//'
match self {
Self::Commande(commande) => write!(f, "{}", commande),
Self::Bloc(_bloc) => write!(f, "bloc inconnu"),
}
}
} }

View file

@ -130,7 +130,6 @@ pub fn format_de_variable(nom: &str) -> bool {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use std::collections::HashMap;
use super::*; use super::*;
#[test] #[test]
@ -148,7 +147,7 @@ mod test {
]; ];
for commentaire in commentaires { for commentaire in commentaires {
match pendragon.compile(commentaire.into()) { match pendragon.compile(commentaire.into()) {
Ok(_) => assert_eq!(pendragon.programme.commandes.len(), 0, "Le commentaire '{}' ne devrait pas générer de commande", commentaire), Ok(_) => assert_eq!(pendragon.programme.contenu.len(), 0, "Le commentaire '{}' ne devrait pas générer de commande", commentaire),
Err(erreurs) => { Err(erreurs) => {
let affichage_erreurs = erreurs.iter().map(|item| format!("{}", item.raison())).collect::<Vec<_>>().join("\n\n"); let affichage_erreurs = erreurs.iter().map(|item| format!("{}", item.raison())).collect::<Vec<_>>().join("\n\n");
panic!("Erreur de compilation du commentaire '{}' : \n{}", commentaire, affichage_erreurs) panic!("Erreur de compilation du commentaire '{}' : \n{}", commentaire, affichage_erreurs)

View file

@ -5,19 +5,19 @@ use super::*;
pub struct Programme { pub struct Programme {
pub variables: HashMap<String, TypeElement>, pub variables: HashMap<String, TypeElement>,
pub commandes: Vec<Commande>, pub contenu: Vec<Phrase>,
} }
impl Programme { impl Programme {
pub fn nouveau() -> Self { pub fn nouveau() -> Self {
Self { Self {
variables: HashMap::new(), variables: HashMap::new(),
commandes: vec![], contenu: vec![],
} }
} }
pub fn ajoute_commande(&mut self, commande: Commande) { pub fn ajoute_commande(&mut self, commande: Commande) {
self.commandes.push(commande); self.contenu.push(Phrase::Commande(commande));
} }
pub fn ajoute_variable(&mut self, nom: String, type_variable: TypeElement) -> Result<(), ErreurPendragon> { pub fn ajoute_variable(&mut self, nom: String, type_variable: TypeElement) -> Result<(), ErreurPendragon> {
@ -54,32 +54,27 @@ impl Programme {
pub fn execute(&self) -> Result<(), ErreurPendragon> { pub fn execute(&self) -> Result<(), ErreurPendragon> {
let mut variables_globales: HashMap<String, Element> = HashMap::new(); let mut variables_globales: HashMap<String, Element> = HashMap::new();
for commande in &self.commandes { for phrase in &self.contenu {
match commande { let Phrase::Commande(commande) = phrase else {
Commande::Definis(nom, type_element) => { println!("doit executer bloc");
variables_globales.insert(nom.to_string(), type_element.comme_element()); continue;
} };
Commande::Demande(nom) => { commande.execute(&mut variables_globales)?;
let valeur = variables_globales[nom].type_element().demande_valeur(&nom)?;
variables_globales.insert(nom.to_string(), valeur);
}
Commande::Modifie(nom, expression) => {
let valeur = match variables_globales[nom].type_element() {
TypeElement::Entier => Element::Entier(nombre::calcule_nombre(expression.clone(), &variables_globales)?),
TypeElement::Texte => Element::Texte(texte::calcule_texte(expression.clone(), &variables_globales)?),
TypeElement::Booleen => Element::Booleen(booleen::calcule_booleen(expression.clone(), &variables_globales)?),
};
variables_globales.insert(nom.to_string(), valeur);
}
Commande::Affiche(expression) => {
println!("{}", texte::calcule_texte(expression.to_vec(), &variables_globales)?);
}
}
} }
Ok(()) Ok(())
} }
} }
pub struct Bloc {
condition: Vec<Element>,
contenu: Vec<Phrase>,
}
pub enum Phrase {
Bloc(Bloc),
Commande(Commande),
}
pub enum Commande { pub enum Commande {
Definis(String, TypeElement), Definis(String, TypeElement),
Demande(String), Demande(String),
@ -87,6 +82,32 @@ pub enum Commande {
Affiche(Vec<Element>), Affiche(Vec<Element>),
} }
impl Commande {
fn execute(&self, variables: &mut HashMap<String, Element>) -> Result<(), ErreurPendragon> {
match self {
Commande::Definis(nom, type_element) => {
variables.insert(nom.to_string(), type_element.comme_element());
}
Commande::Demande(nom) => {
let valeur = variables[nom].type_element().demande_valeur(&nom)?;
variables.insert(nom.to_string(), valeur);
}
Commande::Modifie(nom, expression) => {
let valeur = match variables[nom].type_element() {
TypeElement::Entier => Element::Entier(nombre::calcule_nombre(expression.clone(), variables)?),
TypeElement::Texte => Element::Texte(texte::calcule_texte(expression.clone(), variables)?),
TypeElement::Booleen => Element::Booleen(booleen::calcule_booleen(expression.clone(), variables)?),
};
variables.insert(nom.to_string(), valeur);
}
Commande::Affiche(expression) => {
println!("{}", texte::calcule_texte(expression.to_vec(), variables)?);
}
}
Ok(())
}
}
#[derive(PartialEq, Debug, Clone)] #[derive(PartialEq, Debug, Clone)]
pub enum TypeElement { pub enum TypeElement {
Entier, Entier,