A simple scripting language in C++
Ferenc Szontágh
2025-04-13 86904d513734134beffc29c6f4012d53a99f25c5
src/ScriptExceptionMacros.h
@@ -1,19 +1,43 @@
#ifndef SCRIPT_EXCEPTION_MACROS_H
#define SCRIPT_EXCEPTION_MACROS_H
#include "ScriptException.hpp"
//
// Purpose of macros: unified exception handling with extended error information (source file and line number)
//
// Invalid token type - expected different type
#define THROW_UNEXPECTED_TOKEN_ERROR(token, expected) \
    ScriptInterpreter::throwUnexpectedTokenError(token, expected, __FILE__, __LINE__)
    throw ScriptException::makeUnexpectedTokenError(token, expected, __FILE__, __LINE__)
#define THROW_UNEXPECTED_TOKEN_ERROR_HELPER(token, expected, file, line) \
    throw ScriptException::makeUnexpectedTokenError(token, expected, file, line)
// Accessing unknown (undefined) variable
#define THROW_UNDEFINED_VARIABLE_ERROR(name, token) \
    ScriptInterpreter::throwUndefinedVariableError(name, token, __FILE__, __LINE__)
    throw ScriptException::makeUndefinedVariableError(name, token, __FILE__, __LINE__)
#define THROW_VARIABLE_TYPE_MISSMATCH_ERROR(target_variable_name, target_variable_type, source_variable_name,       \
                                            source_variable_type, token)                                            \
    ScriptInterpreter::throwVariableTypeMissmatchError(target_variable_name, target_variable_type,                  \
                                                       source_variable_name, source_variable_type, token, __FILE__, \
                                                       __LINE__)
// Unknown (undefined) function call
#define THROW_UNDEFINED_FUNCTION_ERROR(name, token) \
    throw ScriptException::makeUndefinedFunctionError(name, token, __FILE__, __LINE__)
// Variable type mismatch - e.g. string instead of number
#define THROW_VARIABLE_TYPE_MISSMATCH_ERROR(target_variable_name, target_variable_type, source_variable_name,         \
                                            source_variable_type, token)                                              \
    throw ScriptException::makeVariableTypeMismatchError(target_variable_name, target_variable_type,                  \
                                                         source_variable_name, source_variable_type, token, __FILE__, \
                                                         __LINE__)
// Redefining a variable with the same name is not allowed
#define THROW_VARIABLE_REDEFINITION_ERROR(name, token) \
    ScriptInterpreter::throwVariableRedefinitionError(name, token, __FILE__, __LINE__)
    throw ScriptException::makeVariableRedefinitionError(name, token, __FILE__, __LINE__)
#define THROW_FUNCTION_REDEFINITION_ERROR(name, token) \
    throw ScriptException::makeFunctionRedefinitionError(name, token, __FILE__, __LINE__)
// Invalid or incorrect function argument
#define THROW_INVALID_FUNCTION_ARGUMENT_ERROR(functionName, argName, token) \
    throw ScriptException::makeFunctionInvalidArgumentError(functionName, argName, token, __FILE__, __LINE__)
#endif  // SCRIPT_EXCEPTION_MACROS_H