while loop
This commit is contained in:
parent
bbb817854a
commit
dedb49bba0
|
@ -6,7 +6,7 @@ use crate::sophie;
|
||||||
pub const TEXTE_ROUGE: &str = "\x1b[31m";
|
pub const TEXTE_ROUGE: &str = "\x1b[31m";
|
||||||
pub const TEXTE_VERT: &str = "\x1b[32m";
|
pub const TEXTE_VERT: &str = "\x1b[32m";
|
||||||
//pub const TEXTE_JAUNE: &str = "\x1b[33m";
|
//pub const TEXTE_JAUNE: &str = "\x1b[33m";
|
||||||
//pub const TEXTE_BLEU: &str = "\x1b[34m";
|
pub const TEXTE_BLEU: &str = "\x1b[34m";
|
||||||
pub const TEXTE_GRIS: &str = "\x1b[37m";
|
pub const TEXTE_GRIS: &str = "\x1b[37m";
|
||||||
pub const TEXTE_NORMAL: &str = "\x1b[0m";
|
pub const TEXTE_NORMAL: &str = "\x1b[0m";
|
||||||
|
|
||||||
|
@ -120,10 +120,10 @@ impl fmt::Display for ErreurPendragon {
|
||||||
impl fmt::Display for Commande {
|
impl fmt::Display for Commande {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {//'
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {//'
|
||||||
match self {
|
match self {
|
||||||
Self::Definis(nom, type_element) => write!(f, "{}:{}", nom, type_element.nom()),
|
Self::Definis(nom, type_element) => write!(f, "{}{}{}:{}", TEXTE_VERT, nom, TEXTE_NORMAL, type_element.nom()),
|
||||||
Self::Demande(nom) => write!(f, "{}?", nom),
|
Self::Demande(nom) => write!(f, "{}{}{}?", TEXTE_VERT, nom, TEXTE_NORMAL),
|
||||||
Self::Modifie(nom, expression) => write!(f, "{}={:?}", nom, expression),
|
Self::Modifie(nom, expression) => write!(f,"{}{}{}={}[{}{}{}]{}", TEXTE_VERT, nom, TEXTE_NORMAL, TEXTE_ROUGE, TEXTE_NORMAL, liste_element(&expression), TEXTE_ROUGE, TEXTE_NORMAL),
|
||||||
Self::Affiche(expression) => write!(f, "#{:?}", expression),
|
Self::Affiche(expression) => write!(f, "#{}[{}{}{}]{}", TEXTE_ROUGE, TEXTE_NORMAL, liste_element(&expression), TEXTE_ROUGE, TEXTE_NORMAL),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -134,9 +134,9 @@ impl fmt::Display for Element {
|
||||||
Self::Entier(nombre) => write!(f, "{}", sophie::nombre::nombre_comme_texte(*nombre)),
|
Self::Entier(nombre) => write!(f, "{}", sophie::nombre::nombre_comme_texte(*nombre)),
|
||||||
Self::Texte(texte) => write!(f, "\"{}\"", texte),
|
Self::Texte(texte) => write!(f, "\"{}\"", texte),
|
||||||
Self::Booleen(booleen) => write!(f, "{}", sophie::booleen::booleen_comme_texte(*booleen)),
|
Self::Booleen(booleen) => write!(f, "{}", sophie::booleen::booleen_comme_texte(*booleen)),
|
||||||
Self::Variable(nom, type_variable) => write!(f, "{}:{}", nom, type_variable.nom()),
|
Self::Variable(nom, type_variable) => write!(f, "{}{}{}:{}", TEXTE_VERT, nom, TEXTE_NORMAL, type_variable.nom()),
|
||||||
Self::Operateur(operateur) => write!(f, "{}", operateur),
|
Self::Operateur(operateur) => write!(f, "{}", operateur),
|
||||||
Self::Comparaison(comparaison) => write!(f, "{}.", comparaison),
|
Self::Comparaison(comparaison) => write!(f, "{}", comparaison),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -151,9 +151,14 @@ impl fmt::Display for Comparaison {
|
||||||
for element in &self.membre_b {
|
for element in &self.membre_b {
|
||||||
texte_membre_b += &format!(" {}", element);
|
texte_membre_b += &format!(" {}", element);
|
||||||
}
|
}
|
||||||
write!(f, "({}{:?}{})",
|
let comparaison = if let Some(type_comparaison) = &self.type_comparaison {
|
||||||
|
format!("{}", type_comparaison)
|
||||||
|
} else {
|
||||||
|
"?".to_string()
|
||||||
|
};
|
||||||
|
write!(f, "({}{}{})",
|
||||||
texte_membre_a,
|
texte_membre_a,
|
||||||
self.type_comparaison,
|
comparaison,
|
||||||
texte_membre_b,
|
texte_membre_b,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -162,12 +167,12 @@ impl fmt::Display for Comparaison {
|
||||||
impl fmt::Display for TypeComparaison {
|
impl fmt::Display for TypeComparaison {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {//'
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {//'
|
||||||
match self {
|
match self {
|
||||||
Self::Egal => write!(f, "égal à"),
|
Self::Egal => write!(f, "=="),
|
||||||
Self::Different => write!(f, "différent de"),
|
Self::Different => write!(f, "!="),
|
||||||
Self::SuperieurEgal => write!(f, "supérieur ou égal à"),
|
Self::SuperieurEgal => write!(f, ">="),
|
||||||
Self::InferieurEgal => write!(f, "inférieur ou égal à"),
|
Self::InferieurEgal => write!(f, "<="),
|
||||||
Self::Superieur => write!(f, "supérieur à"),
|
Self::Superieur => write!(f, ">"),
|
||||||
Self::Inferieur => write!(f, "inférieur à"),
|
Self::Inferieur => write!(f, "<"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -179,10 +184,10 @@ impl fmt::Display for Operateur {
|
||||||
Self::Et => write!(f, "et"),
|
Self::Et => write!(f, "et"),
|
||||||
Self::Non => write!(f, "non"),
|
Self::Non => write!(f, "non"),
|
||||||
Self::ParentheseBooleen => write!(f, "["),
|
Self::ParentheseBooleen => write!(f, "["),
|
||||||
Self::Puis => write!(f, ";"),
|
Self::Puis => write!(f, "~"),
|
||||||
Self::Plus => write!(f, "+"),
|
Self::Plus => write!(f, "+"),
|
||||||
Self::Moins => write!(f, "-"),
|
Self::Moins => write!(f, "-"),
|
||||||
Self::Fois => write!(f, "*"),
|
Self::Fois => write!(f, "x"),
|
||||||
Self::Divise => write!(f, "/"),
|
Self::Divise => write!(f, "/"),
|
||||||
Self::ParentheseEntier => write!(f, "("),
|
Self::ParentheseEntier => write!(f, "("),
|
||||||
}
|
}
|
||||||
|
@ -191,14 +196,9 @@ 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 : {:?}\n", self.variables);
|
let mut texte: String = format!("{}variables : {:?}{}\n\n", TEXTE_GRIS, self.variables, TEXTE_NORMAL);
|
||||||
let mut nombre_retour: usize = 0;
|
|
||||||
for phrase in &self.contenu {
|
for phrase in &self.contenu {
|
||||||
if texte.len()/30 > nombre_retour {
|
texte += &format!("{}\n", phrase);
|
||||||
texte += "\n";
|
|
||||||
nombre_retour = texte.len()/30;
|
|
||||||
}
|
|
||||||
texte += &format!("{}, ", phrase);
|
|
||||||
}
|
}
|
||||||
write!(f, "{}", texte)
|
write!(f, "{}", texte)
|
||||||
}
|
}
|
||||||
|
@ -215,15 +215,25 @@ impl fmt::Display for Phrase {
|
||||||
|
|
||||||
impl fmt::Display for Bloc {
|
impl fmt::Display for Bloc {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {//'
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {//'
|
||||||
let mut texte: String = format!("\n[{:?}] : [", self.condition);
|
let mut texte: String = format!("|{}| : {}[{}", liste_element(&self.condition), TEXTE_BLEU, TEXTE_NORMAL);
|
||||||
let mut nombre_retour: usize = 0;
|
for (index, phrase) in self.contenu.iter().enumerate() {
|
||||||
for phrase in &self.contenu {
|
texte += &format!("{}", phrase);
|
||||||
if texte.len()/30 > nombre_retour {
|
if index < self.contenu.len() - 1 {
|
||||||
texte += "\n";
|
texte += &format!("{},{} ", TEXTE_GRIS, TEXTE_NORMAL);
|
||||||
nombre_retour = texte.len()/30;
|
|
||||||
}
|
}
|
||||||
texte += &format!("{}, ", phrase);
|
|
||||||
}
|
}
|
||||||
write!(f, "{}]", texte)
|
write!(f, "{}{}]{}", texte, TEXTE_BLEU, TEXTE_NORMAL)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn liste_element(expression: &Vec<Element>) -> String {
|
||||||
|
let mut texte = String::new();
|
||||||
|
for (index, element) in expression.iter().enumerate() {
|
||||||
|
texte += &format!("{}", element);
|
||||||
|
if index < expression.len() - 1 {
|
||||||
|
texte += &format!("{},{} ", TEXTE_GRIS, TEXTE_NORMAL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
texte
|
||||||
|
}
|
10
src/main.rs
10
src/main.rs
|
@ -16,7 +16,8 @@ fn main() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let debug_mode = arguments.contains(&"--debug".to_string());
|
let mode_debug = arguments.contains(&"-d".to_string());
|
||||||
|
let mode_interprete = arguments.contains(&"-i".to_string());
|
||||||
|
|
||||||
let chemin_de_fichier = &arguments[1];
|
let chemin_de_fichier = &arguments[1];
|
||||||
let mut pendragon = Pendragon::nouveau();
|
let mut pendragon = Pendragon::nouveau();
|
||||||
|
@ -39,11 +40,12 @@ fn main() {
|
||||||
}
|
}
|
||||||
display::message_compilation_ok(debut.elapsed());
|
display::message_compilation_ok(debut.elapsed());
|
||||||
|
|
||||||
if debug_mode {
|
if mode_debug {
|
||||||
println!("\n{}\n", pendragon.programme);
|
println!("\n{}\n", pendragon.programme);
|
||||||
}
|
}
|
||||||
|
if !mode_interprete {
|
||||||
|
return
|
||||||
|
}
|
||||||
display::message_execution(chemin_de_fichier);
|
display::message_execution(chemin_de_fichier);
|
||||||
let debut = Instant::now();
|
let debut = Instant::now();
|
||||||
if let Err(raison) = pendragon.programme.execute() {
|
if let Err(raison) = pendragon.programme.execute() {
|
||||||
|
|
|
@ -102,7 +102,7 @@ impl Pendragon {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compile_bloc(&mut self, phrase: &str) -> Result<Bloc, ErreurPendragon> {
|
fn compile_bloc(&mut self, phrase: &str) -> Result<Bloc, ErreurPendragon> {
|
||||||
let phrase = phrase.trim();
|
let phrase = phrase.trim().replace("Tant que", "Tant-que");
|
||||||
let parties: Vec<&str> = phrase.splitn(2, ' ').collect();
|
let parties: Vec<&str> = phrase.splitn(2, ' ').collect();
|
||||||
if parties.len() == 1 {
|
if parties.len() == 1 {
|
||||||
return Err(ErreurPendragon::ManqueArgument)
|
return Err(ErreurPendragon::ManqueArgument)
|
||||||
|
@ -112,6 +112,7 @@ impl Pendragon {
|
||||||
}
|
}
|
||||||
|
|
||||||
match parties[0] {
|
match parties[0] {
|
||||||
|
"Tant-que" => Ok(Bloc::nouveau(self.elements_booleen(parties[1])?, true)),
|
||||||
"Si" => Ok(Bloc::nouveau(self.elements_booleen(parties[1])?, false)),
|
"Si" => Ok(Bloc::nouveau(self.elements_booleen(parties[1])?, false)),
|
||||||
autre => Err(ErreurPendragon::BlocInconnu(autre.into())),
|
autre => Err(ErreurPendragon::BlocInconnu(autre.into())),
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,7 +60,6 @@ pub struct Bloc {
|
||||||
pub condition: Vec<Element>,
|
pub condition: Vec<Element>,
|
||||||
pub repete: bool,
|
pub repete: bool,
|
||||||
pub contenu: Vec<Phrase>,
|
pub contenu: Vec<Phrase>,
|
||||||
pub contenu_sinon: Vec<Phrase>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Bloc {
|
impl Bloc {
|
||||||
|
@ -69,7 +68,6 @@ impl Bloc {
|
||||||
condition,
|
condition,
|
||||||
repete,
|
repete,
|
||||||
contenu: vec![],
|
contenu: vec![],
|
||||||
contenu_sinon: vec![],
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,17 +75,9 @@ impl Bloc {
|
||||||
self.contenu.push(Phrase::Commande(commande));
|
self.contenu.push(Phrase::Commande(commande));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ajoute_commande_sinon(&mut self, commande: Commande) {
|
|
||||||
self.contenu_sinon.push(Phrase::Commande(commande));
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn ajoute_bloc(&mut self, bloc: Bloc) {
|
pub fn ajoute_bloc(&mut self, bloc: Bloc) {
|
||||||
self.contenu.push(Phrase::Bloc(bloc));
|
self.contenu.push(Phrase::Bloc(bloc));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ajoute_bloc_sinon(&mut self, bloc: Bloc) {
|
|
||||||
self.contenu_sinon.push(Phrase::Bloc(bloc));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum Phrase {
|
pub enum Phrase {
|
||||||
|
|
|
@ -37,14 +37,14 @@ impl Bloc {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if self.contenu_sinon.len() > 0 {
|
} //else if self.contenu_sinon.len() > 0 {
|
||||||
for phrase in &self.contenu_sinon {
|
// for phrase in &self.contenu_sinon {
|
||||||
match phrase {
|
// match phrase {
|
||||||
Phrase::Commande(commande) => commande.execute(variables)?,
|
// Phrase::Commande(commande) => commande.execute(variables)?,
|
||||||
Phrase::Bloc(bloc) => bloc.execute(variables)?,
|
// Phrase::Bloc(bloc) => bloc.execute(variables)?,
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
11
test.dr
11
test.dr
|
@ -10,10 +10,7 @@ Nota Bene : 3*A*(3*A+C).
|
||||||
|
|
||||||
Affiche "Résultat : " puis B.
|
Affiche "Résultat : " puis B.
|
||||||
|
|
||||||
Si A est inférieur ou égal à trois,
|
Tant que A est inférieur ou égal à sept,
|
||||||
Affiche "A<=3".
|
Modifie A avec A plus un.
|
||||||
Si B est inférieur ou égal à trois,
|
Affiche "A : " puis A.
|
||||||
Affiche "B<=3".
|
Affiche "Fin".
|
||||||
Affiche "fin B".
|
|
||||||
|
|
||||||
Affiche "test".
|
|
Loading…
Reference in a new issue