Skip to content

Commit

Permalink
Ignore literal suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
seven332 committed Sep 30, 2024
1 parent 6c493fa commit 383d1ae
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
9 changes: 9 additions & 0 deletions tools/build_wasm.sh
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions writer/include/writer/writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 11 additions & 3 deletions writer/src/mini_printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<float>(l->value)) << l->suffix;
Expand All @@ -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
);
}
Expand Down
2 changes: 0 additions & 2 deletions writer/src/operator_group.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "operator_group.h"

#include <cstdint>

namespace wgslx::writer {

OperatorGroup toOperatorGroup(tint::core::BinaryOp op) {
Expand Down
11 changes: 9 additions & 2 deletions writer/src/writer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, {});
Expand Down Expand Up @@ -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, {});
Expand Down

0 comments on commit 383d1ae

Please sign in to comment.