chore: add sema checker source and reference pdf

dyz
olivame 2 weeks ago
parent 3e066b8375
commit 304599b17b

@ -124,7 +124,7 @@ g++ -std=c++17 \
-Isrc \
-Ibuild-sema/generated/antlr4 \
-Ithird_party/antlr4-runtime-4.13.2/runtime/src \
build-sema/sema_check.cpp \
tools/sema_check.cpp \
build-sema/src/sem/libsem.a \
build-sema/src/frontend/libfrontend.a \
build-sema/src/utils/libutils.a \

Binary file not shown.

@ -0,0 +1,34 @@
#include <exception>
#include <iostream>
#include <string>
#include "frontend/AntlrDriver.h"
#include "sem/Sema.h"
#include "utils/Log.h"
int main(int argc, char** argv) {
if (argc < 2) {
std::cerr << "usage: sema_check <input.sy> [more.sy...]\n";
return 2;
}
bool failed = false;
for (int i = 1; i < argc; ++i) {
const std::string path = argv[i];
try {
auto antlr = ParseFileWithAntlr(path);
auto* comp_unit = dynamic_cast<SysYParser::CompUnitContext*>(antlr.tree);
if (!comp_unit) {
throw std::runtime_error(FormatError("sema_check", "语法树根节点不是 compUnit"));
}
(void)RunSema(*comp_unit);
std::cout << "OK " << path << "\n";
} catch (const std::exception& ex) {
failed = true;
std::cout << "ERR " << path << "\n";
PrintException(std::cout, ex);
}
}
return failed ? 1 : 0;
}
Loading…
Cancel
Save