Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

program generation now working

  • Loading branch information...
commit b5d51cdc7712ddae98dc90cec94c17d93ac3c68b 1 parent 2f5f8b1
@joearms authored
View
1  .gitignore
@@ -3,6 +3,7 @@
erl_parse.erl
.eunit/*
deps/*
+gen/*.erl
ebin
priv/*
*.o
View
4 TODO
@@ -1,3 +1,5 @@
+* means done
+
1) Add sections
begin{latex}
@@ -14,5 +16,5 @@
Pid = "/dev/spawn !! Fun"
-3) fix the erl99 bug :-)
+* 3) fix the erl99 bug :-)
View
12 dev.erl2
@@ -1,11 +1,18 @@
defMods mod1 end.
+addMod shell.
+
+def fac = fun(0) -> 1; (N) -> N*fac(N-1) end.
+
+Fac50 = fac(50).
+
+
addMod mod1.
-A = aaaaaaaaaaaaaaaaaaaaaaaaaaaa.
+A = aaaaaaaaaaaaaaaaaaaaaaa.
B = bbbbbbbbbbbbbbbbbbbbbbbbbbbb.
-def foo(X,1) -> X + A;
+def foo(X,1) -> X + A +Fac50;
foo(P,2) -> P + B;
foo(A,X) -> A + X + B;
foo(A,B) -> A + B
@@ -17,7 +24,6 @@ def foo1(X) ->
end,
F(X, A) end.
-erl2:dump().
erl2:make_mods().
View
10 erl2.erl
@@ -30,6 +30,7 @@
batch([A]) ->
F = atom_to_list(A),
run0(F),
+ os:cmd("rm ge/*.erl"),
init:stop().
%% ho ho
@@ -40,18 +41,16 @@ run0(F) ->
put(current_function, none),
run(F).
-
run(F) ->
case consult(F) of
{ok, Exprs} ->
- dump("exprs.tmp", Exprs),
+ dump("gen/exprs.tmp", Exprs),
B0 = erl_eval:new_bindings(),
case (catch erl_eval:exprs(Exprs, B0, {eval, fun local/3})) of
{'EXIT', Why} ->
io:format("Error:~p~n",[Why]),
io:format("Compile failed~n");
_Other ->
- dump("all.gen", lists:sort(get())),
io:format("Success~n")
end;
error ->
@@ -174,7 +173,10 @@ munge_toks([H|T]) -> [H|munge_toks(T)];
munge_toks([]) -> [].
make_mods() ->
- erl2_codegen:start().
+ io:format("make_mods saving generated code...~n"),
+ L = get(),
+ dump("gen/all.gen", L),
+ erl2_codegen:start(L).
clone(Old, New) ->
%% Update the list of defining modules
View
60 erl2_codegen.erl
@@ -4,8 +4,7 @@
test() ->
epp:parse_file("./erl2_codegen.erl","","").
-start() ->
- L = get(),
+start(L) ->
Mods = get_mods(L),
[compile_mod(I, L) || I <- Mods].
@@ -16,7 +15,14 @@ get_mods(L) ->
compile_mod(Mod, L) ->
Funcs = get_funcs(Mod, L),
- [compile_func(I) || I<- Funcs].
+ put(current_mod, Mod),
+ SMod = atom_to_list(Mod),
+ File = "gen/" ++ SMod ++ ".erl",
+ {ok,Stream} = file:open(File, [write]),
+ io:format(Stream, "-module(~s).\n-compile(export_all).~n~n",[SMod]),
+ [compile_func(I, Stream) || I<- Funcs],
+ file:close(Stream),
+ io:format("Created:~s~n",[File]).
get_funcs(Mod, L) ->
lists:sort(elib1_misc:remove_duplicates(
@@ -25,27 +31,59 @@ get_funcs(Mod, L) ->
%%----------------------------------------------------------------------
-compile_func({Name,Arity,{Clauses, Bs}}) ->
- io:format("transform ~p/~p~n",[Name,Arity]),
+compile_func({Name,Arity,{Clauses, Bs}}, Stream) ->
+ %% io:format("transform ~p/~p~n",[Name,Arity]),
%% Step 1 - make a binding list
Bs1 = [Var || {Var,_} <- Bs],
Clauses1 = [transform_clauses(I, Bs1, Bs) || I <- Clauses],
F = {function,1,Name,Arity,Clauses1},
- S = erl_pp:form(F),
- io:format("~s~n",[S]).
+ Str = erl_pp:form(F),
+ io:format(Stream, "~s~n",[Str]).
-transform_clauses({clause, Ln, H, G, B}=C, Vars, Bs) ->
+transform_clauses({clause, Ln, H, G, B}, Vars, Bs) ->
Stack = [varsin(H),Vars],
Add = import_bindings0(B, Stack),
%% io:format("Add=~p~n",[Add]),
+ B1 = xform_body(B),
case Add of
- [] -> C;
+ [] ->
+ {clause, Ln, H, G, B1};
_ ->
M = [make_match(Ln, I, Bs) || I <-Add],
- B1 = M ++ B,
- {clause, Ln, H, G, B1}
+ B2 = M ++ B1,
+ {clause, Ln, H, G, B2}
end.
+xform_body(X) ->
+ deep_replace(X, fun fix_99/1).
+
+fix_99({call99,{Mod,Func},Args}) ->
+ case get(current_mod) of
+ Mod ->
+ {yes, {call,-1,{atom,-1,Func}, Args}};
+ _ ->
+ {yes, {call,-1,{remote, {atom,-1,Mod},{atom,-1,Func}}, Args}}
+ end;
+fix_99(_) ->
+ no.
+
+deep_replace(X, F) ->
+ case F(X) of
+ no ->
+ deep_replace1(X, F);
+ {yes, New} ->
+ deep_replace(New, F)
+ end.
+
+deep_replace1(T, F) when is_tuple(T) ->
+ L = tuple_to_list(T),
+ L1 = deep_replace(L, F),
+ list_to_tuple(L1);
+deep_replace1([H|T], F) ->
+ [deep_replace(H, F) | deep_replace(T, F)];
+deep_replace1(X, _) ->
+ X.
+
make_match(Ln, Var, Bs) ->
{match,Ln,{var,Ln,Var},value(Ln, Var, Bs)}.
View
18 erl_eval.erl
@@ -1333,9 +1333,6 @@ define_function(Name, Arity, Clauses, Bs0) ->
put1({fundef,RealName}, {Clauses1, Bs0}),
RealName.
-modname([{defun,_,_}|T]) -> modname(T);
-modname([H|_]) -> H.
-
funcname({Ran,L}, X) ->
case lists:member(X, L) of
true -> element(1, X);
@@ -1344,7 +1341,7 @@ funcname({Ran,L}, X) ->
funcname(none, X) ->
element(1, X).
-resolve({call,Ln1,{remote,_,{atom,_,Mod},{atom,_,Ln2,Func}}=Z,Args}) ->
+resolve({call,Ln1,{remote,_,{atom,_,Mod},{atom,_,_,Func}}=Z,Args}) ->
Args1 = resolve(Args),
case lists:member(Mod, get(defining_modules)) of
true ->
@@ -1376,19 +1373,6 @@ resolve_unqualified_call(F, Arity) ->
end,
{Mod, FName}.
-findit(C, N, A) ->
- Ret = findit0(C, N, A),
- %% io:format("findit:~p ~p => ~p~n",[C,{N,A},Ret]),
- Ret.
-
-findit0([{defun,Ln,L},H|_], Name, Arity) ->
- case lists:member({Name,Arity}, L) of
- true -> {H, Name, Arity}; %% exported
- false -> {H, {Ln,Name}, Arity}
- end;
-findit0([H|_], M, A) ->
- {H, M, A}.
-
put1(current_function,V) -> put(current_function, V);
put1(current_module,V) -> put(current_module, V);
put1({fundef,{Mod,Func,Sub,Arity}}=K, V) ->
View
37 example1.erl2
@@ -3,24 +3,51 @@
%% In the generated module mod1.erl
%% we import the result of the earlier computation
-defMods mod1 end.
+defMods mod1 shell end.
+
+addMod shell.
def fac = fun(0) -> 1; (N) -> N*fac(N-1) end.
F25 = fac(25).
addMod mod1.
- defExports a/1 end.
- def a(N) -> F25 + N end.
+def a(N) -> F25 + N end.
addMod shell.
io:format("mod1:a(10)=~p~n",[mod1:a(10)]).
+erl2:make_mods().
+
%% Sample output
-%% $./erl2 example1.erl2
+%% $./erl2 example1.erl2
+%% Created:"gen/exprs.tmp"
%% mod1:a(10)=15511210043330985984000010
-%% Created:"all.gen"
+%% make_mods saving generated code...
+%% Created:"gen/all.gen"
+%% Created:gen/mod1.erl
+%% Created:gen/shell.erl
%% Success
+
+
+%% $ cat gen/shell.erl
+%% -module(shell).
+%% -compile(export_all).
+
+%% fac(0) ->
+%% 1;
+%% fac(N) ->
+%% N * fac(N - 1).
+
+%% $cat gen/mod1.erl
+%% -module(mod1).
+%% -compile(export_all).
+
+%% a(N) ->
+%% F25 = 15511210043330985984000000,
+%% F25 + N.
+
+
View
2  example6.erl2
@@ -1,5 +1,5 @@
%% This is a patch to tests.erl2
-%% we'll remove the erronious function
+%% we'll remove the erroneous function
erl2:run("tests.erl2").
Please sign in to comment.
Something went wrong with that request. Please try again.