-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlwScript.cpp
162 lines (135 loc) · 3.49 KB
/
lwScript.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
#include <string>
#include <string_view>
#include "lwscript.h"
#if defined(_WIN32) || defined(_WIN64)
#pragma warning(disable : 4996)
#endif
lwscript::Lexer *gLexer{nullptr};
lwscript::Parser *gParser{nullptr};
lwscript::AstPassManager *gAstPassManager;
lwscript::Compiler *gCompiler{nullptr};
lwscript::VM *gVm{nullptr};
struct Config
{
std::string_view sourceFilePath;
bool isSerializeBinaryChunk{false};
std::string_view serializeBinaryFilePath;
} gConfig;
int32_t PrintVersion()
{
lwscript::Logger::Info(LWSCRIPT_VERSION);
return EXIT_FAILURE;
}
int32_t PrintUsage()
{
lwscript::Logger::Info(TEXT("Usage: lwscript [option]:"));
lwscript::Logger::Info(TEXT("-h or --help:show usage info."));
lwscript::Logger::Info(TEXT("-v or --version:show current lwscript version"));
lwscript::Logger::Info(TEXT("-s or --serialize: serialize source file as bytecode binary file"));
lwscript::Logger::Info(TEXT("-f or --file:run source file with a valid file path,like : lwscript -f examples/array.cd."));
return EXIT_FAILURE;
}
void Run(STD_STRING_VIEW content)
{
auto tokens = gLexer->ScanTokens(content);
#ifndef NDEBUG
for (const auto &token : tokens)
lwscript::Logger::Println(TEXT("{}"), *token);
#endif
auto stmt = gParser->Parse(tokens);
gAstPassManager->Execute(stmt);
#ifndef NDEBUG
lwscript::Logger::Println(TEXT("{}"), stmt->ToString());
#endif
auto mainFunc = gCompiler->Compile(stmt);
#ifndef NDEBUG
auto str = mainFunc->ToStringWithChunk();
lwscript::Logger::Println(TEXT("{}"), str);
#endif
if (gConfig.isSerializeBinaryChunk)
{
auto data = mainFunc->chunk.Serialize();
lwscript::WriteBinaryFile(gConfig.serializeBinaryFilePath, data);
}
else
{
gVm->Run(mainFunc);
}
}
void Repl()
{
STD_STRING line;
STD_STRING allLines;
PrintVersion();
lwscript::Logger::Print(TEXT(">> "));
while (getline(CIN, line))
{
allLines += line;
if (line == TEXT("clear"))
allLines = TEXT("");
else
Run(allLines);
lwscript::Logger::Print(TEXT(">> "));
}
}
void RunFile(std::string_view path)
{
STD_STRING content = lwscript::ReadFile(path);
Run(content);
}
int32_t ParseArgs(int32_t argc, const char *argv[])
{
for (size_t i = 0; i < argc; ++i)
{
if (strcmp(argv[i], "-f") == 0 || strcmp(argv[i], "--file") == 0)
{
if (i + 1 < argc)
gConfig.sourceFilePath = argv[++i];
else
return PrintUsage();
}
if (strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--serialize") == 0)
{
if (i + 1 < argc)
{
gConfig.isSerializeBinaryChunk = true;
gConfig.serializeBinaryFilePath = argv[++i];
}
else
return PrintUsage();
}
if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
return PrintUsage();
if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0)
return PrintVersion();
}
return EXIT_SUCCESS;
}
int32_t main(int32_t argc, const char *argv[])
{
#if defined(_WIN32) || defined(_WIN64)
system("chcp 65001");
#endif
if (ParseArgs(argc, argv) == EXIT_FAILURE)
return EXIT_FAILURE;
gLexer = new lwscript::Lexer();
gParser = new lwscript::Parser();
gAstPassManager = new lwscript::AstPassManager();
gCompiler = new lwscript::Compiler();
gVm = new lwscript::VM();
gAstPassManager
#ifdef LWSCRIPT_CONSTANT_FOLD_OPT
->Add<lwscript::ConstantFoldPass>()
#endif
->Add<lwscript::SyntaxCheckPass>();
if (!gConfig.sourceFilePath.empty())
RunFile(gConfig.sourceFilePath);
else
Repl();
SAFE_DELETE(gLexer);
SAFE_DELETE(gParser);
SAFE_DELETE(gAstPassManager);
SAFE_DELETE(gCompiler);
SAFE_DELETE(gVm);
return EXIT_SUCCESS;
}