-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interpreter.cpp
79 lines (67 loc) · 2.72 KB
/
Interpreter.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
// Copyright (c) Qinetik 2024.
//
// Created by Waqas Tahir on 28/02/2024.
//
#include "lexer/Lexi.h"
#include "utils/Utils.h"
#include "ast/utils/ExpressionEvaluator.h"
#include "cst/base/CSTConverter.h"
#include "compiler/SymbolResolver.h"
#include <chrono>
int main(int argc, char *argv[]) {
if (argc == 0) {
std::cout << "A file path argument is required so the file can be parsed";
return 0;
}
auto srcFilePath = argv[1];
auto lexer = benchLexFile(srcFilePath);
// printTokens(lexer.tokens);
for(const auto& err : lexer.errors) {
std::cerr << err.representation(argv[1], "Lexer") << std::endl;
}
GlobalInterpretScope global_scope(nullptr, nullptr)
CSTConverter converter(true, "interpreter", global_scope);
converter.convert(lexer.tokens);
for(const auto& err : converter.diagnostics) {
std::cerr << err.representation(argv[1], "Parser") << std::endl;
}
Scope scope(std::move(converter.nodes));
// std::cout << "[Representation]\n" << scope.representation() << "\n";
GlobalInterpretScope interpretScope(nullptr, &scope, nullptr, argv[1]);
define_all(interpretScope);
// Print started
// std::cout << "[Interpreter] Started" << std::endl;
// Save start time
auto start = std::chrono::steady_clock::now();
// Actual interpretation
{
SymbolResolver linker(argv[0], srcFilePath, true);
for(const auto& func : interpretScope.global_fns) {
linker.declare(func.first, func.second.get());
}
scope.link_asynchronously(linker);
if(!linker.errors.empty()){
for(const auto& err : linker.errors) {
std::cerr << "[Linker] " << err.message << std::endl;
}
return 1;
}
}
scope.interpret(interpretScope);
// Save end time
auto end = std::chrono::steady_clock::now();
// Calculating duration in different units
auto nanos = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
auto micros = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
auto seconds = std::chrono::duration_cast<std::chrono::seconds>(end - start).count();
// Printing stats
std::cout << std::endl << "[Interpreter] Completed " << ' ';
std::cout << "[Nanoseconds:" << nanos << "]";
std::cout << "[Microseconds:" << micros << "]";
std::cout << "[Milliseconds:" << millis << "]" << std::endl;
for(const auto& err : interpretScope.errors) {
std::cerr << "[Interpreter] " << err << '\n';
}
return 0;
}