-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
134 lines (113 loc) · 4.48 KB
/
main.cpp
File metadata and controls
134 lines (113 loc) · 4.48 KB
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
#include "utils/library_utils.hpp"
#include "utils/code_generator.hpp"
#include "utils/ast_utils.hpp"
#include "utils/directory_manager.hpp"
#include "multiline/code_block.hpp"
#include "flags/flags.hpp"
#include "model/file_model.hpp"
#include "ast/processing_ast.hpp"
#include <iostream>
#include <string>
#include <sstream>
#include <unordered_map>
using namespace ast_tools;
using namespace Options;
using namespace SourceUtils;
using namespace DirectoryUtils;
int main(int argc, char **argv) {
DirectoryUtils::create_dirs();
std::string current_line;
auto file_model = FileModel();
auto code_generator = CodeGenerator();
std::unordered_map<std::string, void *> memory_state;
auto old_model = file_model;
while (getline(std::cin, current_line)) {
std::cout.setstate(std::ios::failbit);
Options::Flag flag = Options::check_for_flag(current_line);
switch (flag) {
case SIGNATURE: {
std::string name = Options::parse_after_flag(current_line);
std::string current_source = file_model.to_string();
std::vector<std::string> signatures = AstUtils::get_function_signatures_by_name(current_source, name);
break;
}
case LOAD: {
load_header(current_line, file_model);
break;
}
case CREATE_OBJECT: {
std::string block = CodeBlock::read_block();
if (CheckIsFunction(block)) {
file_model.add_function(block);
} else if (CheckIsClass(block)) {
file_model.add_record(block);
}
// file_model.add_code_block(block);
break;
}
case EVAL: {
std::cout.clear();
std::string expression = Options::parse_after_flag(current_line);
std::stringstream output;
output << COUT << " << " << expression << " << " << ENDL << ";";
file_model.update_last_statement(output.str());
break;
}
case EXIT: {
DirectoryUtils::clean_up();
return 0;
}
case DEFAULT: {
std::string headers;
for (auto& s : file_model.get_headers()) {
headers += s + "\n";
}
headers += current_line;
if (!AstUtils::is_variable_declaration(headers)) {
file_model.update_last_statement(current_line);
}
break;
}
}
if (flag != DEFAULT && flag != EVAL) {
continue;
}
for (auto& declaration : file_model.get_variable_declarations()) {
std::string variable_name = file_model.get_variable_name_by_declaration(declaration);
size_t size = LibraryUtils::get_size_of(variable_name);
void *memory = malloc(size);
void *so = nullptr;
void *variable_memory = LibraryUtils::load_variable_by_name(so, variable_name);
memcpy(memory, variable_memory, size);
dlclose(so);
memory_state.insert(std::make_pair(variable_name, memory));
}
if (flag == DEFAULT && AstUtils::is_variable_declaration(current_line)) {
file_model.add_variable_declaration(current_line);
}
std::string lib_name = LibraryUtils::get_library_name();
std::string source_file_name = code_generator.get_source_file_name();
std::ofstream source_file(source_file_name);
code_generator.generate_source_file(file_model, source_file);
source_file.close();
int code = LibraryUtils::build_library(source_file_name, lib_name);
if (!code) {
// now that the new library is built, we assign cached values to the variables
for (auto &state: memory_state) {
std::string variable = state.first;
void *memory = state.second;
void *so = nullptr;
void *new_memory = LibraryUtils::load_variable_by_name(so, variable);
size_t size = LibraryUtils::get_size_of(variable);
memcpy(new_memory, memory, size);
dlclose(so);
}
LibraryUtils::load_main_function();
} else {
file_model = old_model;
}
old_model = file_model;
}
DirectoryUtils::clean_up();
return 0;
}