-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
209 lines (173 loc) · 5.08 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "llvm/ADT/APFloat.h"
#include "Language/Lexer.hpp"
#include "Language/AbstractSyntaxTree.hpp"
#include "Language/Parser.hpp"
#include <fstream>
#include <filesystem>
#include "Language/NucleusToml.hpp"
int Lexer::CurrentToken;
std::string Lexer::IdentifierStr;
std::string Lexer::NumValString;
char Lexer::CharVal;
std::string Lexer::StringString;
std::string Lexer::EntireScriptContent;
int Lexer::LastECChar = -1;
std::string Lexer::GetSavedString = "";
bool Lexer::RecordString = false;
std::map<char, int> Parser::BinaryOpPrecedence;
AST::Array* Parser::lastArray = nullptr;
AST::NestedArray* Parser::lastNestedArray = nullptr;
unsigned int Parser::bracketCount = 0;
std::vector<std::string> Parser::localArrayNames, Parser::localNestedArrayNames;
std::string Parser::globalAutoExterns;
std::vector<std::pair<std::string, std::string>> Parser::localStructVariables;
std::string Parser::currentIdentifierString;
bool Parser::dotCommaAsOperator = true, Parser::beginNestedArray = false, Parser::endNestedArray = true, Parser::disableOperators = false;
std::vector<std::unique_ptr<AST::Function>> ParseTesting::allParsedFunctions;
std::vector<std::unique_ptr<AST::StructEx>> Parser::AllStructs;
std::vector<ParsedShelf> Parser::AllShelfs;
SourceLocation Lexer::CurrentLocation;
SourceLocation Lexer::LexerLocation = {1, 0};
std::vector<std::string> NucleusTOML::folders;
std::vector<std::string> NucleusTOML::CPPIncludes;
void MainLoop()
{
bool closeLoop = false;
while(!closeLoop)
{
//fprintf(stderr, "nucleus> ");
if(Lexer::CurrentToken == Token::TK_EndOfFile)
{
Lexer::CurrentToken = 0;
closeLoop = true;
break;
}
else if(Lexer::CurrentToken == Token::TK_Shelf)
{
ParseTesting::Shelf();
Lexer::GetNextToken();
}
else if(Lexer::CurrentToken == Token::TK_Define)
{
std::cout << "Looking for Function...\n";
ParseTesting::Definition();
Lexer::GetNextToken();
}
else if(Lexer::CurrentToken == Token::TK_Extern)
{
//std::cout << "Looking for Extern...\n";
ParseTesting::Extern();
Lexer::GetNextToken();
}
else if(Lexer::CurrentToken == Token::TK_Struct)
{
std::cout << "Looking for Struct...\n";
ParseTesting::Struct();
Lexer::GetNextToken();
}
else if(Lexer::CurrentToken == ']')
{
if(Parser::AllShelfs.size() - 1 >= 0)
Parser::AllShelfs.pop_back();
Lexer::GetNextToken();
}
else
{
Lexer::GetNextToken();
}
}
}
#ifdef _WIN32
#define DLLEXPORT __declspec(dllexport)
#else
#define DLLEXPORT
#endif
extern "C" DLLEXPORT double putchard(double X) {
fputc((char)X, stderr);
return 0;
}
extern "C" DLLEXPORT double printd(double X) {
fprintf(stderr, "%f\n", X);
return 0;
}
void Compile(int profile)
{
llvm::InitializeNativeTarget();
llvm::InitializeNativeTargetAsmPrinter();
llvm::InitializeNativeTargetAsmParser();
Parser::BinaryOpPrecedence[';'] = 1;
Parser::BinaryOpPrecedence['='] = 2;
Parser::BinaryOpPrecedence['<'] = 10;
Parser::BinaryOpPrecedence['>'] = 10;
Parser::BinaryOpPrecedence['+'] = 20;
Parser::BinaryOpPrecedence['-'] = 20;
Parser::BinaryOpPrecedence['*'] = 40;
//fprintf(stderr, "nucleus> ");
Lexer::GetNextToken();
CodeGeneration::StartJIT();
CodeGeneration::Initialize();
NucleusTOML::Read("Nucleus.toml");
Lexer::EntireScriptContent = "extern ";
for(auto i : NucleusTOML::folders)
{
if(!std::filesystem::exists(i))
{
std::string getName = i;
i = NucleusTOML::GetExecutableDirectory() + "/" + i;
if(!std::filesystem::exists(i))
{
std::cout << "Error: Directory '" << getName << "' does not exist.\n";
exit(1);
}
}
for (const auto & entry : std::filesystem::directory_iterator(i))
{
if(entry.path().u8string().find(".toml") != std::string::npos)
{
NucleusTOML::Read(entry.path().u8string());
}
if(entry.path().u8string().find(".nk") != std::string::npos)
{
std::ifstream ifs(entry.path().u8string().c_str());
std::string content( (std::istreambuf_iterator<char>(ifs) ),
(std::istreambuf_iterator<char>() ) );
Lexer::EntireScriptContent += content;
Lexer::EntireScriptContent += "\n";
}
}
}
std::string path = std::filesystem::current_path().u8string();
for (const auto & entry : std::filesystem::directory_iterator(path))
{
if(entry.path().u8string().find(".nk") != std::string::npos)
{
std::ifstream ifs(entry.path().u8string().c_str());
std::string content( (std::istreambuf_iterator<char>(ifs) ),
(std::istreambuf_iterator<char>() ) );
Lexer::EntireScriptContent += content;
Lexer::EntireScriptContent += "\n";
}
}
//std::cout << Lexer::EntireScriptContent << "\n";
MainLoop();
ParseTesting::CompileFunctions();
CodeGeneration::CompileToObjectCode();
}
int main(int argc, const char* argv[])
{
for(int i = 0; i < argc; i++)
{
std::string arg = argv[i];
if(arg == "release")
{
std::cout << "Compiling Release...\n";
Compile(1);
}
else if(arg == "debug")
{
std::cout << "Compiling Debug...\n";
Compile(0);
}
}
return 0;
}