Permalink
Browse files

various and sundry fixes for the system discovered during bootstrapping

  • Loading branch information...
1 parent a6dd094 commit c996e1f1a7bc3888eb5057d4c8e358ba3fa76aef @ericbmerritt ericbmerritt committed Jan 19, 2012
Showing with 99 additions and 17 deletions.
  1. +85 −6 src/jxa_clause.erl
  2. +13 −10 src/jxa_parser.erl
  3. +1 −1 src/jxa_try.erl
View
@@ -33,13 +33,10 @@ comp_pattern(Path0, {Ctx0, Guards0}, Arg) when is_atom(Arg) ->
jxa_ctx:annots(Ctx0)),
case jxa_ctx:resolve_reference(Arg, -1, Ctx0) of
{variable, Var} ->
- Annots = jxa_annot:get_line(jxa_path:path(Path0),
- compiler_generated,
- jxa_ctx:annots(Ctx0)),
%% The reference already exists. So we create a new variable and
%% add a guard for to test for equality
GenSym = joxa:gensym(),
- CerlVar = cerl:ann_c_var([compiler_generated | Annots], GenSym),
+ CerlVar = cerl:ann_c_var(Annots, GenSym),
Guards1 = [cerl:ann_c_call(Annots,
cerl:ann_c_atom([compiler_generated],
'erlang'),
@@ -198,12 +195,15 @@ compile_clause_body(Path0, Ctx0, [Pattern, ['when', Guards], Body]) ->
GuardAnnots =
jxa_annot:get_line(jxa_path:add_path(jxa_path:incr(Path0)),
jxa_ctx:annots(Ctx2)),
+ Idx =
+ jxa_annot:get_idx(jxa_path:add_path(jxa_path:incr(Path0)),
+ jxa_ctx:annots(Ctx2)),
{Ctx3, CerlGuard} =
jxa_expression:comp(
jxa_path:add(jxa_path:incr(jxa_path:add(jxa_path:incr(Path0)))),
Ctx2, Guards),
- CompleteGuards = mk_guards(GuardAnnots,
- [CerlGuard | PatternGuards]),
+ CompleteGuards = check_guards(Idx, mk_guards(GuardAnnots,
+ [CerlGuard | PatternGuards])),
{Ctx4, CerlBody} =
jxa_expression:comp(jxa_path:add(jxa_path:incr(2, Path0)),
@@ -255,3 +255,82 @@ mk_list(Path0, Acc0, [H | T]) ->
jxa_path:add_path(Path0),
jxa_ctx:annots(Ctx0)),
{Acc2, cerl:ann_c_cons(Annots, CerlH, CerlT)}.
+
+check_guards(AST) ->
+ case cerl:type(AST) of
+ call ->
+ valid_guard(cerl:atom_val(cerl:call_module(AST)),
+ cerl:atom_val(cerl:call_name(AST)),
+ cerl:call_arity(AST)) andalso
+ lists:all(fun check_guards/1, cerl:call_args(AST));
+ cons ->
+ check_guards(cerl:cons_head(AST)) andalso
+ check_guards(cerl:cons_tail(AST));
+ binary ->
+ true;
+ bitstring ->
+ true;
+ tuple ->
+ lists:all(fun check_guards/1, cerl:tuple_es(AST));
+ var ->
+ true;
+ literal ->
+ true;
+ _ ->
+ false
+ end.
+
+check_guards(Idx, AST) ->
+ case check_guards(AST) of
+ true ->
+ AST;
+ false ->
+ ?JXA_THROW({invalid_guard, Idx})
+ end.
+
+valid_guard(erlang, abs, 1) -> true;
+valid_guard(erlang, bitsize, 1) -> true;
+valid_guard(erlang, byte_size, 1) -> true;
+valid_guard(erlang, element, 2) -> true;
+valid_guard(erlang, float, 1) -> true;
+valid_guard(erlang, hd, 1) -> true;
+valid_guard(erlang, length, 1) -> true;
+valid_guard(erlang, node, 0) -> true;
+valid_guard(erlang, node, 1) -> true;
+valid_guard(erlang, round, 1) -> true;
+valid_guard(erlang, self, 0) -> true;
+valid_guard(erlang, size, 1) -> true;
+valid_guard(erlang, tl, 1) -> true;
+valid_guard(erlang, trunc, 1) -> true;
+valid_guard(erlang, tuple_size, 1) -> true;
+valid_guard(erlang, is_binary, 1) -> true;
+valid_guard(erlang, is_alive, 0) -> true;
+valid_guard(erlang, is_boolean, 1) -> true;
+valid_guard(erlang, is_function, 1) -> true;
+valid_guard(erlang, is_function, 2) -> true;
+valid_guard(erlang, is_integer, 1) -> true;
+valid_guard(erlang, is_float, 1) -> true;
+valid_guard(erlang, is_list, 1) -> true;
+valid_guard(erlang, is_atom, 1) -> true;
+valid_guard(erlang, is_number, 1) -> true;
+valid_guard(erlang, is_pid, 1) -> true;
+valid_guard(erlang, is_port, 1) -> true;
+valid_guard(erlang, is_record, 2) -> true;
+valid_guard(erlang, is_record, 3) -> true;
+valid_guard(erlang, is_reference, 1) -> true;
+valid_guard(erlang, is_tuple, 1) -> true;
+valid_guard(erlang, 'and', 2) -> true;
+valid_guard(erlang, 'or', 2) -> true;
+valid_guard(erlang, '>', 2) -> true;
+valid_guard(erlang, '<', 2) -> true;
+valid_guard(erlang, '==', 2) -> true;
+valid_guard(erlang, '=<', 2) -> true;
+valid_guard(erlang, '>=', 2) -> true;
+valid_guard(erlang, '/=', 2) -> true;
+valid_guard(erlang, '=:=', 2) -> true;
+valid_guard(erlang, '=/=', 2) -> true;
+valid_guard(_, _, _) ->
+ false.
+
+
+
View
@@ -120,7 +120,6 @@ do_parse(Path0, Annots0, Ast, Input, Idx0) ->
do_parse(jxa_path:incr(Path0), Annots1,
[FinalAST | Ast],
Rest, Idx1)
-
end.
-spec transform_ast(jxa_path:state(), jxa_annot:annotations(),
@@ -509,20 +508,24 @@ fun_reference(Input, Index) ->
symbol(Input, Index) ->
p(Input, Index, symbol,
p_seq([p_string(":"),
- fun ident/2]),
- fun([_, Result = {ident, _, _}], Idx) ->
- {quote, Result, Idx}
+ p_one_or_more(
+ p_and([p_not(
+ p_charclass(<<"[,\\\\{}\t\n\s\r\\(\\)\\[\\]\"]">>)),
+ p_anything()]))]),
+ fun([_, Value], Idx) ->
+ Result =
+ list_to_atom(binary_to_list(iolist_to_binary(Value))),
+ {quote, {ident, Result, Idx}, Idx}
end).
%% done
-spec ident(binary(), index()) -> intermediate_ast().
ident(Input, Index) ->
p(Input, Index, ident,
-
p_seq([p_not(p_charclass(<<"[:;~`'\\\\,><{}/\t\n\s\r\\(\\)\\[\\]\"]">>)),
p_zero_or_more(
p_and([p_not(
- p_charclass(<<"[,\\\\{}/\t\n\s\r\\(\\)\\[\\]\"]">>)),
+ p_charclass(<<"[,\\\\><{}/\t\n\s\r\\(\\)\\[\\]\"]">>)),
p_anything()]))]),
fun(Node, Idx) ->
@@ -549,14 +552,14 @@ value(Input, Index) ->
p_choose([fun float/2,
fun integer/2,
fun fun_reference/2,
- fun ident/2,
+ fun binary/2,
fun symbol/2,
+ fun ident/2,
fun list/2,
fun tuple/2,
fun string/2,
fun quote/2,
- fun char/2,
- fun binary/2]),
+ fun char/2]),
fun ignorable/2]))(I,D) end,
fun(Node, _Idx) ->
lists:nth(2, Node)
@@ -794,7 +797,7 @@ ident_test() ->
?memo(?assertMatch({{ident, 'null', {1, _}}, <<>>, _}, value(<<"null">>, index()))),
?memo(?assertMatch({{ident, 'Hello?', {1, _}}, <<>>, _}, value(<<"Hello?">>, index()))),
?memo(?assertMatch({{ident, 'boo88', {1, _}}, <<>>, _}, value(<<"boo88">>, index()))),
- ?memo(?assertMatch({{ident, 'bock', {1, _}}, <<":">>, _}, value(<<"bock:">>, index()))),
+ ?memo(?assertMatch({{ident, 'bock:', {1, _}}, <<>>, _}, value(<<"bock:">>, index()))),
?memo(?assertMatch({{ident, 'bock', {1, _}}, <<"{">>, _}, value(<<"bock{">>, index()))),
?memo(?assertMatch({{ident, 'bock', {1, _}}, <<"[">>, _}, value(<<"bock[">>, index()))),
?memo(?assertMatch({{ident, 'bock', {1, _}}, <<"(ee">>, _}, value(<<"bock(ee">>, index()))).
View
@@ -26,7 +26,7 @@ comp(Path0, Ctx0, ['__try', Expr, ['catch', [Type, Value], CatchExpr]])
{Ctx3, CerlCatch} =
jxa_expression:comp(jxa_path:add(jxa_path:incr(2, jxa_path:add(jxa_path:incr(2, Path0)))),
Ctx2, CatchExpr),
- {Ctx3,
+ {jxa_ctx:pop_scope(Ctx3),
cerl:ann_c_try(Annots, CerlExpr,
[TryVar],
TryVar,

0 comments on commit c996e1f

Please sign in to comment.