Add return keyword

This commit is contained in:
ala89 2023-12-26 12:46:05 +01:00
parent 69276515b1
commit 6d608f00cd
3 changed files with 10 additions and 4 deletions

View File

@ -16,11 +16,11 @@ enum class TokenType {
Identifier, Litteral, Plus, Minus, DoublePlus, DoubleMinus, DoubleEqual, Land, Identifier, Litteral, Plus, Minus, DoublePlus, DoubleMinus, DoubleEqual, Land,
Lor, Lt, Gt, Leq, Geq, NotEqual, Not, Star, Slash, Percent, Equal, Semicolon, Lor, Lt, Gt, Leq, Geq, NotEqual, Not, Star, Slash, Percent, Equal, Semicolon,
LParenthese, RParenthese, LCurlyBracket, RCurlyBracket, If, Else, While, For, LParenthese, RParenthese, LCurlyBracket, RCurlyBracket, If, Else, While, For,
Break, Continue, Comma Break, Continue, Return, Comma
}; };
enum class Type { enum class Type {
Int, Double Int, Double, Void
}; };
using TokenData = variant<int, double, string, Type>; using TokenData = variant<int, double, string, Type>;

View File

@ -17,6 +17,7 @@ vector<tuple<string, TokenType>> simpleTokens = {
{ "for", TokenType::For }, { "for", TokenType::For },
{ "break", TokenType::Break }, { "break", TokenType::Break },
{ "continue", TokenType::Continue }, { "continue", TokenType::Continue },
{ "return", TokenType::Return },
{ "++", TokenType::DoublePlus }, { "++", TokenType::DoublePlus },
{ "--", TokenType::DoubleMinus }, { "--", TokenType::DoubleMinus },
{ "==", TokenType::DoubleEqual }, { "==", TokenType::DoubleEqual },
@ -46,6 +47,7 @@ string _debug_get_type_name(Type type) {
switch (type) { switch (type) {
case Type::Int: return "INT"; case Type::Int: return "INT";
case Type::Double: return "DOUBLE"; case Type::Double: return "DOUBLE";
case Type::Void: return "VOID";
default: return "Unknown"; default: return "Unknown";
} }
} }
@ -82,6 +84,7 @@ string _debug_get_token_type_name(TokenType type) {
case TokenType::For: return "For"; case TokenType::For: return "For";
case TokenType::Break: return "Break"; case TokenType::Break: return "Break";
case TokenType::Continue: return "Continue"; case TokenType::Continue: return "Continue";
case TokenType::Return: return "Return";
case TokenType::Comma: return "Comma"; case TokenType::Comma: return "Comma";
default: return "Unknown"; default: return "Unknown";
} }
@ -184,6 +187,9 @@ void _debug_print_token(Token token) {
case TokenType::Continue: case TokenType::Continue:
cout << "Continue"; cout << "Continue";
break; break;
case TokenType::Return:
cout << "Return";
break;
case TokenType::Comma: case TokenType::Comma:
cout << "Comma"; cout << "Comma";
break; break;

View File

@ -11,7 +11,7 @@ int main() {
/* All tokens */ /* All tokens */
vector<string> inputs = { vector<string> inputs = {
"int", "a", "=", "x", "++", "--", "==", "&&", "||", "<", ">", "<=", ">=", "!=", "!", "*", "/", "%", "=", ";", "(", ")", "{", "}", "if", "else", "while", "for", "break", "continue", "," "int", "a", "=", "x", "++", "--", "==", "&&", "||", "<", ">", "<=", ">=", "!=", "!", "*", "/", "%", "=", ";", "(", ")", "{", "}", "if", "else", "while", "for", "break", "continue", "return", ","
}; };
vector<TokenType> expectedTypes = { vector<TokenType> expectedTypes = {
@ -21,7 +21,7 @@ int main() {
TokenType::Not, TokenType::Star, TokenType::Slash, TokenType::Percent, TokenType::Equal, TokenType::Not, TokenType::Star, TokenType::Slash, TokenType::Percent, TokenType::Equal,
TokenType::Semicolon, TokenType::LParenthese, TokenType::RParenthese, TokenType::LCurlyBracket, TokenType::Semicolon, TokenType::LParenthese, TokenType::RParenthese, TokenType::LCurlyBracket,
TokenType::RCurlyBracket, TokenType::If, TokenType::Else, TokenType::While, TokenType::For, TokenType::RCurlyBracket, TokenType::If, TokenType::Else, TokenType::While, TokenType::For,
TokenType::Break, TokenType::Continue, TokenType::Comma TokenType::Break, TokenType::Continue, TokenType::Return, TokenType::Comma
}; };
for (size_t i = 0; i < inputs.size(); i++) { for (size_t i = 0; i < inputs.size(); i++) {