Update README.md
[miniREPL.git] / Sources / token.cpp
blobdadd5e16e484ed91c13ae914d1cf7f7bd4ccf1f7
1 /*
2 * miniREPL
3 * https://github.com/vrmiguel/minirepl
5 * Copyright (c) 2020 Vinícius R. Miguel <vinicius.miguel at unifesp.br>
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
26 #include "Headers/token.h"
28 std::ostream& operator<<(std::ostream &strm, const Token &tok)
30 string vartype;
31 switch (tok.var_type)
33 case INTEGER:
34 vartype = "INTEGER";
35 break;
37 case PLUS:
38 vartype = "PLUS";
39 break;
41 case MINUS:
42 vartype = "MINUS";
43 break;
45 case SPACE:
46 vartype = "SPACE"; // TODO: SPACE is not getting printed
47 return strm << "Token(" << vartype << ", " << "\' \'" << ")";
48 break;
50 case MULT:
51 vartype = "MULT";
52 break;
54 case DIV:
55 vartype = "DIV";
56 break;
58 case EOL:
59 vartype = "EOL";
60 break;
62 case EOFile:
63 vartype = "EOF";
64 break;
66 case STRING:
67 vartype = "STRING";
68 break;
70 default:
71 vartype = "UNKNOWN"; // Control really shouldn't reach this point
72 break;
74 return strm << "Token(" << vartype << ", " << tok.var_value << ")";
77 bool operator==(const Token t1, const Token t2)
79 return t1.var_type == t2.var_type && t1.var_value == t2.var_value;
82 bool contains(vector<Token> tokens, Token t)
84 return std::count(tokens.begin(), tokens.end(), t);
87 inline bool is_valid(int var_type)
89 return (var_type > -1) && (var_type < TOKEN_TYPE_COUNT);
92 Token::Token(int var_type, string var_value)
94 if (is_valid(var_type))
96 this->var_type = var_type;
97 this->var_value = var_value;
98 } else {
99 cerr << "The type" << var_type << "is not valid.";
100 std::exit(1);
104 Token::Token(int var_type, char var_value)
106 if (is_valid(var_type))
108 this->var_type = var_type;
109 this->var_value = string(1, var_value);
110 } else
112 cerr << "The type" << var_type << "is not valid.";
113 std::exit(1);