File exceptions.hpp

File List > include > simplesquirrel > exceptions.hpp

Go to the documentation of this file.

#pragma once
#ifndef SSQ_EXCEPTIONS_HEADER_H
#define SSQ_EXCEPTIONS_HEADER_H

#include <string>
#include <sstream>
#include "type.hpp"

namespace ssq {
    class Exception: public std::exception {
    public:
        Exception(const char* msg):message(msg) {

        }
        virtual const char* what() const throw() override {
            return message;
        }
    private:
        const char* message;
    };
    class NotFoundException: public Exception {
    public:
        NotFoundException(const char* msg):Exception(msg) {
            std::stringstream ss;
            ss << "Not found: " << msg;
            message = ss.str();
        }

        virtual const char* what() const throw() override {
            return message.c_str();
        }
    private:
        std::string message;
    };
    class CompileException: public Exception {
    public:
        CompileException(const char* msg):Exception(msg) { 
            message = std::string(msg);
        }

        CompileException(const char* msg, const char* source, int line, int column):Exception(msg) { 
            std::stringstream ss;
            ss << "Compile error at " << source << ":" << line << ":" << column << " " << msg;
            message = ss.str();
        }
        
        virtual const char* what() const throw() override {
            return message.c_str();
        }
    private:
        std::string message;
    };
    class TypeException: public Exception {
    public:
        TypeException(const char* msg):Exception(msg) {
            message = std::string(msg);
        }

        TypeException(const char* msg, const char* expected, const char* got):Exception(msg) {
            std::stringstream ss;
            ss << "Type error " << msg << " expected: " << expected << " got: " << got;
            message = ss.str();
        }

        virtual const char* what() const throw() override {
            return message.c_str();
        }
    private:
        std::string message;
    };
    class RuntimeException: public Exception {
    public:
        RuntimeException(const char* msg):Exception(msg) {
            message = std::string(msg);
        }

        RuntimeException(const char* msg, const char* source, const char* func, int line):Exception(msg) {
            std::stringstream ss;
            ss << "Runtime error at (" << func << ") " << source << ":" << line << ": " << msg;
            message = ss.str();
        }

        virtual const char* what() const throw() override {
            return message.c_str();
        }
    private:
        std::string message;
    };
}

#endif