Skip to content

Commit

Permalink
updating ex_pp to better handle literal values
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Vorreuter committed Aug 19, 2009
1 parent 70e3eca commit 3cecc33
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 30 deletions.
7 changes: 3 additions & 4 deletions Makefile
@@ -1,4 +1,4 @@
VERSION=0.2
VERSION=0.3
PKGNAME=excavator
LIBDIR=`erl -eval 'io:format("~s~n", [code:lib_dir()])' -s init stop -noshell`
ROOTDIR=`erl -eval 'io:format("~s~n", [code:root_dir()])' -s init stop -noshell`
Expand All @@ -24,7 +24,7 @@ rel: compile
erl -pa ebin -noshell -run excavator build_rel -s init stop

package: clean
@mkdir $(PKGNAME)-$(VERSION)/ && cp -rf ebin excavator include Makefile public README.markdown src support t $(PKGNAME)-$(VERSION)
@mkdir $(PKGNAME)-$(VERSION)/ && cp -rf ebin include Makefile public README.markdown src support t templates $(PKGNAME)-$(VERSION)
@COPYFILE_DISABLE=true tar zcf $(PKGNAME)-$(VERSION).tgz $(PKGNAME)-$(VERSION)
@rm -rf $(PKGNAME)-$(VERSION)/

Expand All @@ -33,5 +33,4 @@ install:
@mkdir -p $(prefix)/$(ROOTDIR)/bin
for i in ebin/*.beam include/*.hrl ebin/*.app; do install $$i $(prefix)/$(LIBDIR)/$(PKGNAME)-$(VERSION)/$$i ; done
cp *.boot $(prefix)/$(ROOTDIR)/bin/
@mkdir -p $(prefix)/etc/init.d
cp excavator $(prefix)/etc/init.d/
@mkdir -p $(prefix)/etc/init.d
1 change: 1 addition & 0 deletions include/excavator.hrl
Expand Up @@ -24,6 +24,7 @@
-define(CRITICAL_MSG(Format, Args), ex_logger:critical_msg(Format, Args)).

-record(http_response, {status, headers, body}).
-record(http_request, {method=get, url=[], headers=[], body=[]}).

-define(ADD, fun ex_util:add/3).
-define(GLOBAL_ADD, fun ex_util:global_add/3).
Expand Down
50 changes: 29 additions & 21 deletions src/ex_pp.erl
Expand Up @@ -69,7 +69,7 @@ parse(Filename, MainArgs) when is_list(Filename) ->

process_root_level_form({function,L,main,Arity,Clauses}) ->
{function,L,main,Arity,[
{clause,L1,Args,Guards,[to_cons(lists:reverse(build_instrs(Tokens, [])))]}
{clause,L1,Args,Guards,[to_cons(build_instrs(Tokens))]}
|| {clause,L1,Args,Guards,Tokens} <- Clauses]};

process_root_level_form(Form) -> Form.
Expand All @@ -82,36 +82,33 @@ module_name(_, {ok, true}) ->
module_name(Filename, _) ->
list_to_atom(binary_to_list(erlang:md5(Filename))).

build_instrs([], Acc) -> Acc;
build_instrs([{atom,_,ok}|Tail], Acc) ->
build_instrs(Tail, Acc);
build_instrs([Instr|Tail], Acc) ->
build_instrs(Tail, [build_instr(Instr)|Acc]).
build_instrs(Tokens) ->
[build_instr(Token) || Token <- Tokens].

build_cons_instrs({cons,_,Instr,{nil,_}}) ->
{cons, ?L, build_instr(Instr), {nil,?L}};

build_cons_instrs({cons,_,Instr,Cons}) ->
{cons, ?L, build_instr(Instr), build_cons_instrs(Cons)}.

build_instr({call, _, {atom, _, each}, [Key, Source, Instrs]}) ->
{tuple, ?L, [{atom,?L,instr}, {atom,?L,each}, to_cons([Key, Source, build_instr(Instrs)])]};
{tuple, ?L, [{atom,?L,instr}, {atom,?L,each}, to_cons([Key, Source, build_cons_instrs(Instrs)])]};

build_instr({call, _, {atom, _, condition}, [Condition, Instrs]}) ->
{tuple, ?L, [{atom,?L,instr}, {atom,?L,condition}, to_cons([expand_condition(Condition), build_instr(Instrs)])]};
{tuple, ?L, [{atom,?L,instr}, {atom,?L,condition}, to_cons([expand_condition(Condition), build_cons_instrs(Instrs)])]};

build_instr({call, _, {atom, _, condition}, [Condition, TrueInstrs, FalseInstrs]}) ->
{tuple, ?L, [{atom,?L,instr}, {atom,?L,condition}, to_cons([expand_condition(Condition), build_instr(TrueInstrs), build_instr(FalseInstrs)])]};
{tuple, ?L, [{atom,?L,instr}, {atom,?L,condition}, to_cons([expand_condition(Condition), build_cons_instrs(TrueInstrs), build_cons_instrs(FalseInstrs)])]};

build_instr({call, _, {atom, _, onfail}, [Error, Instrs]}) ->
{tuple, ?L, [{atom,?L,instr}, {atom,?L,onfail}, to_cons([Error, build_instr(Instrs)])]};
{tuple, ?L, [{atom,?L,instr}, {atom,?L,onfail}, to_cons([Error, build_cons_instrs(Instrs)])]};

build_instr({call, _, {atom, _, onfail}, [Error, Instrs, FailInstrs]}) ->
{tuple, ?L, [{atom,?L,instr}, {atom,?L,onfail}, to_cons([Error, build_instr(Instrs), build_instr(FailInstrs)])]};
{tuple, ?L, [{atom,?L,instr}, {atom,?L,onfail}, to_cons([Error, build_cons_instrs(Instrs), build_cons_instrs(FailInstrs)])]};

build_instr({call, _, {atom, _, function}, [Function]}) ->
{tuple, ?L, [{atom,?L,instr}, {atom,?L,function}, to_cons([Function])]};

build_instr({cons,_,Instr,{nil,_}}) ->
{cons, ?L, build_instr(Instr), {nil,?L}};

build_instr({cons,_,Instr,Cons}) ->
{cons, ?L, build_instr(Instr), build_instr(Cons)};


build_instr({call, _, {atom, _, assert}, [Condition]}) ->
{tuple, ?L, [{atom,?L,instr}, {atom,?L,assert}, to_cons([expand_condition(Condition)])]};

Expand All @@ -121,9 +118,20 @@ build_instr({call, _, {atom, _, Instr}, Args})
Instr==onfail; Instr==commit; Instr==add;
Instr==gassign; Instr==gadd ->
{tuple, ?L, [{atom,?L,instr}, {atom,?L,Instr}, to_cons(Args)]};

build_instr(Term) ->
{tuple, ?L, [{atom,?L,instr}, {atom,?L,term}, to_cons([Term])]}.

build_instr({Type, _, _} = Term) when Type == atom;
Type == tuple;
Type == integer;
Type == float;
Type == bin;
%%Type == var;
Type == string ->
{tuple, ?L, [{atom,?L,instr}, {atom,?L,term}, to_cons([Term])]};

build_instr({cons, _, _, _} = Cons) ->
{tuple, ?L, [{atom,?L,instr}, {atom,?L,term}, to_cons([Cons])]};

build_instr(Term) -> Term.

expand_condition({op, _, Op, Left, Right}) ->
{tuple, ?L, [{atom, ?L, op}, {atom, ?L, Op}, expand_condition(Left), expand_condition(Right)]};
Expand Down
4 changes: 2 additions & 2 deletions src/excavator.erl
Expand Up @@ -51,7 +51,7 @@ init(_) ->
build_rel() ->
{ok, FD} = file:open("excavator.rel", [write]),
RelInfo = {release,
{"excavator", "0.2"},
{"excavator", "0.3"},
{erts, "5.7.2"}, [
{kernel, "2.13.2"},
{stdlib, "1.16.2"},
Expand All @@ -61,7 +61,7 @@ build_rel() ->
{mochiweb, "0.2"},
{mochixpath, "0.1"},
{dynamic_compile, "0.1"},
{excavator, "0.2"}
{excavator, "0.3"}
]
},
io:format(FD, "~p.", [RelInfo]),
Expand Down
8 changes: 5 additions & 3 deletions t/excavator_t_001.t
Expand Up @@ -19,11 +19,13 @@ start() ->
application:start(excavator),
test_server:start_link(),

ex_engine:run(ex_pp:parse("templates/iteration_tests.ex", [])),
%[a,b,c] = ex_engine:run(ex_pp:parse("templates/non_instr_tests.ex")),

ok = ex_engine:run(ex_pp:parse("templates/iteration_tests.ex", [])),

ex_engine:run(ex_pp:parse("templates/assignment_tests.ex", [])),
ok = ex_engine:run(ex_pp:parse("templates/assignment_tests.ex", [])),

ex_engine:run(ex_pp:parse("templates/assertion_tests.ex", [])),
ok = ex_engine:run(ex_pp:parse("templates/assertion_tests.ex", [])),

etap:is(ex_engine:run(ex_pp:parse("templates/overloaded.ex", [a])), "ABCD", "overloaded ok"),
etap:is(ex_engine:run(ex_pp:parse("templates/overloaded.ex", [b])), "ABCE", "overloaded ok"),
Expand Down
10 changes: 10 additions & 0 deletions templates/non_instr_tests.ex
@@ -0,0 +1,10 @@
main() ->
abc,
"abc",
1,
1.0,
<<"abc">>,
{a,b,c},
[a,b,c],
{ABC, _} = {[a,b,c], abc},
ABC.

0 comments on commit 3cecc33

Please sign in to comment.