leostera / caramel

:candy: a functional language for building type-safe, scalable, and maintainable applications

Home Page:https://caramel.run

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support expression sequences

michallepicki opened this issue · comments

Describe the bug
When combining multiple OCaml expressions with ( expr1 ; expr2 ) or begin expr1 ; expr2 end, the resulting Erlang code is "flattened" and can have different result than expected or not compile at all.

To Reproduce

  1. Create a file main.ml containing
let print thing = Io.format "~0tp~n" [ thing ]

let main _ =
  print
    ( print "hej" ;
      true )

or

let print thing = Io.format "~0tp~n" [ thing ]

let main _ =
  print
    begin
      print "hej" ;
      true
    end
  1. Run command caramel compile main.ml && escript main.erl
  2. See error:
main.erl:10: function print/2 undefined

This happens because of this incorrect generated Erlang code:

% Source code generated with Caramel.
-module(main).

-export([main/1]).
-export([print/1]).

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

-spec main(_) -> ok.
main(_) -> print(print(<<"hej">>),
true).

Expected behavior
Erlang gets generated with:

-spec main(_) -> ok.
main(_) -> print(begin print(<<"hej">>), true end).

Environment (please complete the following information):

  • OS: Ubuntu Linux 18.04
  • Caramel version 0.1
  • Erlang version 23.2.2

I will look into this one

With #80 as of e2f88eb we're getting this output:

% Source code generated with Caramel.
-module(main).

-export([main/1]).
-export([print/1]).

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

-spec main(_) -> ok.
main(_) -> print(begin
  print(<<"hej">>),
  true
end).

and it runs:

$ caramel compile main.ml
Compiling main.erl      OK
$ escript main.erl
<<"hej">>
true