From ec1382f68d1132071b357c2aa3b89bc8a2017d67 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 22 Dec 2024 21:00:40 +0000 Subject: Update wasmIntConst to use exhaustive check --- .../shedlang/compiler/backends/wasm/PatioToWasm.java | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/PatioToWasm.java b/src/main/java/org/shedlang/compiler/backends/wasm/PatioToWasm.java index 707d9a87..6d9bf4da 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/PatioToWasm.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/PatioToWasm.java @@ -816,14 +816,17 @@ public class PatioToWasm { } private static WasmInstruction wasmIntConst(WasmValueType wasmValueType, int value) { - // TODO: find a better way of doing this. Turn the number types into an enum? - if (wasmValueType == WasmValueType.I32) { - return new WasmI32Const(value); - } else if (wasmValueType == WasmValueType.I64) { - return new WasmI64Const(value); - } else { - throw new IllegalArgumentException("Cannot create int const for " + wasmValueType); - } + return switch (wasmValueType) { + case WasmValueType.Number number -> { + yield switch (number.type()) { + case I32 -> new WasmI32Const(value); + case I64 -> new WasmI64Const(value); + case F32, F64 -> { + throw new IllegalArgumentException("Cannot create int const for " + number); + } + }; + } + }; } private static WasmInstruction wasmIntEq(WasmValueType wasmValueType) { -- cgit v1.2.3