-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParser.h
87 lines (74 loc) · 2.06 KB
/
Parser.h
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
#ifndef PARSER_H
#define PARSER_H
#include <cstdint>
#include <string>
#include <vector>
#include "Expr.h"
namespace lx {
class Env;
class Tokenizer
{
public:
static std::vector<std::string> run (const std::string& str);
};
class Parser
{
private:
static bool is_digit (char c)
{
if (c >= '0' && c <= '9') {
return true;
} else {
return false;
}
}
// FIXME
static bool is_integer (const std::string& token)
{
bool ret = false;
uint32_t size = token.size();
if (is_digit(token[0]) ||
((token[0] == '+' || token[0] == '-') && size > 1)) {
ret = true;
for (uint32_t i = 1; i < size; i++) {
if (!is_digit(token[i])) {
ret = false;
break;
}
}
}
return ret;
}
// FIXME
static bool is_float (const std::string& token)
{
bool ret = false;
uint32_t size = token.size();
if (is_digit(token[0]) ||
((token[0] == '+' || token[0] == '-') && size > 1)) {
ret = true;
bool has_dot = false;
for (uint32_t i = 1; i < size; i++) {
if (token[i] == '.') {
if (!has_dot) {
has_dot = true;
continue;
} else {
ret = false;
break;
}
}
if (!is_digit(token[i])) {
ret = false;
break;
}
}
}
return ret;
}
public:
static Expr* run (std::vector<std::string>::const_iterator& iter);
static Expr* run (const std::vector<std::string>& tokens);
};
}
#endif // PARSER_H