Skip to content

Commit

Permalink
Add migration code for f32/f64.
Browse files Browse the repository at this point in the history
Add a `WIT_REQUIRE_F32_F64` environment variable controlling whether
the `float32` and `float64` syntax is acccepted, similar to the
transition for semicolons.
  • Loading branch information
sunfishcode committed Jan 9, 2024
1 parent 0616ef1 commit 8bb43ec
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
22 changes: 20 additions & 2 deletions crates/wit-component/src/printing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use wit_parser::*;

// NB: keep in sync with `crates/wit-parser/src/ast/lex.rs`
const PRINT_SEMICOLONS_DEFAULT: bool = true;
const PRINT_F32_F64_DEFAULT: bool = true;

/// A utility for printing WebAssembly interface definitions to a string.
pub struct WitPrinter {
Expand All @@ -19,6 +20,7 @@ pub struct WitPrinter {
emit_docs: bool,

print_semicolons: bool,
print_f32_f64: bool,
}

impl Default for WitPrinter {
Expand All @@ -31,6 +33,10 @@ impl Default for WitPrinter {
Ok(s) => s == "1",
Err(_) => PRINT_SEMICOLONS_DEFAULT,
},
print_f32_f64: match std::env::var("WIT_REQUIRE_F32_F64") {
Ok(s) => s == "1",
Err(_) => PRINT_F32_F64_DEFAULT,
},
}
}
}
Expand Down Expand Up @@ -437,8 +443,20 @@ impl WitPrinter {
Type::S16 => self.output.push_str("s16"),
Type::S32 => self.output.push_str("s32"),
Type::S64 => self.output.push_str("s64"),
Type::Float32 => self.output.push_str("float32"),
Type::Float64 => self.output.push_str("float64"),
Type::Float32 => {
if self.print_f32_f64 {
self.output.push_str("f32")
} else {
self.output.push_str("float32")
}
}
Type::Float64 => {
if self.print_f32_f64 {
self.output.push_str("f64")
} else {
self.output.push_str("float64")
}
}
Type::Char => self.output.push_str("char"),
Type::String => self.output.push_str("string"),

Expand Down
15 changes: 13 additions & 2 deletions crates/wit-parser/src/ast/lex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct Tokenizer<'a> {
span_offset: u32,
chars: CrlfFold<'a>,
require_semicolons: bool,
require_f32_f64: bool,
}

#[derive(Clone)]
Expand Down Expand Up @@ -118,12 +119,14 @@ pub enum Error {

// NB: keep in sync with `crates/wit-component/src/printing.rs`.
const REQUIRE_SEMICOLONS_BY_DEFAULT: bool = true;
const REQUIRE_F32_F64_BY_DEFAULT: bool = true;

impl<'a> Tokenizer<'a> {
pub fn new(
input: &'a str,
span_offset: u32,
require_semicolons: Option<bool>,
require_f32_f64: Option<bool>,
) -> Result<Tokenizer<'a>> {
detect_invalid_input(input)?;

Expand All @@ -139,6 +142,12 @@ impl<'a> Tokenizer<'a> {
Err(_) => REQUIRE_SEMICOLONS_BY_DEFAULT,
}
}),
require_f32_f64: require_f32_f64.unwrap_or_else(|| {
match std::env::var("WIT_REQUIRE_F32_F64") {
Ok(s) => s == "1",
Err(_) => REQUIRE_F32_F64_BY_DEFAULT,
}
}),
};
// Eat utf-8 BOM
t.eatc('\u{feff}');
Expand Down Expand Up @@ -285,8 +294,10 @@ impl<'a> Tokenizer<'a> {
"s16" => S16,
"s32" => S32,
"s64" => S64,
"f32" | "float32" => Float32,
"f64" | "float64" => Float64,
"f32" => Float32,
"f64" => Float64,
"float32" if !self.require_f32_f64 => Float32,
"float64" if !self.require_f32_f64 => Float64,
"char" => Char,
"resource" => Resource,
"own" => Own,
Expand Down

0 comments on commit 8bb43ec

Please sign in to comment.