-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
68 lines (63 loc) · 1.39 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
#include <filesystem>
#include <format>
#include <fstream>
#include <iostream>
#include "chunk.hpp"
#include "compiler.hpp"
#include "debug.hpp"
#include "vm.hpp"
static void repl()
{
std::string line;
for (;;)
{
std::cout << "> ";
if (!std::getline(std::cin, line))
{
std::cout << std::endl;
break;
}
clox::compiler comp{std::move(line)};
auto chunks = comp.compile();
if (chunks)
{
clox::vm vm{std::move(*chunks)};
vm.interpret();
}
}
}
static int run_file(const std::filesystem::path& path)
{
std::string source;
std::ifstream file(path);
if (!file.is_open())
{
std::cerr << std::format("Could not open file \"{}\"", path.c_str())
<< std::endl;
return 74;
}
std::getline(file, source, '\0');
/* clox::InterpretResult result = interpret(source);
if (result == clox::InterpretResult::INTERPRET_COMPILE_ERROR)
return 65;
if (result == clox::InterpretResult::INTERPRET_RUNTIME_ERROR)
return 70; */
return -1;
}
int main(int argc, char** argv)
{
if (argc == 1)
{
repl();
}
else if (argc == 2)
{
run_file(argv[1]);
}
else
{
std::cerr << "Usage: clox [path]" << std::endl;
return 64;
}
return 0;
}