Skip to content

Commit

Permalink
let set default value for function parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
manuel-rubio committed Feb 3, 2016
1 parent 7d6fb34 commit e3f6da3
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 7 deletions.
2 changes: 1 addition & 1 deletion doc/COMPATIBILITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Features
- [x] insensitive name of functions
- [x] call to functions
- [x] args by reference
- [ ] default values as params in functions
- [x] default values as params in functions
- [x] include/require code from other files
- Error handling
- [x] launch errors
Expand Down
3 changes: 2 additions & 1 deletion include/ephp.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@
class :: class_name() | undefined,
name :: binary(),
idx = [] :: [array_index() | {object, binary()} | {class, binary()}],
default_value = null :: mixed(),
line :: line()
}).

Expand Down Expand Up @@ -302,7 +303,7 @@
name :: binary(),
access = public :: access_types(),
type = normal :: normal | static,
init_value = null :: any()
init_value = null :: mixed()
}).

-type class_attr() :: #class_attr{}.
Expand Down
19 changes: 15 additions & 4 deletions src/ephp_context.erl
Original file line number Diff line number Diff line change
Expand Up @@ -517,9 +517,10 @@ resolve(#array{elements=ArrayElements}, State) ->
resolve(#concat{texts=Texts}, State) ->
resolve_txt(Texts, State);

resolve(#call{name=#function{args=FuncArgs,code=Code,use=Use},args=RawArgs},
resolve(#call{name=#function{args=RawFuncArgs,code=Code,use=Use},args=RawArgs},
#state{ref=Ref,vars=Vars,const=Const}=State) ->
{Args, NState} = resolve_args(RawArgs, State),
{Args, NStatePrev} = resolve_args(RawArgs, State),
{FuncArgs, NState} = resolve_func_args(RawFuncArgs, NStatePrev),
{ok, NewVars} = ephp_vars:start_link(),
{ok, SubContext} = start_mirror(NState#state{
vars=NewVars,
Expand Down Expand Up @@ -570,8 +571,9 @@ resolve(#call{type=normal,name=Fun,args=RawArgs,line=Index}=_Call,
end,
destroy(Mirror),
{Value, NState};
{ok,#reg_func{type=php, args=FuncArgs, code=Code}} ->
{Args, NState} = resolve_args(RawArgs, State),
{ok,#reg_func{type=php, args=RawFuncArgs, code=Code}} ->
{Args, NStatePrev} = resolve_args(RawArgs, State),
{FuncArgs, NState} = resolve_func_args(RawFuncArgs, NStatePrev),
{ok, NewVars} = ephp_vars:start_link(),
{ok, SubContext} = start_mirror(NState#state{
vars=NewVars,
Expand Down Expand Up @@ -671,6 +673,15 @@ resolve(#function{name=undefined,use=Use}=Anon, #state{vars=Vars}=State) ->
resolve(Unknown, _State) ->
ephp_error:error({error, eundeftoken, undefined, ?E_CORE_ERROR, Unknown}).

resolve_func_args(RawFuncArgs, State) ->
lists:foldl(fun
(#variable{default_value=Val}=Var, {Vars, S}) when Val =/= null ->
{Value, NewState} = resolve(Val,S),
{Vars ++ [Var#variable{default_value=Value}], NewState};
(Var, {Vars, NewState}) ->
{Vars ++ [Var], NewState}
end, {[], State}, RawFuncArgs).

resolve_args(RawArgs, State) ->
lists:foldl(fun(Arg, {Args,S}) ->
{A,NewState} = resolve(Arg,S),
Expand Down
9 changes: 8 additions & 1 deletion src/ephp_parser.peg
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,14 @@ st_if_simple <- if space? conditions_use space? ( code_block / statement / error
end, line=Index}
`;

st_function <- function space key space? '(' space? (var (space? ',' space? var)*
param <- var (space? '=' space? expression)? `
case Node of
[Var,[]] -> Var;
[Var,[_,_,_,Expression]] -> Var#variable{default_value=Expression}
end
`;

st_function <- function space key space? '(' space? (param (space? ',' space? param)*
space?)? ')' space? code_block
`
case Node of
Expand Down
3 changes: 3 additions & 0 deletions src/ephp_vars.erl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ zip_args(VarsSrc, VarsDst, ValArgs, FuncArgs) ->
(FuncArg, [{_,ArgVal}|RestArgs]) ->
set(VarsDst, FuncArg, ArgVal),
RestArgs;
(#variable{default_value=Val}=FuncArg, []) when Val =/= null ->
set(VarsDst, FuncArg, Val),
[];
(_FuncArg, []) ->
[]
end, ValArgs, FuncArgs),
Expand Down
3 changes: 3 additions & 0 deletions test/code/test_func_default_params.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Hi mum!
Hello world
hello world
10 changes: 10 additions & 0 deletions test/code/test_func_default_params.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

function show($greeting='hello', $who='world') {
print $greeting . " " . $who . "\n";
}

print show("Hi", "mum!");
print show("Hello");
print show();

0 comments on commit e3f6da3

Please sign in to comment.