-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
134 lines (115 loc) · 3.66 KB
/
Copy pathmain.cpp
File metadata and controls
134 lines (115 loc) · 3.66 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 <iostream>
#include <fstream>
#include <filesystem>
#include "src/Lexer/Lexer.hpp"
#include "src/Parser/Parser.hpp"
#include "src/Bytecode/Bytecode.hpp"
#include "src/Bytecode/Generator.hpp"
#include "src/VirtualMachine/VirtualMachine.hpp"
enum CompType
{
DEV,
INTERP,
};
CompType type = CompType::INTERP;
int main(int argc, char **argv)
{
if (type == CompType::DEV)
{
// std::filesystem::current_path("../../../playground/gui/src");
// Lexer lexer("main.vtx");
std::filesystem::current_path("../../../playground");
Lexer lexer("source.vtx");
lexer.tokenize();
Parser parser(lexer.nodes, lexer.file_name);
parser.parse(0, "_");
parser.remove_op_node(";");
auto ast = parser.nodes;
VM vm;
std::shared_ptr<FunctionObj> main = std::make_shared<FunctionObj>();
main->name = "";
main->arity = 0;
main->chunk = Chunk();
CallFrame main_frame;
main_frame.name = "source.vtx";
main_frame.function = main;
main_frame.sp = 0;
main_frame.ip = main->chunk.code.data();
main_frame.frame_start = 0;
generate_bytecode(parser.nodes, main_frame.function->chunk, parser.file_name);
add_code(main_frame.function->chunk, OP_EXIT);
disassemble_chunk(main_frame.function->chunk, "Test");
auto offsets = instruction_offsets(main_frame.function->chunk);
main_frame.function->instruction_offsets = offsets;
vm.frames.push_back(main_frame);
evaluate(vm);
std::cin.get();
exit(0);
}
if (type == CompType::INTERP)
{
if (argc == 1)
{
std::cout << "You must enter a source path e.g: vortex dev/main.vtx\n";
return 1;
}
std::string path = argv[1];
std::vector<std::string> args;
std::string import_path;
if (argc > 1)
{
for (int i = 1; i < argc; i++)
{
args.push_back(argv[i]);
}
}
for (int i = 0; i < args.size(); i++)
{
std::string arg = args[i];
if (arg == "-m" || arg == "-modules")
{
if (i == args.size() - 1)
{
std::cout << "Invalid module path";
return 1;
}
else
{
import_path = args[i + 1];
}
}
}
Lexer lexer(path);
lexer.tokenize();
auto parent_path = std::filesystem::path(path).parent_path();
if (parent_path != "")
{
std::filesystem::current_path(parent_path);
}
Parser parser(lexer.nodes, lexer.file_name);
parser.parse(0, "_");
parser.remove_op_node(";");
auto ast = parser.nodes;
VM vm;
std::shared_ptr<FunctionObj> main = std::make_shared<FunctionObj>();
main->name = "";
main->arity = 0;
main->chunk = Chunk();
main->chunk.import_path = import_path;
CallFrame main_frame;
main_frame.name = path;
main_frame.function = main;
main_frame.sp = 0;
main_frame.ip = main->chunk.code.data();
main_frame.frame_start = 0;
vm.argc = argc;
vm.argv = argv;
generate_bytecode(parser.nodes, main_frame.function->chunk, path);
auto offsets = instruction_offsets(main_frame.function->chunk);
main_frame.function->instruction_offsets = offsets;
vm.frames.push_back(main_frame);
add_code(main_frame.function->chunk, OP_EXIT);
evaluate(vm);
exit(0);
}
}