Skip to content
This repository was archived by the owner on Sep 9, 2019. It is now read-only.

Named args #29

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
ae06ec8
- Initial release: Tests with and without names
thekid Jun 7, 2012
66bc31f
- Add rule "arguments" and change all instances to use it
thekid Jun 7, 2012
e151618
- Add syntactic support for named argument lists
thekid Jun 7, 2012
31925d9
- Fix up argument list emitting, params may also be a hashmap
thekid Jun 7, 2012
3c9fcca
- Refactor method parameters to [:TypeName] with param names as keys
thekid Jun 8, 2012
0e56627
- Make named arguments work even when passed in divergent order
thekid Jun 8, 2012
2702346
- Fix situations when parameters are omitted
thekid Jun 8, 2012
3e61de6
- Add reference to xp-framework/rfc#251
thekid Jun 8, 2012
2d9d296
- Change arguments rule no longer to reuse expressionlist / maplitera…
thekid Jun 8, 2012
137bfeb
- Change params from [:TypeName] to [:X] where X = [ type: TypeName, …
thekid Jun 9, 2012
a4acb97
- Change params from [:TypeName] to [:X] where X = [ type: TypeName, …
thekid Jun 10, 2012
0d37b70
- Change params from [:TypeName] to [:X] where X = [ type: TypeName, …
thekid Jun 10, 2012
6e9517c
- Change params from [:TypeName] to [:X] where X = [ type: TypeName, …
thekid Jun 10, 2012
52268dc
- First working implementation
thekid Jun 10, 2012
0d517e9
- Add xp.compiler.ast.[String, Integer, Decimal, Boolean, Null, Array…
thekid Jun 10, 2012
d883623
- Add tests for argument defaults
thekid Jun 10, 2012
79b765a
- Fix parameter default reflection for arrays and maps
thekid Jun 10, 2012
8931b8f
- Add `net.xp_lang.tests.execution.source.NamedArgsTest`
thekid Jun 10, 2012
741b7f9
- Refactor: Rename nodeOfDefaultValue() to nodeOf()
thekid Jun 10, 2012
a6bb5b3
Merge branch 'master' into named-args
thekid Jun 10, 2012
25b6952
- MFH
thekid May 11, 2013
2356abc
- Add NamedArgsTest
thekid May 11, 2013
7d9fb18
Merge branch 'master' into named-args
thekid May 13, 2013
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
59 changes: 37 additions & 22 deletions compiler/src/main/php/xp/compiler/emit/source/Emitter.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,24 +205,40 @@ protected function emitUses($b, array $types) {
}
$uses && $b->insert('uses(\''.implode("', '", $uses).'\');', 0);
}

/**
* Emit parameters
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.Node[] params
* @param xp.compiler.types.Types
* @param xp.compiler.ast.Node[] arguments
* @param bool brackets
* @return int
*/
protected function emitInvocationArguments($b, array $params, $brackets= true) {
protected function emitInvocationArguments($b, $ptr, array $arguments, $brackets= TRUE) {
$brackets && $b->append('(');
$s= sizeof($params)- 1;
foreach ($params as $i => $param) {
$this->emitOne($b, $param);
$i < $s && $b->append(',');
$s= sizeof($arguments)- 1;
$i= 0;
if (is_string(key($arguments))) { // Named
$p= sizeof($ptr->parameters)- 1;
foreach ($ptr->parameters as $name => $param) {
if (isset($arguments[$name])) {
$this->emitOne($b, $arguments[$name]);
} else if ($param['default']) {
$this->emitOne($b, $param['default']);
} else {
$this->error('P404', 'Cannot omit '.$ptr->name.'()\'s parameter "'.$name.'"');
}
$i++ < $p && $b->append(',');
}
} else { // Ordered
foreach ($arguments as $param) {
$this->emitOne($b, $param);
$i++ < $s && $b->append(',');
}
}
$brackets && $b->append(')');
return sizeof($params);
return sizeof($arguments);
}

/**
Expand All @@ -244,11 +260,11 @@ protected function emitInvocation($b, $inv) {
// Static method call vs. function call
if (true === $ptr) {
$b->append($inv->name);
$this->emitInvocationArguments($b, (array)$inv->arguments);
$this->emitInvocationArguments($b, null, (array)$inv->arguments);
$this->scope[0]->setType($inv, TypeName::$VAR);
} else {
$b->append($ptr->holder->literal().'::'.$ptr->name());
$this->emitInvocationArguments($b, (array)$inv->arguments);
$this->emitInvocationArguments($b, $ptr, (array)$inv->arguments);
$this->scope[0]->setType($inv, $ptr->returns);
}
}
Expand Down Expand Up @@ -447,7 +463,7 @@ public function emitDynamicMemberAccess($b, $access) {
public function emitStaticMethodCall($b, $call) {
$ptr= $this->resolveType($call->type);
$b->append($ptr->literal().'::'.$call->name);
$this->emitInvocationArguments($b, (array)$call->arguments);
$this->emitInvocationArguments($b, $ptr->getMethod($call->name), (array)$call->arguments);

// Record type
$this->scope[0]->setType($call, $ptr->hasMethod($call->name) ? $ptr->getMethod($call->name)->returns : TypeName::$VAR);
Expand All @@ -469,15 +485,15 @@ public function emitInstanceCall($b, $call) {
$b->append(') ? null : call_user_func(')->append($var);
if ($call->arguments) {
$b->append(', ');
$this->emitInvocationArguments($b, $call->arguments, false);
$this->emitInvocationArguments($b, null, $call->arguments, false);
}
$b->append('))');
} else {
$b->append('call_user_func(');
$this->emitOne($b, $call->target);
if ($call->arguments) {
$b->append(', ');
$this->emitInvocationArguments($b, $call->arguments, false);
$this->emitInvocationArguments($b, null, $call->arguments, false);
}
$b->append(')');
}
Expand All @@ -499,7 +515,7 @@ public function emitMethodCall($b, $call) {
$b->insert($ext->holder->literal().'::'.$call->name.'(', $mark);
if ($call->arguments) {
$b->append(', ');
$this->emitInvocationArguments($b, $call->arguments, false);
$this->emitInvocationArguments($b, $ext, $call->arguments, false);
}
$b->append(')');
$this->scope[0]->setType($call, $ext->returns);
Expand All @@ -514,7 +530,7 @@ public function emitMethodCall($b, $call) {
$b->insert('(NULL === ('.$var.'=', $mark);
$b->append(') ? NULL : ')->append($var)->append('->');
$b->append($call->name);
$this->emitInvocationArguments($b, (array)$call->arguments);
$this->emitInvocationArguments($b, $ptr->getMethod($call->name), (array)$call->arguments);
$b->append(')');
} else {

Expand All @@ -534,7 +550,7 @@ public function emitMethodCall($b, $call) {
}

$b->append('->'.$call->name);
$this->emitInvocationArguments($b, (array)$call->arguments);
$this->emitInvocationArguments($b, $ptr->getMethod($call->name), (array)$call->arguments);
}

// Record type
Expand Down Expand Up @@ -1187,8 +1203,7 @@ protected function emitFinally($b, $finally) {
*/
protected function emitDynamicInstanceCreation($b, $new) {
$b->append('new ')->append('$')->append($new->variable);
$this->emitInvocationArguments($b, (array)$new->parameters);

$this->emitInvocationArguments($b, null, (array)$new->parameters);
$this->scope[0]->setType($new, new TypeName('lang.Object'));
}

Expand Down Expand Up @@ -1256,12 +1271,12 @@ protected function emitInstanceCreation($b, $new) {
$b->append('>\'');
if ($new->parameters) {
$b->append(',');
$this->emitInvocationArguments($b, (array)$new->parameters, false);
$this->emitInvocationArguments($b, $ptr->getConstructor(), (array)$new->parameters, false);
}
$b->append(')');
} else {
$b->append('new '.$ptr->literal());
$this->emitInvocationArguments($b, (array)$new->parameters);
$this->emitInvocationArguments($b, $ptr->getConstructor(), (array)$new->parameters);
}
}

Expand Down Expand Up @@ -2280,8 +2295,8 @@ protected function emitClass($b, $declaration) {
$arguments= array();
$parameters= array();
if ($parentType->hasConstructor()) {
foreach ($parentType->getConstructor()->parameters as $i => $type) {
$parameters[]= array('name' => '��a'.$i, 'type' => $type); // TODO: default
foreach ($parentType->getConstructor()->parameters as $i => $param) {
$parameters[]= array('name' => '��a'.$i, 'type' => $param['type']); // TODO: default
$arguments[]= new VariableNode('��a'.$i);
}
$body= array(new StaticMethodCallNode(new TypeName('parent'), '__construct', $arguments));
Expand Down
31 changes: 26 additions & 5 deletions compiler/src/main/php/xp/compiler/grammar/xp.jay
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,27 @@ throws_opt:
/* empty */ { $$= NULL; }
| T_THROWS name_list { $$= $2; }
;

arguments:
/* empty */ { $$= NULL; }
| argument_list
;

argument_list:
argument
| argument_list ',' argument_opt { $$= array_merge($1, $3); }
;

argument_opt:
/* empty */ { $$= array(); }
| argument
;

argument:
T_WORD ':' expression { $$= array($1 => $3); }
| T_STRING ':' expression { $$= array($1 => $3); }
| expression { $$= array($1); }
;
// }}}

// {{{ Overloadable operators
Expand Down Expand Up @@ -709,7 +730,7 @@ expression_opt:
;

instancecreation:
typename '(' expressionlist_opt ')' classbody_opt {
typename '(' arguments ')' classbody_opt {
$$= new InstanceCreationNode();
$$->type= $1;
$$->parameters= $3;
Expand All @@ -731,7 +752,7 @@ staticmember:
T_VARIABLE {
$$= new StaticMemberAccessNode(NULL, $1);
}
| T_WORD '(' expressionlist_opt ')' {
| T_WORD '(' arguments ')' {
$$= new StaticMethodCallNode(NULL, $1, $3);
}
| T_WORD {
Expand Down Expand Up @@ -774,7 +795,7 @@ expression:
$$= $4;
}
}
| T_WORD '(' { $2= $yyLex->create(new InvocationNode($1)); } expressionlist_opt ')' chain_opt {
| T_WORD '(' { $2= $yyLex->create(new InvocationNode($1)); } arguments ')' chain_opt {
$2->arguments= $4;
if ($6) {
$$= $6[0];
Expand Down Expand Up @@ -984,10 +1005,10 @@ chain:
| nav member {
$$= $yyLex->create(new MemberAccessNode(NULL, $2, $1));
}
| nav member '(' { $1= $yyLex->create(new MethodCallNode(NULL, $2, NULL, $1)); } expressionlist_opt ')' {
| nav member '(' { $1= $yyLex->create(new MethodCallNode(NULL, $2, NULL, $1)); } arguments ')' {
$1->arguments= $5;
}
| nav '(' { $1= $yyLex->create(new InstanceCallNode(NULL, NULL, $1)); } expressionlist_opt ')' {
| nav '(' { $1= $yyLex->create(new InstanceCallNode(NULL, NULL, $1)); } arguments ')' {
$1->arguments= $4;
}
;
Expand Down
Loading