1
+ #include " Expecter.h"
2
+
3
+ using namespace FPL ;
4
+ using namespace FPL ::Data;
5
+ using namespace FPL ::Tokenizer;
6
+
7
+ std::optional<Token> ExpectIdentifiant (FPL::Data::Data &data, std::string_view name) {
8
+ if (data.current_token == data.end_token ) { return std::nullopt; }
9
+ if (data.current_token ->TokenType != Tokenizer::IDENTIFIANT) { return std::nullopt; }
10
+ if (!name.empty () && data.current_token ->TokenText != name) { return std::nullopt; }
11
+
12
+ auto returnToken = data.current_token ;
13
+ data.current_token ++;
14
+ return *returnToken;
15
+ }
16
+
17
+ std::optional<Token> ExpectOperator (FPL::Data::Data &data, std::string_view name) {
18
+ if (data.current_token == data.end_token ) { return std::nullopt; }
19
+ if (data.current_token ->TokenType != Tokenizer::OPERATEUR) { return std::nullopt; }
20
+ if (data.current_token ->TokenText != name && !name.empty ()) { return std::nullopt; }
21
+
22
+ auto returnToken = data.current_token ;
23
+ data.current_token ++;
24
+ return *returnToken;
25
+ }
26
+
27
+ std::optional<Types::Types> ExpectType (FPL::Data::Data &data) {
28
+ auto possibleType = ExpectIdentifiant (data);
29
+ if (!possibleType.has_value ()) { return std::nullopt; }
30
+
31
+ auto foundType = data.AllFPLTypes .find (possibleType->TokenText );
32
+ if (foundType == data.AllFPLTypes .end ()) {
33
+ data.current_token --;
34
+ return std::nullopt;
35
+ }
36
+
37
+ return foundType->second ;
38
+ }
39
+
40
+ std::optional<Statement::Statement> ExpectValue (FPL::Data::Data &data) {
41
+ std::optional<Statement::Statement> res;
42
+
43
+ if (data.current_token ->TokenType == FPL::Tokenizer::DECIMAL) {
44
+ Statement::Statement doubleLitteralStatement (
45
+ Statement::StatementKind::LITTERAL,
46
+ data.current_token ->TokenText ,
47
+ Types::Types (" decimal" , FPL::Types::DOUBLE)
48
+ );
49
+ res = doubleLitteralStatement;
50
+ } else if (data.current_token ->TokenType == Tokenizer::ENTIER) {
51
+ Statement::Statement integerLitteralStatement (
52
+ Statement::StatementKind::LITTERAL,
53
+ data.current_token ->TokenText ,
54
+ Types::Types (" entier" , Types::INT)
55
+ );
56
+ res = integerLitteralStatement;
57
+ } else if (data.current_token ->TokenType == Tokenizer::CHAINE_LITTERAL) {
58
+ Statement::Statement stringLitteralStatement (
59
+ Statement::StatementKind::LITTERAL,
60
+ data.current_token ->TokenText ,
61
+ Types::Types (" texte" , Types::STRING)
62
+ );
63
+ res = stringLitteralStatement;
64
+
65
+ } else {
66
+ return std::nullopt;
67
+ }
68
+
69
+ data.current_token ++;
70
+ return res;
71
+ }
72
+
73
+ bool ExpectEgalOperators (FPL::Data::Data &data) {
74
+ if (ExpectOperator (data, " -" ).has_value ()) {
75
+ if (ExpectOperator (data, " >" ).has_value ()) {
76
+ return true ;
77
+ }
78
+ }
79
+ return false ;
80
+ }
81
+
82
+ std::optional<std::string> ExpectConditionOperator (FPL::Data::Data& data) {
83
+ if (ExpectOperator (data, " >" ).has_value ()) {
84
+ if (ExpectOperator (data, " =" ).has_value ()) {
85
+ return " >=" ;
86
+ }
87
+ return " >" ;
88
+ } else if (ExpectOperator (data, " <" ).has_value ()) {
89
+ if (ExpectOperator (data, " =" ).has_value ()) {
90
+ return " <=" ;
91
+ }
92
+ return " <" ;
93
+ } else if (ExpectOperator (data, " =" ).has_value ()) {
94
+ return " =" ;
95
+ }
96
+ return std::nullopt;
97
+ }
0 commit comments