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

Pipe operator support #91

Closed
wants to merge 4 commits into from
Closed
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
4 changes: 4 additions & 0 deletions caramel/compiler/ocaml_to_erlang/names.ml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ let ocaml_to_erlang_primitive_op t f =
Name.qualified
~m:(Name.atom (Atom.mk "caramel_runtime"))
~f:(Name.atom (Atom.mk "binary_concat"))
| "|>" ->
Name.qualified
~m:(Name.atom (Atom.mk "caramel_runtime"))
~f:(Name.atom (Atom.mk "pipe"))
| "!=" | "<>" -> to_erl_op "=/="
| "not" -> to_erl_op "not"
| "&&" -> to_erl_op "and"
Expand Down
3 changes: 3 additions & 0 deletions stdlib/caramel_runtime.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

-export([
binary_concat/2,
pipe/2,
recv/0,
recv/1
]).
Expand All @@ -16,3 +17,5 @@ recv(Timeout) ->

binary_concat(A, B) when is_binary(A) and is_binary(B) ->
<< (A)/binary, (B)/binary >>.

pipe(A, B) -> B(A).
2 changes: 2 additions & 0 deletions stdlib/caramel_runtime.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ external recv_with_timeout : int -> 'a = "recv"
external recv_and_wait : unit -> 'a = "recv"

external ( ^ ) : string -> string -> string = "binary_concat"

external ( |> ) : 'a -> ('a -> 'b) -> 'b = "pipe"
6 changes: 6 additions & 0 deletions tests/compiler/functions.t/pipe.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let print_int number = Io.format "~0tp~n" [ number ]

let subtract x y = y - x
let main _ =
let divide x y = y / x in
10 |> subtract 2 |> divide 4 |> print_int
26 changes: 26 additions & 0 deletions tests/compiler/functions.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
multiple_clauses.ml
partial_functions.ml
pattern_aliases.ml
pipe.ml
qualified_calls.ml
qualified_calls_helper.ml
redefine.ml
Expand Down Expand Up @@ -222,6 +223,31 @@
end.


$ caramel compile pipe.ml
Compiling pipe.erl OK
$ cat pipe.erl
% Source code generated with Caramel.
-module(pipe).

-export([main/1]).
-export([print_int/1]).
-export([subtract/2]).

-spec print_int(_) -> ok.
print_int(Number) -> io:format(<<"~0tp~n">>, [Number | []]).

-spec subtract(integer(), integer()) -> integer().
subtract(X, Y) -> erlang:'-'(Y, X).

-spec main(_) -> ok.
main(_) ->
Divide = fun
(X, Y) -> erlang:'div'(Y, X)
end,
caramel_runtime:pipe(caramel_runtime:pipe(caramel_runtime:pipe(10, subtract(2)), Divide(4)), fun print_int/1).



$ caramel compile qualified_calls_helper.ml qualified_calls.ml
Compiling qualified_calls_helper__nested.erl OK
Compiling qualified_calls_helper.erl OK
Expand Down