Skip to content

Commit 31825f3

Browse files
author
Cesar
committedFeb 13, 2023
refactoring the scanner into two classes
1 parent c9aecb6 commit 31825f3

File tree

5 files changed

+69
-48
lines changed

5 files changed

+69
-48
lines changed
 

‎CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ include_directories(/lib/error)
2121
add_executable(lox_test tests/lox_main_test.cpp)
2222
target_link_libraries(lox_test PRIVATE Catch2::Catch2WithMain)
2323

24-
add_executable(interpreter main.cpp lib/scanner.h lib/error/report.h)
24+
add_executable(interpreter main.cpp lib/parser/scanner.h lib/error/report.h lib/parser/tokens.h)

‎lib/scanner.h ‎lib/parser/scanner.h

+2-45
Original file line numberDiff line numberDiff line change
@@ -11,50 +11,8 @@
1111
#include <string>
1212
#include <vector>
1313
#include <sstream>
14-
#include "error/report.h"
15-
16-
17-
enum TokenType {
18-
LEFT_PAREN, RIGHT_PAREN, LEFT_BRACE, RIGHT_BRACE,
19-
COMMA, DOT, MINUS, PLUS, SEMICOLON, SLASH, STAR, BANG,
20-
BANG_EQUAL, EQUAL, EQUAL_EQUAL, GREATER, GREATER_EQUAL,
21-
LESS, LESS_EQUAL,
22-
23-
// Literals.
24-
IDENTIFIER, STRING, NUMBER,
25-
26-
// Keywords.
27-
AND, CLASS, ELSE, FALSE, FUN, FOR, IF, NIL, OR,
28-
PRINT, RETURN, SUPER, THIS, TRUE, VAR, WHILE, _EOF,
29-
};
30-
31-
class Object{};
32-
33-
class Token {
34-
private:
35-
TokenType type;
36-
std::string lexeme;
37-
Object literal;
38-
int line;
39-
40-
public:
41-
Token(TokenType _type, std::string _lexeme, Object _literal, int _line):
42-
type{_type},
43-
lexeme(_lexeme),
44-
literal(_literal),
45-
line{_line}
46-
{}
47-
48-
std::string toString(){
49-
std::stringstream ss;
50-
ss << "type" << " " << lexeme << " " << "literal";
51-
return ss.str();
52-
}
53-
54-
std::string getLexeme(){
55-
return lexeme;
56-
}
57-
};
14+
#include "../error/report.h"
15+
#include "tokens.h"
5816

5917
class Scanner {
6018
private:
@@ -159,7 +117,6 @@ class Scanner {
159117
};
160118

161119

162-
163120
std::vector<std::string> tokenizer(std::string basicString, char placeholder = ' '){
164121
auto size = basicString.size();
165122
std::vector<std::string> tokens;

‎lib/parser/tokens.h

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//
2+
// Created by Cesar Valdez on 13/02/2023.
3+
//
4+
5+
#ifndef INTERPRETER_TOKENS_H
6+
#define INTERPRETER_TOKENS_H
7+
#include <iostream>
8+
9+
enum TokenType {
10+
LEFT_PAREN, RIGHT_PAREN, LEFT_BRACE, RIGHT_BRACE,
11+
COMMA, DOT, MINUS, PLUS, SEMICOLON, SLASH, STAR, BANG,
12+
BANG_EQUAL, EQUAL, EQUAL_EQUAL, GREATER, GREATER_EQUAL,
13+
LESS, LESS_EQUAL,
14+
15+
// Literals.
16+
IDENTIFIER, STRING, NUMBER,
17+
18+
// Keywords.
19+
AND, CLASS, ELSE, FALSE, FUN, FOR, IF, NIL, OR,
20+
PRINT, RETURN, SUPER, THIS, TRUE, VAR, WHILE, _EOF,
21+
};
22+
23+
class Object{};
24+
25+
class Token {
26+
private:
27+
TokenType type;
28+
std::string lexeme;
29+
Object literal;
30+
int line;
31+
32+
public:
33+
Token(TokenType _type, std::string _lexeme, Object _literal, int _line):
34+
type{_type},
35+
lexeme(_lexeme),
36+
literal(_literal),
37+
line{_line}
38+
{}
39+
40+
std::string toString(){
41+
std::stringstream ss;
42+
ss << "type" << " " << lexeme << " " << "literal";
43+
return ss.str();
44+
}
45+
46+
std::string getLexeme(){
47+
return lexeme;
48+
}
49+
50+
TokenType getType(){
51+
return type;
52+
}
53+
};
54+
55+
#endif //INTERPRETER_TOKENS_H

‎main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <iostream>
22
#include <vector>
3-
#include "lib/scanner.h"
3+
#include "lib/parser/scanner.h"
44

55
using namespace std;
66

‎tests/lox_main_test.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//
44

55
#include <catch2/catch_test_macros.hpp>
6-
#include "../lib/scanner.h"
6+
#include "../lib/parser/scanner.h"
77
#include "catch2/matchers/catch_matchers_vector.hpp"
88

99
unsigned int Factorial( unsigned int number ) {
@@ -62,5 +62,14 @@ TEST_CASE("Testing Scanner multiple operators !*+-/=<> with comments input"){
6262
auto t2 = tokens[1];
6363
REQUIRE( t2.getLexeme() == "*" );
6464

65+
for(auto tk : tokens){
66+
67+
if(tk.getType() == TokenType::_EOF)
68+
break;
69+
70+
auto pos = code.find_first_of(tk.getLexeme());
71+
REQUIRE( pos != std::string::npos );
72+
}
73+
6574
REQUIRE( tokens.size() == 11 );
6675
}

0 commit comments

Comments
 (0)
Please sign in to comment.