Kind1
Kind1 copied to clipboard
Improve monadic syntax to support switch and case
It could help to have support of switch and case/if inside monads. E.g. for a binary format reader:
Wasm.Reader.val_type: Wasm.Reader(Wasm.ValType)
(rst)
open rst
let len = 1#32
def f = ((rst)
rst@offset <- U32.add(rst@offset, len)
) :: Wasm.Reader.State -> Wasm.Reader.State
if Wasm.Reader.State.ensure_bounds(rst, len) then
let byte = Buffer8.get(rst.offset, rst.buffer)
switch U8.eql(byte) {
Wasm.Encoding.i32: Wasm.Reader.Reply.value!(f(rst), Wasm.ValType.i32),
Wasm.Encoding.i64: Wasm.Reader.Reply.value!(f(rst), Wasm.ValType.i64),
Wasm.Encoding.f32: Wasm.Reader.Reply.value!(f(rst), Wasm.ValType.f32),
Wasm.Encoding.f64: Wasm.Reader.Reply.value!(f(rst), Wasm.ValType.f64),
} default Wasm.Reader.Reply.fail!(
rst.start,
rst.offset,
Wasm.Reader.Error.Code.invalid_val_type
)
else
let code = Wasm.Reader.Error.Code.unexpected_end
Wasm.Reader.Reply.fail!(rst.start, rst.offset, code)
It could be rewritten maybe as:
Wasm.Reader.val_type: Wasm.Reader(Wasm.ValType)
Wasm.Reader {
get byte = Wasm.Reader.u8
switch U8.eql(byte) {
Wasm.Encoding.i32: return Wasm.ValType.i32,
Wasm.Encoding.i64: return Wasm.ValType.i64,
Wasm.Encoding.f32: return Wasm.ValType.f32,
Wasm.Encoding.f64: return Wasm.ValType.f64,
} default Wasm.Reader.fail("Expected a Value Type")
}
I'm thinking about that, but that would be different to the way Haskell does it. I'm not sure if there is a good reason not to do that.
Closed because the repository now only contains the code for the Kind2 language.