Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add most of the RustGlue code (does not work yet) #5930

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions crates/compiler/build/src/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1385,6 +1385,8 @@ fn run_build_command(mut command: Command, file_to_build: &str, flaky_fail_count
command_string.push(arg);
}

command.current_dir(std::env::current_dir().unwrap());

let cmd_str = command_string.to_str().unwrap();
let cmd_output = command.output().unwrap();
let max_flaky_fail_count = 10;
Expand Down
34 changes: 33 additions & 1 deletion crates/compiler/builtins/roc/List.roc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ interface List
replace,
update,
append,
appendIfOk,
prepend,
prependIfOk,
map,
len,
withCapacity,
Expand All @@ -15,7 +18,6 @@ interface List
single,
repeat,
reverse,
prepend,
join,
keepIf,
contains,
Expand Down Expand Up @@ -321,6 +323,21 @@ append = \list, element ->
|> List.reserve 1
|> List.appendUnsafe element

## If the given [Result] is `Ok`, add it to the end of a list.
## Otherwise, return the list unmodified.
##
## ```
## List.appendIfOk [1, 2, 3] (Ok 4)
##
## [0, 1, 2]
## |> List.appendIfOk (Err 3)
## ```
appendIfOk : List a, Result a * -> List a
appendIfOk = \list, result ->
when result is
Ok elem -> append list elem
Err _ -> list

## Writes the element after the current last element unconditionally.
## In other words, it is assumed that
##
Expand All @@ -337,6 +354,21 @@ appendUnsafe : List a, a -> List a
## ```
prepend : List a, a -> List a

## If the given [Result] is `Ok`, add it to the beginning of a list.
## Otherwise, return the list unmodified.
##
## ```
## List.prepend [1, 2, 3] (Ok 0)
##
## [2, 3, 4]
## |> List.prepend (Err 1)
## ```
prependIfOk : List a, Result a * -> List a
prependIfOk = \list, result ->
when result is
Ok elem -> prepend list elem
Err _ -> list

## Returns the length of the list - the number of elements it contains.
##
## One [List] can store up to 2,147,483,648 elements (just over 2 billion), which
Expand Down
7 changes: 6 additions & 1 deletion crates/compiler/gen_dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1941,7 +1941,12 @@ trait Backend<'a> {
self.build_eq(sym, &args[0], &Symbol::DEV_TMP, &arg_layouts[0]);
self.free_symbol(&Symbol::DEV_TMP)
}
Symbol::LIST_GET | Symbol::LIST_SET | Symbol::LIST_REPLACE | Symbol::LIST_APPEND => {
Symbol::LIST_GET
| Symbol::LIST_SET
| Symbol::LIST_REPLACE
| Symbol::LIST_APPEND
| Symbol::LIST_APPEND_IF_OK
| Symbol::LIST_PREPEND_IF_OK => {
// TODO: This is probably simple enough to be worth inlining.
let fn_name = self.lambda_name_to_string(
func_name,
Expand Down
2 changes: 2 additions & 0 deletions crates/compiler/module/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1434,6 +1434,8 @@ define_builtins! {
82 LIST_UPDATE: "update"
83 LIST_WALK_WITH_INDEX: "walkWithIndex"
84 LIST_CHUNKS_OF: "chunksOf"
85 LIST_APPEND_IF_OK: "appendIfOk"
86 LIST_PREPEND_IF_OK: "prependIfOk"
}
7 RESULT: "Result" => {
0 RESULT_RESULT: "Result" exposed_type=true // the Result.Result type alias
Expand Down
59 changes: 54 additions & 5 deletions crates/glue/src/RustGlue.roc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ app "rust-glue"
pf.Shape.{ Shape, RocFn },
pf.File.{ File },
pf.TypeId.{ TypeId },
"../static/roc_fn/Cargo.toml" as rocFnCargoToml : Str,
"../static/roc_fn/src/lib.rs" as rocFnLibRs : Str,
"../static/Cargo.toml" as rocAppCargoToml : Str,
"../../roc_std/Cargo.toml" as rocStdCargoToml : Str,
"../../roc_std/src/lib.rs" as rocStdLib : Str,
Expand Down Expand Up @@ -34,15 +36,24 @@ makeGlue = \typesByArch ->

"""

typesByArch
|> List.map convertTypesToFile
|> List.append { name: "roc_app/src/lib.rs", content: modFileContent }
|> List.concat staticFiles
rocAppFiles =
typesByArch
|> List.map convertTypesToFile
|> List.append { name: "roc_app/src/lib.rs", content: modFileContent }

rocFnFile =
List.first typesByArch
|> Result.map convertEffectsToFile

staticFiles
|> List.concat rocAppFiles
|> List.appendIfOk rocFnFile
|> Ok

## These are always included, and don't depend on the specifics of the app.
staticFiles : List File
staticFiles = [
{ name: "roc_fn/Cargo.toml", content: rocFnCargoToml },
{ name: "roc_app/Cargo.toml", content: rocAppCargoToml },
{ name: "roc_std/Cargo.toml", content: rocStdCargoToml },
{ name: "roc_std/src/lib.rs", content: rocStdLib },
Expand Down Expand Up @@ -127,6 +138,44 @@ convertTypesToFile = \types ->
content: content |> generateEntryPoints types,
}

convertEffectsToFile : Types -> File
convertEffectsToFile = \_types ->
# TODO actually generate the effect types
# effectsStr = List.walk (Types.effects types) "" \accum, T name id -> generateEffect accum types name id
effectsStr = ""

{
name: "roc_fn/src/lib.rs",
content: Str.replaceFirst rocFnLibRs "// {{ROC_HOSTED_FNS}} //" effectsStr,
}

# generateEffect : Str, Types, Str, TypeId -> Str
# generateEffect = \buf, types, name, id ->
# { args, ret } =
# when Types.shape types id is
# Function rocFn ->
# {
# args:
# rocFn.args
# |> List.map \argId -> typeName types argId
# |> Str.joinWith ", ",
#
# ret: typeName types rocFn.ret
# }
#
# _ ->
# # This will compile to a thunk, e.g. `{} -> Ret`
# { args: "", ret: typeName types id }
#
# """
# \(buf)
# HostedFn {
# name: "\(name)",
# arg_types: &[\(args)],
# ret_type: \(ret),
# },
# """

generateEntryPoints : Str, Types -> Str
generateEntryPoints = \buf, types ->
List.walk (Types.entryPoints types) buf \accum, T name id -> generateEntryPoint accum types name id
Expand Down Expand Up @@ -2016,6 +2065,7 @@ archName = \arch ->
X86x64 ->
"x86_64"

fileHeader : Str
fileHeader =
"""
// ⚠️ GENERATED CODE ⚠️ - this entire file was generated by the `roc glue` CLI command
Expand All @@ -2037,7 +2087,6 @@ fileHeader =
#![allow(clippy::clone_on_copy)]



"""

indent = " "
Expand Down
Loading