-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken_stream.cpp
More file actions
90 lines (75 loc) · 1.34 KB
/
token_stream.cpp
File metadata and controls
90 lines (75 loc) · 1.34 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
#include "token_stream.h"
#include <iostream>
#include "error.h"
Token_stream ts;
Token Token_stream::get ()
{
if (full)
{
full = false;
return buffer;
}
char ch;
if (!std::cin.get(ch)) {
return Token{quit};
}
switch (ch)
{
case '(':
case ')':
case '+':
case '-':
case '*':
case '/':
case '%':
case ';':
case '=':
return Token{ ch };
case '\n':
return Token{print};
case 'q':
return Token{quit};
case ' ': case '\t': case '\r':
return get();
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
{
std::cin.putback(ch);
double val;
std::cin >> val;
return Token{ number, val };
}
default:
if (isalpha(ch))
{
std::string s = "";
s += ch;
while (std::cin.get(ch) &&
(isalpha(ch) || isdigit(ch) || ch == '_'))
s += ch;
std::cin.putback(ch);
if (s == declkey) return Token{ let };
return Token{ name, s };
}
error("Bad token");
}
}
void Token_stream::putback (Token t)
{
if (full)
error("putback() into a full buffer");
buffer = t;
full = true;
}
void Token_stream::ignore (char c)
{
if (full && c == buffer.kind)
{
full = false;
return;
}
full = false;
char ch;
while (std::cin >> ch)
if (ch == c) return;
}