From 0d234f6bcdc307b44b0440978df79c20c108c09e Mon Sep 17 00:00:00 2001 From: Dhruvan Date: Sun, 24 Nov 2024 03:31:00 -0500 Subject: [PATCH] refactor(headers): streamline header file includes and ensure consistent guard definitions --- libhelix/core/include/cast.h | 6 +- libhelix/core/include/concepts.h | 6 +- libhelix/core/include/core.h | 1 + libhelix/core/include/dtypes.h | 415 +----------------------------- libhelix/core/include/finally.h | 6 +- libhelix/core/include/generator.h | 6 +- libhelix/core/include/libc.h | 3 +- libhelix/core/include/libcxx.h | 6 +- libhelix/core/include/print.h | 13 +- libhelix/core/include/refs.h | 4 +- libhelix/core/include/traits.h | 18 +- libhelix/core/include/types.h | 4 +- tests/test_runner.py | 12 +- 13 files changed, 51 insertions(+), 449 deletions(-) diff --git a/libhelix/core/include/cast.h b/libhelix/core/include/cast.h index 94682b3..9b7850d 100644 --- a/libhelix/core/include/cast.h +++ b/libhelix/core/include/cast.h @@ -13,12 +13,12 @@ /// /// ///------------------------------------------------------------------------------------ Helix ---/// -#ifndef __$LIBHELIX_CAST__ -#define __$LIBHELIX_CAST__ - #include "concepts.h" #include "config.h" +#ifndef __$LIBHELIX_CAST__ +#define __$LIBHELIX_CAST__ + H_NAMESPACE_BEGIN H_STD_NAMESPACE_BEGIN diff --git a/libhelix/core/include/concepts.h b/libhelix/core/include/concepts.h index 556eba3..1cdc93c 100644 --- a/libhelix/core/include/concepts.h +++ b/libhelix/core/include/concepts.h @@ -13,13 +13,13 @@ /// /// ///------------------------------------------------------------------------------------ Helix ---/// -#ifndef __$LIBHELIX_CONCEPTS__ -#define __$LIBHELIX_CONCEPTS__ - #include "config.h" #include "dtypes.h" #include "traits.h" +#ifndef __$LIBHELIX_CONCEPTS__ +#define __$LIBHELIX_CONCEPTS__ + H_NAMESPACE_BEGIN H_STD_NAMESPACE_BEGIN namespace concepts { diff --git a/libhelix/core/include/core.h b/libhelix/core/include/core.h index 58f1c64..050fb47 100644 --- a/libhelix/core/include/core.h +++ b/libhelix/core/include/core.h @@ -39,5 +39,6 @@ #include "refs.h" #include "traits.h" #include "types.h" +#include "question.h" #endif \ No newline at end of file diff --git a/libhelix/core/include/dtypes.h b/libhelix/core/include/dtypes.h index 1a451a1..7076faa 100644 --- a/libhelix/core/include/dtypes.h +++ b/libhelix/core/include/dtypes.h @@ -13,20 +13,19 @@ /// /// ///------------------------------------------------------------------------------------ Helix ---/// -#ifndef __$LIBHELIX_DTYPES__ -#define __$LIBHELIX_DTYPES__ - #include #include #include #include -#include #include #include #include -#include #include "config.h" + +#ifndef __$LIBHELIX_DTYPES__ +#define __$LIBHELIX_DTYPES__ + H_NAMESPACE_BEGIN using byte = libcxx::byte; @@ -47,409 +46,3 @@ using $int = int; H_NAMESPACE_END #endif - - -// template -// class $int { -// private: -// static constexpr size_t MAX_DIGITS = _Size; -// bool isNegative = false; -// uint8_t digits[MAX_DIGITS] = {0}; -// size_t digitCount = 1; - -// template -// constexpr $int fromNumeric(T value) const; -// constexpr void multiplyByBase(uint8_t base); -// constexpr void addDigit(uint8_t value); -// constexpr void karatsubaMultiply(const $int& other, $int& result) const; -// constexpr void fastAdd(const $int& other, $int& result) const; -// constexpr void fastSubtract(const $int& other, $int& result) const; - -// public: -// template >> -// constexpr $int(T value); -// constexpr $int() = default; -// constexpr explicit $int(const char* numStr); -// constexpr void parse(const char* numStr); -// constexpr bool operator==(const $int& other) const; -// constexpr bool operator!=(const $int& other) const; -// constexpr bool operator<(const $int& other) const; -// constexpr bool operator<=(const $int& other) const; -// constexpr bool operator>(const $int& other) const; -// constexpr bool operator>=(const $int& other) const; -// constexpr $int operator+(const $int& other) const; -// constexpr $int operator-(const $int& other) const; -// constexpr $int operator*(const $int& other) const; -// constexpr $int operator/(const $int& other) const; -// constexpr $int operator%(const $int& other) const; - -// // Overloaded operators for compatibility with all numeric types - -// template >> -// constexpr $int operator+(T value) const; - -// template >> -// constexpr $int operator-(T value) const; - -// template >> -// constexpr $int operator*(T value) const; - -// template >> -// constexpr $int operator/(T value) const; - -// template >> -// constexpr $int operator%(T value) const; - - -// template >> -// constexpr $int& operator+=(T value) { *this = *this + value; return *this; } - -// template >> -// constexpr $int& operator-=(T value) { *this = *this - value; return *this; } - -// template >> -// constexpr $int& operator*=(T value) { *this = *this * value; return *this; } - -// template >> -// constexpr $int& operator/=(T value) { *this = *this / value; return *this; } - -// template >> -// constexpr $int& operator%=(T value) { *this = *this % value; return *this; } - - -// template >> -// constexpr bool operator==(T value) const; - -// template >> -// constexpr bool operator!=(T value) const; - -// template >> -// constexpr bool operator<(T value) const; - -// template >> -// constexpr bool operator<=(T value) const; - -// template >> -// constexpr bool operator>(T value) const; - -// template >> -// constexpr bool operator>=(T value) const; -// }; - -// $int<> operator""i(const char *numStr) { return $int<>(numStr); } - -// constexpr void $int<>::multiplyByBase(uint8_t base) { -// uint16_t carry = 0; -// for (size_t i = 0; i < digitCount; ++i) { -// uint16_t result = digits[i] * base + carry; -// digits[i] = result % 10; -// carry = result / 10; -// } -// while (carry != 0 && digitCount < MAX_DIGITS) { -// digits[digitCount++] = carry % 10; -// carry /= 10; -// } -// if (carry != 0) { -// throw std::overflow_error("Exceeded maximum digit storage"); -// } -// } - -// constexpr void $int<>::addDigit(uint8_t value) { -// uint16_t carry = value; -// for (size_t i = 0; i < digitCount; ++i) { -// uint16_t result = digits[i] + carry; -// digits[i] = result % 10; -// carry = result / 10; -// if (carry == 0) { -// return; -// } -// } -// while (carry != 0 && digitCount < MAX_DIGITS) { -// digits[digitCount++] = carry % 10; -// carry /= 10; -// } -// if (carry != 0) { -// throw std::overflow_error("Exceeded maximum digit storage"); -// } -// } - -// constexpr void $int<>::karatsubaMultiply(const $int& other, $int& result) const { -// // Implement fast multiplication using Karatsuba algorithm -// if (digitCount == 1 || other.digitCount == 1) { -// uint16_t carry = 0; -// for (size_t i = 0; i < other.digitCount; ++i) { -// for (size_t j = 0; j < digitCount; ++j) { -// uint16_t res = digits[j] * other.digits[i] + result.digits[i + j] + carry; -// result.digits[i + j] = res % 10; -// carry = res / 10; -// } -// if (carry != 0) { -// result.digits[i + digitCount] = carry; -// carry = 0; -// } -// } -// result.digitCount = digitCount + other.digitCount; -// while (result.digitCount > 1 && result.digits[result.digitCount - 1] == 0) { -// --result.digitCount; -// } -// return; -// } -// // Placeholder for recursive multiplication. -// } - -// constexpr void $int<>::fastAdd(const $int& other, $int& result) const { -// size_t maxDigits = std::max(digitCount, other.digitCount); -// uint16_t carry = 0; -// for (size_t i = 0; i < maxDigits; ++i) { -// uint16_t sum = digits[i] + other.digits[i] + carry; -// result.digits[i] = sum % 10; -// carry = sum / 10; -// } -// result.digitCount = maxDigits; -// if (carry != 0) { -// result.digits[result.digitCount++] = carry; -// } -// } - -// constexpr void $int<>::fastSubtract(const $int& other, $int& result) const { -// if (*this < other) { -// throw std::invalid_argument("Subtraction result would be negative"); -// } -// uint16_t borrow = 0; -// for (size_t i = 0; i < digitCount; ++i) { -// int16_t sub = digits[i] - (i < other.digitCount ? other.digits[i] : 0) - borrow; -// if (sub < 0) { -// sub += 10; -// borrow = 1; -// } else { -// borrow = 0; -// } -// result.digits[i] = sub; -// } -// result.digitCount = digitCount; -// while (result.digitCount > 1 && result.digits[result.digitCount - 1] == 0) { -// --result.digitCount; -// } -// } - -// template -// constexpr $int $int<>::fromNumeric(T value) const { -// static_assert(std::is_arithmetic_v, "Template parameter must be a numeric type"); -// $int result; -// if constexpr (std::is_signed_v) { -// if (value < 0) { -// result.isNegative = true; -// value = -value; -// } -// } -// result.digitCount = 0; -// do { -// result.digits[result.digitCount++] = value % 10; -// value /= 10; -// } while (value != 0); -// return result; -// } - -// constexpr $int<>::$int(const char* numStr) { -// parse(numStr); -// } - -// template -// constexpr $int<>::$int(T value) { -// *this = fromNumeric(value); -// } - -// constexpr void $int<>::parse(const char* numStr) { -// std::memset(digits, 0, sizeof(digits)); -// digitCount = 1; -// isNegative = false; - -// size_t start = 0; -// int base = 10; - -// // Handle sign -// if (numStr[start] == '-') { -// isNegative = true; -// start++; -// } else if (numStr[start] == '+') { -// start++; -// } - -// // Detect base -// if (numStr[start] == '0') { -// if (numStr[start + 1] == 'x' || numStr[start + 1] == 'X') { -// base = 16; -// start += 2; -// } else if (numStr[start + 1] == 'b' || numStr[start + 1] == 'B') { -// base = 2; -// start += 2; -// } else if (std::isdigit(numStr[start + 1])) { -// base = 8; -// start += 1; -// } -// } - -// // Parse digits -// for (size_t i = start; numStr[i] != '\0'; ++i) { -// char c = numStr[i]; -// uint8_t value = 0; - -// if (c >= '0' && c <= '9') { -// value = c - '0'; -// } else if (c >= 'a' && c <= 'f') { -// value = c - 'a' + 10; -// } else if (c >= 'A' && c <= 'F') { -// value = c - 'A' + 10; -// } else { -// throw std::invalid_argument("Invalid character in numeric string"); -// } - -// if (value >= base) { -// throw std::invalid_argument("Digit exceeds base"); -// } - -// multiplyByBase(base); -// addDigit(value); -// } - -// // Remove leading zeros -// while (digitCount > 1 && digits[digitCount - 1] == 0) { -// --digitCount; -// } -// } - -// constexpr bool $int<>::operator==(const $int& other) const { -// if (isNegative != other.isNegative || digitCount != other.digitCount) { -// return false; -// } -// for (size_t i = 0; i < digitCount; ++i) { -// if (digits[i] != other.digits[i]) { -// return false; -// } -// } -// return true; -// } - -// constexpr bool $int<>::operator!=(const $int& other) const { -// return !(*this == other); -// } - -// constexpr bool $int<>::operator<(const $int& other) const { -// if (isNegative != other.isNegative) { -// return isNegative; -// } -// if (digitCount != other.digitCount) { -// return isNegative ? digitCount > other.digitCount : digitCount < other.digitCount; -// } -// for (size_t i = 0; i < digitCount; ++i) { -// if (digits[digitCount - 1 - i] != other.digits[digitCount - 1 - i]) { -// return isNegative ? digits[digitCount - 1 - i] > other.digits[digitCount - 1 - i] -// : digits[digitCount - 1 - i] < other.digits[digitCount - 1 - i]; -// } -// } -// return false; -// } - -// constexpr bool $int<>::operator<=(const $int& other) const { -// return *this < other || *this == other; -// } - -// constexpr bool $int<>::operator>(const $int& other) const { -// return !(*this <= other); -// } - -// constexpr bool $int<>::operator>=(const $int& other) const { -// return !(*this < other); -// } - -// constexpr $int $int<>::operator+(const $int& other) const { -// $int result; -// fastAdd(other, result); -// return result; -// } - -// constexpr $int $int<>::operator-(const $int& other) const { -// $int result; -// fastSubtract(other, result); -// return result; -// } - -// constexpr $int $int<>::operator*(const $int& other) const { -// $int result; -// karatsubaMultiply(other, result); -// return result; -// } - -// constexpr $int $int<>::operator/(const $int& other) const { -// // Implement fast division algorithm using long division -// if (other == $int("0")) { -// throw std::domain_error("Division by zero"); -// } -// $int result; -// $int remainder; -// for (size_t i = digitCount; i > 0; --i) { -// remainder = remainder * 10 + digits[i - 1]; -// uint8_t quotient_digit = 0; -// while (remainder >= other) { -// remainder = remainder - other; -// ++quotient_digit; -// } -// result.digits[i - 1] = quotient_digit; -// } -// result.digitCount = digitCount; -// while (result.digitCount > 1 && result.digits[result.digitCount - 1] == 0) { -// --result.digitCount; -// } -// return result; -// } - -// constexpr $int $int<>::operator%(const $int& other) const { -// // Implement fast modulo algorithm -// if (other == $int("0")) { -// throw std::domain_error("Modulo by zero"); -// } -// $int remainder; -// for (size_t i = digitCount; i > 0; --i) { -// remainder = remainder * 10 + digits[i - 1]; -// while (remainder >= other) { -// remainder = remainder - other; -// } -// } -// return remainder; -// } - -// // Overloaded operators for compatibility with all numeric types -// template -// constexpr $int $int<>::operator+(T value) const { return *this + fromNumeric(value); } -// template -// constexpr $int $int<>::operator-(T value) const { return *this - fromNumeric(value); } -// template -// constexpr $int $int<>::operator*(T value) const { return *this * fromNumeric(value); } -// template -// constexpr $int $int<>::operator/(T value) const { return *this / fromNumeric(value); } -// template -// constexpr $int $int<>::operator%(T value) const { return *this % fromNumeric(value); } - -// // template -// // constexpr $int& $int<>::operator+=(T value) { *this = *this + value; return *this; } -// // template -// // constexpr $int& $int<>::operator-=(T value) { *this = *this - value; return *this; } -// // template -// // constexpr $int& $int<>::operator*=(T value) { *this = *this * value; return *this; } -// // template -// // constexpr $int& $int<>::operator/=(T value) { *this = *this / value; return *this; } -// // template -// // constexpr $int& $int<>::operator%=(T value) { *this = *this % value; return *this; } - -// template -// constexpr bool $int<>::operator==(T value) const { return *this == fromNumeric(value); } -// template -// constexpr bool $int<>::operator!=(T value) const { return *this != fromNumeric(value); } -// template -// constexpr bool $int<>::operator<(T value) const { return *this < fromNumeric(value); } -// template -// constexpr bool $int<>::operator<=(T value) const { return *this <= fromNumeric(value); } -// template -// constexpr bool $int<>::operator>(T value) const { return *this > fromNumeric(value); } -// template -// constexpr bool $int<>::operator>=(T value) const { return *this >= fromNumeric(value); } diff --git a/libhelix/core/include/finally.h b/libhelix/core/include/finally.h index 261d7da..8b5fd2b 100644 --- a/libhelix/core/include/finally.h +++ b/libhelix/core/include/finally.h @@ -13,14 +13,14 @@ /// /// ///------------------------------------------------------------------------------------ Helix ---/// -#ifndef __$LIBHELIX_FINALLY__ -#define __$LIBHELIX_FINALLY__ - #include #include "config.h" #include "refs.h" +#ifndef __$LIBHELIX_FINALLY__ +#define __$LIBHELIX_FINALLY__ + H_NAMESPACE_BEGIN /// \include belongs to the helix standard library. diff --git a/libhelix/core/include/generator.h b/libhelix/core/include/generator.h index 0b8922a..b5c2af3 100644 --- a/libhelix/core/include/generator.h +++ b/libhelix/core/include/generator.h @@ -17,15 +17,15 @@ /// /// ///----------------------------------------------------------------------------------------------/// -#ifndef __$LIBHELIX_GENERATOR__ -#define __$LIBHELIX_GENERATOR__ - #include #include #include "config.h" #include "refs.h" +#ifndef __$LIBHELIX_GENERATOR__ +#define __$LIBHELIX_GENERATOR__ + H_NAMESPACE_BEGIN H_STD_NAMESPACE_BEGIN diff --git a/libhelix/core/include/libc.h b/libhelix/core/include/libc.h index 3d3d3e3..1fecfc5 100644 --- a/libhelix/core/include/libc.h +++ b/libhelix/core/include/libc.h @@ -13,10 +13,11 @@ /// /// ///------------------------------------------------------------------------------------ Helix ---/// +#include "config.h" + #ifndef __$LIBHELIX_LIBC__ #define __$LIBHELIX_LIBC__ -#include "config.h" H_NAMESPACE_BEGIN namespace libc { diff --git a/libhelix/core/include/libcxx.h b/libhelix/core/include/libcxx.h index 3db9e12..5c5a456 100644 --- a/libhelix/core/include/libcxx.h +++ b/libhelix/core/include/libcxx.h @@ -13,12 +13,12 @@ /// /// ///------------------------------------------------------------------------------------ Helix ---/// -#ifndef __$LIBHELIX_LIBCXX__ -#define __$LIBHELIX_LIBCXX__ - #include "config.h" #include "refs.h" +#ifndef __$LIBHELIX_LIBCXX__ +#define __$LIBHELIX_LIBCXX__ + H_NAMESPACE_BEGIN H_STD_NAMESPACE_BEGIN diff --git a/libhelix/core/include/print.h b/libhelix/core/include/print.h index 522746e..eb4b9f4 100644 --- a/libhelix/core/include/print.h +++ b/libhelix/core/include/print.h @@ -13,20 +13,21 @@ /// /// ///------------------------------------------------------------------------------------ Helix ---/// -#ifndef __$LIBHELIX_PRINT__ -#define __$LIBHELIX_PRINT__ - #include +#ifndef _MSC_VER +#include +#endif + #include "concepts.h" #include "config.h" #include "dtypes.h" #include "refs.h" #include "traits.h" -#ifndef _MSC_VER -#include -#endif + +#ifndef __$LIBHELIX_PRINT__ +#define __$LIBHELIX_PRINT__ H_NAMESPACE_BEGIN H_STD_NAMESPACE_BEGIN diff --git a/libhelix/core/include/refs.h b/libhelix/core/include/refs.h index a2d7d1a..5a588bf 100644 --- a/libhelix/core/include/refs.h +++ b/libhelix/core/include/refs.h @@ -13,11 +13,11 @@ /// /// ///------------------------------------------------------------------------------------ Helix ---/// +#include "config.h" + #ifndef __$LIBHELIX_REFS__ #define __$LIBHELIX_REFS__ -#include "config.h" - H_NAMESPACE_BEGIN H_STD_NAMESPACE_BEGIN namespace ref { diff --git a/libhelix/core/include/traits.h b/libhelix/core/include/traits.h index bb930a0..cf6971b 100644 --- a/libhelix/core/include/traits.h +++ b/libhelix/core/include/traits.h @@ -13,11 +13,11 @@ /// /// ///------------------------------------------------------------------------------------ Helix ---/// +#include "config.h" + #ifndef __$LIBHELIX_TRAITS__ #define __$LIBHELIX_TRAITS__ -#include "config.h" - H_NAMESPACE_BEGIN H_STD_NAMESPACE_BEGIN namespace traits { @@ -73,8 +73,8 @@ concept same_as = integral_constant>::value || template constexpr bool same_as_v = false; -template -constexpr bool same_as_v<_Ty, _Ty> = true; +template +constexpr bool same_as_v<_Tp, _Tp> = true; template concept same_as = same_as_v<_Tp, _Up>; @@ -91,12 +91,18 @@ struct _LIBCPP_TEMPLATE_VIS is_derived_of : public integral_constant inline constexpr bool is_derived_of_v = __is_base_of(_Bp, _Dp); +template +struct is_class : integral_constant {}; // determine whether _Tp is a class + +template +constexpr bool is_class_v = __is_class(_Tp); + namespace _const::utils { template constexpr bool is_const_v = false; - template - constexpr bool is_const_v = true; + template + constexpr bool is_const_v = true; } // namespace _const::utils template diff --git a/libhelix/core/include/types.h b/libhelix/core/include/types.h index 49674f1..6121859 100644 --- a/libhelix/core/include/types.h +++ b/libhelix/core/include/types.h @@ -13,11 +13,11 @@ /// /// ///------------------------------------------------------------------------------------ Helix ---/// +#include + #ifndef __$LIBHELIX_TYPES__ #define __$LIBHELIX_TYPES__ -#include - using u8 = unsigned char; using i8 = signed char; using u16 = unsigned short; diff --git a/tests/test_runner.py b/tests/test_runner.py index 33aa46f..4b00688 100644 --- a/tests/test_runner.py +++ b/tests/test_runner.py @@ -89,7 +89,7 @@ def compile_and_execute(compiler_path, file_path, output_path): logger.error(f"Compilation failed for {file_path}. Error: {compile_process.stderr}") return compile_process.stdout, compile_process.stderr, False - if not os.path.exists(output_path): + if not os.path.exists(output_path + (".exe" if os.name == "nt" else "")): # see if theres any error by rerunning the command with --lsp-mode and --emit-ir compile_cmd2 = [compiler_path, file_path, "--lsp-mode", "--emit-ir", "-o", output_path] logger.debug(f"Compile command: {' '.join(compile_cmd2)}") @@ -101,19 +101,19 @@ def compile_and_execute(compiler_path, file_path, output_path): logger.debug(f"Compilation successful for {file_path}") # Execute the compiled output - exec_process = subprocess.run([output_path], capture_output=True, text=True) + exec_process = subprocess.run([output_path + (".exe" if os.name == "nt" else "")], capture_output=True, text=True) logger.debug(f"Execution output: {exec_process.stdout}") return exec_process.stdout.strip(), exec_process.stderr.strip(), True finally: # Clean up the executable - if os.path.exists(output_path): - os.remove(output_path) - logger.debug(f"Cleaned up executable: {output_path}") + if os.path.exists(output_path + (".exe" if os.name == "nt" else "")): + os.remove(output_path + (".exe" if os.name == "nt" else "")) + logger.debug(f"Cleaned up executable: {output_path + ('.exe' if os.name == 'nt' else '')}") def run_test(compiler_path, folder_path, file_name): """Run a single test case.""" file_path = os.path.join(folder_path, file_name) - output_path = os.path.join(folder_path, f"{os.path.splitext(file_name)[0]}.out") + output_path = os.path.join(folder_path, f"{os.path.splitext(file_name)[0]}") logger.info(f"Running test for file: {file_name}") expected_output, is_error_check = parse_expected_output(file_path)