@@ -660,68 +660,32 @@ raw_instr :: { [Instruction] }
660
660
then Right $ [BlockInstr $2 tu (instr ++ instr' )]
661
661
else Left " Block labels have to match"
662
662
}
663
- | ' loop' opt(ident) raw_loop {% (: []) `fmap` $3 $2 }
664
- | ' if' opt(ident) raw_if_result {% $3 $2 }
665
-
666
- raw_loop :: { Maybe Ident -> Either String Instruction }
667
- : ' end' opt(ident) {
668
- \ident ->
669
- if ident == $2 || isNothing $2
670
- then Right $ LoopInstr ident [] []
671
- else Left " Loop labels have to match"
672
- }
673
- | raw_instr list (instruction) 'end' opt(ident) {
674
- \ident ->
675
- if ident == $4 || isNothing $4
676
- then Right $ LoopInstr ident [] ($1 ++ concat $2 )
677
- else Left " Loop labels have to match"
678
- }
679
- | ' (' raw_loop1 { $2 }
680
-
681
- raw_loop1 :: { Maybe Ident -> Either String Instruction }
682
- : ' result' list(valtype) ' )' list(instruction) ' end' opt(ident) {
683
- \ident ->
684
- if ident == $6 || isNothing $6
685
- then Right $ LoopInstr ident $2 (concat $4 )
686
- else Left " Loop labels have to match"
687
- }
688
- | folded_instr1 list (instruction) 'end' opt(ident) {
689
- \ident ->
690
- if ident == $4 || isNothing $4
691
- then Right $ LoopInstr ident [] ($1 ++ concat $2 )
692
- else Left " Loop labels have to match"
693
- }
694
-
695
- raw_if_result :: { Maybe Ident -> Either String [Instruction] }
696
- : raw_else {
697
- \ident ->
698
- if ident == (snd $1 ) || isNothing (snd $1 )
699
- then Right [IfInstr ident [] [] $ fst $1 ]
700
- else Left " If labels have to match"
663
+ | ' loop' opt(ident) typeuse_cont(pair(folded_instr1_list, block_end), block_end) {%
664
+ let (tu, rest) = $3 in
665
+ let (instr, (instr' , identAfter)) = either (\a -> ([], a)) id rest in
666
+ if $2 == identAfter || isNothing identAfter
667
+ then Right $ [LoopInstr $2 tu (instr ++ instr' )]
668
+ else Left " Block labels have to match"
701
669
}
702
- | raw_instr list (instruction) raw_else {
703
- \ident ->
704
- if ident == (snd $3 ) || isNothing (snd $3 )
705
- then Right [IfInstr ident [] ($1 ++ concat $2 ) $ fst $3 ]
706
- else Left " If labels have to match"
670
+ | ' if' opt(ident) typeuse_cont(pair(folded_instr1_list, raw_if_end), raw_if_end) {%
671
+ let (tu, rest) = $3 in
672
+ let (trueBranch, falseBranch, identAfter) = either id (\(t, (t' , f, i)) -> (t ++ t' , f, i)) rest in
673
+ if $2 == identAfter || isNothing identAfter
674
+ then Right $ [IfInstr $2 tu trueBranch falseBranch]
675
+ else Left " If labels have to match"
707
676
}
708
- | ' (' raw_if_result1 { $2 }
709
677
710
- raw_if_result1 :: { Maybe Ident -> Either String [Instruction] }
711
- : ' result' list(valtype) ' )' list(instruction) raw_else {
712
- \ident ->
713
- if ident == (snd $5 ) || isNothing (snd $5 )
714
- then Right [IfInstr ident $2 (concat $4 ) $ fst $5 ]
715
- else Left " If labels have to match"
678
+ raw_if_end
679
+ : raw_if_else {
680
+ let (falseBranch, ident) = $1 in
681
+ ([], falseBranch, ident)
716
682
}
717
- | folded_instr1 list (instruction) raw_else {
718
- \ident ->
719
- if ident == (snd $3 ) || isNothing (snd $3 )
720
- then Right [IfInstr ident [] ($1 ++ concat $2 ) $ fst $3 ]
721
- else Left " If labels have to match"
683
+ | raw_instr list (instruction) raw_if_else {
684
+ let (falseBranch, ident) = $3 in
685
+ ($1 ++ concat $2 , falseBranch, ident)
722
686
}
723
687
724
- raw_else :: { ([Instruction], Maybe Ident) }
688
+ raw_if_else :: { ([Instruction], Maybe Ident) }
725
689
: ' end' opt(ident) { ([], $2 ) }
726
690
| ' else' opt(ident) list(instruction) ' end' opt(ident) {%
727
691
if matchIdents $2 $5
@@ -748,28 +712,14 @@ folded_instr1 :: { [Instruction] }
748
712
let (instr, instr' ) = either (\a -> ([], a)) id rest in
749
713
[BlockInstr $2 typeUse (instr ++ instr' )]
750
714
}
751
- | ' loop' opt(ident) folded_loop { [$3 $2 ] }
752
- | ' if' opt(ident) ' (' folded_if_result { $4 $2 }
753
-
754
- folded_loop :: { Maybe Ident -> Instruction }
755
- : ' )' { \ident -> LoopInstr ident [] [] }
756
- | ' (' folded_loop1 { $2 }
757
- | raw_instr list (instruction) ')' { \ident -> LoopInstr ident [] ($1 ++ concat $2 ) }
758
-
759
- folded_loop1 :: { Maybe Ident -> Instruction }
760
- : ' result' list(valtype) ' )' list(instruction) ' )' { \ident -> LoopInstr ident $2 (concat $4 ) }
761
- | folded_instr1 list (instruction) ')' { \ident -> LoopInstr ident [] ($1 ++ concat $2 ) }
762
-
763
- folded_if_result :: { Maybe Ident -> [Instruction] }
764
- : ' result' list(valtype) ' )' ' (' folded_then_else {
765
- \ident ->
766
- let (pred, (trueBranch, falseBranch)) = $5 in
767
- pred ++ [IfInstr ident $2 trueBranch falseBranch]
715
+ | ' loop' opt(ident) typeuse_cont(pair(folded_instr1_list, instr_list_closed), instr_list_closed) {
716
+ let (typeUse, rest) = $3 in
717
+ let (instr, instr' ) = either (\a -> ([], a)) id rest in
718
+ [LoopInstr $2 typeUse (instr ++ instr' )]
768
719
}
769
- | folded_then_else {
770
- \ident ->
771
- let (pred, (trueBranch, falseBranch)) = $1 in
772
- pred ++ [IfInstr ident [] trueBranch falseBranch]
720
+ | ' if' opt(ident) ' (' typeuse1_cont(folded_then_else, never) {
721
+ let (typeUse, Right (pred, (trueBranch, falseBranch))) = $4 in
722
+ pred ++ [IfInstr $2 typeUse trueBranch falseBranch]
773
723
}
774
724
775
725
folded_then_else :: { ([Instruction], ([Instruction], [Instruction])) }
@@ -1225,12 +1175,12 @@ data Instruction =
1225
1175
}
1226
1176
| LoopInstr {
1227
1177
label :: Maybe Ident,
1228
- resultType :: [ValueType] ,
1178
+ blockType :: TypeUse ,
1229
1179
body :: [Instruction]
1230
1180
}
1231
1181
| IfInstr {
1232
1182
label :: Maybe Ident,
1233
- resultType :: [ValueType] ,
1183
+ blockType :: TypeUse ,
1234
1184
trueBranch :: [Instruction],
1235
1185
falseBranch :: [Instruction]
1236
1186
}
@@ -1503,10 +1453,10 @@ desugarize fields = do
1503
1453
matchTypeUse defs typeUse
1504
1454
extractTypeDefFromInstruction defs (BlockInstr { body, blockType }) =
1505
1455
extractTypeDefFromInstructions (matchTypeUse defs blockType) body
1506
- extractTypeDefFromInstruction defs (LoopInstr { body }) =
1507
- extractTypeDefFromInstructions defs body
1508
- extractTypeDefFromInstruction defs (IfInstr { trueBranch, falseBranch }) =
1509
- extractTypeDefFromInstructions defs $ trueBranch ++ falseBranch
1456
+ extractTypeDefFromInstruction defs (LoopInstr { body, blockType }) =
1457
+ extractTypeDefFromInstructions (matchTypeUse defs blockType) body
1458
+ extractTypeDefFromInstruction defs (IfInstr { blockType, trueBranch, falseBranch }) =
1459
+ extractTypeDefFromInstructions (matchTypeUse defs blockType) $ trueBranch ++ falseBranch
1510
1460
extractTypeDefFromInstruction defs _ = defs
1511
1461
1512
1462
funcTypesEq :: FuncType -> FuncType -> Bool
@@ -1659,14 +1609,26 @@ desugarize fields = do
1659
1609
Just idx -> return $ S.TypeIndex idx
1660
1610
Nothing -> Left " unknown type"
1661
1611
S.Block bt <$> mapM (synInstrToStruct ctx' ) body
1662
- synInstrToStruct ctx LoopInstr {label, resultType, body} =
1663
- let ctx' = ctx { ctxLabels = label : ctxLabels ctx } in
1664
- S.Loop resultType <$> mapM (synInstrToStruct ctx' ) body
1665
- synInstrToStruct ctx IfInstr {label, resultType, trueBranch, falseBranch} = do
1612
+ synInstrToStruct ctx@FunCtx { ctxMod = Module { types } } LoopInstr {label, blockType, body} = do
1613
+ let ctx' = ctx { ctxLabels = label : ctxLabels ctx }
1614
+ bt <- case blockType of
1615
+ AnonimousTypeUse (FuncType [] []) -> return $ S.Inline Nothing
1616
+ AnonimousTypeUse (FuncType [] [vt]) -> return $ S.Inline (Just vt)
1617
+ typed -> case getTypeIndex types typed of
1618
+ Just idx -> return $ S.TypeIndex idx
1619
+ Nothing -> Left " unknown type"
1620
+ S.Loop bt <$> mapM (synInstrToStruct ctx' ) body
1621
+ synInstrToStruct ctx@FunCtx { ctxMod = Module { types } } IfInstr {label, blockType, trueBranch, falseBranch} = do
1666
1622
let ctx' = ctx { ctxLabels = label : ctxLabels ctx }
1623
+ bt <- case blockType of
1624
+ AnonimousTypeUse (FuncType [] []) -> return $ S.Inline Nothing
1625
+ AnonimousTypeUse (FuncType [] [vt]) -> return $ S.Inline (Just vt)
1626
+ typed -> case getTypeIndex types typed of
1627
+ Just idx -> return $ S.TypeIndex idx
1628
+ Nothing -> Left " unknown type"
1667
1629
trueBranch' <- mapM (synInstrToStruct ctx' ) trueBranch
1668
1630
falseBranch' <- mapM (synInstrToStruct ctx' ) falseBranch
1669
- return $ S.If resultType trueBranch' falseBranch'
1631
+ return $ S.If bt trueBranch' falseBranch'
1670
1632
1671
1633
synFunctionToStruct :: Module -> Function -> Either String S.Function
1672
1634
synFunctionToStruct mod Function { funcType, locals, body } = do
0 commit comments