From d1d31a7d153b7902b0975c82555b29135cd8ce7e Mon Sep 17 00:00:00 2001 From: primardj Date: Sun, 14 Jan 2024 17:54:27 +0000 Subject: [PATCH] create folder objects to contains Structure and Entree --- src/main.rs | 1 + src/objects/entree.rs | 28 +++++++++ src/objects/mod.rs | 3 + src/objects/structure.rs | 91 +++++++++++++++++++++++++++++ src/parser.rs | 122 +++------------------------------------ 5 files changed, 132 insertions(+), 113 deletions(-) create mode 100644 src/objects/entree.rs create mode 100644 src/objects/mod.rs create mode 100644 src/objects/structure.rs diff --git a/src/main.rs b/src/main.rs index ced5470..48056ea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ mod parser; mod job; mod variables; mod error; +mod objects; use std::process::exit; use variables::Variables; diff --git a/src/objects/entree.rs b/src/objects/entree.rs new file mode 100644 index 0000000..cbdabed --- /dev/null +++ b/src/objects/entree.rs @@ -0,0 +1,28 @@ + +use crate::objects::structure::Structure; + +// Définition de l'objet Entree, utilise un object Structure. + +pub struct Entree { + /* + * Correspond à une entrée. C'est ce que renvois le parser. + */ + size: i32, + inputs: Vec, +} + +impl Entree { + pub fn new() -> Self { + Self { + size: 0, + inputs: Vec::new(), + } + } + + pub fn add_structure(&mut self, structure: Structure) -> i8 { + self.inputs.push(structure); + self.size +=1; + 0 + } + +} diff --git a/src/objects/mod.rs b/src/objects/mod.rs new file mode 100644 index 0000000..f4cf9bd --- /dev/null +++ b/src/objects/mod.rs @@ -0,0 +1,3 @@ + +pub mod structure; +pub mod entree; diff --git a/src/objects/structure.rs b/src/objects/structure.rs new file mode 100644 index 0000000..ef5bc42 --- /dev/null +++ b/src/objects/structure.rs @@ -0,0 +1,91 @@ + +// Definition des structures utilisés par le parseur. +// s'utilise par l'appel de la fonction static parse, qui prends en param une ligne de commande +// (Vect) et qui renvois une Entree. + +pub struct Structure { + /* + * Il y à 5 types de structs differentes. + * - commande + * - control + * - def variable + * - def function + * - pipes + */ + + name: String, + contents: Vec, +} + +impl Structure { + /* + * name: "commande" + * contents: ["nom commande", "parametter", "arg1", ...] + * + * parametter define if the command is in background or not. For now, parametter can only be + * either "" or "&" + */ + fn new_commande(cont: Vec) -> Self { + Self { + name: String::from("commande"), + contents: cont, + } + } + /* + * name: "control" + * contents: ["if|elif|else|while|for", "condition", "liste String avec (faudra la reparser + * quand on tomberas dessus pour obtenir son objet de classe Entree) "] + */ + fn new_control(cont: Vec) -> Self { + Self { + name: String::from("control"), + contents: cont, + } + } + /* + * name: "variable" + * contents: ["type [string|list]","parametter", "name", "value"] + * + * parametter says if the variable is to export or not. right now it's either "" or "export" + */ + fn new_variable(cont: Vec) -> Self { + Self { + name: String::from("variable"), + contents: cont, + } + } + /* + * name: "function" + * contents: ["name", "nb parametter", "param1", ..., "liste String interieur fonction(faudra + * la repasser quand on tomberas dessus pour obtenir son objet de classe Entree)"] + */ + fn new_function(cont: Vec) -> Self { + Self { + name: String::from("variable"), + contents: cont, + } + } + /* + * name: "pipe" + * contents: ["String Commande1 (faudra la repasser dans le parser)", "String Commande2(tout + * le reste du pipe faudra le repasser dans le parser)"] + */ + fn new_pipe(cont: Vec) -> Self { + Self { + name: String::from("pipe"), + contents: cont, + } + } + pub fn get_name(&self) -> &String { + &self.name + } + pub fn get_contents(&self) -> &Vec { + &self.contents + } + + fn add_to_contents(&mut self, word: &String) -> &Self { + let _ = &self.contents.push(word.to_string()); + self + } + +} diff --git a/src/parser.rs b/src/parser.rs index dd244ea..f4d48f0 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,118 +1,9 @@ -// Definition des structures utilisés par le parseur. -// s'utilise par l'appel de la fonction static parse, qui prends en param une ligne de commande -// (Vect) et qui renvois une Entree. +// This is the parser. It can be called by two commands: +// parse(command_line: &String) -> Vec +// parse2(command_line: &String) -> Entree -pub struct Structure { - /* - * Il y à 5 types de structs differentes. - * - commande - * - control - * - def variable - * - def function - * - pipes - */ - - name: String, - contents: Vec, -} - -impl Structure { - /* - * name: "commande" - * contents: ["nom commande", "parametter", "arg1", ...] - * - * parametter define if the command is in background or not. For now, parametter can only be - * either "" or "&" - */ - fn new_commande(cont: Vec) -> Self { - Self { - name: String::from("commande"), - contents: cont, - } - } - /* - * name: "control" - * contents: ["if|elif|else|while|for", "condition", "liste String avec (faudra la reparser - * quand on tomberas dessus pour obtenir son objet de classe Entree) "] - */ - fn new_control(cont: Vec) -> Self { - Self { - name: String::from("control"), - contents: cont, - } - } - /* - * name: "variable" - * contents: ["type [string|list]","parametter", "name", "value"] - * - * parametter says if the variable is to export or not. right now it's either "" or "export" - */ - fn new_variable(cont: Vec) -> Self { - Self { - name: String::from("variable"), - contents: cont, - } - } - /* - * name: "function" - * contents: ["name", "nb parametter", "param1", ..., "liste String interieur fonction(faudra - * la repasser quand on tomberas dessus pour obtenir son objet de classe Entree)"] - */ - fn new_function(cont: Vec) -> Self { - Self { - name: String::from("variable"), - contents: cont, - } - } - /* - * name: "pipe" - * contents: ["String Commande1 (faudra la repasser dans le parser)", "String Commande2(tout - * le reste du pipe faudra le repasser dans le parser)"] - */ - fn new_pipe(cont: Vec) -> Self { - Self { - name: String::from("pipe"), - contents: cont, - } - } - pub fn get_name(&self) -> &String { - &self.name - } - pub fn get_contents(&self) -> &Vec { - &self.contents - } - - fn add_to_contents(&mut self, word: &String) -> &Self { - let _ = &self.contents.push(word.to_string()); - self - } - -} - -pub struct Entree { - /* - * Correspond à une entrée. C'est ce que renvois le parser. - */ - size: i32, - inputs: Vec, -} - -impl Entree { - pub fn new() -> Self { - Self { - size: 0, - inputs: Vec::new(), - } - } - - pub fn add_structure(&mut self, structure: Structure) -> i8 { - self.inputs.push(structure); - self.size +=1; - 0 - } - -} +use crate::objects::entree::Entree; fn parse_after_whitespace(char_splitted_command_line: &mut Vec, argv: &mut Vec) -> Vec { @@ -163,6 +54,11 @@ pub fn parse(command_line: &String) -> Vec { argv } +// Up to this line is the old version parser. + + + + pub fn parse2(command_line: &String) -> Entree { // prends en paramettre un ligne de commande, et retourne une Entree. let mut jaaj: Entree = Entree::new();