-
Notifications
You must be signed in to change notification settings - Fork 0
/
LScript.cpp
executable file
·83 lines (74 loc) · 1.51 KB
/
LScript.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
#include <iostream>
#include <fstream>
#include <sstream>
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
#include "Parser.h"
#include "Lexer.h"
#include "Interpreter.h"
Interpreter interpreter;
static void run(std::string str)
{
Lexer lexer = Lexer(str);
std::vector<Token> tokens = lexer.lexAll();
#ifdef LDEBUG
for (const auto& token : tokens)
{
std::cout << token << std::endl;
}
#endif
Parser parser = Parser(tokens);
std::list<std::unique_ptr<Stmt>> stmt_list = parser.parse();
if (!stmt_list.empty())
interpreter.interpret(std::move(stmt_list));
}
#ifdef __EMSCRIPTEN__
extern "C"
{
EMSCRIPTEN_KEEPALIVE
void crun(const char *c_str)
{
run(c_str);
}
}
#endif
static int runFile(char *script_name)
{
std::ifstream fileStream(script_name);
std::ostringstream oss;
oss << fileStream.rdbuf();
std::string fileStr = oss.str();
if (fileStr.empty())
return 1;
run(fileStr);
return 0;
}
static int runPrompt()
{
for (;;)
{
std::string line;
std::cout << "> ";
std::getline(std::cin, line);
if (!line.empty())
run(line);
}
return 0;
}
int main(int argc, char **argv)
{
if (argc > 2)
{
std::cerr << "Usage: LScript [script]" << std::endl;
return 1;
}
#ifdef LDEBUG
std::string script;
std::cout << "Run script: ";
std::cin >> script;
return runFile((char*)std::string("../scripts/" + script).c_str());
#else
return (argc == 2) ? runFile(argv[1]) : runPrompt();
#endif
}