-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.cpp
More file actions
72 lines (68 loc) · 2.37 KB
/
scanner.cpp
File metadata and controls
72 lines (68 loc) · 2.37 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
#include <scanner.hpp>
#include <sstream>
#include <map>
#include <error.hpp>
using namespace std;
// this map contains all the single character operator and their token type
static map<char, Type> lookup{{'>', Type::Move}, {'+', Type::Copy}, {'[', Type::ZeroStart}, {']', Type::ZeroEnd}, {'{', Type::EmptyStart}, {'}', Type::EmptyEnd}};
// generates the list of token from the code
vector<Token> scan(const string &code)
{
int line{1}, column{1}, pointer{0}, end{(int)code.length()};
// string stream to contain data for error reporting
stringstream ss,msg;
msg << "unexpected character";
bool err = false;
vector<Token> tokens;
// lambda expressions to common functions
auto clear = [&]() {
stringstream().swap(ss);
};
auto addToken = [&](Type type) {
string value = ss.str();
tokens.push_back(Token{type, value, line, column - (int)value.length() + 1});
clear();
};
auto isAlphabet = [&](int i) -> bool {
return (i >= 0 && i < end) && ((code[i] >= 'a' && code[i] <= 'z') || (code[i] >= 'A' && code[i] <= 'Z'));
};
auto isNumber = [&](int i) -> bool {
return (i >= 0 && i < end) && (code[i] >= '0' && code[i] <= '9');
};
// loops till we ran out of character
while (pointer < end)
{
ss << code[pointer];
// check for different conditions and tokens accordingly
if (isAlphabet(pointer) && !isAlphabet(pointer + 1))
addToken(Type::Stack);
else if (isNumber(pointer) && !isNumber(pointer + 1))
addToken(Type::Number);
else if (!isNumber(pointer) && !isAlphabet(pointer))
{
// search in lookup table
auto iter = lookup.find(code[pointer]);
if (iter != lookup.end())
addToken(iter->second);
// newline condition
else if (code[pointer] == '\n')
{
line++;
column = 0;
}
// the character is unexpected
else if (code[pointer] != ' ' && code[pointer] != '\t')
{
msg << '\n' << code[pointer] << " at line:" << line << " column:" << column;
err = true;
}
clear();
}
column++;
pointer++;
}
// finally, if there was any error
if (err)
error(msg.str());
return tokens;
}