-
Notifications
You must be signed in to change notification settings - Fork 2
/
lexer.cpp
45 lines (39 loc) · 1.16 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
#include <assert.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <vector>
#include "lexer.h"
#include "pool.h"
namespace rift {
Token Lexer::identOrKeyword(char start, std::istream & input) {
std::string x(1, start);
while (isNumber(input.peek()) or isLetter(input.peek()))
x += input.get();
if (x == "function")
return Token(Token::Type::kwFunction);
else if (x == "if")
return Token(Token::Type::kwIf);
else if (x == "else")
return Token(Token::Type::kwElse);
else if (x == "while")
return Token(Token::Type::kwWhile);
else if (x == "c")
return Token(Token::Type::kwC);
else if (x == "eval")
return Token(Token::Type::kwEval);
else if (x == "length")
return Token(Token::Type::kwLength);
else if (x == "type")
return Token(Token::Type::kwType);
else
return Token(Token::Type::ident, Pool::addToPool(x));
}
Token Lexer::stringLiteral(std::istream & input) {
std::string x;
while (input.peek() != '"')
x += input.get();
input.get(); // closing "
return Token(Token::Type::character, Pool::addToPool(x));
}
}