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

Support unary operators '+'/'-' #54

Merged
merged 8 commits into from
Jan 6, 2014
Merged
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
73 changes: 34 additions & 39 deletions src/ice.erl
Original file line number Diff line number Diff line change
Expand Up @@ -51,33 +51,7 @@ rework_tree (Tree) ->
{i_apply, IAbsExpr};

({call, _, FunExpr, Params}) ->
case {FunExpr,Params} of
{"atan2",[{b_param,N},{b_param,M}]} ->
ice_primop:atan2(N, M);
{Fun,[{b_param,N}]} when Fun == "floor";
Fun == "ceil";
Fun == "sin";
Fun == "cos";
Fun == "tan";
Fun == "asin";
Fun == "acos";
Fun == "atan";
Fun == "sinh";
Fun == "cosh";
Fun == "tanh";
Fun == "asinh";
Fun == "acosh";
Fun == "atanh";
Fun == "exp";
Fun == "log";
Fun == "log10";
Fun == "pow";
Fun == "sqrt";
Fun == "abs" ->
ice_primop:(list_to_existing_atom(Fun))(N);
_ ->
{fn_call, FunExpr, Params}
end;
bind_primop_to_base_fn_call(FunExpr, Params);

({where, _, Exp, DimDecls, VarDecls}) ->
TopExpr = Exp,
Expand All @@ -98,19 +72,24 @@ rework_tree (Tree) ->
({tuple, _, Assocs}) -> {t, Assocs};
({tuple_element, _, Lhs, Rhs}) -> {Lhs, Rhs};

({'or', _, A, B}) -> ice_primop:tor(A, B);
({'and', _, A, B}) -> ice_primop:tand(A, B);
({'==', _, A, B}) -> ice_primop:eq(A, B);
({'!=', _, A, B}) -> ice_primop:neq(A, B);
({'<', _, A, B}) -> ice_primop:lt(A, B);
({'<=', _, A, B}) -> ice_primop:lte(A, B);
({'==', _, A, B}) -> ice_primop:eq(A, B);
({'>=', _, A, B}) -> ice_primop:gte(A, B);
({'>', _, A, B}) -> ice_primop:gt(A, B);
({'!=', _, A, B}) -> ice_primop:neq(A, B);
({'+', _, A, B}) -> ice_primop:plus(A, B);
({'-', _, A, B}) -> ice_primop:minus(A, B);
({'*', _, A, B}) -> ice_primop:times(A, B);
({'/', _, A, B}) -> ice_primop:divide(A, B);
({'%', _, A, B}) -> ice_primop:mod(A, B);
({'>=', _, A, B}) -> ice_primop:gte(A, B);

({'not', _, A }) -> ice_primop:'not'(A);
({'or', _, A, B}) -> ice_primop:'or'(A, B);
({'and', _, A, B}) -> ice_primop:'and'(A, B);

({'+', _, A }) -> ice_primop:plus(A);
({'-', _, A }) -> ice_primop:minus(A);
({'+', _, A, B}) -> ice_primop:plus(A, B);
({'-', _, A, B}) -> ice_primop:minus(A, B);
({'*', _, A, B}) -> ice_primop:times(A, B);
({'/', _, A, B}) -> ice_primop:divide(A, B);
({'%', _, A, B}) -> ice_primop:mod(A, B);

({bool, _, Boolean}) -> Boolean;
({raw_string, _, S}) -> {string, S};
Expand All @@ -131,9 +110,25 @@ rework_tree (Tree) ->
end.


unwrap_elsifs ([{if_expr,_,Cond,Then}|Rest], Else) ->
bind_primop_to_base_fn_call(FunExpr, Params) when is_list(FunExpr) ->
case lists:all(fun({Type,_}) -> Type == b_param end, Params) of
true ->
Ps = lists:map(fun({_,P}) -> P end, Params),
try apply(ice_primop, list_to_atom(FunExpr), Ps) of
Primop -> Primop
catch
error:undef ->
{fn_call, FunExpr, Params}
end;
false ->
{fn_call, FunExpr, Params}
end;
bind_primop_to_base_fn_call(FunExpr, Params) ->
{fn_call, FunExpr, Params}.

unwrap_elsifs([{if_expr,_,Cond,Then}|Rest], Else) ->
{'if', Cond, Then, unwrap_elsifs(Rest,Else)};
unwrap_elsifs ([], Else) ->
unwrap_elsifs([], Else) ->
Else.

%% End of Module.
3 changes: 1 addition & 2 deletions src/ice_core.erl
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ eval({primop, Primop, Eis}, I, E, K, D, W, T) ->
{true, Dims} ->
{Dims, MaxT};
{false, Dis1} ->
F = ice_primop:f(Primop),
{apply(F, Dis1), MaxT}
{apply(ice_primop_eval, Primop, Dis1), MaxT}
end;

%%-------------------------------------------------------------------------------------
Expand Down
190 changes: 64 additions & 126 deletions src/ice_primop.erl
Original file line number Diff line number Diff line change
@@ -1,146 +1,84 @@
%%-------------------------------------------------------------------------------------
%% Primitive operations
%% Primitive operations - AST nodes
%%-------------------------------------------------------------------------------------
-module(ice_primop).

-export([eq/2, neq/2]).
-export([tand/2, tor/2]).
-export([lt/2, gt/2]).
-export([lte/2, gte/2]).
-export([plus/2, minus/2, times/2, divide/2]).
-export([mod/2]).
-export([ilogn/1]).
-export([lt/2, lte/2,
gt/2, gte/2]).

-export(['not'/1, 'and'/2, 'or'/2]).

-export([floor/1, ceil/1]).
-export([ 'sin'/1, 'cos'/1, 'tan'/1
, 'asin'/1, 'acos'/1, 'atan'/1, 'atan2'/2
, 'sinh'/1, 'cosh'/1, 'tanh'/1
, 'asinh'/1, 'acosh'/1, 'atanh'/1
, 'exp'/1, 'log'/1, 'log10'/1, 'pow'/1, 'sqrt'/1]).
-export([abs/1]).

-export([f/1]).

-define(primop1(Name),
Name (N) -> {primop, Name, [N]}).
-define(primop2(Name),
Name (N,M) -> {primop, Name, [N,M]}).
-export([plus/1, minus/1]).
-export([plus/2, minus/2,
times/2, divide/2]).
-export([mod/2]).

%%----------------------------------------------------------------------------
%% Functions for creating the AST nodes of primitive operations
%%----------------------------------------------------------------------------
eq (A, B) ->
{primop, '==', [A,B]}.
neq (A, B) ->
{primop, '/=', [A,B]}.
-export([pow/2, sqrt/1]).
-export([exp/1, log/1]).
-export([log10/1]).
-export([log2/1]).

tand (A, B) ->
{primop, 'and', [A,B]}.
tor (A, B) ->
{primop, 'or', [A,B]}.
-export([ sin/1, cos/1, tan/1
, asin/1, acos/1, atan/1, atan2/2]).
-export([ sinh/1, cosh/1, tanh/1
, asinh/1, acosh/1, atanh/1]).

lt (A, B) ->
{primop, '<', [A,B]}.
gt (A, B) ->
{primop, '>', [A,B]}.
-define(OP1(F, Op),
F(A) -> {primop, Op, [A]}).
-define(OP2(F, Op),
F(A,B) -> {primop, Op, [A,B]}).

lte (A, B) ->
{primop, '=<', [A,B]}.
gte (A, B) ->
{primop, '>=', [A,B]}.
-define(OP1(Op), ?OP1(Op, Op)).
-define(OP2(Op), ?OP2(Op, Op)).

plus (A, B) ->
{primop, '+', [A,B]}.
minus (A, B) ->
{primop, '-', [A,B]}.
times (A, B) ->
{primop, '*', [A,B]}.
divide (A, B) ->
{primop, '/', [A,B]}.
%%----------------------------------------------------------------------------
%% Functions for creating the AST nodes of primitive operations
%%----------------------------------------------------------------------------

mod (A, B) ->
{primop, '%', [A,B]}.
?OP2(eq, '==').
?OP2(neq, '/=').
?OP2(lt, '<').
?OP2(lte, '=<').
?OP2(gt, '>').
?OP2(gte, '>=').

ilogn (N) ->
{primop, 'ilogn', [N]}.
?OP1('not').
?OP2('and').
?OP2('or').

?primop1('floor').
?primop1('ceil').
?OP1(floor).
?OP1(ceil).
?OP1(abs).

?primop1('sin').
?primop1('cos').
?primop1('tan').
?primop1('asin').
?primop1('acos').
?primop1('atan').
?primop2('atan2').
?primop1('sinh').
?primop1('cosh').
?primop1('tanh').
?primop1('asinh').
?primop1('acosh').
?primop1('atanh').
?primop1('exp').
?primop1('log').
?primop1('log10').
?primop1('pow').
?primop1('sqrt').
?OP1(plus, '+').
?OP1(minus, '-').
?OP2(plus, '+').
?OP2(minus, '-').
?OP2(times, '*').
?OP2(divide, '/').
?OP2(mod, '%').

?primop1('abs').
?OP2(pow).
?OP1(sqrt).
?OP1(exp).
?OP1(log).
?OP1(log10).
?OP1(log2).

%%----------------------------------------------------------------------------
%% @doc Return the Erlang function evaluating the primitive operation.
%%----------------------------------------------------------------------------
f('%') ->
fun %% http://stackoverflow.com/a/858649/1418165
(X, Y) -> (X rem Y + Y) rem Y
end;
f('ilogn') ->
fun
(0) -> 0;
(X) -> round(math:log(X) / math:log(2))
end;
f('floor') ->
fun %% https://erlangcentral.org/wiki/index.php/Floating_Point_Rounding
(X) when X >= 0 -> trunc(X);
(X) ->
T = trunc(X),
case X - T of
0 -> T;
_ -> T - 1
end
end;
f('ceil') ->
fun %% https://erlangcentral.org/wiki/index.php/Floating_Point_Rounding
(X) when X < 0 -> trunc(X);
(X) ->
T = trunc(X),
case X - T of
0 -> T;
_ -> T + 1
end
end;
f('atan2'=Fun) ->
fun math:Fun/2;
f(Fun) when Fun == 'sin';
Fun == 'cos';
Fun == 'tan';
Fun == 'asin';
Fun == 'acos';
Fun == 'atan';
Fun == 'sinh';
Fun == 'cosh';
Fun == 'tanh';
Fun == 'asinh';
Fun == 'acosh';
Fun == 'atanh';
Fun == 'exp';
Fun == 'log';
Fun == 'log10';
Fun == 'pow';
Fun == 'sqrt' ->
fun math:Fun/1;
f('abs') ->
fun erlang:abs/1;
f(Op) ->
fun erlang:Op/2.
?OP1(sin).
?OP1(cos).
?OP1(tan).
?OP1(asin).
?OP1(acos).
?OP1(atan).
?OP2(atan2).
?OP1(sinh).
?OP1(cosh).
?OP1(tanh).
?OP1(asinh).
?OP1(acosh).
?OP1(atanh).
Loading