-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokens.cpp
68 lines (67 loc) · 1.5 KB
/
tokens.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
// token.cpp
// calc variables
//
// Created by Kyra Thompson on 7/18/18.
// Copyright © 2018 Kyra Thompson. All rights reserved.
//
#include "std_lib_facilities.h"
#include "tokens.h"
Token Token_stream::popback() {
Token t = buffer.back();
buffer.pop_back();
return t;
}
void Token_stream::putback(Token t){
buffer.push_back(t);
}
Token Token_stream::get(){
if(buffer.size() > 0){
Token t = popback();
return t;
}
char ch;
cin >> ch;
switch(ch) {
case print:
case quit:
case'(':
case'+':
case ')':
case'-':
case'*':
case '/':
case '%':
case '=':
case power:
return Token{ch};
case'0': case'1': case'2' :case'3': case'4':
case'5': case '6': case'7': case'8': case '9': case '.':
{
cin.putback(ch);
double val;
cin >> val;
return Token{number, val};
}
default:
if(isalpha(ch)){
string s;
s+=ch;
while(cin.get(ch) && (isalpha(ch) || isdigit(ch))) s+=ch;
cin.putback(ch);
return Token{name,s};
}
return Token{invalid, double(ch)};
}
return Token{quit};
}
void Token_stream::ignore(char c){
while(buffer.size()>0){
Token t = popback();
if (t.kind == c) return;
}
char ch = 0;
while(cin >> ch){
if(ch == c) return;
}
}