-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbc_tokenizer.h
More file actions
57 lines (44 loc) · 1.14 KB
/
Copy pathbc_tokenizer.h
File metadata and controls
57 lines (44 loc) · 1.14 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
// A simple arbitrary precision library and interactive text program
// Copyright © 2020, Dave McKellar
// Mozilla Public Licensed
#ifndef BC_TOKENIZER_H
#define BC_TOKENIZER_H
#include <string>
struct Token {
typedef enum {
T_EOL,
T_SPACE,
T_MINUS,
T_NUMBER,
T_WORD,
T_PUNCT,
T_OTHER
} TokType;
TokType type;
std::string string;
Token() { type = T_OTHER; }
std::string toString() const;
bool isOpenBracket() const { return type == T_PUNCT && string == "("; }
bool isCloseBracket() const { return type == T_PUNCT && string == ")"; }
bool isBang() const { return type == T_PUNCT && string == "!"; }
};
class Tokenizer {
typedef enum { EXPECT_ANY, EXPECT_OPERATOR } Expect;
std::string mLine;
int mPos;
Token mUnget;
bool isEol();
int getChar();
void ungetChar();
public:
Tokenizer(const char* line) {
mLine = line;
mPos = 0;
}
static Token::TokType type(const int);
static std::string typeStr(const Token::TokType);
Token getToken(const Expect expect = EXPECT_ANY);
Token getOperatorToken() { return getToken(EXPECT_OPERATOR); }
void ungetToken(const Token& tok);
};
#endif