mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-17 12:28:45 +00:00
95 lines
3.6 KiB
C++
95 lines
3.6 KiB
C++
#include <fstream>
|
|
#include <iostream>
|
|
#include <sstream>
|
|
|
|
// for clangd
|
|
#include "../include/parse_tree.hpp"
|
|
#include "../include/global_info.hpp"
|
|
#include "../include/interpreter_tree.hpp"
|
|
#include "../include/build_visitor.hpp"
|
|
#include "../include/print_visitor.hpp"
|
|
#include "../include/find_symbols_visitor.hpp"
|
|
#include "../include/link_symbols_visitor.hpp"
|
|
#include "../include/type_check_visitor.hpp"
|
|
#include "../include/typed_print_visitor.hpp"
|
|
#include "../include/execute_visitor.hpp"
|
|
#include "../include/error_handling.hpp"
|
|
|
|
int main(int argc, char** argv) { // TODO, only test version
|
|
if (argc < 2 || argc > 2) {
|
|
std::cout << "Wrong argument count (provide one argument - source file)\n";
|
|
return 0;
|
|
}
|
|
|
|
std::string filename = argv[1];
|
|
|
|
std::ifstream in;
|
|
in.open(filename); // TODO handle errors
|
|
|
|
std::stringstream source_stream;
|
|
|
|
source_stream << in.rdbuf();
|
|
|
|
in.close();
|
|
|
|
std::string source = source_stream.str();
|
|
|
|
parser::ParseTree parse_tree(source);
|
|
|
|
if (!parse_tree.IsProperlyParsed()) {
|
|
error_handling::HandleParsingError("There are some parsing errors in file", {0, 0}, {0, 0});
|
|
}
|
|
|
|
std::unique_ptr<interpreter::tokens::SourceFile> source_file =
|
|
std::make_unique<interpreter::tokens::SourceFile>();
|
|
|
|
info::GlobalInfo global_info;
|
|
info::ContextManager<info::type::Type, info::type::TypeManager> type_context_manager;
|
|
info::ContextManager<info::value::Value, info::value::ValueManager> context_manager;
|
|
|
|
interpreter::BuildVisitor build_visitor(parse_tree);
|
|
interpreter::PrintVisitor print_visitor(std::cout);
|
|
interpreter::FindSymbolsVisitor find_symbols_visitor(global_info);
|
|
interpreter::LinkSymbolsVisitor link_symbols_visitor(global_info);
|
|
interpreter::TypeCheckVisitor type_check_visitor(global_info, type_context_manager);
|
|
// interpreter::TypedPrintVisitor typed_print_visitor(std::cout, type_context_manager);
|
|
|
|
build_visitor.VisitSourceFile(source_file.get());
|
|
|
|
std::cout << "\n---------------------------------- Untyped -------------------------------------\n\n";
|
|
print_visitor.VisitSourceFile(source_file.get());
|
|
|
|
try {
|
|
find_symbols_visitor.VisitSourceFile(source_file.get());
|
|
} catch (...) { error_handling::HandleInternalError("find_symbols_visitor exception", "main", std::nullopt); }
|
|
|
|
try {
|
|
link_symbols_visitor.VisitSourceFile(source_file.get());
|
|
} catch (...) { error_handling::HandleInternalError("link_symbols_visitor exception", "main", std::nullopt); }
|
|
|
|
try {
|
|
type_check_visitor.VisitSourceFile(source_file.get());
|
|
} catch (...) { error_handling::HandleInternalError("type_check_visitor exception", "main", std::nullopt); }
|
|
|
|
std::optional<utils::IdType> maybe_main_partition_id = global_info.FindPartition({"main"});
|
|
|
|
if (!maybe_main_partition_id.has_value()) {
|
|
error_handling::HandleGeneralError("No main partition found");
|
|
}
|
|
|
|
const info::GlobalInfo::PartitionInfo& main_partition =
|
|
global_info.GetPartitionInfo(maybe_main_partition_id.value());
|
|
|
|
std::cout << "\n---------------------------------- Execution -------------------------------------\n\n";
|
|
|
|
interpreter::ExecuteVisitor execute_visitor(global_info,
|
|
type_context_manager,
|
|
context_manager);
|
|
|
|
//try {
|
|
execute_visitor.ExecutePartition(main_partition.node);
|
|
//} catch (...) { error_handling::HandleInternalError("execute_visitor exception", "main", std::nullopt); }
|
|
|
|
// std::cout << "\n---------------------------------- Typed -------------------------------------\n\n";
|
|
// typed_print_visitor.VisitSourceFile(source_file.get());
|
|
}
|