Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions build.roc
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ import cli.Env
main! : _ => Result {} _
main! = |_args|

roc_cmd = Env.var!("ROC") |> Result.with_default("roc")
roc_cmd = Env.var!("ROC") ?? "roc"

debug_mode =
when Env.var!("DEBUG") is
Ok(str) if !(Str.is_empty(str)) -> Debug
Ok(str) if !Str.is_empty(str) -> Debug
_ -> Release

roc_version!(roc_cmd)?

os_and_arch = get_os_and_arch!({})?
os_and_arch = get_os_and_arch!()?

stub_lib_path = "platform/libapp.${stub_file_extension(os_and_arch)}"

Expand All @@ -49,11 +49,11 @@ roc_version! = |roc_cmd|
Cmd.exec!(roc_cmd, ["version"])
|> Result.map_err(RocVersionCheckFailed)

get_os_and_arch! : {} => Result OSAndArch _
get_os_and_arch! = |{}|
get_os_and_arch! : () => Result OSAndArch _
get_os_and_arch! = ||
info!("Getting the native operating system and architecture ...")?

convert_os_and_arch!(Env.platform!({}))
convert_os_and_arch!(Env.platform!())

OSAndArch : [
MacosArm64,
Expand Down Expand Up @@ -117,7 +117,7 @@ get_rust_target_folder! = |debug_mode|
cargo_build_host! : [Debug, Release] => Result {} _
cargo_build_host! = |debug_mode|

cargo_build_args! = |{}|
cargo_build_args! = ||
when debug_mode is
Debug ->
info!("Building rust host in debug mode...")?
Expand All @@ -127,7 +127,7 @@ cargo_build_host! = |debug_mode|
info!("Building rust host ...")?
Ok(["build", "--release"])

args = cargo_build_args!({})?
args = cargo_build_args!()?

Cmd.exec!("cargo", args)
|> Result.map_err(ErrBuildingHostBinaries)
Expand Down
21 changes: 11 additions & 10 deletions examples/command.roc
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@ import pf.Stdout
import pf.Cmd

main! = |_args|
status_example!({})?
status_example!()?

output_example!({})?
output_example!()?

exec_example!({})?
exec_example!()?

Ok({})

exec_example! : {} => Result {} _
exec_example! = |{}| Cmd.exec!("echo", ["EXEC"])
exec_example! : () => Result {} _
exec_example! = ||
Cmd.exec!("echo", ["EXEC"])

# Run "env" with verbose option, clear all environment variables, and pass in
# "FOO" and "BAZ".
status_example! : {} => Result {} _
status_example! = |{}|
status_example! : () => Result {} _
status_example! = ||
result =
Cmd.new("env")
|> Cmd.arg("-v")
Expand All @@ -35,8 +36,8 @@ status_example! = |{}|

# Run "env" with verbose option, clear all environment variables, and pass in
# only as an environment variable "FOO"
output_example! : {} => Result {} _
output_example! = |{}|
output_example! : () => Result {} _
output_example! = ||

output =
Cmd.new("env")
Expand All @@ -45,6 +46,6 @@ output_example! = |{}|
|> Cmd.args(["-v"])
|> Cmd.output!

msg = Str.from_utf8(output.stdout) |> Result.with_default("Failed to decode stdout")
msg = Str.from_utf8(output.stdout) ?? "Failed to decode stdout"

Stdout.write!(msg)
6 changes: 3 additions & 3 deletions examples/countdown.roc
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import pf.Stdout

main! = |_args|
Stdout.line!("\nLet's count down from 3 together - all you have to do is press <ENTER>.")?
_ = Stdin.line!({})
_ = Stdin.line!()
tick!(3)

tick! = |n|
if n == 0 then
Stdout.line!("🎉 SURPRISE! Happy Birthday! 🎂")?
Ok({})
else
Stdout.line!("${Num.to_str n}...")?
_ = Stdin.line!({})
Stdout.line!("${Num.to_str(n)}...")?
_ = Stdin.line!()
tick!(n - 1)
16 changes: 8 additions & 8 deletions examples/echo.roc
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import pf.Stdout

main! = |_args|
Stdout.line!("Shout into this cave and hear the echo!")?
tick!({})
tick!()

tick! : {} => Result {} [StdoutErr _]
tick! = |{}|
when Stdin.line!({}) is
tick! : () => Result {} [StdoutErr _]
tick! = ||
when Stdin.line!() is
Ok(str) ->
Stdout.line!(echo(str))?
tick!({})
tick!()

Err(EndOfFile) ->
Stdout.line!(echo("Received end of input (EOF)."))?
Expand All @@ -32,10 +32,10 @@ echo = |shout|
|> Str.to_utf8
|> List.map_with_index(
|_, i|
length = (List.len(Str.to_utf8(shout)) - i)
phrase = (List.split_at(Str.to_utf8(shout), length)).before
length = List.len(Str.to_utf8(shout)) - i
{ before: phrase} = List.split_at(Str.to_utf8(shout), length)

List.concat(silence((if i == 0 then 2 * length else length)), phrase),
List.concat(silence(if i == 0 then 2 * length else length), phrase),
)
|> List.join
|> Str.from_utf8
Expand Down
6 changes: 3 additions & 3 deletions examples/file-mixed.roc
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import pf.Dir

out_txt_path = "out.txt"

task! = |{}|
task! = ||

cwd_str = Path.display(Env.cwd!({})?)
cwd_str = Path.display(Env.cwd!()?)

Stdout.line!("cwd: ${cwd_str}")?

Expand All @@ -34,7 +34,7 @@ task! = |{}|
Ok({})

main! = |_args|
when task!({}) is
when task!() is
Ok({}) -> Stdout.line!("Successfully wrote a string to out.txt")
Err(err) ->
msg =
Expand Down
6 changes: 3 additions & 3 deletions examples/file-read.roc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import pf.File

main! = |_args|

run!({})
run!()
? |err|
msg =
when err is
Expand All @@ -21,9 +21,9 @@ main! = |_args|

Ok({})

run! = |{}|
run! = ||
file_name = "LICENSE"
contents = File.read_utf8!(file_name)?
lines = Str.split_on(contents, "\n")

Stdout.line!(Str.concat("First line of ${file_name}: ", (List.first(lines) |> Result.with_default("err"))))
Stdout.line!(Str.concat("First line of ${file_name}: ", List.first(lines) ?? "err"))
4 changes: 2 additions & 2 deletions examples/form.roc
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ main! = |_args|

Stdout.line!("What's your first name?")?

first = Stdin.line!({})?
first = Stdin.line!()?

Stdout.line!("What's your last name?")?

last = Stdin.line!({})?
last = Stdin.line!()?

Stdout.line!("Hi, ${first} ${last}! 👋")
4 changes: 2 additions & 2 deletions examples/piping.roc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ main! = |_args|
Stdout.line!("I read ${Num.to_str(lines)} lines from stdin.")

count! = |n|
when Stdin.line!({}) is
Ok(_) -> count!((n + 1))
when Stdin.line!() is
Ok(_) -> count!(n + 1)
Err(_) -> n
8 changes: 4 additions & 4 deletions examples/stdin.roc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import pf.Stdin
main! = |_args|
Stdout.line!("Enter a series of number characters (0-9):")?

number_bytes = take_number_bytes!({})?
number_bytes = take_number_bytes!()?

if List.is_empty(number_bytes) then
Stderr.line!("Expected a series of number characters (0-9)")
Expand All @@ -21,9 +21,9 @@ main! = |_args|
Err(_) ->
Stderr.line!("Error, bad utf8")

take_number_bytes! : {} => Result (List U8) _
take_number_bytes! = |{}|
bytes_read = Stdin.bytes!({})?
take_number_bytes! : () => Result (List U8) _
take_number_bytes! = ||
bytes_read = Stdin.bytes!()?

number_bytes =
List.walk(
Expand Down
8 changes: 4 additions & 4 deletions examples/tcp-client.roc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import pf.Stderr
# To run this example: check the README.md in this folder

main! = |_args|
when run!({}) is
when run!() is
Ok({}) -> Ok({})
Err(err) -> handle_err!(err)

Expand Down Expand Up @@ -42,8 +42,8 @@ handle_err! = |error|

other -> Stderr.line!("Got other error: ${Inspect.to_str(other)}")

run! : {} => Result {} _
run! = |{}|
run! : () => Result {} _
run! = ||

stream = Tcp.connect!("127.0.0.1", 8085)?

Expand All @@ -58,7 +58,7 @@ tick! : Tcp.Stream => Result {} _
tick! = |stream|
Stdout.write!("> ")?

out_msg = Stdin.line!({})?
out_msg = Stdin.line!()?

Tcp.write_utf8!(stream, "${out_msg}\n")?

Expand Down
2 changes: 1 addition & 1 deletion examples/temp-dir.roc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import pf.Path
## for example: `roc build examples/temp-dir.roc --linker=legacy`
main! = |_args|

temp_dir_str = Path.display(Env.temp_dir!({}))
temp_dir_str = Path.display(Env.temp_dir!())

Stdout.line!("The temp dir path is ${temp_dir_str}")
|> Result.map_err(|err| Exit(1, "Failed to print temp dir:\n\t${Inspect.to_str(err)}"))
4 changes: 2 additions & 2 deletions examples/time.roc
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import pf.Sleep
# To run this example: check the README.md in this folder

main! = |_args|
start = Utc.now!({})
start = Utc.now!()

Sleep.millis!(1500)

finish = Utc.now!({})
finish = Utc.now!()

duration = Num.to_str(Utc.delta_as_nanos(start, finish))

Expand Down
40 changes: 18 additions & 22 deletions platform/Env.roc
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import Host

## Reads the [current working directory](https://en.wikipedia.org/wiki/Working_directory)
## from the environment. File operations on relative [Path]s are relative to this directory.
cwd! : {} => Result Path [CwdUnavailable]
cwd! = |{}|
bytes = Host.cwd!({}) |> Result.with_default([])
cwd! : () => Result Path [CwdUnavailable]
cwd! = ||
bytes = Host.cwd!() ?? []

if List.is_empty(bytes) then
Err(CwdUnavailable)
Expand All @@ -30,13 +30,12 @@ cwd! = |{}|
## to this directory.
set_cwd! : Path => Result {} [InvalidCwd]
set_cwd! = |path|
Host.set_cwd!(InternalPath.to_bytes(path))
|> Result.map_err(|{}| InvalidCwd)
Host.set_cwd!(InternalPath.to_bytes(path)) |> Result.map_err(|_| InvalidCwd)

## Gets the path to the currently-running executable.
exe_path! : {} => Result Path [ExePathUnavailable]
exe_path! = |{}|
when Host.exe_path!({}) is
exe_path! : () => Result Path [ExePathUnavailable]
exe_path! = ||
when Host.exe_path!() is
Ok(bytes) -> Ok(InternalPath.from_os_bytes(bytes))
Err({}) -> Err(ExePathUnavailable)

Expand All @@ -46,8 +45,7 @@ exe_path! = |{}|
## [Unicode replacement character](https://unicode.org/glossary/#replacement_character) ('�').
var! : Str => Result Str [VarNotFound]
var! = |name|
Host.env_var!(name)
|> Result.map_err(|{}| VarNotFound)
Host.env_var!(name) |> Result.map_err(|_| VarNotFound)

## Reads the given environment variable and attempts to decode it.
##
Expand Down Expand Up @@ -81,17 +79,16 @@ decode! = |name|
Err({}) -> Err(VarNotFound)
Ok(var_str) ->
Str.to_utf8(var_str)
|> Decode.from_bytes(EnvDecoding.format({}))
|> Decode.from_bytes(EnvDecoding.format())
|> Result.map_err(|_| DecodeErr(TooShort))

## Reads all the process's environment variables into a [Dict].
##
## If any key or value contains invalid Unicode, the [Unicode replacement character](https://unicode.org/glossary/#replacement_character)
## will be used in place of any parts of keys or values that are invalid Unicode.
dict! : {} => Dict Str Str
dict! = |{}|
Host.env_dict!({})
|> Dict.from_list
dict! : () => Dict Str Str
dict! = ||
Dict.from_list(Host.env_dict!())

# ## Walks over the process's environment variables as key-value arguments to the walking function.
# ##
Expand Down Expand Up @@ -135,10 +132,10 @@ OS : [LINUX, MACOS, WINDOWS, OTHER Str]
##
## Note these values are constants from when the platform is built.
##
platform! : {} => { arch : ARCH, os : OS }
platform! = |{}|
platform! : () => { arch : ARCH, os : OS }
platform! = ||

from_rust = Host.current_arch_os!({})
from_rust = Host.current_arch_os!()

arch =
when from_rust.arch is
Expand Down Expand Up @@ -166,7 +163,6 @@ platform! = |{}|
## to create a uniquely named file. Creating a file or directory with a fixed or predictable name may
## result in “insecure temporary file” security vulnerabilities.
##
temp_dir! : {} => Path
temp_dir! = |{}|
Host.temp_dir!({})
|> InternalPath.from_os_bytes
temp_dir! : () => Path
temp_dir! = ||
InternalPath.from_os_bytes(Host.temp_dir!())
4 changes: 2 additions & 2 deletions platform/EnvDecoding.roc
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ EnvFormat := {} implements [
},
]

format : {} -> EnvFormat
format = |{}| @EnvFormat({})
format : () -> EnvFormat
format = || @EnvFormat({})

decode_bytes_to_num = |bytes, transformer|
when Str.from_utf8(bytes) is
Expand Down
Loading
Loading