-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
128 lines (115 loc) · 4.09 KB
/
main.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
123
124
125
126
127
128
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <chrono>
#include "Lexer.h"
class Timer
{
public:
Timer(const char* name) : m_Name(name), m_Stopped(false)
{
m_StartTimepoint = std::chrono::high_resolution_clock::now();
}
~Timer()
{
if (!m_Stopped)
Stop();
}
void Stop()
{
auto endTimepoint = std::chrono::high_resolution_clock::now();
auto start = std::chrono::time_point_cast<std::chrono::microseconds>(m_StartTimepoint).time_since_epoch().count();
auto end = std::chrono::time_point_cast<std::chrono::microseconds>(endTimepoint).time_since_epoch().count();
m_Stopped = true;
auto duration = end - start;
double ms = duration * 0.001;
std::cout << m_Name << ": " << duration << "us (" << ms << "ms)\n";
}
private:
const char* m_Name;
bool m_Stopped;
std::chrono::time_point<std::chrono::high_resolution_clock> m_StartTimepoint;
};
bool ReadFile(const std::string& strFile, std::string& strOutReadContent)
{
std::ifstream oFile(strFile); //open the input file
if (!oFile.is_open())
return false;
std::stringstream strStream;
strStream << oFile.rdbuf(); //read the file
strOutReadContent = strStream.str();
return true;
}
int main()
{
Lexer oLexer;
oLexer.m_bEnableError = false;
oLexer.define("TOKEN_NEW_LINE","\n", true);
oLexer.define("TOKEN_WS","[ \t\r\b]+", true);
oLexer.define("TOKEN_NUMBER","([0-9]+)|([0-9]+.[0-9]+)");
// oLexer.define(TOKEN_INT,"[0-9]+");
oLexer.define_area("TOKEN_STRING",'"', '"');
oLexer.define("TOKEN_PLUS", "\\+");
oLexer.define("TOKEN_PLUS_PLUS", "\\+\\+");
oLexer.define("TOKEN_MINUS", "-");
oLexer.define("TOKEN_MINUS_MINUS", "--");
oLexer.define("TOKEN_DOUBLE_COLON","::");
oLexer.define("TOKEN_COLON",":");
oLexer.define("TOKEN_LEFT_PAREN","\\(");
oLexer.define("TOKEN_RIGHT_PAREN","\\)");
oLexer.define("TOKEN_LEFT_BRACE","\\{");
oLexer.define("TOKEN_RIGHT_BRACE","\\}");
oLexer.define("TOKEN_LEFT_BRACKET","\\[");
oLexer.define("TOKEN_RIGHT_BRACKET","\\]");
oLexer.define("TOKEN_DOT", "\\.");
oLexer.define("TOKEN_SEMICOLON",";");
oLexer.define("TOKEN_SLASH","/");
oLexer.define("TOKEN_COMMA",",");
oLexer.define("TOKEN_STAR","\\*");
oLexer.define("TOKEN_GREATER",">");
oLexer.define("TOKEN_GREATER_EQUAL",">=");
oLexer.define("TOKEN_LESS", "<");
oLexer.define("TOKEN_LESS_EQUAL", "<=");
oLexer.define("TOKEN_NIL", "nil");
oLexer.define("TOKEN_OR", "\\|\\|");
oLexer.define("TOKEN_AND", "&&");
oLexer.define("TOKEN_PRINT", "print");
oLexer.define("TOKEN_SUPER", "super");
oLexer.define("TOKEN_THIS", "this");
oLexer.define("TOKEN_TRUE", "true");
oLexer.define("TOKEN_FALSE", "false");
oLexer.define("TOKEN_FOR", "for");
oLexer.define("TOKEN_FUN", "func");
oLexer.define("TOKEN_IF", "if");
oLexer.define("TOKEN_IMPORT", "import");
oLexer.define("TOKEN_RETURN", "return");
oLexer.define("TOKEN_VAR", "var");
oLexer.define("TOKEN_DOUBLE_DOT_EQUAL", ":=");
oLexer.define("TOKEN_WHILE", "while");
oLexer.define("TOKEN_SWITCH", "switch");
oLexer.define("TOKEN_BANG", "!");
oLexer.define("TOKEN_BANG_EQUAL", "!=");
oLexer.define("TOKEN_CLASS", "class");
oLexer.define("TOKEN_ELSE", "else");
oLexer.define("TOKEN_IS", "is");
oLexer.define("TOKEN_EQUAL", "=");
oLexer.define("TOKEN_EQUAL_EQUAL", "==");
oLexer.define("TOKEN_SHEBANG","#[^\n\r]*", true);
oLexer.define("TOKEN_IDENTIFIER","[A-Za-z_]+[0-9]*");
oLexer.define("Token Operator", "operator");
oLexer.define("TOKEN_SINGLE_COMMENT","//[^\n\r]*", true);
oLexer.define("Token_Start_MultiComment","/\\*");
oLexer.define("Token_End_MultiComment","\\*/");
oLexer.define("TOKEN_EOF","\\0");
std::string file;
if (ReadFile("test.fox", file)) {
Timer t("Process");
oLexer.process(file);
}
if (ReadFile("test.fox", file)) {
Timer t("Process Async");
oLexer.process_async(file);
// helper::Dump(oLexer);
}
}