From 1f0928a443932edf62c354642e46c10bd6da435d Mon Sep 17 00:00:00 2001 From: Su Xing Date: Thu, 6 Apr 2023 14:30:36 +0800 Subject: [PATCH] Add diagnostic support forgotten in last commit --- src/Diagnostic.cpp | 15 +++++++++++++++ src/Diagnostic.h | 31 +++++++++++++++++++++++++++++++ test/11_add2.sy | 7 +++++++ 3 files changed, 53 insertions(+) create mode 100644 src/Diagnostic.cpp create mode 100644 src/Diagnostic.h create mode 100644 test/11_add2.sy diff --git a/src/Diagnostic.cpp b/src/Diagnostic.cpp new file mode 100644 index 0000000..a7133b2 --- /dev/null +++ b/src/Diagnostic.cpp @@ -0,0 +1,15 @@ +#include +using namespace std; +#include "Diagnostic.h" + +namespace sysy { + +Diagnostic error(antlr4::ParserRuleContext *ctx, const std::string &msg) { + auto line = ctx->start->getLine(); + auto col = ctx->start->getCharPositionInLine(); + ostringstream ss; + ss << line << ':' << col << ": error " << msg << '\n'; + return Diagnostic(ss.str()); +} + +} // namespace sysy \ No newline at end of file diff --git a/src/Diagnostic.h b/src/Diagnostic.h new file mode 100644 index 0000000..577fa67 --- /dev/null +++ b/src/Diagnostic.h @@ -0,0 +1,31 @@ +#pragma once + +#include "antlr4-runtime.h" +#include +#include +#include + +namespace sysy { + +class Diagnostic { +private: + const std::string message; + +public: + Diagnostic(const std::string &message) : message(message) {} + Diagnostic(std::string &&message) : message(std::move(message)) {} + Diagnostic(const Diagnostic &) = default; + Diagnostic(Diagnostic &&) = default; + +public: + ~Diagnostic() { + if (not message.empty()) { + std::cerr << message << '\n'; + exit(EXIT_FAILURE); + } + } +}; // class Diagnostic + +Diagnostic error(antlr4::ParserRuleContext *ctx, const std::string &msg); + +} // namespace sysy \ No newline at end of file diff --git a/test/11_add2.sy b/test/11_add2.sy new file mode 100644 index 0000000..ff966fa --- /dev/null +++ b/test/11_add2.sy @@ -0,0 +1,7 @@ +//test add +int main(){ + int a, b; + a = 10; + b = 0; + return a + b; +}