saqut-compiler/src/cli/commands/check.hpp

60 lines
1.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef SAQUT_CLI_CHECK
#define SAQUT_CLI_CHECK
#include <iostream>
#include "cli/args.hpp"
#include "tokenizer/tokenizer.hpp"
#include "parser/parser.hpp"
#include "symbol/symbol_table.hpp"
#include "symbol/symbol_collector.hpp"
#include "semantic/type_checker.hpp"
#include "semantic/structural_validator.hpp"
#include "diagnostic/diagnostic_engine.hpp"
#include "vendor/nlohmann/json.hpp"
inline int cmdCheck(const CliArgs& args) {
std::string filePath = inputFilePath(args);
std::string source = readSource(args);
if (source.empty()) return 1;
Tokenizer tokenizer;
auto tokens = tokenizer.scan(source, filePath);
Parser parser;
ASTNode* ast = parser.parse(tokens);
DiagnosticEngine diag;
if (!ast) {
diag.report("E000", SourceLocation{}, "AST üretilemedi");
nlohmann::json out;
out["file"] = filePath;
out["diagnostics"] = diag.toJsonObj();
std::cout << (args.compact ? out.dump() : out.dump(2)) << "\n";
for (auto* t : tokens) delete t;
return 1;
}
SymbolTable table;
SymbolCollector(table, diag).collect(ast);
// Sembol toplama hataları varsa tip denetimine geçme
// (çözümsüz semboller tip denetiminde sahte E003 üretir)
if (!diag.hasErrors()) {
TypeChecker(table, diag).check(ast);
StructuralValidator(diag).validate(ast);
}
nlohmann::json out;
out["file"] = filePath;
out["diagnostics"] = diag.toJsonObj();
std::cout << (args.compact ? out.dump() : out.dump(2)) << "\n";
delete ast;
for (auto* t : tokens) delete t;
return diag.hasErrors() ? 1 : 0;
}
#endif // SAQUT_CLI_CHECK