Skip to content

Commit b377c54

Browse files
committed
Fixs, Opti, Amélioration de l'instruction renvoyer dans une fonction.
1 parent eda322f commit b377c54

File tree

1 file changed

+79
-4
lines changed

1 file changed

+79
-4
lines changed

src/Parser.cpp

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ namespace FPL {
164164
fonction.HasReturn = true;
165165
fonction.ReturnValue = v->StatementName;
166166
fonction.ReturnType = v->StatementType;
167+
fonction.ReturnIsIdentfiant = false;
167168

168169
if (CheckerIdentifiant().has_value() || CheckerValue().has_value() || CheckerType().has_value()) {
169170
std::cerr << "Vous ne pouvez plus rajouter d'instruction ou autre apres l'instruction 'renvoyer'." << std::endl;
@@ -175,6 +176,24 @@ namespace FPL {
175176
exit(1);
176177
}
177178
} else {
179+
auto va = CheckerIdentifiant();
180+
if (va.has_value()) {
181+
if (CheckerOperateur(";").has_value()) {
182+
fonction.HasReturn = true;
183+
fonction.ReturnIsIdentfiant = true;
184+
fonction.ReturnIdentifantValue = va->mText;
185+
186+
if (CheckerIdentifiant().has_value() || CheckerValue().has_value() || CheckerType().has_value()) {
187+
std::cerr << "Vous ne pouvez plus rajouter d'instruction ou autre apres l'instruction 'renvoyer'." << std::endl;
188+
exit(1);
189+
}
190+
break;
191+
} else {
192+
std::cerr << "Vous devez utiliser la ';' pour mettre fin a l'instruction." << std::endl;
193+
exit(1);
194+
}
195+
}
196+
178197
std::cerr << "Veuillez indiquer la valeur de retour de la fonction '" << fonction.FonctionName << "'." << std::endl;
179198
exit(1);
180199
}
@@ -289,7 +308,7 @@ namespace FPL {
289308

290309
std::vector<std::string> toErase;
291310
for(auto const &var: mVariables) {
292-
if(var.second.InFonction && !var.second.IsGlobal) {
311+
if(var.second.InFonction && !var.second.IsGlobal && !fonction.ReturnIsIdentfiant && fonction.ReturnIdentifantValue != var.second.VariableName) {
293312
toErase.push_back(var.first);
294313
}
295314
}
@@ -423,7 +442,7 @@ namespace FPL {
423442
if (isFonction(PossibleFonction->mText)) {
424443
FonctionDefinition f = mFonctions[PossibleFonction->mText];
425444

426-
if (VarType->mType != f.ReturnType.mType && VarType->mType != AUTO) {
445+
if (VarType->mType != f.ReturnType.mType && VarType->mType != AUTO && !f.ReturnIsIdentfiant) {
427446
std::cerr << "Vous devez utiliser le meme type que le type de retour de la fonction." << std::endl;
428447
exit(1);
429448
}
@@ -440,7 +459,63 @@ namespace FPL {
440459

441460
executeFonctionContent(f, PossibleFonction->mText);
442461

443-
DefineVariable(fonction, VarName->mText, VarType.value(), f.ReturnValue, (std::optional<Statement> &) std::nullopt, false, true);
462+
if (!f.ReturnIsIdentfiant) {
463+
DefineVariable(fonction, VarName->mText, VarType.value(), f.ReturnValue, (std::optional<Statement> &) std::nullopt, false, true);
464+
} else {
465+
if (isVariable(f.ReturnIdentifantValue)) {
466+
VariableDefinition var = mVariables[f.ReturnIdentifantValue];
467+
468+
if (var.VariableType.mType != VarType->mType && VarType->mType != AUTO) {
469+
std::cerr << "Vous devez donner une valeur a la variable qui correspond au type." << std::endl;
470+
exit(1);
471+
}
472+
473+
VariableDefinition variable;
474+
variable.VariableName = VarName->mText;
475+
if (VarType->mType == AUTO) {
476+
variable.VariableType = var.VariableType;
477+
} else {
478+
variable.VariableType = Type(VarType->mName, VarType->mType);
479+
}
480+
variable.IsGlobal = false;
481+
variable.HasReturnValue = true;
482+
variable.InFonction = false;
483+
if (fonction.has_value()) {
484+
variable.InFonction = true;
485+
}
486+
variable.VariableValue = var.VariableValue;
487+
mVariables[variable.VariableName] = variable;
488+
auto it = mVariables.find(f.ReturnIdentifantValue);
489+
mVariables.erase(it);
490+
return true;
491+
} else if (isArgument(f.FonctionName, f.ReturnIdentifantValue)) {
492+
ArgumentDefinition arg = mArguments[f.FonctionName][f.ReturnIdentifantValue];
493+
494+
if (arg.ArgType.mType != VarType->mType && VarType->mType != AUTO) {
495+
std::cerr << "Vous devez donner une valeur a la variable qui correspond au type." << std::endl;
496+
exit(1);
497+
}
498+
499+
VariableDefinition variable;
500+
variable.VariableName = VarName->mText;
501+
if (VarType->mType == AUTO) {
502+
variable.VariableType = arg.ArgType;
503+
} else {
504+
variable.VariableType = Type(VarType->mName, VarType->mType);
505+
}
506+
variable.IsGlobal = false;
507+
variable.HasReturnValue = true;
508+
variable.InFonction = false;
509+
if (fonction.has_value()) {
510+
variable.InFonction = true;
511+
}
512+
variable.VariableValue = arg.ArgValue ;
513+
mVariables[variable.VariableName] = variable;
514+
return true;
515+
}
516+
std::cerr << "Votre retour de la fonction " << f.FonctionName << " n'est pas une variable ou un argument." << std::endl;
517+
exit(1);
518+
}
444519
return true;
445520
} else if (isVariable(PossibleFonction->mText)) {
446521
VariableDefinition var = mVariables[PossibleFonction->mText];
@@ -791,7 +866,7 @@ namespace FPL {
791866
return true;
792867
}
793868
}
794-
std::cerr << "Vous devez specifier une argument d'une fonction ou une variable." << std::endl;
869+
std::cerr << "Vous devez specifier un argument d'une fonction ou une variable." << std::endl;
795870
exit(1);
796871
} else {
797872
std::cerr << "Vous devez mettre le symbole ';' pour mettre fin a l'instruction." << std::endl;

0 commit comments

Comments
 (0)