Add comparison operators

This commit is contained in:
ala89 2023-11-22 16:03:27 +01:00
parent 574f73b637
commit fb261e26f9
2 changed files with 75 additions and 15 deletions

View File

@ -11,7 +11,7 @@ using namespace std;
/**
* Tokens definition
*/
enum class TokenType { Type, Identifier, Int, Plus, Minus, DoublePlus, DoubleMinus, DoubleEqual, Star, Slash, Percent, Equal, Semicolon, LParenthese, RParenthese, LCurlyBracket, RCurlyBracket, If, Else };
enum class TokenType { Type, Identifier, Int, Plus, Minus, DoublePlus, DoubleMinus, DoubleEqual, Lt, Gt, Leq, Geq, NotEqual, Not, Star, Slash, Percent, Equal, Semicolon, LParenthese, RParenthese, LCurlyBracket, RCurlyBracket, If, Else };
enum class Type { Int };
using TokenData = variant<int, string, Type>;
@ -42,23 +42,35 @@ ExprStatement ->
| Type ParIdentifier // Declaration
Expr ->
| T
| T + Expr
| T - Expr
Expr -> Comp
T ->
| U
| U * T
| U / T
| U % T
Comp ->
| Sum
| Sum == Comp
| Sum != Comp
| Sum < Comp
| Sum > Comp
| Sum <= Comp
| Sum >= Comp
U ->
| F
| - U
| + U
Sum ->
| Term
| Term + Sum
| Term - Sum
F ->
Term ->
| Unary
| Unary * Term
| Unary / Term
| Unary % Term
Unary ->
| Val
| - Unary
| + Unary
| ! Unary
Val ->
| Number
| ++ParIdentifier

View File

@ -35,6 +35,24 @@ void _debug_print_token(Token token) {
case TokenType::DoubleEqual:
cout << "==";
break;
case TokenType::Lt:
cout << "<";
break;
case TokenType::Gt:
cout << ">";
break;
case TokenType::Leq:
cout << "<=";
break;
case TokenType::Geq:
cout << ">=";
break;
case TokenType::NotEqual:
cout << "!=";
break;
case TokenType::Not:
cout << "!";
break;
case TokenType::Star:
cout << "*";
break;
@ -134,6 +152,36 @@ vector<Token> tokenize(vector<string> input, int initial_line) {
tokens.emplace_back(token);
j += 2;
}
else if (str.starts_with("<")) {
Token token = { .type = TokenType::Lt, .pos = pos };
tokens.emplace_back(token);
j += 1;
}
else if (str.starts_with(">")) {
Token token = { .type = TokenType::Gt, .pos = pos };
tokens.emplace_back(token);
j += 1;
}
else if (str.starts_with("<=")) {
Token token = { .type = TokenType::Leq, .pos = pos };
tokens.emplace_back(token);
j += 2;
}
else if (str.starts_with(">=")) {
Token token = { .type = TokenType::Geq, .pos = pos };
tokens.emplace_back(token);
j += 2;
}
else if (str.starts_with("!=")) {
Token token = { .type = TokenType::NotEqual, .pos = pos };
tokens.emplace_back(token);
j += 2;
}
else if (str.starts_with("!")) {
Token token = { .type = TokenType::Not, .pos = pos };
tokens.emplace_back(token);
j += 1;
}
else if (str.starts_with("+")) {
Token token = { .type = TokenType::Plus, .pos = pos };
tokens.emplace_back(token);