From eef1cb2a691362585e15db59b778075e51a54c2f Mon Sep 17 00:00:00 2001 From: Folkert Date: Fri, 19 Mar 2021 00:27:34 +0100 Subject: [PATCH] use multi-backpassing in base64 example --- compiler/parse/src/expr.rs | 2 + examples/benchmarks/Base64/Decode.roc | 60 ++++++++++++--------------- 2 files changed, 29 insertions(+), 33 deletions(-) diff --git a/compiler/parse/src/expr.rs b/compiler/parse/src/expr.rs index 4986fbb6d6..6e34bfdf70 100644 --- a/compiler/parse/src/expr.rs +++ b/compiler/parse/src/expr.rs @@ -1224,6 +1224,8 @@ fn parse_expr_end<'a>( match word2(b'<', b'-', EExpr::BackpassArrow).parse(arena, state) { Err((_, fail, state)) => Err((MadeProgress, fail, state)), Ok((_, _, state)) => { + let min_indent = start.col; + let parse_body = space0_before_e( move |a, s| parse_loc_expr(min_indent + 1, a, s), min_indent, diff --git a/examples/benchmarks/Base64/Decode.roc b/examples/benchmarks/Base64/Decode.roc index beb0d46077..71d33e6ba9 100644 --- a/examples/benchmarks/Base64/Decode.roc +++ b/examples/benchmarks/Base64/Decode.roc @@ -11,47 +11,41 @@ decodeBase64 = \width -> Bytes.Decode.loop loopHelp { remaining: width, string: loopHelp : { remaining : Nat, string : Str } -> Decoder (Bytes.Decode.Step { remaining : Nat, string : Str } Str) loopHelp = \{ remaining, string } -> if remaining >= 3 then - Bytes.Decode.map3 - Bytes.Decode.u8 - Bytes.Decode.u8 - Bytes.Decode.u8 - \x, y, z -> - a : U32 - a = Num.intCast x - b : U32 - b = Num.intCast y - c : U32 - c = Num.intCast z - combined = Num.bitwiseOr (Num.bitwiseOr (Num.shiftLeftBy 16 a) (Num.shiftLeftBy 8 b)) c - Loop - { - remaining: remaining - 3, - string: Str.concat string (bitsToChars combined 0) - } + x, y, z <- Bytes.Decode.map3 Bytes.Decode.u8 Bytes.Decode.u8 Bytes.Decode.u8 + + a : U32 + a = Num.intCast x + b : U32 + b = Num.intCast y + c : U32 + c = Num.intCast z + combined = Num.bitwiseOr (Num.bitwiseOr (Num.shiftLeftBy 16 a) (Num.shiftLeftBy 8 b)) c + Loop + { + remaining: remaining - 3, + string: Str.concat string (bitsToChars combined 0) + } else if remaining == 0 then Bytes.Decode.succeed (Done string) else if remaining == 2 then - Bytes.Decode.map2 - Bytes.Decode.u8 - Bytes.Decode.u8 - \x, y -> - a : U32 - a = Num.intCast x - b : U32 - b = Num.intCast y - combined = Num.bitwiseOr (Num.shiftLeftBy 16 a) (Num.shiftLeftBy 8 b) - Done (Str.concat string (bitsToChars combined 1)) + x, y <- Bytes.Decode.map2 Bytes.Decode.u8 Bytes.Decode.u8 + + a : U32 + a = Num.intCast x + b : U32 + b = Num.intCast y + combined = Num.bitwiseOr (Num.shiftLeftBy 16 a) (Num.shiftLeftBy 8 b) + Done (Str.concat string (bitsToChars combined 1)) else # remaining = 1 - Bytes.Decode.map - Bytes.Decode.u8 - \x -> - a : U32 - a = Num.intCast x - Done (Str.concat string (bitsToChars (Num.shiftLeftBy 16 a) 2)) + x <- Bytes.Decode.map Bytes.Decode.u8 + + a : U32 + a = Num.intCast x + Done (Str.concat string (bitsToChars (Num.shiftLeftBy 16 a) 2)) bitsToChars : U32, Int * -> Str