Skip to content

Commit

Permalink
Merge pull request #151 from roc-lang/dollar
Browse files Browse the repository at this point in the history
WIP: Use new $(...) interpolation syntax
  • Loading branch information
Anton-4 authored Jan 26, 2024
2 parents 273191c + 019417e commit 865c615
Show file tree
Hide file tree
Showing 16 changed files with 78 additions and 77 deletions.
10 changes: 5 additions & 5 deletions examples/command.roc
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ first =
Ok {} -> Stdout.line "Success"
Err (ExitCode code) ->
codeStr = Num.toStr code
Stdout.line "Child exited with non-zero code: \(codeStr)"
Stdout.line "Child exited with non-zero code: $(codeStr)"

Err KilledBySignal -> Stdout.line "Child was killed by signal"
Err (IOError err) -> Stdout.line "IOError executing: \(err)"
Err (IOError err) -> Stdout.line "IOError executing: $(err)"

# Run "stat" with environment variable "FOO" set to "BAR" and three arguments: "--format", "'%A'", and "LICENSE".
# Capture stdout and stderr and print them.
Expand All @@ -52,12 +52,12 @@ second =
|> Task.map \output -> ("Success", output.stdout, output.stderr)
|> Task.onErr \(output, err) ->
when err is
ExitCode code -> Task.ok ("Child exited with non-zero code: \(Num.toStr code)", output.stdout, output.stderr)
ExitCode code -> Task.ok ("Child exited with non-zero code: $(Num.toStr code)", output.stdout, output.stderr)
KilledBySignal -> Task.ok ("Child was killed by signal", output.stdout, output.stderr)
IOError ioErr -> Task.ok ("IOError executing: \(ioErr)", output.stdout, output.stderr)
IOError ioErr -> Task.ok ("IOError executing: $(ioErr)", output.stdout, output.stderr)
|> Task.await

stdoutStr = Str.fromUtf8 stdout |> Result.withDefault "Failed to decode stdout"
stderrStr = Str.fromUtf8 stderr |> Result.withDefault "Failed to decode stderr"

Stdout.write "STATUS \(status)\nSTDOUT \(stdoutStr)\nSTDERR \(stderrStr)\n"
Stdout.write "STATUS $(status)\nSTDOUT $(stdoutStr)\nSTDERR $(stderrStr)\n"
2 changes: 1 addition & 1 deletion examples/countdown.roc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ tick = \n ->
_ <- await (Stdout.line "🎉 SURPRISE! Happy Birthday! 🎂")
Task.ok (Done {})
else
_ <- await (n |> Num.toStr |> \s -> "\(s)..." |> Stdout.line)
_ <- await (n |> Num.toStr |> \s -> "$(s)..." |> Stdout.line)
_ <- await Stdin.line
Task.ok (Step (n - 1))
6 changes: 3 additions & 3 deletions examples/env.roc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ main : Task {} I32
main =
task =
Env.decode "EDITOR"
|> Task.await (\editor -> Stdout.line "Your favorite editor is \(editor)!")
|> Task.await (\editor -> Stdout.line "Your favorite editor is $(editor)!")
|> Task.await (\{} -> Env.decode "SHLVL")
|> Task.await
(\lvl ->
Expand All @@ -16,15 +16,15 @@ main =
n ->
lvlStr = Num.toStr n

Stdout.line "Your current shell level is \(lvlStr)!")
Stdout.line "Your current shell level is $(lvlStr)!")
|> Task.await \{} -> Env.decode "LETTERS"

Task.attempt task \result ->
when result is
Ok letters ->
joinedLetters = Str.joinWith letters " "

Stdout.line "Your favorite letters are: \(joinedLetters)"
Stdout.line "Your favorite letters are: $(joinedLetters)"

Err _ ->
Stderr.line "I couldn't find your favorite letters in the environment variables!"
8 changes: 4 additions & 4 deletions examples/file-mixedBROKEN.roc
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ main =
cwd <- Env.cwd |> Task.await
cwdStr = Path.display cwd

_ <- Stdout.line "cwd: \(cwdStr)" |> Task.await
_ <- Stdout.line "cwd: $(cwdStr)" |> Task.await
dirEntries <- Dir.list cwd |> Task.await
contentsStr = Str.joinWith (List.map dirEntries Path.display) "\n "

_ <- Stdout.line "Directory contents:\n \(contentsStr)\n" |> Task.await
_ <- Stdout.line "Directory contents:\n $(contentsStr)\n" |> Task.await
_ <- Stdout.line "Writing a string to out.txt" |> Task.await
_ <- File.writeUtf8 path "a string!" |> Task.await
contents <- File.readUtf8 path |> Task.await
Stdout.line "I read the file back. Its contents: \"\(contents)\""
Stdout.line "I read the file back. Its contents: \"$(contents)\""

Task.attempt task \result ->
when result is
Expand All @@ -41,5 +41,5 @@ main =
_ -> "Uh oh, there was an error!"

{} <- Stderr.line msg |> Task.await

Task.err 1 # 1 is an exit code to indicate failure
4 changes: 2 additions & 2 deletions examples/file-read.roc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ main =
contents <- File.readUtf8 path |> await
lines = Str.split contents "\n"

Stdout.line (Str.concat "First line of \(fileName): " (List.first lines |> Result.withDefault "err"))
Stdout.line (Str.concat "First line of $(fileName): " (List.first lines |> Result.withDefault "err"))

Task.attempt task \result ->
when result is
Expand All @@ -32,5 +32,5 @@ main =
_ -> "Uh oh, there was an error!"

{} <- Stderr.line msg |> await

Task.err 1 # 1 is an exit code to indicate failure
4 changes: 2 additions & 2 deletions examples/form.roc
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ main : Task {} I32
main =
_ <- await (Stdout.line "What's your first name?")
firstName <- await Stdin.line

_ <- await (Stdout.line "What's your last name?")
lastName <- await Stdin.line

Stdout.line "Hi, \(unwrap firstName) \(unwrap lastName)! 👋"
Stdout.line "Hi, $(unwrap firstName) $(unwrap lastName)! 👋"

unwrap : [Input Str, End] -> Str
unwrap = \input ->
Expand Down
3 changes: 1 addition & 2 deletions examples/piping.roc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ app "piping"
# Try piping in some text like this: `echo -e "test\n123" | roc piping.roc`
main =
lines <- Task.loop 0 count |> Task.await
Stdout.line "I read \(Num.toStr lines) lines from stdin."
Stdout.line "I read $(Num.toStr lines) lines from stdin."

count = \n ->
result <- Stdin.line |> Task.await
Expand All @@ -19,4 +19,3 @@ count = \n ->
Input _ -> Step (n + 1)
End -> Done n
Task.ok state

2 changes: 1 addition & 1 deletion examples/stdin.roc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ main =
else
when Str.fromUtf8 numberBytes is
Ok nStr ->
Stdout.line "Got number \(nStr)"
Stdout.line "Got number $(nStr)"

Err _ ->
Stderr.line "Error, bad utf8"
Expand Down
18 changes: 10 additions & 8 deletions examples/tcp-client.roc
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ main =
errStr = Tcp.connectErrToStr err
Stderr.line
"""
Failed to connect: \(errStr)
Failed to connect: $(errStr)
If you don't have anything listening on port 8085, run:
\$ nc -l 8085
If you want an echo server you can run:
$ ncat -e \$(which cat) -l 8085
"""
Expand All @@ -33,23 +34,24 @@ main =

Err (TcpPerformErr (TcpReadErr err)) ->
errStr = Tcp.streamErrToStr err
Stderr.line "Error while reading: \(errStr)"
Stderr.line "Error while reading: $(errStr)"

Err (TcpPerformErr (TcpWriteErr err)) ->
errStr = Tcp.streamErrToStr err
Stderr.line "Error while writing: \(errStr)"
Stderr.line "Error while writing: $(errStr)"

tick : Tcp.Stream -> Task.Task {} _
tick = \stream ->
_ <- Stdout.write "> " |> await

input <- Stdin.line |> await

outMsg = when input is
End -> "Received end of input (EOF)."
Input msg -> msg
outMsg =
when input is
End -> "Received end of input (EOF)."
Input msg -> msg

_ <- Tcp.writeUtf8 "\(outMsg)\n" stream |> await
_ <- Tcp.writeUtf8 "$(outMsg)\n" stream |> await

inMsg <- Tcp.readLine stream |> await
Stdout.line "< \(inMsg)"
Stdout.line "< $(inMsg)"
2 changes: 1 addition & 1 deletion examples/time.roc
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ main =

duration = Utc.deltaAsNanos start finish |> Num.toStr

Stdout.line "Completed in \(duration)ns"
Stdout.line "Completed in $(duration)ns"
Loading

0 comments on commit 865c615

Please sign in to comment.