-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvreme.cpp
More file actions
137 lines (123 loc) · 2.75 KB
/
vreme.cpp
File metadata and controls
137 lines (123 loc) · 2.75 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* Copyright (C) 2017-2019 Miloš Stojanović
*
* SPDX-License-Identifier: MIT
*/
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include "Output.h"
#include "Parser.h"
#include "Time.h"
#include "Token.h"
using namespace std;
bool isOp(const char c)
{
switch (c) {
case '+':
case '-':
return true;
default:
return false;
}
}
bool isCommand(const string& token)
{
if (token == "exit" ||
token == "clear" ||
token == "help")
return true;
else
return false;
}
TOKEN_TYPE getTokenType(string token)
{
if (isOp(token[0]))
return OP;
else if (isCommand(token))
return COMMAND;
else if (Time::isTime(token))
return TIME;
else
return NONE;
}
int lex(vector<Token>& tokens, const char token_group[])
{
string token_string;
const char *token_start = token_group;
size_t token_length = 0;
bool op_encountered = false;
if (!token_group)
return EXIT_FAILURE;
do {
if (isOp(*token_group) || op_encountered || *token_group == '\0') {
op_encountered = isOp(*token_group);
if (token_length > 0) {
token_string = string(token_start, token_length);
TOKEN_TYPE type = getTokenType(token_string);
if (type != NONE) {
tokens.emplace_back(token_string, type);
} else {
error_output << "* Error: Unknown format" << '\n';
error_output << print_format_help;
error_output << "* Try '" << Bold("help") << "' for more info" << '\n';
return EXIT_FAILURE;
}
}
token_start += token_length;
token_length = 0;
}
token_length++;
} while (*token_group++);
return EXIT_SUCCESS;
}
int main(int argc, char *argv[])
{
bool lex_enabled = false;
if (argc > 1) {
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--clean") == 0) {
error_output.clean_enabled = true;
} else if (strcmp(argv[i], "--lex") == 0) {
lex_enabled = true;
} else {
error_output << "Unknown argument: " << argv[i] << '\n';
return EXIT_FAILURE;
}
}
}
if (!error_output.clean_enabled)
cout << "Time calculation:" << '\n';
int exit_status = EXIT_SUCCESS;
Parser parser;
// Iterate over user inputs (full lines)
while (true) {
string line;
getline(cin, line);
if (cin.eof())
return exit_status;
exit_status = EXIT_SUCCESS;
istringstream ss(line);
vector<Token> tokens;
// Iterate over whitespace-separated strings
while (!ss.eof() && exit_status != EXIT_FAILURE) {
string token_group;
ss >> token_group;
exit_status = lex(tokens, token_group.c_str());
}
for (const Token& token : tokens) {
if (lex_enabled) {
cout << ' ' << token.getStr();
} else {
if (exit_status == EXIT_FAILURE)
break;
exit_status = parser.parse(token);
}
}
if (lex_enabled && !tokens.empty())
cout << '\n';
}
}