Skip to content

Commit 72342b0

Browse files
committed
Added Expecter file to get access anywhere like Data.
1 parent 2a8f92e commit 72342b0

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

src/Essentials/Utils/Expecter.cpp

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
}

src/Essentials/Utils/Expecter.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <iostream>
5+
#include <string_view>
6+
#include <vector>
7+
#include <map>
8+
#include <optional>
9+
10+
#include "../Statement.h"
11+
#include "../Tokenizer.h"
12+
#include "../Types.h"
13+
#include "ErrorsCodeManagement.h"
14+
15+
#include "Data.h"
16+
17+
using namespace FPL;
18+
using namespace FPL::Tokenizer;
19+
20+
std::optional<Statement::Statement> ExpectValue(FPL::Data::Data& data);
21+
std::optional<Types::Types> ExpectType(FPL::Data::Data& data);
22+
std::optional<Token> ExpectOperator(FPL::Data::Data& data, std::string_view name = std::string());
23+
std::optional<Token> ExpectIdentifiant(FPL::Data::Data& data, std::string_view name = std::string());
24+
std::optional<std::string> ExpectConditionOperator(FPL::Data::Data& data);
25+
26+
bool ExpectEgalOperators(FPL::Data::Data& data);

0 commit comments

Comments
 (0)