@@ -6,64 +6,101 @@ namespace FPL {
66 mTypes [" entier" ] = Type (" entier" , INT);
77 mTypes [" decimal" ] = Type (" decimal" , DOUBLE);
88 mTypes [" texte" ] = Type (" texte" , STRING);
9+ mTypes [" auto" ] = Type (" auto" , AUTO);
910 }
1011
11- bool Parser::FunctionChecker (auto parseStart) {
12+ bool Parser::AppelerInstruction (auto parseStart) {
13+ auto PossibleFonctionName = CheckerIdentifiant ();
14+ if (PossibleFonctionName.has_value ()) {
15+ if (CheckerOperateur (" ;" ).has_value ()) {
16+ if (isFonction (PossibleFonctionName->mText )) {
17+ FonctionDefinition fonction = mFonctions [PossibleFonctionName->mText ];
18+ auto contentFonction = fonction.FonctionContent ;
19+ std::string finalContent;
20+ for (auto a : contentFonction) {
21+ finalContent += a += " " ;
22+ }
23+ TokenBuilding t;
24+ std::vector<Token> tokens = t.parseToken (finalContent);
25+ auto FEndToken = tokens.end ();
26+ auto FCurrToken = tokens.begin ();
27+
28+ while (FCurrToken != FEndToken) { // Tant que tout le fichier n'est pas parcouru et qu'on n'a pas analysé tous les éléments.
29+ if (ManagerInstruction (FCurrToken)) {
30+
31+ } else {
32+ if (FCurrToken->mText .empty ()) {
33+ ++FCurrToken;
34+ continue ;
35+ }
36+
37+ std::cerr << " Identifier inconnu : " << FCurrToken->mText << std::endl;
38+ ++FCurrToken;
39+ }
40+ }
41+ return true ;
42+ }
43+ }
44+ }
45+ return false ;
46+ }
47+
48+ bool Parser::FonctionInstruction (auto parseStart) {
1249 auto PeutEtreNom = CheckerIdentifiant ();
1350 if (PeutEtreNom.has_value ()) {
14- // On a le nom d'une fonction.
1551 if (CheckerOperateur (" (" ).has_value ()) {
16- // On a surement une fonction finie !
17-
1852 FonctionDefinition fonction;
1953 fonction.FonctionName = PeutEtreNom->mText ;
20-
21- while (CheckerOperateur (" )" ) == std::nullopt ) {
54+ while (!CheckerOperateur (" )" ).has_value ()) {
2255 auto type = CheckerType ();
2356
24- // Si aucun paramètre ou l'utilisateur a utilisé l'argument 'vide' ou juste fermer.
25- if (type->mType == VOID) { break ; }
57+ if (type->mType == VOID) { // Si aucun paramètre ou l'utilisateur a utilisé l'argument 'vide' ou juste fermer.
58+ if (CheckerOperateur (" )" ).has_value ()) {
59+ break ;
60+ } else {
61+ std::cerr << " Vous devez fermer les parentheses de la fonction." << std::endl;
62+ }
63+ }
2664
27- if (!type.has_value () || type == std:: nullopt ) {
28- throw std::runtime_error ( " Vous devez spécifier un type d'argument." ) ;
65+ if (!type.has_value ()) {
66+ std::cerr << " Vous devez spécifier un type d'argument." << std::endl ;
2967 }
3068
3169 // Ajout de l'argument...
32- auto variableName = CheckerIdentifiant ();
33- if (!variableName .has_value ()) {
34- throw std::runtime_error ( " Vous devez spécifier un nom unique à l'argument." ) ;
70+ auto possibleArg = CheckerIdentifiant ();
71+ if (!possibleArg .has_value ()) {
72+ std::cerr << " Vous devez spécifier un nom unique à l'argument." << std::endl ;
3573 }
3674 ArgumentDefinition param;
3775 param.ArgType .mName = type->mName ;
3876 param.ArgType .mType = type->mType ;
39- param.ArgName = variableName ->mText ;
77+ param.ArgName = possibleArg ->mText ;
4078 fonction.ArgsFonction .push_back (param);
4179
4280 if (CheckerOperateur (" )" ).has_value ()) {
4381 break ;
4482 }
4583 if (!CheckerOperateur (" ," ).has_value ()) {
46- throw std::runtime_error (
47- " Vous devez utiliser la ',' pour separer les arguments de la fonction." );
84+ std::cerr << " Vous devez utiliser la ',' pour separer les arguments de la fonction." << std::endl;
4885 }
4986 }
5087
51- mFonctions [fonction.FonctionName ] = fonction;
52-
5388 // On récupère le code entre les {} :
54-
5589 if (CheckerOperateur (" {" )) {
5690 while (!CheckerOperateur (" }" ).has_value ()) {
91+ if (mCurrentToken ->mType == CHAINE_LITERAL) {
92+ mCurrentToken ->mText += " \" " ;
93+ }
5794 fonction.FonctionContent .push_back (mCurrentToken ->mText );
5895 ++mCurrentToken ;
5996
6097 if (CheckerOperateur (" }" ).has_value ()) {
6198 break ;
6299 }
63100 }
64-
65101 }
66102
103+ mFonctions [fonction.FonctionName ] = fonction;
67104 return true ;
68105 } else {
69106 mCurrentToken = parseStart;
@@ -100,7 +137,6 @@ namespace FPL {
100137 if (VarValue->StatementType .mType == STRING) {
101138 std::replace (VarValue->StatementName .begin (), VarValue->StatementName .end (), ' "' , ' ' );
102139 }
103- std::cout << VarValue->StatementName << std::endl;
104140 if (VarType->mType == INT) {
105141 int v;
106142 std::cin >> v;
@@ -191,8 +227,23 @@ namespace FPL {
191227 if (CheckerOperateur (" >" ).has_value ()) {
192228 auto VarValue = CheckerValue ();
193229 if (VarValue.has_value ()) {
230+ if (VarType->mType == AUTO) {
231+ if (CheckerOperateur (" ;" ).has_value ()) {
232+ VariableDefinition variable;
233+ variable.VariableName = VarName->mText ;
234+ variable.VariableType = Type (VarValue->StatementType .mName , VarValue->StatementType .mType );
235+ variable.VariableValue = VarValue->StatementName ;
236+
237+ mVariables [variable.VariableName ] = variable;
238+
239+ return true ;
240+ } else {
241+ std::cerr << " Merci de signifier la fin de la declaration de la variable avec ';'." << std::endl;
242+ }
243+ }
244+
194245 if (VarValue->StatementType .mType == VarType->mType ) {
195- if (CheckerOperateur (" ;" )) {
246+ if (CheckerOperateur (" ;" ). has_value () ) {
196247 VariableDefinition variable;
197248 variable.VariableName = VarName->mText ;
198249 variable.VariableType = Type (VarType->mName , VarType->mType );
@@ -201,7 +252,7 @@ namespace FPL {
201252 mVariables [variable.VariableName ] = variable;
202253 return true ;
203254 } else {
204- std::cerr << " Merci de signifier la fin de la déclaration de la variable avec ';'." << std::endl;
255+ std::cerr << " Merci de signifier la fin de la declaration de la variable avec ';'." << std::endl;
205256 }
206257 } else {
207258 std::cerr << " Vous devez donner une valeur qui est de même type que la variable." << std::endl;
@@ -289,15 +340,23 @@ namespace FPL {
289340 --mCurrentToken ;
290341 while (!CheckerOperateur (" ;" ).has_value ()) {
291342 auto Value = CheckerValue ();
292- if (Value->StatementType .mType == STRING) {
293- std::replace (Value->StatementName .begin (), Value->StatementName .end (), ' "' , ' ' );
294- }
343+ if (Value.has_value ()) {
344+ if (Value->StatementType .mType == STRING) {
345+ std::replace (Value->StatementName .begin (), Value->StatementName .end (), ' "' , ' ' );
346+ }
295347
296- if (CheckerOperateur (" [" ).has_value ()) {
348+ std::cout << Value->StatementName ;
349+ std::cout << " " ;
350+ }
351+ else if (CheckerOperateur (" [" ).has_value ()) {
297352 auto var = CheckerIdentifiant ();
298353 if (isVariable (var->mText )) {
299354 if (CheckerOperateur (" ]" ).has_value ()) {
300- std::cout << mVariables [var->mText ].VariableValue ;
355+ auto finalMsg = mVariables [var->mText ].VariableValue ;
356+ if (mVariables [var->mText ].VariableType .mType == STRING) {
357+ std::replace (finalMsg.begin (), finalMsg.end (), ' "' , ' ' );
358+ }
359+ std::cout << finalMsg;
301360 std::cout << " " ;
302361 } else {
303362 std::cerr << " Ces operateurs sont utilises dans cette instruction pour introduire une variable, merci de cloturer l'insertion avec ']'." << std::endl;
@@ -308,9 +367,6 @@ namespace FPL {
308367 }
309368 }
310369
311- std::cout << Value->StatementName ;
312- std::cout << " " ;
313-
314370 if (CheckerOperateur (" ;" ).has_value ()) {
315371 break ;
316372 }
@@ -333,13 +389,13 @@ namespace FPL {
333389 std::cerr << " La variable n'existe pas." << std::endl;
334390 }
335391 }
336- std::cerr << " Vous devez ouvrir les guillemets pour transmettre une chaine de caractères ou le nom de votre variable sous ce format : 'envoyer ( variable) <- " << std::endl;
392+ std::cerr << " Vous devez ouvrir les guillemets pour transmettre une chaine de caractères ou le nom de votre variable sous ce format : 'envoyer [ variable]; " << std::endl;
337393 }
338394 return false ;
339395 }
340396
341- bool Parser::ManagerInstruction () {
342- auto parseStart = mCurrentToken ; // std::vector<Token>::iterator
397+ bool Parser::ManagerInstruction (auto & token ) {
398+ auto parseStart = token ; // std::vector<Token>::iterator
343399 auto PeutEtreInstruction = CheckerIdentifiant ();
344400 if (PeutEtreInstruction.has_value ()) {
345401 if (PeutEtreInstruction->mText == " envoyer" ) {
@@ -349,9 +405,11 @@ namespace FPL {
349405 } else if (PeutEtreInstruction->mText == " changer" ) {
350406 if (ChangerInstruction ()) { return true ; } else { return false ; }
351407 } else if (PeutEtreInstruction->mText == " definir" ) {
352- if (FunctionChecker (parseStart)) {return true ;} else {return false ;}
353- } else {
354- mCurrentToken = parseStart;
408+ if (FonctionInstruction (parseStart)) {return true ;} else {return false ;}
409+ } else if (PeutEtreInstruction->mText == " appeler" ) {
410+ if (AppelerInstruction (parseStart)) { return true ; } else {return false ;}
411+ } else {
412+ token = parseStart;
355413 }
356414 return false ;
357415 }
@@ -365,10 +423,11 @@ namespace FPL {
365423 mCurrentToken = tokens.begin ();
366424
367425 while (mCurrentToken != mEndToken ) { // Tant que tout le fichier n'est pas parcouru et qu'on n'a pas analysé tous les éléments.
368- if (ManagerInstruction ()) {
426+ if (ManagerInstruction (mCurrentToken )) {
369427
370428 } else {
371429 if (mCurrentToken ->mText .empty ()) {
430+ ++mCurrentToken ;
372431 continue ;
373432 }
374433
@@ -383,6 +442,7 @@ namespace FPL {
383442 if (mCurrentToken ->mType != IDENTIFIANT) { return std::nullopt ; }
384443 if (mCurrentToken ->mText != name && !name.empty ()) { return std::nullopt ; }
385444
445+ std::cout << " 1: " << mCurrentToken ->mText << std::endl;
386446 auto returnToken = mCurrentToken ;
387447 ++mCurrentToken ;
388448 return *returnToken;
@@ -439,13 +499,16 @@ namespace FPL {
439499 }
440500
441501 bool Parser::isVariable (std::string &name) {
442- if (mVariables .contains (name)) {
443- return true ;
444- }
502+ if (mVariables .contains (name)) { return true ; }
503+ return false ;
504+ }
505+
506+ bool Parser::isFonction (std::string &name) {
507+ if (mFonctions .contains (name)) { return true ; }
445508 return false ;
446509 }
447510
448- void Parser::DebugPrint () const {
511+ [[maybe_unused]] void Parser::DebugPrint () const {
449512 for (auto &funcPair: mFonctions ) {
450513 for (auto &e: funcPair.second .FonctionContent ) {
451514 std::cout << e << std::endl;
0 commit comments