Add the panic function to diag and change getCurrentNS to return a ref
This commit is contained in:
parent
c6e5bddf10
commit
5a9c256325
|
@ -103,7 +103,7 @@ public:
|
|||
bool setCurrentNS(llvm::StringRef ns_name);
|
||||
|
||||
/// Return the current namespace that is being processed at the moment
|
||||
std::shared_ptr<Namespace> getCurrentNS();
|
||||
Namespace &getCurrentNS();
|
||||
|
||||
/// Lookup the namespace with the give name in the current context and
|
||||
/// return a shared pointer to it or a `nullptr` in it doesn't exist.
|
||||
|
|
|
@ -80,9 +80,18 @@ public:
|
|||
void enqueueError(llvm::StringRef msg);
|
||||
void emitSyntaxError(reader::LocationRange loc, errors::ErrorVariant &e,
|
||||
llvm::StringRef msg = "");
|
||||
|
||||
/// Throw out an error with the given `msg` and terminate the execution
|
||||
void panic(llvm::StringRef msg);
|
||||
};
|
||||
|
||||
/// Create a new instance of the `DiagnosticEngine` from the give
|
||||
/// `SereneContext`
|
||||
std::unique_ptr<DiagnosticEngine> makeDiagnosticEngine(SereneContext &ctx);
|
||||
|
||||
/// Throw out an error with the given `msg` and terminate the execution
|
||||
void panic(SereneContext &ctx, llvm::StringRef msg);
|
||||
|
||||
} // namespace serene
|
||||
|
||||
#endif
|
||||
|
|
|
@ -23,6 +23,8 @@
|
|||
#include "serene/reader/location.h"
|
||||
#include "serene/slir/generatable.h"
|
||||
|
||||
#include <llvm/Support/FormatVariadic.h>
|
||||
|
||||
namespace serene {
|
||||
|
||||
void SereneContext::insertNS(std::shared_ptr<Namespace> ns) {
|
||||
|
@ -46,15 +48,14 @@ bool SereneContext::setCurrentNS(llvm::StringRef ns_name) {
|
|||
return false;
|
||||
};
|
||||
|
||||
std::shared_ptr<Namespace> SereneContext::getCurrentNS() {
|
||||
// TODO: replace the assertion with a runtime check
|
||||
assert(!this->current_ns.empty() && "Current namespace is not set");
|
||||
|
||||
if (namespaces.count(this->current_ns)) {
|
||||
return namespaces[this->current_ns];
|
||||
Namespace &SereneContext::getCurrentNS() {
|
||||
if (this->current_ns.empty() || !namespaces.count(this->current_ns)) {
|
||||
panic(*this, llvm::formatv("getCurrentNS: Namespace '{0}' does not exist",
|
||||
this->current_ns)
|
||||
.str());
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
return *namespaces[this->current_ns];
|
||||
};
|
||||
|
||||
void SereneContext::setOperationPhase(CompilationPhase phase) {
|
||||
|
|
|
@ -177,7 +177,32 @@ void DiagnosticEngine::emitSyntaxError(reader::LocationRange loc,
|
|||
exit(1);
|
||||
};
|
||||
|
||||
void DiagnosticEngine::panic(llvm::StringRef msg) {
|
||||
// TODO: Use Diagnostic class here instead
|
||||
// TODO: Provide a trace if possible
|
||||
|
||||
llvm::ColorMode mode =
|
||||
ctx.opts.withColors ? llvm::ColorMode::Auto : llvm::ColorMode::Disable;
|
||||
|
||||
llvm::WithColor s(llvm::errs(), llvm::raw_ostream::SAVEDCOLOR, true, false,
|
||||
mode);
|
||||
s << "\n[";
|
||||
s.changeColor(llvm::raw_ostream::Colors::RED);
|
||||
s << "Panic";
|
||||
s.resetColor();
|
||||
s << "]: ";
|
||||
|
||||
s << msg << "\n";
|
||||
// TODO: Use a proper error code
|
||||
std::exit(1);
|
||||
};
|
||||
|
||||
std::unique_ptr<DiagnosticEngine> makeDiagnosticEngine(SereneContext &ctx) {
|
||||
return std::make_unique<DiagnosticEngine>(ctx);
|
||||
}
|
||||
|
||||
void panic(SereneContext &ctx, llvm::StringRef msg) {
|
||||
ctx.diagEngine->panic(msg);
|
||||
};
|
||||
|
||||
} // namespace serene
|
||||
|
|
|
@ -92,7 +92,7 @@ MaybeNode Call::make(SereneContext &ctx, List *list) {
|
|||
|
||||
// TODO: Lookup the symbol in the namespace via a method that looks
|
||||
// into the current environment.
|
||||
auto maybeResult = ctx.getCurrentNS()->semanticEnv.lookup(sym->name);
|
||||
auto maybeResult = ctx.getCurrentNS().semanticEnv.lookup(sym->name);
|
||||
|
||||
if (!maybeResult.hasValue()) {
|
||||
std::string msg =
|
||||
|
|
|
@ -101,8 +101,8 @@ MaybeNode Def::make(SereneContext &ctx, List *list) {
|
|||
}
|
||||
|
||||
// auto analayzedValuePtr = analyzedValue;
|
||||
auto result = ctx.getCurrentNS()->semanticEnv.insert_symbol(binding->name,
|
||||
analyzedValue);
|
||||
auto result = ctx.getCurrentNS().semanticEnv.insert_symbol(binding->name,
|
||||
analyzedValue);
|
||||
|
||||
if (result.succeeded()) {
|
||||
return makeSuccessfulNode<Def>(list->location, binding->name,
|
||||
|
|
|
@ -38,7 +38,7 @@ namespace exprs {
|
|||
Fn::Fn(SereneContext &ctx, reader::LocationRange &loc, List &args, Ast body)
|
||||
: Expression(loc), args(args), body(body) {
|
||||
this->setName(
|
||||
llvm::formatv("___fn___{0}", ctx.getCurrentNS()->nextFnCounter()));
|
||||
llvm::formatv("___fn___{0}", ctx.getCurrentNS().nextFnCounter()));
|
||||
};
|
||||
|
||||
ExprType Fn::getType() const { return ExprType::Fn; };
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
#include "serene/serene.h"
|
||||
|
||||
#include <llvm/Support/CommandLine.h>
|
||||
#include <llvm/Support/FormatVariadic.h>
|
||||
#include <llvm/Support/raw_ostream.h>
|
||||
|
||||
using namespace serene;
|
||||
|
@ -66,7 +67,9 @@ int main(int argc, char *argv[]) {
|
|||
while (true) {
|
||||
// Read line
|
||||
std::string line;
|
||||
auto quit = linenoise::Readline("user> ", line);
|
||||
std::string prompt = ctx->getCurrentNS().name + "> ";
|
||||
|
||||
auto quit = linenoise::Readline(prompt.c_str(), line);
|
||||
|
||||
if (quit) {
|
||||
break;
|
||||
|
|
Loading…
Reference in New Issue