Skip to content
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
7 changes: 7 additions & 0 deletions rebar.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
{erl_opts, [debug_info]}.
{deps, [
{jsx, "2.8.0"}
]}.
{profiles, [
{test, [
{deps, [
{elmer_runtime, {git, "git://github.com/elmer-compiler/elmer_runtime.git", {branch, "master"}}}
]}
]}
]}.
30 changes: 24 additions & 6 deletions src/elmer_compiler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,34 @@ compile_elmo_file(ElmoFile, Output) ->
Erl = elmer_to_erl:from_file(ElmoFile),
dump(Output, ElmoFile, Erl).

dump(absform, Filename, Erl) ->
{Filename, Erl};
dump(absform, ElmoFile, Erl) ->
{ElmoFile, Erl};

dump(erlsrc, Filename, Erl) ->
dump(erlsrc, ElmoFile, Erl) ->
Src = lists:map(fun erl_pp:form/1, Erl),
Bin = erlang:iolist_to_binary(Src),
{Filename, Bin};
{ElmoFile, Bin};

dump(stdout, Filename, Erl) ->
dump({beam, OutputDir}, ElmoFile, Erl) ->
io:format("~s~n", [ElmoFile]),
%% io:format("~p~n", [Erl]),
case compile:forms(Erl, [report_errors, report_warnings, return_errors]) of
{ok, Module, Bin} ->
%% OutputFilename = filename:basename(ElmoFile, ".elmo") ++ ".beam",
OutputFilename = filename:basename(ElmoFile, ".elmo") ++ ".beam",
OutputFilepath = filename:join(filename:absname(OutputDir), OutputFilename),
io:format("~s~n", [OutputFilepath]),
io:format("~s~n", [Module]),
ok = file:write_file(OutputFilepath, io_lib:fwrite("~p.\n", [Bin])),
{ElmoFile, Bin};
X ->
{_, Bin} = dump(erlsrc, ElmoFile, Erl),
io:format("~s~n~p~n", [Bin, X]),
ok = X
end;

dump(stdout, ElmoFile, Erl) ->
io:format("-----~n|~s~n-----~n~s~n", [
Filename,
ElmoFile,
lists:concat(lists:map(fun erl_pp:form/1, Erl))]).

26 changes: 20 additions & 6 deletions src/elmer_to_erl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ compile(?JSON_DEF(Name, Value), Elmo = #elmo{ defs = Defs }) ->
Def = to_erl({def, Name, to_erl(Value)}),
Elmo#elmo{ defs = [Def | Defs]};

compile(?JSON_TAILDEF(Name, Args, Content), Elmo = #elmo{ defs = Defs })->
compile(?JSON_TAILDEF(Name, Args, Content), Elmo = #elmo{ defs = Defs }) ->
VArgs = [{var, ?ELINE, elmer_util:var(A)} || A <- Args],
Fun = {'fun', ?ELINE, {clauses, [{clause, ?ELINE, VArgs, [], exps(to_erl(Content))}]}},
Def = to_erl({def, Name, ?ELMER_PARTIAL(Fun, length(Args))}),
Expand Down Expand Up @@ -130,7 +130,7 @@ to_erl({var, Name}) ->
{var, ?ELINE, elmer_util:var(Name)};

to_erl(?JSON_LET(Defs, Body)) ->
[{match, ?ELINE, {var, ?ELINE, elmer_util:var(Name)}, to_erl(Val)} || ?JSON_DEF(Name, Val) <- Defs] ++ exps(to_erl(Body));
[match_expr(Def) || Def <- Defs] ++ exps(to_erl(Body));

to_erl(?JSON_IF(Conds, Else)) ->
to_erl({ifelse, Conds, Else});
Expand All @@ -156,16 +156,22 @@ to_erl(?JSON_REF(Name, ?JSON_MODULE(Module, Package))) ->
to_erl({call, {Name, Module, Package}, []});

to_erl(?JSON_REF(Name, ?JSON_TOPLEVEL)) ->
{call, ?ELINE, {atom, ?ELINE, elmer_util:btoa(Name)}, []};
NameAtom = {atom, ?ELINE, elmer_util:btoa(Name)},
ModuleAtom = {atom, ?ELINE, '?MODULE'},
{call, ?ELINE, {remote, ?ELINE, ModuleAtom, NameAtom}, []};

to_erl(?JSON_CALL(Fun, Args)) ->
{call, ?ELINE, to_erl(Fun), [to_erl({cons, Args})]};

to_erl(?JSON_TAILCALL(Fun, Args)) ->
{call, ?ELINE, {call, ?ELINE, {atom, ?ELINE, elmer_util:btoa(Fun)}, []}, [to_erl({cons, Args})]};
ModuleAtom = {atom, ?ELINE, '?MODULE'},
NameAtom = {atom, ?ELINE, elmer_util:btoa(Fun)},
{call, ?ELINE, {call, ?ELINE, {remote, ?ELINE, ModuleAtom, NameAtom}, []}, [to_erl({cons, Args})]};

to_erl(?JSON_TOPLVAR(Name)) ->
{call, ?ELINE, {atom, ?ELINE, elmer_util:btoa(Name)}, []};
NameAtom = {atom, ?ELINE, elmer_util:btoa(Name)},
ModuleAtom = {atom, ?ELINE, '?MODULE'},
{call, ?ELINE, {remote, ?ELINE, ModuleAtom, NameAtom}, []};

to_erl(?JSON_MODVAR(Name, Module, Package)) ->
to_erl({call, {Name, Module, Package}, []});
Expand Down Expand Up @@ -229,7 +235,7 @@ to_erl({ifelse, [[CondA, ThenA] | Conds], Else}) ->

to_erl({ifelse, Cond, Then, Else}) ->
ThenClause = {clause, ?ELINE, [{atom, 0, true}], [], exps(to_erl(Then))},
ElseClause = {clause, ?ELINE, [{var, 0, '_else'}], [], exps(to_erl(Else)) },
ElseClause = {clause, ?ELINE, [{var, 0, '_'}], [], exps(to_erl(Else)) },
{'case', ?ELINE, to_erl(Cond), [ThenClause, ElseClause]};

to_erl({'case', Var, Leaf = ?JSON_LEAF(_), Jumps}) ->
Expand Down Expand Up @@ -287,3 +293,11 @@ case_pattern(?JSON_CONSTRUCTOR(?JSON_REF(DataName, _))) ->
case_pattern(Lit = ?JSON_LIT(_)) ->
to_erl(Lit).

match_expr(?JSON_DEF(Name, Val)) ->
{match, ?ELINE, {var, ?ELINE, elmer_util:var(Name)}, to_erl(Val)};

match_expr(?JSON_TAILDEF(Name, Args, Content)) ->
VArgs = [{var, ?ELINE, elmer_util:var(A)} || A <- Args],
Fun = {'fun', ?ELINE, {clauses, [{clause, ?ELINE, VArgs, [], exps(to_erl(Content))}]}},
{match, ?ELINE, {var, ?ELINE, elmer_util:var(Name)}, ?ELMER_PARTIAL(Fun, length(Args))}.

20 changes: 14 additions & 6 deletions test/elmer_to_erl_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,28 @@ on_cwd(Cwd, Fun) ->
ok = file:set_cwd(OldDir)
end.

elm_compile(ElmModuleName) ->
elm_compile(ElmModuleName, Format) ->
ElmFileName = ElmModuleName ++ ".elm",
on_cwd("test/files", fun () -> elmer_compiler:compile([ElmFileName], absform, []) end).
on_cwd("test/files", fun () -> elmer_compiler:compile([ElmFileName], Format, []) end).

elm_compile_module(ElmModuleName) ->

elm_compile_module(ElmModuleName, Format) ->
ElmoFileName = ?BUILD_DIR ++ "/" ++ ElmModuleName ++ ".elmo",
Compiled = elm_compile(ElmModuleName),
Compiled = elm_compile(ElmModuleName, Format),
proplists:get_value(ElmoFileName, Compiled, elm_not_compiled).

elm_absform_module(ElmModuleName) ->
elm_compile_module(ElmModuleName, absform).


assert_elm_compiles_to_erl(ElmModuleName) ->
CompiledForms = reset_forms_line(elm_compile_module(ElmModuleName)),
CompiledForms = reset_forms_line(elm_absform_module(ElmModuleName)),
%% Ignore file attribute from expected erl code. (TODO: fix when we have original elm file name)
[{attribute, _, file, _} | ExpectedForms ] = reset_forms_line(erl_parse_file(ElmModuleName)),
?assertEqual(ExpectedForms, CompiledForms).

does_not_compile_Invalid_test() ->
?assertEqual(error, elm_compile("Invalid")).
?assertEqual(error, elm_compile("Invalid", absform)).

compiles_StringLit_test() ->
assert_elm_compiles_to_erl("StringLit").
Expand Down Expand Up @@ -138,6 +143,9 @@ compiles_RecordTypeAlias_test() ->
compiles_AnonFun_test() ->
assert_elm_compiles_to_erl("AnonFun").

compiles_LetInTailDef_test() ->
assert_elm_compiles_to_erl("LetInTailDef").

compiles_FwdFunApplication_test() ->
assert_elm_compiles_to_erl("FwdFunApplication").

Expand Down
1 change: 1 addition & 0 deletions test/files/Elm.LetInTailDef.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-module('Elm.LetInTailDef').
8 changes: 8 additions & 0 deletions test/files/Elm.Native.Utils.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-module('Elm.Native.Utils').
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Elm.Native.* should not be implemented here, I'm thinking of having a new repo on the elmer-compiler organization that mirror those from elm-lang, for example one repo for core, one for html, etc, and they MUST use the same semantic version than the package from elm. So for example, Elm.Native.Utils should be on elmer-core etc. IIRC the elm-compiler just concatenates those Native javascript modules when everything is compiled to javascript. But we must require the user to list each elmer-* native package as another erlang dependency at runtime.


-export([ append/0 ]).

append() ->
elmer_runtime:partial(fun (Xs, Ys) when is_binary(Xs) -> erlang:iolist_to_binary([Xs, Ys]);
(Xs, Ys) when is_list(Xs) -> lists:append(Xs, Ys)
end, 2).
12 changes: 12 additions & 0 deletions test/files/LetInTailDef.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module LetInTailDef exposing (foo)

foo =
let
sum x y =
if y == 0 then
x
else
sum (x + y) 0
val = sum 2 3
in
val
4 changes: 4 additions & 0 deletions test/files/RunExample.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module RunExample exposing (greet)

greet name =
"Howdy, " ++ name
50 changes: 50 additions & 0 deletions test/native_elmer_test.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
-module(native_elmer_test).

-ifdef(TEST).

-include_lib("eunit/include/eunit.hrl").

-define(ELINE, 0).
-define(USER_BUILD_DIR, "elm-stuff/build-artifacts/0.17.1/user/project/1.0.0").
-define(CORE_BUILD_DIR, "elm-stuff/build-artifacts/0.17.1/elm-lang/core/4.0.5").

on_cwd(Cwd, Fun) ->
{ok, OldDir} = file:get_cwd(),
try file:set_cwd(Cwd) of
ok -> Fun()
after
ok = file:set_cwd(OldDir)
end.

elm_compile(ElmModuleName, Format) ->
ElmFileName = ElmModuleName ++ ".elm",
on_cwd("test/files", fun () -> elmer_compiler:compile([ElmFileName], Format, []) end).


user_build_filepath(ElmModuleName) ->
?USER_BUILD_DIR ++ "/" ++ ElmModuleName ++ ".elmo".

core_build_filepath(ElmModuleName) ->
?CORE_BUILD_DIR ++ "/" ++ ElmModuleName ++ ".elmo".

elm_load_module(ElmModuleName, CoreModuleName) ->
Compiled = elm_compile(ElmModuleName, {beam, "build"}),
UserElmoFileName = user_build_filepath(ElmModuleName),
{ok, Module, CompiledBinary} = proplists:get_value(UserElmoFileName, Compiled, elm_not_compiled),
{module, Module} = code:load_binary(Module, ElmModuleName, CompiledBinary),
%% TODO Resolve many core module dependencies
CoreElmoFileName = core_build_filepath(CoreModuleName),
{ok, CoreModule, CoreCompiledBinary} = proplists:get_value(CoreElmoFileName, Compiled, elm_not_compiled),
{module, CoreModule} = code:load_binary(CoreModule, CoreModuleName, CoreCompiledBinary),
Module.

runs_RunExample_test() ->
elm_load_module("RunExample", "Basics"),
%% TODO Figure out where to put Native modules
Result = ('Elm.RunExample':greet())([<<"doodie">>]),
?assertEqual(<<"Howdy, doodie">>, Result),
Result2 = ('Elm.Native.Utils':append())([[1,2],[3,4]]),
?assertEqual([1,2,3,4], Result2).

-endif. %% TEST