Skip to content

Commit

Permalink
Add Array, String, arguments member operator stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
KlausTrainer committed Apr 29, 2012
1 parent 4137989 commit d39a2c3
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 23 deletions.
53 changes: 36 additions & 17 deletions src/erlyjs_compiler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,18 @@ ast({{new, {identifier, L, 'Array'}, {'(', Values}}, [length]}, CtxTrav) ->
ValuesAst = element(1, element(1, ast({new, {identifier, L, 'Array'}, {'(', Values}}, CtxTrav))),
{{erlyjs_array:get_length(ValuesAst), #ast_inf{}}, CtxTrav};
ast({Object, {'[]', Value}}, {Ctx, Trav}) ->
member(Object, Value, Ctx, Trav);
Value1 = case Value of
{string, L, Val} ->
try list_to_integer(Val) of
Int -> {integer, L, Int}
catch
error:badarg -> Value
end;
_ ->
Value
end,
ValueAst = element(1, element(1, ast(Value1, {Ctx, Trav}))),
member(Object, ValueAst, Ctx, Trav);
ast({{{string, _, Value}, Names}, {'(', Args}}, {Ctx, Trav}) ->
call(string, Value, Names, Args, Ctx, Trav);
ast({{{identifier, _, Name}, Names}, {'(', Args}}, {Ctx, Trav}) ->
Expand All @@ -258,6 +269,8 @@ ast({{identifier, _, Name}, [Prop]}, {Ctx, Trav}) ->
case Metadata of
{array, _} when Prop =:= length ->
{{erlyjs_array:get_length(Var), Inf}, {Ctx, Trav}};
{string, _} when Prop =:= length ->
{{application(none, atom(size), [Var]), Inf}, {Ctx, Trav}};
{_, Props} ->
case proplists:get_value(Prop, Props) of
undefined -> {{atom(undefined), Inf}, {Ctx, Trav}};
Expand Down Expand Up @@ -570,6 +583,8 @@ var_declare(Key, Value, Ctx, Trav) ->
function ->
{function, {params, Params, body, _Body}} = Value,
{function, [{length, integer(length(Params))}]};
string ->
{string, []};
_ ->
nil
end,
Expand All @@ -579,26 +594,30 @@ var_declare(Key, Value, Ctx, Trav) ->
{{Ast, Inf}, {Ctx, Trav3}}.


member(Object, Value, Ctx, Trav) ->
{Array, Inf} = case Object of
member(Object, ValueAst, Ctx, Trav) ->
case Object of
{identifier, _, Name} ->
{{{Var, Metadata}, Inf0}, _} = var_ast(Name, Ctx#js_ctx{action = get_all}, Trav),
{{{Var, Metadata}, _}, _} = var_ast(Name, Ctx#js_ctx{action = get_all}, Trav),
case Metadata of
{array, _} -> {Var, Inf0}
{array, _} -> array_member(Var, ValueAst, Ctx, Trav);
{arguments, _} -> array_member(Var, ValueAst, Ctx, Trav);
{string, _} -> string_member(Var, ValueAst, Ctx, Trav)
end;
{'[', Values} ->
element(1, ast({'[', Values}, {Ctx, Trav}))
end,
Value1 = element(1, element(1, ast(Value, {Ctx, Trav}))),
Value2 = case type(Value1) of
binary ->
application(none, atom(list_to_integer),
[application(none, atom(binary_to_list), [Value1])]);
_ ->
Value1
end,
Value3 = infix_expr(Value2, operator('+'), integer(1)),
{{application(atom(lists), atom(nth), [Value3, Array]), Inf}, {Ctx, Trav}}.
Array = element(1, element(1, ast({'[', Values}, {Ctx, Trav}))),
array_member(Array, ValueAst, Ctx, Trav)
end.

array_member(ArrayAst, ValueAst, Ctx, Trav) ->
ValueAst1 = infix_expr(ValueAst, operator('+'), integer(1)),
{{application(atom(lists), atom(nth), [ValueAst1, ArrayAst]), #ast_inf{}}, {Ctx, Trav}}.

string_member(StringAst, ValueAst, Ctx, Trav) ->
ValueAst1 = infix_expr(ValueAst, operator('+'), integer(1)),
StringAst1 = application(none, atom(binary_to_list), [StringAst]),
ResultAst = application(none, atom(list_to_binary),
[list([application(atom(lists), atom(nth), [ValueAst1, StringAst1])])]),
{{ResultAst, #ast_inf{}}, {Ctx, Trav}}.


name_search(_, [], _) ->
Expand Down
28 changes: 28 additions & 0 deletions tests/functions/arguments_4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Mandatory. Return here a description of the test case.
function test_description() {
return "arguments";
}

// Mandatory. Return here an array of arguments the testsuite will use
// to invoke the test() function. For no arguments return an empty array.
function test_args() {
return [];
}

// Mandatory. Return here the expected test result.
function test_ok() {
return 3;
}

// Optional. Provide here any global code.


// Mandatory. The actual test.
// Testsuite invokes this function with the arguments from test_args()
// and compares the return value with the expected result from test_ok().
function test() {
function abc(a, b, c) {
return arguments[2];
}
return abc(1, 2, 3, 4, 5);
}
29 changes: 29 additions & 0 deletions tests/functions/arguments_5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Mandatory. Return here a description of the test case.
function test_description() {
return "arguments";
}

// Mandatory. Return here an array of arguments the testsuite will use
// to invoke the test() function. For no arguments return an empty array.
function test_args() {
return [];
}

// Mandatory. Return here the expected test result.
function test_ok() {
return 5;
}

// Optional. Provide here any global code.


// Mandatory. The actual test.
// Testsuite invokes this function with the arguments from test_args()
// and compares the return value with the expected result from test_ok().
function test() {
function abc(a, b, c) {
var b = arguments["4"];
return b;
}
return abc(1, 2, 3, 4, 5);
}
11 changes: 5 additions & 6 deletions tests/global_object_string/indexOf_1.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
// Mandatory. Provide here a description of the test case.
function test_description() {
return "Prediefined core object 'String', method: 'indexOf'";
return "Predefined core object 'String', method: 'indexOf'";
}

// Mandatory. Provide here the arguments the testsuite will use
// Mandatory. Provide here the arguments the testsuite will use
// to invoke the test() function.
function test_args() {
return [];
}

// Mandatory. Provide here the expected test result.
// Mandatory. Provide here the expected test result.
function test_ok() {
return 0;
}

// Optional. Provide here any global code.


// Mandatory. The actual test.
// Mandatory. The actual test.
// Testsuite invokes this function with the arguments from test_args()
// and compares the return value with the expected result from test_result().
function test() {
function test() {
var a = "Blue Whale";
return a.indexOf("Blue");
}

26 changes: 26 additions & 0 deletions tests/global_object_string/index_1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Mandatory. Provide here a description of the test case.
function test_description() {
return "string index";
}

// Mandatory. Provide here the arguments the testsuite will use
// to invoke the test() function.
function test_args() {
return [];
}

// Mandatory. Provide here the expected test result.
function test_ok() {
return "F";
}

// Optional. Provide here any global code.


// Mandatory. The actual test.
// Testsuite invokes this function with the arguments from test_args()
// and compares the return value with the expected result from test_result().
function test() {
var a = "Foo";
return a[0];
}
27 changes: 27 additions & 0 deletions tests/global_object_string/index_2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Mandatory. Provide here a description of the test case.
function test_description() {
return "string index";
}

// Mandatory. Provide here the arguments the testsuite will use
// to invoke the test() function.
function test_args() {
return [];
}

// Mandatory. Provide here the expected test result.
function test_ok() {
return "o";
}

// Optional. Provide here any global code.


// Mandatory. The actual test.
// Testsuite invokes this function with the arguments from test_args()
// and compares the return value with the expected result from test_result().
function test() {
var a = "Foo",
b = a[a.length - 1];
return b;
}
27 changes: 27 additions & 0 deletions tests/global_object_string/index_3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Mandatory. Provide here a description of the test case.
function test_description() {
return "string index";
}

// Mandatory. Provide here the arguments the testsuite will use
// to invoke the test() function.
function test_args() {
return [];
}

// Mandatory. Provide here the expected test result.
function test_ok() {
return "F";
}

// Optional. Provide here any global code.


// Mandatory. The actual test.
// Testsuite invokes this function with the arguments from test_args()
// and compares the return value with the expected result from test_result().
function test() {
var a = "Foo",
b = a["0"];
return b;
}

0 comments on commit d39a2c3

Please sign in to comment.