Add comparison operators
This commit is contained in:
parent
574f73b637
commit
fb261e26f9
@ -11,7 +11,7 @@ using namespace std;
|
|||||||
/**
|
/**
|
||||||
* Tokens definition
|
* 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 };
|
enum class Type { Int };
|
||||||
|
|
||||||
using TokenData = variant<int, string, Type>;
|
using TokenData = variant<int, string, Type>;
|
||||||
@ -42,23 +42,35 @@ ExprStatement ->
|
|||||||
| Type ParIdentifier // Declaration
|
| Type ParIdentifier // Declaration
|
||||||
|
|
||||||
|
|
||||||
Expr ->
|
Expr -> Comp
|
||||||
| T
|
|
||||||
| T + Expr
|
|
||||||
| T - Expr
|
|
||||||
|
|
||||||
T ->
|
Comp ->
|
||||||
| U
|
| Sum
|
||||||
| U * T
|
| Sum == Comp
|
||||||
| U / T
|
| Sum != Comp
|
||||||
| U % T
|
| Sum < Comp
|
||||||
|
| Sum > Comp
|
||||||
|
| Sum <= Comp
|
||||||
|
| Sum >= Comp
|
||||||
|
|
||||||
U ->
|
Sum ->
|
||||||
| F
|
| Term
|
||||||
| - U
|
| Term + Sum
|
||||||
| + U
|
| Term - Sum
|
||||||
|
|
||||||
F ->
|
Term ->
|
||||||
|
| Unary
|
||||||
|
| Unary * Term
|
||||||
|
| Unary / Term
|
||||||
|
| Unary % Term
|
||||||
|
|
||||||
|
Unary ->
|
||||||
|
| Val
|
||||||
|
| - Unary
|
||||||
|
| + Unary
|
||||||
|
| ! Unary
|
||||||
|
|
||||||
|
Val ->
|
||||||
| Number
|
| Number
|
||||||
|
|
||||||
| ++ParIdentifier
|
| ++ParIdentifier
|
||||||
|
@ -35,6 +35,24 @@ void _debug_print_token(Token token) {
|
|||||||
case TokenType::DoubleEqual:
|
case TokenType::DoubleEqual:
|
||||||
cout << "==";
|
cout << "==";
|
||||||
break;
|
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:
|
case TokenType::Star:
|
||||||
cout << "*";
|
cout << "*";
|
||||||
break;
|
break;
|
||||||
@ -134,6 +152,36 @@ vector<Token> tokenize(vector<string> input, int initial_line) {
|
|||||||
tokens.emplace_back(token);
|
tokens.emplace_back(token);
|
||||||
j += 2;
|
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("+")) {
|
else if (str.starts_with("+")) {
|
||||||
Token token = { .type = TokenType::Plus, .pos = pos };
|
Token token = { .type = TokenType::Plus, .pos = pos };
|
||||||
tokens.emplace_back(token);
|
tokens.emplace_back(token);
|
||||||
|
Loading…
Reference in New Issue
Block a user