Skip to content

Commit

Permalink
Improve writeExprInParens to avoid writing unnecessary parens
Browse files Browse the repository at this point in the history
  • Loading branch information
vipentti committed Dec 16, 2023
1 parent 012bc10 commit 5f91450
Show file tree
Hide file tree
Showing 85 changed files with 453 additions and 441 deletions.
20 changes: 16 additions & 4 deletions src/Visp.Compiler/Syntax/SynWriter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1216,7 +1216,7 @@ module Write =
if kind = CollectionKind.HashBrace then
writeExprToValue
else
writeExpr
writeExprInParens

let items =
if kind = CollectionKind.FsList then
Expand Down Expand Up @@ -1475,10 +1475,22 @@ module Write =
()

and private writeExprInParens w (st: WriteState) ex =
// TODO: Should we check if we have a tuple and then not add parens?
char w '('
let needsParens =
match ex with
| SynExpr.Const _
| SynExpr.Tuple _
| SynExpr.Literal _
| SynExpr.Symbol _ -> false

| _ -> true

if needsParens then
char w '('

writeExpr w st ex
char w ')'

if needsParens then
char w ')'

and private writeOp w (st: WriteState) (op: SynOp) =
let opState = WriteState.Inline
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ let state = { Todo = () }
// line 4 @"test"
let hello (name: string) =
// line 6 @"test"
printfn ("hello %s") (name)
printfn "hello %s" name

// line 8 @"test"
hello ("world")
hello "world"

Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ let state = { Todo = () }
// line 4 @"test"
let hello (name: string) =
// line 6 @"test"
sprintf ("hello %s") (name)
sprintf "hello %s" name

// line 8 @"test"
hello ("world")
hello "world"

Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ let arr =
// line 8 @"array-0.visp"
[|1;2;3;4;5|]
// line 10 @"array-0.visp"
printfn ("arr[0] %A") ((arr.[0]))
printfn "arr[0] %A" ((arr.[0]))
// line 11 @"array-0.visp"
printfn ("arr[1] %A") ((arr.[1]))
printfn "arr[1] %A" ((arr.[1]))
// line 13 @"array-0.visp"
let visp_result_todo = arr
// line 13 @"array-0.visp"
printfn ("%A") (visp_result_todo)
printfn "%A" visp_result_todo

Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,39 @@ type Grid = char[,]
// line 10 @"array2d-0.visp"
let arr =
// line 10 @"array2d-0.visp"
Array2D.create (5) (5) ('.')
Array2D.create 5 5 '.'
// line 12 @"array2d-0.visp"
let y = 4
// line 14 @"array2d-0.visp"
printfn ("arr[0,0]=%A") ((arr.[0, 0]))
printfn "arr[0,0]=%A" ((arr.[0, 0]))
// line 15 @"array2d-0.visp"
printfn ("arr[1,2]=%A") ((arr.[1, 1]))
printfn "arr[1,2]=%A" ((arr.[1, 1]))
// line 16 @"array2d-0.visp"
printfn ("arr[3,3]=%A") (Array2D.get (arr) (3) (3))
printfn "arr[3,3]=%A" (Array2D.get arr 3 3)
// line 18 @"array2d-0.visp"
arr.[2, 2] <- '#'
// line 19 @"array2d-0.visp"
arr.[3, 4] <- '#'
// line 20 @"array2d-0.visp"
arr.[4, 4] <- '#'
// line 21 @"array2d-0.visp"
printfn ("arr[3,4]=%A") (arr
printfn "arr[3,4]=%A" (arr
|> (fun a1 ->
// line 21 @"array2d-0.visp"
(a1.[y - 1, (1) +
(1) +
(1) +
(1)])))
(a1.[y - 1, 1 +
1 +
1 +
1])))
// line 22 @"array2d-0.visp"
printfn ("arr[4,4]=%A") (arr
printfn "arr[4,4]=%A" (arr
|> (fun a1 ->
// line 22 @"array2d-0.visp"
(a1.[y, (1) +
(1) +
(1) +
(1)])))
(a1.[y, 1 +
1 +
1 +
1])))
// line 25 @"array2d-0.visp"
let visp_result_todo = arr
// line 25 @"array2d-0.visp"
printfn ("%A") (visp_result_todo)
printfn "%A" visp_result_todo

Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ type Grid = char[,]
// line 10 @"array2d-1.visp"
let arr =
// line 10 @"array2d-1.visp"
Array2D.create (5) (5) ('.')
Array2D.create 5 5 '.'
// line 12 @"array2d-1.visp"
printfn ("arr[*,0]=%A") ((arr.[*, 0]))
printfn "arr[*,0]=%A" ((arr.[*, 0]))
// line 13 @"array2d-1.visp"
printfn ("arr[0,*]=%A") ((arr.[0, *]))
printfn "arr[0,*]=%A" ((arr.[0, *]))
// line 15 @"array2d-1.visp"
let visp_result_todo = arr
// line 15 @"array2d-1.visp"
printfn ("%A") (visp_result_todo)
printfn "%A" visp_result_todo

Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ let state = { Todo = () }
// line 11 @"atom-0.visp"
let inc (v: Value) =
// line 12 @"atom-0.visp"
Value.from ((1L) +
(unwrapInt (v)))
Value.from (1L +
(unwrapInt v))

// line 13 @"atom-0.visp"
let count =
// line 13 @"atom-0.visp"
Value.atom(Value.from(0L))
// line 15 @"atom-0.visp"
printfn ("count is %O") (deref (count))
printfn "count is %O" (deref (count))
// line 17 @"atom-0.visp"
CoreMethods.``swap!``(Value.from(count), inc)
// line 19 @"atom-0.visp"
let visp_result_todo =
// line 19 @"atom-0.visp"
printfn ("count is %O") (deref (count))
printfn "count is %O" (deref (count))
// line 19 @"atom-0.visp"
printfn ("%A") (visp_result_todo)
printfn "%A" visp_result_todo

Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ let xx =
// line 13 @"attributes-0.visp"
(new TestStruct(99))
// line 15 @"attributes-0.visp"
printfn ("Value is %i") ((xx.Value))
printfn "Value is %i" ((xx.Value))
// line 17 @"attributes-0.visp"
let visp_result_todo = ()
// line 17 @"attributes-0.visp"
printfn ("%A") (visp_result_todo)
printfn "%A" visp_result_todo

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ let visp_result_todo =
3
else
// line 8 @"cond-0.visp"
failwith ("unreachable cond")
failwith "unreachable cond"
// line 8 @"cond-0.visp"
printfn ("%A") (visp_result_todo)
printfn "%A" visp_result_todo

Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ let visp_result_todo =
|> (fun arg ->
arg)
// line 7 @"thread-first-0.visp"
printfn ("%A") (visp_result_todo)
printfn "%A" visp_result_todo

Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ let visp_result_todo =
arg)
|> (fun arg ->
// line 15 @"thread-first-1.visp"
printfn ("arg is %O") (arg)
printfn "arg is %O" arg
arg)
// line 11 @"thread-first-1.visp"
printfn ("%A") (visp_result_todo)
printfn "%A" visp_result_todo

Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ let ms =
// line 11 @"thread-last-0.visp"
rx.Match("Game 1:")
// line 13 @"thread-last-0.visp"
printfn ("Groups %A") ((ms.Groups))
printfn "Groups %A" ((ms.Groups))
// line 15 @"thread-last-0.visp"
let gamePrefix =
// line 15 @"thread-last-0.visp"
((ms.Groups).[0])
// line 17 @"thread-last-0.visp"
printfn ("Game Prefix %A") (gamePrefix)
printfn "Game Prefix %A" gamePrefix
// line 19 @"thread-last-0.visp"
let visp_result_todo =
// line 19 @"thread-last-0.visp"
printfn ("Game Prefix %A") (ms
printfn "Game Prefix %A" (ms
|> (fun a1 ->
// line 19 @"thread-last-0.visp"
(a1.Groups))
|> (fun a1 ->
// line 19 @"thread-last-0.visp"
(a1.[0])))
// line 19 @"thread-last-0.visp"
printfn ("%A") (visp_result_todo)
printfn "%A" visp_result_todo

Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ let visp_result_todo =
// line 8 @"cexpr-0.visp"
for x in seq {
// line 9 @"cexpr-0.visp"
yield (1)
yield 1
// line 10 @"cexpr-0.visp"
yield (2)
yield 2
// line 11 @"cexpr-0.visp"
yield (3)} do
yield 3} do
// line 12 @"cexpr-0.visp"
printfn ("x is %i") (x)
printfn "x is %i" x
// line 8 @"cexpr-0.visp"
printfn ("%A") (visp_result_todo)
printfn "%A" visp_result_todo

Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ let SomeTaskCode (cancellationToken: CancellationToken) =
// line 12 @"cexpr-1.visp"
cancellationToken.ThrowIfCancellationRequested()
// line 13 @"cexpr-1.visp"
printfn ("continuing...")
printfn "continuing..."
// line 14 @"cexpr-1.visp"
return (1)}
return 1}

// line 17 @"cexpr-1.visp"
let visp_result_todo =
Expand All @@ -26,5 +26,5 @@ let visp_result_todo =
|> Async.AwaitTask
|> Async.RunSynchronously
// line 17 @"cexpr-1.visp"
printfn ("%A") (visp_result_todo)
printfn "%A" visp_result_todo

Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ open Visp.Runtime.Library

let state = { Todo = () }
// line 8 @"char-0.visp"
printfn ("Char is %O") ('h')
printfn "Char is %O" 'h'
// line 9 @"char-0.visp"
printfn ("Char is %O") ('a')
printfn "Char is %O" 'a'
// line 10 @"char-0.visp"
printfn ("Char is %O") ('a')
printfn "Char is %O" 'a'
// line 12 @"char-0.visp"
printfn ("Char is %O") (':')
printfn "Char is %O" ':'
// line 13 @"char-0.visp"
printfn ("Char is %O") ('(')
printfn "Char is %O" '('
// line 14 @"char-0.visp"
let visp_result_todo =
// line 14 @"char-0.visp"
printfn ("Char is %O") (')')
printfn "Char is %O" ')'
// line 14 @"char-0.visp"
printfn ("%A") (visp_result_todo)
printfn "%A" visp_result_todo

Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ open Visp.Runtime.Library

let state = { Todo = () }
// line 8 @"char-1.visp"
printfn ("Char is '%03o'") (int ('\n'))
printfn "Char is '%03o'" (int '\n')
// line 9 @"char-1.visp"
printfn ("Char is '%03o'") (int ('\r'))
printfn "Char is '%03o'" (int '\r')
// line 10 @"char-1.visp"
let visp_result_todo =
// line 10 @"char-1.visp"
printfn ("Char is '%03o'") (int ('\t'))
printfn "Char is '%03o'" (int '\t')
// line 10 @"char-1.visp"
printfn ("%A") (visp_result_todo)
printfn "%A" visp_result_todo

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let state = { Todo = () }
// line 9 @"char-2.visp"
let visp_result_todo =
// line 9 @"char-2.visp"
printfn ("Chars are %A") (('=', '|', '<', '>'))
printfn "Chars are %A" ('=', '|', '<', '>')
// line 9 @"char-2.visp"
printfn ("%A") (visp_result_todo)
printfn "%A" visp_result_todo

Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ let mutable vec =
temp.Add(Value.from(6))
temp
// line 10 @"dot-shorthands.visp"
vec.[1] <- Value.int (-1)
vec.[1] <- Value.int -1
// line 12 @"dot-shorthands.visp"
printfn ("%O") ((vec.[1]))
printfn "%O" ((vec.[1]))
// line 13 @"dot-shorthands.visp"
printfn ("%O") ((System.Int64.MaxValue))
printfn "%O" ((System.Int64.MaxValue))
// line 15 @"dot-shorthands.visp"
vec.Add(Value.int (64))
vec.Add(Value.int 64)
// line 17 @"dot-shorthands.visp"
let visp_result_todo =
// line 17 @"dot-shorthands.visp"
printfn ("%O") ((vec.[(vec.Count) - 1]))
printfn "%O" ((vec.[(vec.Count) - 1]))
// line 17 @"dot-shorthands.visp"
printfn ("%A") (visp_result_todo)
printfn "%A" visp_result_todo

Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ let state = { Todo = () }
// line 10 @"example-0.visp"
let hello (name: string) =
// line 12 @"example-0.visp"
printfn ("hello %s") (name)
printfn "hello %s" name

// line 14 @"example-0.visp"
let visp_result_todo =
// line 14 @"example-0.visp"
hello ("world")
hello "world"
// line 14 @"example-0.visp"
printfn ("%A") (visp_result_todo)
printfn "%A" visp_result_todo

Loading

0 comments on commit 5f91450

Please sign in to comment.