mirror of
https://github.com/anoma/juvix.git
synced 2024-12-03 09:41:10 +03:00
Formatter: add braces when the iterator body is not enclosed (#3122)
* Closes #3091 * Formatter adds braces when the body is not enclosed in braces or parentheses. Braces-enclosed body is always printed as a block on a new line: ``` for (acc := 0) (x in lst) { x + acc } ``` * If the body is enclosed in ordinary parentheses, then they are preserved and the iterator is printed on a single line, if possible: ``` for (acc := 0) (x in lst) (x + acc) ``` This is sometimes useful when you want iterator application as an argument to something.
This commit is contained in:
parent
ddca867871
commit
3cf79faafb
@ -304,17 +304,17 @@ instance (SingI s) => PrettyPrint (Range s) where
|
|||||||
n <+> ppCode _rangeInKw <+> e
|
n <+> ppCode _rangeInKw <+> e
|
||||||
|
|
||||||
ppIterator :: forall r s. (Members '[ExactPrint, Reader Options] r, SingI s) => IsTop -> Iterator s -> Sem r ()
|
ppIterator :: forall r s. (Members '[ExactPrint, Reader Options] r, SingI s) => IsTop -> Iterator s -> Sem r ()
|
||||||
ppIterator isTop Iterator {..} = do
|
ppIterator _isTop Iterator {..} = do
|
||||||
let n = ppIdentifierType _iteratorName
|
let n = ppIdentifierType _iteratorName
|
||||||
is = ppCode <$> _iteratorInitializers
|
is = ppCode <$> _iteratorInitializers
|
||||||
rngs = ppCode <$> _iteratorRanges
|
rngs = ppCode <$> _iteratorRanges
|
||||||
is' = parens . hsepSemicolon <$> nonEmpty is
|
is' = parens . hsepSemicolon <$> nonEmpty is
|
||||||
rngs' = parens . hsepSemicolon <$> nonEmpty rngs
|
rngs' = parens . hsepSemicolon <$> nonEmpty rngs
|
||||||
b
|
b
|
||||||
| _iteratorBodyBraces = braces (oneLineOrNextNoIndent (ppTopExpressionType _iteratorBody))
|
| _iteratorBodyBraces = space <> braces (blockIndent (ppTopExpressionType _iteratorBody))
|
||||||
| otherwise = line <> ppMaybeTopExpression isTop _iteratorBody
|
| otherwise = parens (oneLineOrNextNoIndent (ppTopExpressionType _iteratorBody))
|
||||||
parensIf _iteratorParens $
|
parensIf _iteratorParens $
|
||||||
hang (n <+?> is' <+?> rngs' <> b)
|
n <+?> is' <+?> rngs' <> b
|
||||||
|
|
||||||
instance PrettyPrint S.AName where
|
instance PrettyPrint S.AName where
|
||||||
ppCode n = annotated (AnnKind (S.getNameKind n)) (noLoc (pretty (n ^. S.anameVerbatim)))
|
ppCode n = annotated (AnnKind (S.getNameKind n)) (noLoc (pretty (n ^. S.anameVerbatim)))
|
||||||
|
@ -1615,9 +1615,10 @@ checkSections sec = topBindings helper
|
|||||||
fs <-
|
fs <-
|
||||||
failMaybe $
|
failMaybe $
|
||||||
mkRec
|
mkRec
|
||||||
^? constructorRhs
|
^? ( constructorRhs
|
||||||
. _ConstructorRhsRecord
|
. _ConstructorRhsRecord
|
||||||
. to mkRecordNameSignature
|
. to mkRecordNameSignature
|
||||||
|
)
|
||||||
let info =
|
let info =
|
||||||
RecordInfo
|
RecordInfo
|
||||||
{ _recordInfoSignature = fs,
|
{ _recordInfoSignature = fs,
|
||||||
|
@ -931,7 +931,8 @@ iterator = do
|
|||||||
do
|
do
|
||||||
(_iteratorBody, _iteratorBodyBraces) <-
|
(_iteratorBody, _iteratorBodyBraces) <-
|
||||||
(,True) <$> braces parseExpressionAtoms
|
(,True) <$> braces parseExpressionAtoms
|
||||||
<|> (,False) <$> parseExpressionAtoms
|
<|> (,False) <$> parens parseExpressionAtoms
|
||||||
|
<|> (,True) <$> parseExpressionAtoms
|
||||||
let _iteratorParens = False
|
let _iteratorParens = False
|
||||||
return $ Iterator {..}
|
return $ Iterator {..}
|
||||||
where
|
where
|
||||||
|
@ -24,11 +24,14 @@ gen : Nat → Tree
|
|||||||
|
|
||||||
preorder : Tree → List Nat
|
preorder : Tree → List Nat
|
||||||
| leaf := 0 :: nil
|
| leaf := 0 :: nil
|
||||||
| (node1 c) := 1 :: preorder c
|
| (node1 c) := 1 :: preorder c
|
||||||
| (node2 l r) := 2 :: preorder l ++ preorder r
|
| (node2 l r) := 2 :: preorder l ++ preorder r
|
||||||
| (node3 l m r) := 3 :: preorder l ++ preorder m ++ preorder r;
|
| (node3 l m r) := 3 :: preorder l ++ preorder m ++ preorder r;
|
||||||
|
|
||||||
combineDigits (xs : List Nat) : Nat := for (acc := 0) (x in xs) acc * 10 + x;
|
combineDigits (xs : List Nat) : Nat :=
|
||||||
|
for (acc := 0) (x in xs) {
|
||||||
|
acc * 10 + x
|
||||||
|
};
|
||||||
|
|
||||||
main : Nat :=
|
main : Nat :=
|
||||||
trace (combineDigits (preorder (gen 3)))
|
trace (combineDigits (preorder (gen 3)))
|
||||||
|
@ -7,6 +7,6 @@ type BoxedList :=
|
|||||||
| boxed (List Nat);
|
| boxed (List Nat);
|
||||||
|
|
||||||
sumBoxedList : BoxedList -> Nat
|
sumBoxedList : BoxedList -> Nat
|
||||||
| (boxed l) := for (acc := 0) (x in l) acc + x;
|
| (boxed l) := for (acc := 0) (x in l) (acc + x);
|
||||||
|
|
||||||
main : Nat := sumBoxedList (boxed [1;2;3]);
|
main : Nat := sumBoxedList (boxed [1;2;3]);
|
||||||
|
@ -4,8 +4,7 @@ module test054;
|
|||||||
import Stdlib.Prelude open;
|
import Stdlib.Prelude open;
|
||||||
|
|
||||||
syntax iterator myfor;
|
syntax iterator myfor;
|
||||||
myfor : {A B : Type} → (A → B → A) → A → List B → A :=
|
myfor : {A B : Type} → (A → B → A) → A → List B → A := foldl {_} {_} {{_}} {_};
|
||||||
foldl {_} {_} {{_}} {_};
|
|
||||||
|
|
||||||
syntax iterator mymap {init := 0};
|
syntax iterator mymap {init := 0};
|
||||||
mymap : {A B : Type} → (A → B) → List A → List B
|
mymap : {A B : Type} → (A → B) → List A → List B
|
||||||
@ -13,7 +12,10 @@ mymap : {A B : Type} → (A → B) → List A → List B
|
|||||||
| f (x :: xs) := f x :: mymap f xs;
|
| f (x :: xs) := f x :: mymap f xs;
|
||||||
|
|
||||||
sum : List Nat → Nat
|
sum : List Nat → Nat
|
||||||
| xs := myfor (acc := 0) (x in xs) {acc + x};
|
| xs :=
|
||||||
|
myfor (acc := 0) (x in xs) {
|
||||||
|
acc + x
|
||||||
|
};
|
||||||
|
|
||||||
sum' : List Nat → Nat
|
sum' : List Nat → Nat
|
||||||
| xs := myfor λ {acc x := acc + x} 0 xs;
|
| xs := myfor λ {acc x := acc + x} 0 xs;
|
||||||
@ -21,39 +23,30 @@ sum' : List Nat → Nat
|
|||||||
lst : List Nat := 1 :: 2 :: 3 :: 4 :: 5 :: nil;
|
lst : List Nat := 1 :: 2 :: 3 :: 4 :: 5 :: nil;
|
||||||
|
|
||||||
syntax iterator myfor2 {init := 1; range := 2};
|
syntax iterator myfor2 {init := 1; range := 2};
|
||||||
myfor2
|
myfor2 : {A B C : Type} → (A → B → C → A) → A → List B → List C → A
|
||||||
: {A B C : Type}
|
|
||||||
→ (A → B → C → A)
|
|
||||||
→ A
|
|
||||||
→ List B
|
|
||||||
→ List C
|
|
||||||
→ A
|
|
||||||
| f acc xs ys :=
|
| f acc xs ys :=
|
||||||
myfor (acc' := acc) (x in xs)
|
myfor (acc' := acc) (x in xs) {
|
||||||
myfor (acc'' := acc') (y in ys)
|
myfor (acc'' := acc') (y in ys) {
|
||||||
f acc'' x y;
|
f acc'' x y
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
syntax iterator myzip2 {init := 2; range := 2};
|
syntax iterator myzip2 {init := 2; range := 2};
|
||||||
myzip2
|
myzip2 : {A A' B C : Type} → (A → A' → B → C → Pair A A') → A → A' → List B → List C → Pair A A'
|
||||||
: {A A' B C : Type}
|
|
||||||
→ (A → A' → B → C → Pair A A')
|
|
||||||
→ A
|
|
||||||
→ A'
|
|
||||||
→ List B
|
|
||||||
→ List C
|
|
||||||
→ Pair A A'
|
|
||||||
| f a b xs ys :=
|
| f a b xs ys :=
|
||||||
myfor (acc, acc' := a, b) (x, y in zip xs ys)
|
myfor (acc, acc' := a, b) (x, y in zip xs ys) {
|
||||||
f acc acc' x y;
|
f acc acc' x y
|
||||||
|
};
|
||||||
|
|
||||||
main : Nat :=
|
main : Nat :=
|
||||||
sum lst
|
sum lst
|
||||||
+ sum' lst
|
+ sum' lst
|
||||||
+ fst (myfor (a, b := 0, 0) (x in lst) b + x, a)
|
+ fst (myfor (a, b := 0, 0) (x in lst) (b + x, a))
|
||||||
+ (myfor2 (acc := 0) (x in lst; y in 1 :: 2 :: nil)
|
+ myfor2 (acc := 0) (x in lst; y in 1 :: 2 :: nil) (acc + x + y)
|
||||||
acc + x + y)
|
|
||||||
+ fst
|
+ fst
|
||||||
(myzip2 (a := 0; b := 0) (x in lst; y in reverse lst)
|
(myzip2 (a := 0; b := 0) (x in lst; y in reverse lst) {
|
||||||
a + x * y, b + y)
|
a + x * y, b + y
|
||||||
+ myfor (a := 0) (x, y in mymap (x in lst) {x, x + 1})
|
})
|
||||||
a + x * y;
|
+ myfor (a := 0) (x, y in mymap (x in lst) (x, x + 1)) {
|
||||||
|
a + x * y
|
||||||
|
};
|
||||||
|
@ -26,7 +26,7 @@ myf'
|
|||||||
| a0 f a b := myf a0 (f a0) a b true;
|
| a0 f a b := myf a0 (f a0) a b true;
|
||||||
|
|
||||||
sum : List Nat -> Nat
|
sum : List Nat -> Nat
|
||||||
| xs := for (acc := 0) (x in xs) {x + acc};
|
| xs := for (acc := 0) (x in xs) (x + acc);
|
||||||
|
|
||||||
funa : {A : Type} -> (A -> A) -> A -> A
|
funa : {A : Type} -> (A -> A) -> A -> A
|
||||||
| {A} f a :=
|
| {A} f a :=
|
||||||
|
@ -3,10 +3,11 @@ module test058;
|
|||||||
|
|
||||||
import Stdlib.Prelude open;
|
import Stdlib.Prelude open;
|
||||||
|
|
||||||
sum (x : Nat) : Nat :=
|
sum (x : Nat) : Nat := for (acc := 0) (n in 1 to x) (acc + n);
|
||||||
for (acc := 0) (n in 1 to x) {acc + n};
|
|
||||||
|
|
||||||
sum' (x : Nat) : Nat :=
|
sum' (x : Nat) : Nat :=
|
||||||
for (acc := 0) (n in 1 to x step 2) {acc + n};
|
for (acc := 0) (n in 1 to x step 2) {
|
||||||
|
acc + n
|
||||||
|
};
|
||||||
|
|
||||||
main : Nat := sum 100 + sum' 100;
|
main : Nat := sum 100 + sum' 100;
|
||||||
|
@ -5,7 +5,7 @@ import Stdlib.Prelude open;
|
|||||||
import Stdlib.Debug.Trace open;
|
import Stdlib.Debug.Trace open;
|
||||||
|
|
||||||
trait
|
trait
|
||||||
type T A := mkT {toNat : A -> Nat};
|
type T A := mkT@{toNat : A -> Nat};
|
||||||
|
|
||||||
instance
|
instance
|
||||||
tNatI : T Nat := mkT id;
|
tNatI : T Nat := mkT id;
|
||||||
@ -29,16 +29,16 @@ tMaybeI {A} {{T A}} : T (Maybe A) :=
|
|||||||
instance
|
instance
|
||||||
tUnitFunI {A} {{T A}} : T (Unit -> A) :=
|
tUnitFunI {A} {{T A}} : T (Unit -> A) :=
|
||||||
mkT@{
|
mkT@{
|
||||||
toNat {A} {{T A}} (f : Unit -> A) : Nat :=
|
toNat {A} {{T A}} (f : Unit -> A) : Nat := T.toNat (f unit)
|
||||||
T.toNat (f unit)
|
|
||||||
};
|
};
|
||||||
|
|
||||||
instance
|
instance
|
||||||
tListI {A} {{T A}} : T (List A) :=
|
tListI {A} {{T A}} : T (List A) :=
|
||||||
mkT@{
|
mkT@{
|
||||||
toNat {A} {{T A}} (xs : List A) : Nat :=
|
toNat {A} {{T A}} (xs : List A) : Nat :=
|
||||||
for (acc := 0) (x in xs)
|
for (acc := 0) (x in xs) {
|
||||||
acc + T.toNat {A} x
|
acc + T.toNat {A} x
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
g : {A : Type} → {{T A}} → Nat := 5;
|
g : {A : Type} → {{T A}} → Nat := 5;
|
||||||
|
@ -15,8 +15,10 @@ type Bool :=
|
|||||||
main : Bool :=
|
main : Bool :=
|
||||||
let
|
let
|
||||||
z : Bool := false;
|
z : Bool := false;
|
||||||
in itconst (a := true; b := false) (c in false; d in false)
|
in itconst (a := true; b := false) (c in false; d in false) {
|
||||||
for (x := true) (y in false)
|
for (x := true) (y in false) {
|
||||||
case x of
|
case x of
|
||||||
| true := y
|
| true := y
|
||||||
| false := z;
|
| false := z
|
||||||
|
}
|
||||||
|
};
|
||||||
|
@ -19,4 +19,7 @@ foldl
|
|||||||
(g : B -> elem -> B)
|
(g : B -> elem -> B)
|
||||||
(ini : B)
|
(ini : B)
|
||||||
(ls : container)
|
(ls : container)
|
||||||
: B := for (acc := ini) (x in ls) {g acc x};
|
: B :=
|
||||||
|
for (acc := ini) (x in ls) {
|
||||||
|
g acc x
|
||||||
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user