-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlexer.cpp
50 lines (43 loc) · 1.37 KB
/
lexer.cpp
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
#include "lexer.h"
const char number = '8';
// make a Token_stream that reads from istream
Token_stream::Token_stream(std::istream& is)
:is(is), full(false), buffer{0} {
}
// get a Token
Token Token_stream::get() {
if (full) { // do we already have a Token ready?
// remove Token from buffer
full = false;
return buffer;
}
char ch;
is >> ch; // note that >> skips whitespace (space, newline, tab, etc.)
switch (ch) {
case ';': // for "print"
case 'q': // for "quit"
case '(': case ')': case '+': case '-': case '*': case '/':
return Token{ch}; // let each character represent itself
case '.':
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9': {
is.putback(ch); // put digit back into the input stream
double val;
is >> val; // read a floating-point number
return Token{number, val};
}
default:
throw Lexer_error("Bad token");
}
}
// put a Token back
void Token_stream::putback(Token t) {
if (full)
throw Lexer_error("putback() into a full buffer");
buffer = t; // copy t to buffer
full = true; // buffer is now full
}
// discard Token in buffer
void Token_stream::ignore() {
full = false;
}