Skip to content

Commit 72711ea

Browse files
committed
Conversions.
1 parent 1e1ba78 commit 72711ea

File tree

2 files changed

+70
-16
lines changed

2 files changed

+70
-16
lines changed

src/Parser.cpp

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace FPL {
44
Parser::Parser() {
5-
mTypes["vide"] = Type("vide", VOID);
5+
mTypes["vide"] = Type("vide", VIDE);
66
mTypes["entier"] = Type("entier", INT);
77
mTypes["decimal"] = Type("decimal", DOUBLE);
88
mTypes["texte"] = Type("texte", STRING);
@@ -22,7 +22,9 @@ namespace FPL {
2222
"decimal",
2323
"texte",
2424
"vide",
25-
"auto"
25+
"auto",
26+
"importer",
27+
"requete"
2628
};
2729
}
2830

@@ -71,10 +73,6 @@ namespace FPL {
7173
mVariables[variable.VariableName] = variable;
7274
}
7375

74-
75-
76-
77-
7876
bool Parser::ImportInstruction(std::optional<FonctionDefinition>& fonction) {
7977
auto fichierName = CheckerValue();
8078
if (fichierName.has_value()) {
@@ -236,7 +234,7 @@ namespace FPL {
236234
while (!CheckerOperateur(")").has_value()) {
237235
auto type = CheckerType();
238236

239-
if (type->mType == VOID) { // Si aucun paramètre ou l'utilisateur a utilisé l'argument 'vide' ou juste fermer.
237+
if (type->mType == VIDE) { // Si aucun paramètre ou l'utilisateur a utilisé l'argument 'vide' ou juste fermer.
240238
if (CheckerOperateur(")").has_value()) {
241239
break;
242240
} else {
@@ -849,6 +847,57 @@ namespace FPL {
849847
return false;
850848
}
851849

850+
bool Parser::ConversionInstruction(std::optional<FonctionDefinition>& fonction) {
851+
auto VarName = CheckerIdentifiant();
852+
if (VarName.has_value()) {
853+
if (isVariable(VarName->mText)) {
854+
auto var = mVariables[VarName->mText];
855+
auto NewType = CheckerType();
856+
if (NewType.has_value()) {
857+
if (CheckerOperateur(";").has_value()) {
858+
if (NewType->mType == INT && var.VariableType.mType == STRING) {
859+
try {
860+
int v = std::stoi(var.VariableValue);
861+
var.VariableValue = v;
862+
var.VariableType = Type("entier", INT);
863+
}
864+
catch (std::invalid_argument const& ex) {
865+
std::cout << "Ca ne peut pas devenir une valeur de type entier !" << "Erreur final : " << ex.what() << std::endl;
866+
}
867+
return true;
868+
} else if (NewType->mType == DOUBLE && var.VariableType.mType == STRING) {
869+
try {
870+
int v = std::stod(var.VariableValue);
871+
var.VariableValue = v;
872+
var.VariableType = Type("decimal", DOUBLE);
873+
}
874+
catch (std::invalid_argument const& ex) {
875+
std::cout << "Ca ne peut pas devenir une valeur de type entier !" << "Erreur final : " << ex.what() << std::endl;
876+
}
877+
return true;
878+
} else {
879+
std::cerr << "La valeur de la variable ne permet une conversion." << std::endl;
880+
exit(1);
881+
}
882+
} else {
883+
std::cerr << "Vous devez mettre le symbole ';' pour mettre fin a l'instruction." << std::endl;
884+
exit(1);
885+
}
886+
} else {
887+
std::cerr << "Vous devez specifier le nouveau type de votre variable." << std::endl;
888+
exit(1);
889+
}
890+
} else {
891+
std::cerr << "La variable est inexistante." << std::endl;
892+
exit(1);
893+
}
894+
} else {
895+
std::cerr << "Vous devez specifier le nom de votre variable." << std::endl;
896+
exit(1);
897+
}
898+
return false;
899+
}
900+
852901
bool Parser::ChangerInstruction(std::optional<FonctionDefinition>& fonction) {
853902
auto VarName = CheckerIdentifiant();
854903
if (VarName.has_value()) {
@@ -915,7 +964,7 @@ namespace FPL {
915964
exit(1);
916965
}
917966
} else {
918-
std::cerr << "Vous devez spécifier le nom de votre variable." << std::endl;
967+
std::cerr << "Vous devez specifier le nom de votre variable." << std::endl;
919968
exit(1);
920969
}
921970
return false;
@@ -998,8 +1047,8 @@ namespace FPL {
9981047
}
9991048

10001049
bool Parser::ManagerInstruction(std::optional<FonctionDefinition>& fonction) {
1001-
auto parseStart = mCurrentToken; // std::vector<Token>::iterator
1002-
auto PeutEtreInstruction = CheckerIdentifiant();
1050+
auto const parseStart = mCurrentToken; // std::vector<Token>::iterator
1051+
auto const PeutEtreInstruction = CheckerIdentifiant();
10031052
if (PeutEtreInstruction.has_value()) {
10041053
if (PeutEtreInstruction->mText == "envoyer") {
10051054
if (PrintInstruction(parseStart, fonction)) { return true; } else { return false; }
@@ -1010,13 +1059,15 @@ namespace FPL {
10101059
} else if (PeutEtreInstruction->mText == "definir") {
10111060
if (FonctionInstruction(parseStart)) {return true;} else {return false;}
10121061
} else if (PeutEtreInstruction->mText == "appeler") {
1013-
if (AppelerInstruction()) { return true; } else {return false;}
1062+
if (AppelerInstruction()) { return true; } else { return false; }
10141063
} else if (PeutEtreInstruction->mText == "saisir") {
10151064
if (SaisirInstruction(fonction)) { return true; } else { return false; }
10161065
} else if (PeutEtreInstruction->mText == "fichier") {
1017-
if (FichierInstruction(fonction)) {return true;} else {return false;}
1066+
if (FichierInstruction(fonction)) {return true;} else { return false; }
10181067
} else if (PeutEtreInstruction->mText == "importer") {
10191068
if (ImportInstruction(fonction)) { return true; } else { return false; }
1069+
} else if (PeutEtreInstruction->mText == "convertir") {
1070+
if (ConversionInstruction(fonction)) { return true; } else { return false; }
10201071
}
10211072
else {
10221073
mCurrentToken = parseStart;

src/Parser.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
#include <vector>
55
#include <string>
66
#include <string_view>
7+
#include <sstream>
78
#include <stdexcept>
9+
#include <algorithm>
810
#include <optional>
911
#include <map>
1012
#include <functional>
11-
#include <algorithm>
1213
#include <fstream>
1314

1415
#include "TokenBuilding.h"
@@ -40,12 +41,16 @@ namespace FPL {
4041
// Variable :
4142
std::map<std::string, VariableDefinition> mVariables;
4243
bool isVariable(std::string &name) const;
43-
bool executeInputs(std::optional<FonctionDefinition> &fonction, std::string& VarName, Type& VarType, const std::string& option);
44+
4445
void ManageVariableName(std::optional<FonctionDefinition> &fonction, std::string name);
4546
void DefineVariable(std::optional<FonctionDefinition>& fonction, std::string& VarName, Type& VarType, std::string& VarValue, std::optional<Statement>& ContentVarValue, bool isGlobal, bool HasReturnV);
4647
bool VariableInstruction(std::optional<FonctionDefinition>& fonction);
4748
bool ChangerInstruction(std::optional<FonctionDefinition>& fonction);
49+
bool ConversionInstruction(std::optional<FonctionDefinition>& fonction);
50+
51+
// Inputs:
4852
bool SaisirInstruction(std::optional<FonctionDefinition>& fonction);
53+
bool executeInputs(std::optional<FonctionDefinition> &fonction, std::string& VarName, Type& VarType, const std::string& option);
4954

5055
// Fonctions :
5156
std::map<std::string, FonctionDefinition> mFonctions;
@@ -60,13 +65,11 @@ namespace FPL {
6065
std::optional<ArgumentDefinition> getArgument(std::string &fonction, std::string &name);
6166

6267
// Fichiers :
63-
6468
bool FichierInstruction(std::optional<FonctionDefinition>& fonction);
6569

6670
// Imports :
6771
bool ImportInstruction(std::optional<FonctionDefinition>& fonction);
6872

69-
7073
// Utils :
7174
bool ManagerInstruction(std::optional<FonctionDefinition>& fonction);
7275
std::vector<Token>::iterator mCurrentToken;

0 commit comments

Comments
 (0)