Add pipe operator

This commit is contained in:
Gaith Hallak 2022-02-28 13:39:44 +04:00
parent cfcf0a8a81
commit b493852464
3 changed files with 15 additions and 6 deletions

View File

@ -1941,7 +1941,12 @@ infer_infix({'::', As}) ->
infer_infix({'++', As}) -> infer_infix({'++', As}) ->
ElemType = fresh_uvar(As), ElemType = fresh_uvar(As),
ListType = {app_t, As, {id, As, "list"}, [ElemType]}, ListType = {app_t, As, {id, As, "list"}, [ElemType]},
{fun_t, As, [], [ListType, ListType], ListType}. {fun_t, As, [], [ListType, ListType], ListType};
infer_infix({'|>', As}) ->
ArgType = fresh_uvar(As),
ResType = fresh_uvar(As),
FunType = {fun_t, As, [], [ArgType], ResType},
{fun_t, As, [], [ArgType, FunType], ResType}.
infer_prefix({'!',As}) -> infer_prefix({'!',As}) ->
Bool = {id, As, "bool"}, Bool = {id, As, "bool"},

View File

@ -703,8 +703,11 @@ expr_to_fcode(Env, _Type, {block, _, Stmts}) ->
expr_to_fcode(Env, _Type, Expr = {app, _, {Op, _}, [_, _]}) when Op == '&&'; Op == '||' -> expr_to_fcode(Env, _Type, Expr = {app, _, {Op, _}, [_, _]}) when Op == '&&'; Op == '||' ->
Tree = expr_to_decision_tree(Env, Expr), Tree = expr_to_decision_tree(Env, Expr),
decision_tree_to_fcode(Tree); decision_tree_to_fcode(Tree);
expr_to_fcode(Env, _Type, {app, _Ann, {Op, _}, [A, B]}) when is_atom(Op) -> expr_to_fcode(Env, Type, {app, Ann, {Op, _}, [A, B]}) when is_atom(Op) ->
{op, Op, [expr_to_fcode(Env, A), expr_to_fcode(Env, B)]}; case Op of
'|>' -> expr_to_fcode(Env, Type, {app, Ann, B, [A]});
_ -> {op, Op, [expr_to_fcode(Env, A), expr_to_fcode(Env, B)]}
end;
expr_to_fcode(Env, _Type, {app, _Ann, {Op, _}, [A]}) when is_atom(Op) -> expr_to_fcode(Env, _Type, {app, _Ann, {Op, _}, [A]}) when is_atom(Op) ->
case Op of case Op of
'-' -> {op, '-', [{lit, {int, 0}}, expr_to_fcode(Env, A)]}; '-' -> {op, '-', [{lit, {int, 0}}, expr_to_fcode(Env, A)]};

View File

@ -308,17 +308,18 @@ expr() -> expr100().
expr100() -> expr100() ->
Expr100 = ?LAZY_P(expr100()), Expr100 = ?LAZY_P(expr100()),
Expr200 = ?LAZY_P(expr200()), Expr150 = ?LAZY_P(expr150()),
choice( choice(
[ ?RULE(lam_args(), keyword('=>'), body(), {lam, _2, _1, _3}) %% TODO: better location [ ?RULE(lam_args(), keyword('=>'), body(), {lam, _2, _1, _3}) %% TODO: better location
, {'if', keyword('if'), parens(Expr100), Expr200, right(tok(else), Expr100)} , {'if', keyword('if'), parens(Expr100), Expr150, right(tok(else), Expr100)}
, ?RULE(Expr200, optional(right(tok(':'), type())), , ?RULE(Expr150, optional(right(tok(':'), type())),
case _2 of case _2 of
none -> _1; none -> _1;
{ok, Type} -> {typed, get_ann(_1), _1, Type} {ok, Type} -> {typed, get_ann(_1), _1, Type}
end) end)
]). ]).
expr150() -> infixl(expr200(), binop('|>')).
expr200() -> infixr(expr300(), binop('||')). expr200() -> infixr(expr300(), binop('||')).
expr300() -> infixr(expr400(), binop('&&')). expr300() -> infixr(expr400(), binop('&&')).
expr400() -> infix(expr500(), binop(['<', '>', '=<', '>=', '==', '!='])). expr400() -> infix(expr500(), binop(['<', '>', '=<', '>=', '==', '!='])).