-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.cpp
122 lines (104 loc) · 2.51 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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#ifndef LEXER_CPP
#define LEXER_CPP
#include <iostream>
#include <vector>
#include "tokens.h"
#include "lexer.h"
#include "error.h"
using namespace std;
//advance to next character
void Lexer::advanceChar() {
lineIdx += 1;
if(lineIdx < line.size() && lineIdx >= 0) {
currChar = line[lineIdx];
} else {
currChar = '\0';
}
}
//constructor
Lexer::Lexer(string inputLine) {
lineIdx = -1;
currChar = '\0';
line = inputLine;
advanceChar();
}
//take input and create list of tokens
vector<Token> Lexer::tokenize() {
vector<Token> tokens;
while(currChar != '\0') {
//empty space
if(isspace(currChar)) {
advanceChar();
}
//number
else if(currChar == '.' || isdigit(currChar)) {
tokens.push_back(createNumber());
}
//variable
else if(isalpha(currChar)) {
tokens.push_back(createVariable());
}
//operators and parentheses
else if(currChar == '=' || currChar == '(' || currChar == ')'
|| currChar == '+' || currChar == '-' || currChar == '*' || currChar == '/') {
tokens.push_back(getOperatorOrParen());
advanceChar();
}
//error
else {
cout << "error: invalid character error ---> '" << currChar << '\'' << endl;
throw MathInterpreterError();
}
}
return tokens;
}
//create a number token
Token Lexer::createNumber() {
int decPointCounter = 0;
string number = "";
number += currChar;
advanceChar();
while(currChar != '\0' && (currChar == '.' || isdigit(currChar))) {
if(currChar == '.') {
decPointCounter += 1;
if(decPointCounter > 1){break;}
}
number += currChar;
advanceChar();
}
if(isalpha(currChar)) {
cout << "error: alphabetic characters cannot be in number, variables must begin with alphabetic character." << endl;
cout << '\'' << currChar << "' <--- ";
throw MathInterpreterError();
}
if(number[0] == '.')
number = '0' + number;
if(number[number.size()-1] == '.')
number += '0';
return Token(T_Number, number);
}
//create operator or separator tokens
Token Lexer::getOperatorOrParen() {
switch(currChar) {
case '=': return Token(T_Equal);
case '(': return Token(T_OpenParen);
case ')': return Token(T_ClosedParen);
case '+': return Token(T_Plus);
case '-': return Token(T_Minus);
case '*': return Token(T_Multiply);
case '/': return Token(T_Divide);
default: return Token(T_UNKNOWN);
}
}
//create variable token
Token Lexer::createVariable() {
string var = "";
var += currChar;
advanceChar();
while(currChar != '\0' && isalnum(currChar)) {
var += currChar;
advanceChar();
}
return Token(T_Variable, var);
}
#endif