-
Notifications
You must be signed in to change notification settings - Fork 0
/
library.cpp
50 lines (40 loc) · 1.24 KB
/
library.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
#include <iostream>
#include <fstream>
#include "library.h"
#include "ParsedScripts/ParsedScript.h"
#include "Parser/Parser.h"
#include "Interpreter/Interpreter.h"
//#define DEBUG
extern "C" {
int interpret(const char* filename) {
#ifdef DEBUG
std::cout << "Using file: " << filename << std::endl;
#endif
std::ifstream file;
try {
file = std::ifstream(filename);
if (!file) {
std::cerr << "Unable to open file: " << filename << std::endl;
return 1; // return with error code
}
} catch (const std::exception& e) {
std::cerr << "Error with opening file (" << filename << "): " << e.what() << std::endl;
return 1; // return with error code
}
std::string fileStr;
std::string line;
while (std::getline(file, line)) {
fileStr += line;
}
file.close();
ParsedScript script = Parser::Parse(fileStr);
#ifdef DEBUG
std::cout << "Script parsed successfully" << std::endl;
#endif
int result = Interpreter::Interpret(&script);
return result;
}
}
int Library::InterpretFile(const std::string& filename) {
return interpret(filename.c_str());
}