-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLox.cpp
89 lines (69 loc) · 1.7 KB
/
Lox.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
#include <iostream>
#include <fstream>
#include "headers/Errors.hpp"
#include "headers/Lox.hpp"
#include "headers/Scanner.hpp"
#include "headers/Parser.hpp"
#include "headers/AstPrinter.hpp"
#include "headers/Interpreter.hpp"
#include "headers/Resolver.hpp"
TWI::Lox::Lox()
{
hadError = false;
}
Interpreter interpreter{};
void TWI::Lox::run(std::string source)
{
Scanner scanner{source};
std::vector<Token> tokens = scanner.scanTokens();
Parser parser{tokens};
std::vector<std::shared_ptr<Stmt>> statements = parser.parse();
if(hadError) return;
// std::cout << "All good before resolving" << std::endl;
Resolver resolver{interpreter};
resolver.resolve(statements);
// std::cout << "All good after resolving" << std::endl;
if(hadError) return;
interpreter.interpret(statements);
}
std::string TWI::Lox::readFile(std::string path)
{
std::ifstream file{path, std::ios::in | std::ios::binary | std::ios::ate};
if (!file)
{
std::cerr << "Could not open file " << path << std::endl;
exit(74);
}
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);
std::string contents(size, '\0');
file.read(&contents[0], size);
file.close();
return contents;
}
void TWI::Lox::runPrompt()
{
for (;;)
{
std::cout << "> ";
std::string line;
std::getline(std::cin, line);
if (line.empty())
break;
run(line.c_str());
hadError = false;
}
}
void TWI::Lox::runFile(std::string path)
{
std::string contents = readFile(path);
run(contents.c_str());
if (hadError)
{
exit(65);
}
if(hadRuntimeError)
{
exit(70);
}
}