Skip to content

Commit

Permalink
Merge pull request #305 from roc-lang/snake_case_builtins
Browse files Browse the repository at this point in the history
Upgrade to `snake_case` builtins
  • Loading branch information
Anton-4 authored Jan 10, 2025
2 parents 24075b3 + 8b813ac commit c89c762
Show file tree
Hide file tree
Showing 60 changed files with 1,289 additions and 1,210 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
on:
pull_request:
# pull_request:
workflow_dispatch:

# this cancels workflows currently in progress if you start a new one
Expand Down
16 changes: 8 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

111 changes: 56 additions & 55 deletions build.roc
Original file line number Diff line number Diff line change
Expand Up @@ -13,50 +13,47 @@ import cli.Env
## Check basic-cli-build-steps.png for a diagram that shows what the code does.
##
main! : _ => Result {} _
main! = \_ ->
main! = \_args ->

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

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

try roc_version! roc_cmd
roc_version!(roc_cmd)?

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

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

try build_stub_app_lib! roc_cmd stub_lib_path
build_stub_app_lib!(roc_cmd, stub_lib_path)?

try cargo_build_host! debug_mode
cargo_build_host!(debug_mode)?

rust_target_folder = try get_rust_target_folder! debug_mode
rust_target_folder = get_rust_target_folder!(debug_mode)?

try copy_host_lib! os_and_arch rust_target_folder
copy_host_lib!(os_and_arch, rust_target_folder)?

try preprocess_host! roc_cmd stub_lib_path rust_target_folder
preprocess_host!(roc_cmd, stub_lib_path, rust_target_folder)?

try info! "Successfully built platform files!"
info!("Successfully built platform files!")?

Ok {}
Ok({})

roc_version! : Str => Result {} _
roc_version! = \roc_cmd ->
try info! "Checking provided roc; executing `$(roc_cmd) version`:"
info!("Checking provided roc; executing `$(roc_cmd) version`:")?

roc_cmd
|> Cmd.exec! ["version"]
|> Result.mapErr RocVersionCheckFailed
Cmd.exec!(roc_cmd, ["version"])
|> Result.map_err(RocVersionCheckFailed)

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

{ os, arch } = Env.platform! {}

convert_os_and_arch!! { os, arch }
convert_os_and_arch!(Env.platform!({}))

OSAndArch : [
MacosArm64,
Expand All @@ -70,19 +67,18 @@ OSAndArch : [
convert_os_and_arch! : _ => Result OSAndArch _
convert_os_and_arch! = \{ os, arch } ->
when (os, arch) is
(MACOS, AARCH64) -> Ok MacosArm64
(MACOS, X64) -> Ok MacosX64
(LINUX, AARCH64) -> Ok LinuxArm64
(LINUX, X64) -> Ok LinuxX64
_ -> Err (UnsupportedNative os arch)
(MACOS, AARCH64) -> Ok(MacosArm64)
(MACOS, X64) -> Ok(MacosX64)
(LINUX, AARCH64) -> Ok(LinuxArm64)
(LINUX, X64) -> Ok(LinuxX64)
_ -> Err(UnsupportedNative(os, arch))

build_stub_app_lib! : Str, Str => Result {} _
build_stub_app_lib! = \roc_cmd, stub_lib_path ->
try info! "Building stubbed app shared library ..."
info!("Building stubbed app shared library ...")?

roc_cmd
|> Cmd.exec! ["build", "--lib", "platform/libapp.roc", "--output", stub_lib_path, "--optimize"]
|> Result.mapErr ErrBuildingAppStub
Cmd.exec!(roc_cmd, ["build", "--lib", "platform/libapp.roc", "--output", stub_lib_path, "--optimize"])
|> Result.map_err(ErrBuildingAppStub)

stub_file_extension : OSAndArch -> Str
stub_file_extension = \os_and_arch ->
Expand All @@ -106,53 +102,58 @@ get_rust_target_folder! = \debug_mode ->

debug_or_release = if debug_mode == Debug then "debug" else "release"

when Env.var! "CARGO_BUILD_TARGET" is
Ok target_env_var ->
if Str.isEmpty target_env_var then
Ok "target/$(debug_or_release)/"
when Env.var!("CARGO_BUILD_TARGET") is
Ok(target_env_var) ->
if Str.is_empty(target_env_var) then
Ok("target/$(debug_or_release)/")
else
Ok "target/$(target_env_var)/$(debug_or_release)/"
Ok("target/$(target_env_var)/$(debug_or_release)/")

Err e ->
try info! "Failed to get env var CARGO_BUILD_TARGET with error $(Inspect.toStr e). Assuming default CARGO_BUILD_TARGET (native)..."
Err(e) ->
info!("Failed to get env var CARGO_BUILD_TARGET with error $(Inspect.to_str(e)). Assuming default CARGO_BUILD_TARGET (native)...")?

Ok "target/$(debug_or_release)/"
Ok("target/$(debug_or_release)/")

cargo_build_host! : [Debug, Release] => Result {} _
cargo_build_host! = \debug_mode ->
cargo_build_args =

cargo_build_args! = \{} ->
when debug_mode is
Debug -> Result.map (info! "Building rust host in debug mode...") \_ -> ["build"]
Release -> Result.map (info! "Building rust host ...") \_ -> ["build", "--release"]
Debug ->
info!("Building rust host in debug mode...")?
Ok(["build"])

Release ->
info!("Building rust host ...")?
Ok(["build", "--release"])

args = cargo_build_args!({})?

"cargo"
|> Cmd.exec! (try cargo_build_args)
|> Result.mapErr ErrBuildingHostBinaries
Cmd.exec!("cargo", args)
|> Result.map_err(ErrBuildingHostBinaries)

copy_host_lib! : OSAndArch, Str => Result {} _
copy_host_lib! = \os_and_arch, rust_target_folder ->

host_build_path = "$(rust_target_folder)libhost.a"

host_dest_path = "platform/$(prebuilt_static_lib_file os_and_arch)"
host_dest_path = "platform/$(prebuilt_static_lib_file(os_and_arch))"

try info! "Moving the prebuilt binary from $(host_build_path) to $(host_dest_path) ..."
info!("Moving the prebuilt binary from $(host_build_path) to $(host_dest_path) ...")?

"cp"
|> Cmd.exec! [host_build_path, host_dest_path]
|> Result.mapErr ErrMovingPrebuiltLegacyBinary
Cmd.exec!("cp", [host_build_path, host_dest_path])
|> Result.map_err(ErrMovingPrebuiltLegacyBinary)

preprocess_host! : Str, Str, Str => Result {} _
preprocess_host! = \roc_cmd, stub_lib_path, rust_target_folder ->

try info! "Preprocessing surgical host ..."
info!("Preprocessing surgical host ...")?

surgical_build_path = "$(rust_target_folder)host"

roc_cmd
|> Cmd.exec! ["preprocess-host", surgical_build_path, "platform/main.roc", stub_lib_path]
|> Result.mapErr ErrPreprocessingSurgicalBinary
Cmd.exec!(roc_cmd, ["preprocess-host", surgical_build_path, "platform/main.roc", stub_lib_path])
|> Result.map_err(ErrPreprocessingSurgicalBinary)

info! : Str => Result {} _
info! = \msg ->
Stdout.line! "\u(001b)[34mINFO:\u(001b)[0m $(msg)"
Stdout.line!("\u(001b)[34mINFO:\u(001b)[0m $(msg)")
27 changes: 0 additions & 27 deletions ci/expect_scripts/http-get-json.exp

This file was deleted.

1 change: 0 additions & 1 deletion ci/rust_http_server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use hyper::service::{make_service_fn, service_fn};
use std::convert::Infallible;

async fn handle_request(_req: Request<Body>) -> Result<Response<Body>, Infallible> {
// Encode.toBytes {foo: "Hello Json!"} Json.utf8
let json_bytes: Vec<u8> = vec![123, 34, 102, 111, 111, 34, 58, 34, 72, 101, 108, 108, 111, 32, 74, 115, 111, 110, 33, 34, 125];

let response = Response::builder()
Expand Down
12 changes: 6 additions & 6 deletions examples/args.roc
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import pf.Arg exposing [Arg]
main! : List Arg => Result {} _
main! = \raw_args ->

args = List.map raw_args Arg.display
args = List.map(raw_args, Arg.display)

# get the second argument, the first is the executable's path
when List.get args 1 |> Result.mapErr (\_ -> ZeroArgsGiven) is
Err ZeroArgsGiven ->
Err (Exit 1 "Error ZeroArgsGiven:\n\tI expected one argument, but I got none.\n\tRun the app like this: `roc main.roc -- input.txt`")
when List.get(args, 1) |> Result.map_err(\_ -> ZeroArgsGiven) is
Err(ZeroArgsGiven) ->
Err(Exit(1, "Error ZeroArgsGiven:\n\tI expected one argument, but I got none.\n\tRun the app like this: `roc main.roc -- input.txt`"))

Ok first_arg ->
Stdout.line! "received argument: $(first_arg)"
Ok(first_arg) ->
Stdout.line!("received argument: $(first_arg)")
32 changes: 16 additions & 16 deletions examples/command.roc
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,45 @@ import pf.Stdout
import pf.Cmd

main! = \_args ->
try status_example! {}
status_example!({})?

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

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

Ok {}
Ok({})

exec_example! : {} => Result {} _
exec_example! = \{} -> Cmd.exec! "echo" ["EXEC"]
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! = \{} ->
result =
Cmd.new "env"
|> Cmd.arg "-v"
Cmd.new("env")
|> Cmd.arg("-v")
|> Cmd.clear_envs
|> Cmd.envs [("FOO", "BAR"), ("BAZ", "DUCK")]
|> Cmd.envs([("FOO", "BAR"), ("BAZ", "DUCK")])
|> Cmd.status!

when result is
Ok exit_code if exit_code == 0 -> Ok {}
Ok exit_code -> Stdout.line! "Child exited with non-zero code: $(Num.toStr exit_code)"
Err err -> Stdout.line! "Error executing command: $(Inspect.toStr err)"
Ok(exit_code) if exit_code == 0 -> Ok({})
Ok(exit_code) -> Stdout.line!("Child exited with non-zero code: $(Num.to_str(exit_code))")
Err(err) -> Stdout.line!("Error executing command: $(Inspect.to_str(err))")

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

output =
Cmd.new "env"
Cmd.new("env")
|> Cmd.clear_envs
|> Cmd.env "FOO" "BAR"
|> Cmd.args ["-v"]
|> Cmd.env("FOO", "BAR")
|> Cmd.args(["-v"])
|> Cmd.output!

msg = Str.fromUtf8 output.stdout |> Result.withDefault "Failed to decode stdout"
msg = Str.from_utf8(output.stdout) |> Result.with_default("Failed to decode stdout")
Stdout.write! msg
Stdout.write!(msg)
16 changes: 8 additions & 8 deletions examples/countdown.roc
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import pf.Stdin
import pf.Stdout

main! = \_args ->
try Stdout.line! "\nLet's count down from 3 together - all you have to do is press <ENTER>."
_ = Stdin.line! {}
tick! 3
Stdout.line!("\nLet's count down from 3 together - all you have to do is press <ENTER>.")?
_ = Stdin.line!({})
tick!(3)
tick! = \n ->
if n == 0 then
try Stdout.line! "πŸŽ‰ SURPRISE! Happy Birthday! πŸŽ‚"
Ok {}
Stdout.line!("πŸŽ‰ SURPRISE! Happy Birthday! πŸŽ‚")?
Ok({})
else
try Stdout.line! (n |> Num.toStr |> \s -> "$(s)...")
_ = Stdin.line! {}
tick! (n - 1)
Stdout.line!((n |> Num.to_str |> \s -> "$(s)..."))?
_ = Stdin.line!({})
tick!((n - 1))
Loading

0 comments on commit c89c762

Please sign in to comment.