-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken.cpp
More file actions
executable file
·123 lines (113 loc) · 2.39 KB
/
token.cpp
File metadata and controls
executable file
·123 lines (113 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "token.h"
Token::Token(const Token &t)
{
str_ = t.str_;
type_ = t.type_;
line_ = t.line_;
col_ = t.col_;
}
size_t Token::get_line() const
{
return line_;
}
size_t Token::get_col() const
{
return col_;
}
int Token::get_int_value() const
{
return boost::lexical_cast<int>(str_.c_str());
}
double Token::get_double_value() const
{
return boost::lexical_cast<double>(str_.c_str());
}
const std::string& Token::get_string() const
{
return str_;
}
LexemeType Token::type() const
{
return type_;
}
bool Token::is_keyword() const
{
for(int i = 0; i < 33; ++i)
{
if(str_ == keywords[i])
return true;
}
return false;
}
void Token::print(std::ofstream &output)
{
if (line_ && col_)
output << std::endl << line_ << " " << col_ << " ";
switch (type_)
{
case id:
case begin_stmt:
case end_stmt:
case if_stmt:
case for_stmt:
case while_stmt:
case repeat_stmt:
case read_stmt:
case readln_stmt:
case write_stmt:
case writeln_stmt:
case break_stmt:
case continue_stmt:
case do_stmt:
case until_stmt:
case then_stmt:
case else_stmt:
if (is_keyword())
output << "keyword ";
else
output << "identifier ";
output << str_ << " " << str_ << std::endl;
break;
case inum:
output << "int " << str_ << " ";
if(str_.size() > 10)
output << "OUT OF RANGE";
else
output << get_int_value();
break;
case dnum:
output << "double " << str_ << " " << get_double_value();
break;
case right_square_paren:
case left_square_paren:
case right_round_paren:
case left_round_paren:
case colon:
case semicolon:
case dot:
case comma:
output << "divider " << str_ << " " << str_;
break;
case plus_op:
case minus_op:
case mul_op:
case div_op:
case lesser_equal:
case greater_equal:
case equal:
case not_equal:
case greater:
case lesser:
case assignment:
case or_op:
case xor_op:
case and_op:
case mod_op:
case not_op:
output << "op " << str_ << " " << str_;
break;
case strng:
output << "string " << str_ << " " << str_;
break;
}
}