From 383d1ae1342cb636b23ca5846891439b5890cc1a Mon Sep 17 00:00:00 2001 From: seven332 Date: Mon, 30 Sep 2024 10:58:44 +0800 Subject: [PATCH] Ignore literal suffix --- tools/build_wasm.sh | 9 +++++++++ writer/include/writer/writer.h | 1 + writer/src/mini_printer.cpp | 14 +++++++++++--- writer/src/operator_group.cpp | 2 -- writer/src/writer_test.cpp | 11 +++++++++-- 5 files changed, 30 insertions(+), 7 deletions(-) create mode 100755 tools/build_wasm.sh diff --git a/tools/build_wasm.sh b/tools/build_wasm.sh new file mode 100755 index 0000000..068a621 --- /dev/null +++ b/tools/build_wasm.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +set -eu + +SOURCE_DIR=$(dirname $0)/.. +BUILD_DIR=$(dirname $0)/../out/wasm + +emcmake cmake -S $(dirname $0)/../ -B $BUILD_DIR -DCMAKE_BUILD_TYPE=Release -DDAWN_EMSCRIPTEN_TOOLCHAIN="" +emmake make -C $BUILD_DIR -j cmd diff --git a/writer/include/writer/writer.h b/writer/include/writer/writer.h index dcb3bfb..4b13d34 100644 --- a/writer/include/writer/writer.h +++ b/writer/include/writer/writer.h @@ -9,6 +9,7 @@ namespace wgslx::writer { struct Options { bool precise_float = false; bool use_type_alias = true; + bool ignore_literal_suffix = true; }; struct Result { diff --git a/writer/src/mini_printer.cpp b/writer/src/mini_printer.cpp index ba6d858..9b6ef3d 100644 --- a/writer/src/mini_printer.cpp +++ b/writer/src/mini_printer.cpp @@ -796,7 +796,7 @@ void MiniPrinter::EmitLiteral(std::stringstream& out, const tint::ast::LiteralEx [&](const tint::ast::BoolLiteralExpression* l) { out << (l->value ? "true" : "false"); }, [&](const tint::ast::FloatLiteralExpression* l) { if (options_->precise_float) { - if (l->suffix == tint::ast::FloatLiteralExpression::Suffix::kNone) { + if (l->suffix == tint::ast::FloatLiteralExpression::Suffix::kNone || options_->ignore_literal_suffix) { out << tint::strconv::DoubleToBitPreservingString(l->value); } else { out << tint::strconv::FloatToBitPreservingString(static_cast(l->value)) << l->suffix; @@ -806,10 +806,18 @@ void MiniPrinter::EmitLiteral(std::stringstream& out, const tint::ast::LiteralEx while (str.ends_with('0')) { str.pop_back(); } - out << str << l->suffix; + out << str; + if (!options_->ignore_literal_suffix) { + out << l->suffix; + } + } + }, + [&](const tint::ast::IntLiteralExpression* l) { + out << l->value; + if (!options_->ignore_literal_suffix) { + out << l->suffix; } }, - [&](const tint::ast::IntLiteralExpression* l) { out << l->value << l->suffix; }, // TINT_ICE_ON_NO_MATCH ); } diff --git a/writer/src/operator_group.cpp b/writer/src/operator_group.cpp index 0de4e8b..8ecda6d 100644 --- a/writer/src/operator_group.cpp +++ b/writer/src/operator_group.cpp @@ -1,7 +1,5 @@ #include "operator_group.h" -#include - namespace wgslx::writer { OperatorGroup toOperatorGroup(tint::core::BinaryOp op) { diff --git a/writer/src/writer_test.cpp b/writer/src/writer_test.cpp index b59f928..4062600 100644 --- a/writer/src/writer_test.cpp +++ b/writer/src/writer_test.cpp @@ -137,7 +137,7 @@ TEST(writer, expression) { for (auto i = 0; i < 100; ++i) { auto code = "const a = " + random.next() + ";"; auto program1 = Parse(code.c_str()); - auto result = Write(program1, {}); + auto result = Write(program1, {.ignore_literal_suffix = false}); auto program2 = Parse(result.wgsl.c_str()); auto r1 = tint::wgsl::writer::Generate(program1, {}); auto r2 = tint::wgsl::writer::Generate(program2, {}); @@ -213,7 +213,14 @@ TEST(writer, dawn_files) { } auto program1 = Parse(content.c_str()); - auto result = Write(program1, {.precise_float = true, .use_type_alias = false}); + auto result = Write( + program1, + { + .precise_float = true, + .use_type_alias = false, + .ignore_literal_suffix = false, + } + ); auto program2 = Parse(result.wgsl.c_str()); auto r1 = tint::wgsl::writer::Generate(program1, {}); auto r2 = tint::wgsl::writer::Generate(program2, {});