Skip to content

Commit c2ea036

Browse files
committed
Fixs, Saisir Instruction, Opti.
1 parent 86008b0 commit c2ea036

File tree

2 files changed

+142
-133
lines changed

2 files changed

+142
-133
lines changed

src/Parser.cpp

Lines changed: 138 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,39 @@ namespace FPL {
2626
};
2727
}
2828

29+
void Parser::ManageVariableName(std::optional<FonctionDefinition>& fonction, std::string name) {
30+
if (isVariable(name)) {
31+
std::cerr << "Veuillez choisir un autre nom pour votre variable." << std::endl;
32+
exit(1);
33+
}
34+
35+
if (std::find(InstructionsList.begin(), InstructionsList.end(), name) != InstructionsList.end()) {
36+
std::cerr << "Le nom doit etre different des types, des instructions." << std::endl;
37+
exit(1);
38+
}
39+
40+
if (fonction != std::nullopt) {
41+
if (isArgument(fonction->FonctionName, name)) {
42+
std::cerr << "Le nom de la variable ne peut pas etre le meme que celui d'un argument de la fonction : " << fonction->FonctionName << "." << std::endl;
43+
exit(1);
44+
}
45+
}
46+
}
47+
48+
void Parser::DefineVariable(std::optional<FonctionDefinition>& fonction, std::string& VarName, Type& VarType, std::string& VarValue) {
49+
VariableDefinition variable;
50+
variable.VariableName = VarName;
51+
variable.VariableType = Type(VarType.mName, VarType.mType);
52+
variable.IsGlobal = false;
53+
variable.HasReturnValue = false;
54+
variable.InFonction = false;
55+
if (fonction.has_value()) {
56+
variable.InFonction = true;
57+
}
58+
variable.VariableValue = VarValue;
59+
mVariables[variable.VariableName] = variable;
60+
}
61+
2962
bool Parser::AppelerInstruction() {
3063
auto PossibleFonctionName = CheckerIdentifiant();
3164
if (PossibleFonctionName.has_value()) {
@@ -260,122 +293,28 @@ namespace FPL {
260293
auto VarType = CheckerType();
261294
if (VarType.has_value()) {
262295
auto VarName = CheckerIdentifiant();
263-
264-
if (isVariable(VarName->mText)) {
265-
std::cerr << "Veuillez choisir un autre nom pour votre variable." << std::endl;
266-
exit(1);
267-
}
268-
269296
if (VarName.has_value()) {
270297
if (VarName->mText == "saisir") {
271298
VarName = CheckerIdentifiant();
272-
273-
if (std::find(InstructionsList.begin(), InstructionsList.end(), VarName->mText) != InstructionsList.end()) {
274-
std::cerr << "Le nom doit etre different des types, des instructions." << std::endl;
275-
exit(1);
276-
}
277-
278299
if (VarName.has_value()) {
279-
if (fonction != std::nullopt) {
280-
if (isArgument(fonction->FonctionName, VarName->mText)) {
281-
std::cerr << "Le nom de la variable ne peut pas etre le meme que celui d'un argument de la fonction : " << fonction->FonctionName << "." << std::endl;
282-
exit(1);
283-
}
284-
}
300+
ManageVariableName(fonction, VarName->mText);
285301

286302
if (CheckerOperateur("-").has_value()) {
287303
if (CheckerOperateur(">").has_value()) {
288-
auto VarValue = CheckerValue();
289-
290-
if (VarValue->StatementType.mType == STRING) {
291-
std::replace(VarValue->StatementName.begin(), VarValue->StatementName.end(), '"', ' ');
292-
}
293-
294-
if (VarValue.has_value()) {
295-
std::cout << VarValue->StatementName << std::endl;
296-
} else {
297-
auto PossibleArgument = CheckerIdentifiant();
298-
if (PossibleArgument.has_value() && fonction != std::nullopt) {
299-
if (isArgument(fonction->FonctionName, PossibleArgument->mText)) {
300-
std::cout << mArguments[fonction->FonctionName][PossibleArgument->mText].ArgValue << std::endl;
301-
} else {
302-
std::cerr << "L'argument " << PossibleArgument->mText <<" est inexistant ." << std::endl;
303-
exit(1);
304-
}
305-
}
306-
}
307-
308-
if (CheckerOperateur(";").has_value()) {
309-
VariableDefinition variable;
310-
variable.VariableName = VarName->mText;
311-
variable.VariableType = Type(VarType->mName, VarType->mType);
312-
variable.IsGlobal = false;
313-
variable.HasReturnValue = false;
314-
variable.InFonction = false;
315-
if (fonction.has_value() || fonction != std::nullopt) {
316-
variable.InFonction = true;
317-
}
318-
319-
if (VarType->mType == INT) {
320-
int v;
321-
std::cin >> v;
322-
variable.VariableValue = std::to_string(v);
323-
mVariables[variable.VariableName] = variable;
324-
} else if (VarType->mType == DOUBLE) {
325-
double v;
326-
std::cin >> v;
327-
variable.VariableValue = std::to_string(v);
328-
mVariables[variable.VariableName] = variable;
329-
} else if (VarType->mType == STRING) {
330-
std::string v;
331-
std::cin >> v;
332-
variable.VariableValue = v;
333-
mVariables[variable.VariableName] = variable;
334-
}
304+
if (executeInputs(fonction, VarName->mText, VarType.value(), "with")) {
335305
return true;
336-
} else {
337-
std::cerr << "Merci de signifier la fin de la declaration de la variable avec ';'." << std::endl;
338-
exit(1);
339306
}
307+
return false;
340308
} else {
341309
std::cerr << "Vous devez utiliser les symboles '->' pour donner une valeur." << std::endl;
342310
exit(1);
343311
}
344312
} else {
345-
if (CheckerOperateur(";").has_value()) {
346-
VariableDefinition variable;
347-
variable.VariableName = VarName->mText;
348-
variable.VariableType = Type(VarType->mName, VarType->mType);
349-
variable.IsGlobal = false;
350-
variable.HasReturnValue = false;
351-
variable.InFonction = false;
352-
if (fonction.has_value() || fonction != std::nullopt) {
353-
variable.InFonction = true;
354-
}
355-
356-
if (VarType->mType == INT) {
357-
int v;
358-
std::cin >> v;
359-
variable.VariableValue = std::to_string(v);
360-
361-
mVariables[variable.VariableName] = variable;
362-
} else if (VarType->mType == DOUBLE) {
363-
double v;
364-
std::cin >> v;
365-
variable.VariableValue = std::to_string(v);
366-
367-
mVariables[variable.VariableName] = variable;
368-
} else if (VarType->mType == STRING) {
369-
std::string v;
370-
std::cin >> v;
371-
variable.VariableValue = v;
372-
mVariables[variable.VariableName] = variable;
373-
}
313+
if (executeInputs(fonction, VarName->mText, VarType.value(), "without")) {
374314
return true;
375-
} else {
376-
std::cerr << "Merci de signifier la fin de la declaration de la variable avec ';'." << std::endl;
377-
exit(1);
378315
}
316+
std::cerr << "Vous devez utiliser les symboles '->' pour donner une valeur." << std::endl;
317+
exit(1);
379318
}
380319
} else {
381320
std::cerr << "Vous devez indiquer un nom a la variable." << std::endl;
@@ -385,18 +324,7 @@ namespace FPL {
385324
else if (VarName->mText == "globale") {
386325
VarName = CheckerIdentifiant();
387326
if (VarName.has_value()) {
388-
389-
if (std::find(InstructionsList.begin(), InstructionsList.end(), VarName->mText) != InstructionsList.end()) {
390-
std::cerr << "Le nom doit etre different des types, des instructions." << std::endl;
391-
exit(1);
392-
}
393-
394-
if (fonction != std::nullopt) {
395-
if (isArgument(fonction->FonctionName, VarName->mText)) {
396-
std::cerr << "Le nom de la variable ne peut pas etre le meme que celui d'un argument de la fonction : " << fonction->FonctionName << "." << std::endl;
397-
exit(1);
398-
}
399-
}
327+
ManageVariableName(fonction, VarName->mText);
400328

401329
if (CheckerOperateur("-").has_value()) {
402330
if (CheckerOperateur(">").has_value()) {
@@ -497,17 +425,8 @@ namespace FPL {
497425
}
498426
else if (VarName->mText == "fonction") {
499427
VarName = CheckerIdentifiant();
500-
501428
if (VarName.has_value()) {
502-
if (std::find(InstructionsList.begin(), InstructionsList.end(), VarName->mText) != InstructionsList.end()) {
503-
std::cerr << "Le nom doit etre different des types, des instructions." << std::endl;
504-
exit(1);
505-
}
506-
507-
if (fonction != std::nullopt && isArgument(fonction->FonctionName, VarName->mText)) {
508-
std::cerr << "Le nom de la variable ne peut pas etre le meme que celui d'un argument de la fonction : " << fonction->FonctionName << "." << std::endl;
509-
exit(1);
510-
}
429+
ManageVariableName(fonction, VarName->mText);
511430

512431
if (CheckerOperateur("-").has_value()) {
513432
if (CheckerOperateur(">").has_value()) {
@@ -588,16 +507,7 @@ namespace FPL {
588507
}
589508
}
590509
else {
591-
592-
if (std::find(InstructionsList.begin(), InstructionsList.end(), VarName->mText) != InstructionsList.end()) {
593-
std::cerr << "Le nom doit etre different des types, des instructions." << std::endl;
594-
exit(1);
595-
}
596-
597-
if (fonction != std::nullopt && isArgument(fonction->FonctionName, VarName->mText)) {
598-
std::cerr << "Le nom de la variable ne peut pas etre le meme que celui d'un argument de la fonction : " << fonction->FonctionName << "." << std::endl;
599-
exit(1);
600-
}
510+
ManageVariableName(fonction, VarName->mText);
601511

602512
if (CheckerOperateur("-").has_value()) {
603513
if (CheckerOperateur(">").has_value()) {
@@ -707,6 +617,97 @@ namespace FPL {
707617
return false;
708618
}
709619

620+
bool Parser::executeInputs(std::optional<FonctionDefinition>& fonction, std::string& VarName, Type& VarType, std::string option) {
621+
if (option == "with") {
622+
auto VarValue = CheckerValue();
623+
624+
if (VarValue->StatementType.mType == STRING) {
625+
std::replace(VarValue->StatementName.begin(), VarValue->StatementName.end(), '"', ' ');
626+
}
627+
628+
if (VarValue.has_value()) {
629+
std::cout << VarValue->StatementName << std::endl;
630+
} else {
631+
auto PossibleArgument = CheckerIdentifiant();
632+
if (PossibleArgument.has_value() && fonction != std::nullopt) {
633+
if (isArgument(fonction->FonctionName, PossibleArgument->mText)) {
634+
std::cout << mArguments[fonction->FonctionName][PossibleArgument->mText].ArgValue << std::endl;
635+
} else {
636+
std::cerr << "L'argument " << PossibleArgument->mText <<" est inexistant ." << std::endl;
637+
exit(1);
638+
}
639+
}
640+
}
641+
}
642+
643+
if (CheckerOperateur(";").has_value()) {
644+
VariableDefinition variable;
645+
variable.VariableName = VarName;
646+
variable.VariableType = Type(VarType.mName, VarType.mType);
647+
variable.IsGlobal = false;
648+
variable.HasReturnValue = false;
649+
variable.InFonction = false;
650+
if (fonction.has_value()) {
651+
variable.InFonction = true;
652+
}
653+
654+
if (VarType.mType == INT) {
655+
int v;
656+
std::cin >> v;
657+
variable.VariableValue = std::to_string(v);
658+
} else if (VarType.mType == DOUBLE) {
659+
double v;
660+
std::cin >> v;
661+
variable.VariableValue = std::to_string(v);
662+
} else if (VarType.mType == STRING) {
663+
std::string v;
664+
std::cin >> v;
665+
variable.VariableValue = v;
666+
}
667+
mVariables[variable.VariableName] = variable;
668+
return true;
669+
} else {
670+
std::cerr << "Merci de signifier la fin de la declaration de la variable avec ';'." << std::endl;
671+
exit(1);
672+
}
673+
return false;
674+
}
675+
676+
bool Parser::SaisirInstruction(std::optional<FonctionDefinition>& fonction) {
677+
auto VarType = CheckerType();
678+
679+
if (VarType.has_value()) {
680+
auto VarName = CheckerIdentifiant();
681+
if (VarName.has_value()) {
682+
ManageVariableName(fonction, VarName->mText);
683+
684+
if (CheckerOperateur("-").has_value()) {
685+
if (CheckerOperateur(">").has_value()) {
686+
if (executeInputs(fonction, VarName->mText, VarType.value(), "with")) {
687+
return true;
688+
}
689+
} else {
690+
std::cerr << "Vous devez utiliser les symboles '->' pour donner une valeur." << std::endl;
691+
exit(1);
692+
}
693+
} else {
694+
if (executeInputs(fonction, VarName->mText, VarType.value(), "without")) {
695+
return true;
696+
}
697+
std::cerr << "Vous devez utiliser les symboles '->' pour donner une valeur." << std::endl;
698+
exit(1);
699+
}
700+
} else {
701+
std::cerr << "Veuillez indiquer le nom de la variable." << std::endl;
702+
exit(1);
703+
}
704+
} else {
705+
std::cerr << "Veuillez donner le type de votre variable." << std::endl;
706+
exit(1);
707+
}
708+
return false;
709+
}
710+
710711
bool Parser::ChangerInstruction(std::optional<FonctionDefinition>& fonction) {
711712
auto VarName = CheckerIdentifiant();
712713
if (VarName.has_value()) {
@@ -869,7 +870,11 @@ namespace FPL {
869870
if (FonctionInstruction(parseStart)) {return true;} else {return false;}
870871
} else if (PeutEtreInstruction->mText == "appeler") {
871872
if (AppelerInstruction()) { return true; } else {return false;}
872-
} else {
873+
}
874+
else if (PeutEtreInstruction->mText == "saisir") {
875+
if (SaisirInstruction(fonction)) { return true; } else { return false; }
876+
}
877+
else {
873878
mCurrentToken = parseStart;
874879
}
875880
}

src/Parser.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@ namespace FPL {
3939
// Variable :
4040
std::map<std::string, VariableDefinition> mVariables;
4141
bool isVariable(std::string &name) const;
42+
bool executeInputs(std::optional<FonctionDefinition> &fonction, std::string& VarName, Type& VarType, std::string option);
43+
void ManageVariableName(std::optional<FonctionDefinition> &fonction, std::string name);
44+
void DefineVariable(std::optional<FonctionDefinition> &fonction, std::string &VarName, Type &VarType, std::string &VarValue);
4245
bool VariableInstruction(std::optional<FonctionDefinition>& fonction);
4346
bool ChangerInstruction(std::optional<FonctionDefinition>& fonction);
47+
bool SaisirInstruction(std::optional<FonctionDefinition>& fonction);
4448

4549
// Fonctions :
4650
std::map<std::string, FonctionDefinition> mFonctions;

0 commit comments

Comments
 (0)