From 5b3afc2287adc846c87ea07d73531af6ea21ee2d Mon Sep 17 00:00:00 2001 From: Pabloader Date: Thu, 19 Jun 2025 08:19:53 +0000 Subject: [PATCH] C++26 --- build/wasmPlugin.ts | 2 +- compile_flags.txt | 2 +- src/games/playground/awoo.cpp | 96 +++++++++++++++++++++++++++++++--- src/games/playground/index.tsx | 2 +- 4 files changed, 92 insertions(+), 10 deletions(-) diff --git a/build/wasmPlugin.ts b/build/wasmPlugin.ts index 32316e4..06a049b 100644 --- a/build/wasmPlugin.ts +++ b/build/wasmPlugin.ts @@ -167,7 +167,7 @@ const wasmPlugin = ({ production }: WasmLoaderConfig = {}): BunPlugin => { ...features, '-I', include, ]; - const std = args.path.endsWith('.cpp') ? '-std=gnu++23' : '-std=gnu23'; + const std = args.path.endsWith('.cpp') ? '-std=gnu++26' : '-std=gnu23'; const compileResult = await $`${cc.cc} -c ${flags} ${std} -o ${objPath} ${args.path}`; if (compileResult.exitCode !== 0) { diff --git a/compile_flags.txt b/compile_flags.txt index 0e10a5a..cfc6ef8 100644 --- a/compile_flags.txt +++ b/compile_flags.txt @@ -1,6 +1,6 @@ --target=wasm32-wasi --sysroot=dist/wasi/share/wasi-sysroot --std=c++23 +-std=c++26 -fno-exceptions -I build/assets/include diff --git a/src/games/playground/awoo.cpp b/src/games/playground/awoo.cpp index 9b462e2..ed88372 100644 --- a/src/games/playground/awoo.cpp +++ b/src/games/playground/awoo.cpp @@ -1,12 +1,94 @@ +#include +#include +#include +#include #include #include +#include +#include +#include -struct boob { - int x; - boob(int x_): x(x_) {}; - operator int() { return rand() + x; } -} boob(rand()); +class bit { + private: + static constexpr bit nor(const bit& a, const bit& b); -EXPORT(awoo) auto awoo() -> int { - return boob; + public: + bit() = default; + bit(bool n) : n(n), composition(n ? "1" : "0") { + } + bit(bool n, std::string_view composition) : n(n), composition(composition) { + } + bit(const bit& other) : bit(other.n, other.composition) { + } + + constexpr bit operator!() const { + const bit& a = *this; + return nor(a, a); + } + + constexpr bit operator|(const bit& b) const { + const bit& a = *this; + return !nor(a, b); + } + + constexpr bit operator&(const bit& b) const { + const bit& a = *this; + return nor(!a, !b); + } + + constexpr bit operator^(const bit& b) const { + const bit& a = *this; + + return (!a & b) | (a & !b); + } + + constexpr std::pair operator+(const bit& b) const { + const bit& a = *this; + + return { + a ^ b, + a & b, + }; + } + + operator bool() const { + return n; + } + + friend std::ostream& operator<<(std::ostream& stream, const bit& val) { + return stream << val.composition; + } + + friend std::istream& operator>>(std::istream& stream, bit& val) { + return stream >> val.n; + } + + private: + bool n = 0; + std::string composition = ""; +}; + +constexpr bit bit::nor(const bit& a, const bit& b) { + bit result{ + !(a.n || b.n), + "(" + a.composition + " NOR " + b.composition + ")", + }; + + return result; } + +uint64_t awoo() { + bit a {1, "a"}; + bit b {1, "b"}; + + auto [sum, carry] = a + b; + + std::cout << "sum = " << sum << "\ncarry = " << carry << std::endl; + + return sum; +} + +EXPORT(play) void play() { + auto awoo = std::format("{2} {1}{0}!\n", 23, "C++", "Hello"); + std::puts(awoo.c_str()); +} \ No newline at end of file diff --git a/src/games/playground/index.tsx b/src/games/playground/index.tsx index b9cedd8..b141282 100644 --- a/src/games/playground/index.tsx +++ b/src/games/playground/index.tsx @@ -1,5 +1,5 @@ import awoo from "./awoo.cpp"; export default function main() { - console.log(awoo, awoo.awoo()); + console.log(awoo, awoo.play()); } \ No newline at end of file