Skip to content

Commit

Permalink
removed more duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
niden committed Nov 28, 2023
1 parent 332d48c commit 4f10143
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 467 deletions.
66 changes: 9 additions & 57 deletions src/Optimizers/FunctionCall/CallUserFuncArrayOptimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,67 +26,19 @@
*
* Optimizer for 'call_user_func_array'
*/
class CallUserFuncArrayOptimizer extends OptimizerAbstract
class CallUserFuncArrayOptimizer extends CallUserFuncOptimizer
{
protected string $zephirMethod = 'ZEPHIR_CALL_USER_FUNC_ARRAY';

/**
* @param array $expression
* @param Call $call
* @param CompilationContext $context
* @param string $symbol
* @param array $resolvedParams
*
* @return bool|CompiledExpression|mixed
* @return string
*/
public function optimize(array $expression, Call $call, CompilationContext $context)
protected function getOutput(string $symbol, array $resolvedParams): string
{
if (!isset($expression['parameters'])) {
return false;
}

if (2 != count($expression['parameters'])) {
return false;
}

/*
* Process the expected symbol to be returned
*/
$call->processExpectedReturn($context);

$symbolVariable = $call->getSymbolVariable(true, $context);
if ($symbolVariable) {
if (!$symbolVariable->isVariable()) {
throw new CompilerException(
'Returned values by functions can only be assigned to variant variables',
$expression
);
}
} else {
$symbolVariable = $context->symbolTable->addTemp('variable', $context);
$symbolVariable->initVariant($context);
}

/*
* Add the last call status to the current symbol table
*/
$call->addCallStatusFlag($context);

$resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression);

$context->headersManager->add('kernel/fcall');

/*
* Add the last call status to the current symbol table
*/
$call->addCallStatusFlag($context);

if ($call->mustInitSymbolVariable()) {
$symbolVariable->initVariant($context);
}

$symbol = $context->backend->getVariableCode($symbolVariable);
$context->codePrinter->output(
'ZEPHIR_CALL_USER_FUNC_ARRAY(' . $symbol . ', ' . $resolvedParams[0] . ', ' . $resolvedParams[1] . ');'
);
$call->addCallStatusOrJump($context);

return new CompiledExpression('variable', $symbolVariable->getName(), $expression);
return $this->zephirMethod
. '(' . $symbol . ', ' . $resolvedParams[0] . ', ' . $resolvedParams[1] . ');';
}
}
18 changes: 17 additions & 1 deletion src/Optimizers/FunctionCall/CallUserFuncOptimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
*/
class CallUserFuncOptimizer extends OptimizerAbstract
{
protected string $zephirMethod = 'ZEPHIR_CALL_USER_FUNC';

/**
* @param array $expression
* @param Call $call
Expand Down Expand Up @@ -82,9 +84,23 @@ public function optimize(array $expression, Call $call, CompilationContext $cont
}

$symbol = $context->backend->getVariableCode($symbolVariable);
$context->codePrinter->output('ZEPHIR_CALL_USER_FUNC(' . $symbol . ', ' . $resolvedParams[0] . ');');
$context->codePrinter->output(
$this->getOutput($symbol, $resolvedParams)
);
$call->addCallStatusOrJump($context);

return new CompiledExpression('variable', $symbolVariable->getName(), $expression);
}

/**
* @param string $symbol
* @param array $resolvedParams
*
* @return string
*/
protected function getOutput(string $symbol, array $resolvedParams): string
{
return $this->zephirMethod
. '(' . $symbol . ', ' . $resolvedParams[0] . ');';
}
}
4 changes: 3 additions & 1 deletion src/Optimizers/FunctionCall/CamelizeOptimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
*/
class CamelizeOptimizer extends OptimizerAbstract
{
protected string $zephirMethod = 'zephir_camelize';

/**
* @param array $expression
* @param Call $call
Expand Down Expand Up @@ -83,7 +85,7 @@ public function optimize(array $expression, Call $call, CompilationContext $cont

$symbol = $context->backend->getVariableCode($symbolVariable);
$context->codePrinter->output(
'zephir_camelize(' . $symbol . ', ' . $resolvedParams[0] . ', ' . $delimiter . ' );'
$this->zephirMethod . '(' . $symbol . ', ' . $resolvedParams[0] . ', ' . $delimiter . ' );'
);

return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression);
Expand Down
63 changes: 2 additions & 61 deletions src/Optimizers/FunctionCall/UncamelizeOptimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,66 +26,7 @@
*
* Optimizes calls to 'uncamelize' using internal function
*/
class UncamelizeOptimizer extends OptimizerAbstract
class UncamelizeOptimizer extends CamelizeOptimizer
{
/**
* @param array $expression
* @param Call $call
* @param CompilationContext $context
*
* @return bool|CompiledExpression|mixed
*
* @throws CompilerException
*/
public function optimize(array $expression, Call $call, CompilationContext $context)
{
if (!isset($expression['parameters'])) {
return false;
}

if (count($expression['parameters']) < 1 || count($expression['parameters']) > 2) {
throw new CompilerException("'uncamelize' only accepts one or two parameters");
}

$delimiter = 'NULL ';
if (2 == count($expression['parameters'])) {
if ('null' == $expression['parameters'][1]['parameter']['type']) {
unset($expression['parameters'][1]);
}
}

/*
* Process the expected symbol to be returned
*/
$call->processExpectedReturn($context);

$symbolVariable = $call->getSymbolVariable(true, $context);
if ($symbolVariable->isNotVariableAndString()) {
throw new CompilerException(
'Returned values by functions can only be assigned to variant variables',
$expression
);
}

$context->headersManager->add('kernel/string');

$symbolVariable->setDynamicTypes('string');

$resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression);

if (isset($resolvedParams[1])) {
$delimiter = $resolvedParams[1];
}

if ($call->mustInitSymbolVariable()) {
$symbolVariable->initVariant($context);
}

$symbol = $context->backend->getVariableCode($symbolVariable);
$context->codePrinter->output(
'zephir_uncamelize(' . $symbol . ', ' . $resolvedParams[0] . ', ' . $delimiter . ' );'
);

return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression);
}
protected string $zephirMethod = 'zephir_uncamelize';
}
50 changes: 33 additions & 17 deletions src/Statements/Let/StaticPropertyAdd.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
*/
class StaticPropertyAdd
{
protected string $methodName = 'addStaticProperty';

/**
* Compiles ClassName::foo = {expr}.
*
Expand All @@ -46,6 +48,7 @@ public function assignStatic(
array $statement
): void {
$classDefinition = $compilationContext->classLookup($className);
$methodName = $this->methodName;

if (!$propertyDefinition = $classDefinition->getProperty($property)) {
throw new CompilerException(
Expand Down Expand Up @@ -93,7 +96,12 @@ public function assignStatic(

switch ($resolvedExpr->getType()) {
case 'null':
$compilationContext->backend->updateStaticProperty($classEntry, $property, 'null', $compilationContext);
$compilationContext->backend->updateStaticProperty(
$classEntry,
$property,
'null',
$compilationContext
);
break;

case 'int':
Expand All @@ -109,7 +117,7 @@ public function assignStatic(
$resolvedExpr->getBooleanCode(),
$compilationContext
);
$compilationContext->backend->addStaticProperty(
$compilationContext->backend->$methodName(
$classEntry,
$property,
$tempVariable,
Expand All @@ -132,7 +140,7 @@ public function assignStatic(
'\'' . $resolvedExpr->getCode() . '\'',
$compilationContext
);
$compilationContext->backend->addStaticProperty(
$compilationContext->backend->$methodName(
$classEntry,
$property,
$tempVariable,
Expand All @@ -154,7 +162,7 @@ public function assignStatic(
$resolvedExpr->getCode(),
$compilationContext
);
$compilationContext->backend->addStaticProperty(
$compilationContext->backend->$methodName(
$classEntry,
$property,
$tempVariable,
Expand Down Expand Up @@ -187,7 +195,7 @@ public function assignStatic(
$tempVariable->setIdle(true);
}

$compilationContext->backend->addStaticProperty(
$compilationContext->backend->$methodName(
$classEntry,
$property,
$tempVariable,
Expand All @@ -197,15 +205,15 @@ public function assignStatic(

case 'bool':
if ('1' == $resolvedExpr->getBooleanCode()) {
$compilationContext->backend->addStaticProperty(
$compilationContext->backend->$methodName(
$classEntry,
$property,
'true',
$compilationContext
);
} else {
if ('0' == $resolvedExpr->getBooleanCode()) {
$compilationContext->backend->addStaticProperty(
$compilationContext->backend->$methodName(
$classEntry,
$property,
'false',
Expand All @@ -214,7 +222,7 @@ public function assignStatic(
} else {
$codePrinter->output('if (' . $resolvedExpr->getBooleanCode() . ') {');
$codePrinter->increaseLevel();
$compilationContext->backend->addStaticProperty(
$compilationContext->backend->$methodName(
$classEntry,
$property,
'true',
Expand All @@ -223,7 +231,7 @@ public function assignStatic(
$codePrinter->decreaseLevel();
$codePrinter->output('} else {');
$codePrinter->increaseLevel();
$compilationContext->backend->addStaticProperty(
$compilationContext->backend->$methodName(
$classEntry,
$property,
'false',
Expand All @@ -242,7 +250,7 @@ public function assignStatic(
true
);
$compilationContext->backend->initArray($tempVariable, $compilationContext);
$compilationContext->backend->addStaticProperty(
$compilationContext->backend->$methodName(
$classEntry,
$property,
$tempVariable,
Expand All @@ -254,7 +262,7 @@ public function assignStatic(
break;

case 'array':
$compilationContext->backend->addStaticProperty(
$compilationContext->backend->$methodName(
$classEntry,
$property,
$resolvedExpr,
Expand Down Expand Up @@ -282,8 +290,12 @@ public function assignStatic(
true
);

$compilationContext->backend->assignLong($tempVariable, $variableVariable, $compilationContext);
$compilationContext->backend->addStaticProperty(
$compilationContext->backend->assignLong(
$tempVariable,
$variableVariable,
$compilationContext
);
$compilationContext->backend->$methodName(
$classEntry,
$property,
$tempVariable,
Expand All @@ -307,7 +319,7 @@ public function assignStatic(
$variableVariable,
$compilationContext
);
$compilationContext->backend->addStaticProperty(
$compilationContext->backend->$methodName(
$classEntry,
$property,
$tempVariable,
Expand All @@ -325,8 +337,12 @@ public function assignStatic(
$compilationContext,
true
);
$compilationContext->backend->assignBool($tempVariable, $variableVariable, $compilationContext);
$compilationContext->backend->addStaticProperty(
$compilationContext->backend->assignBool(
$tempVariable,
$variableVariable,
$compilationContext
);
$compilationContext->backend->$methodName(
$classEntry,
$property,
$tempVariable,
Expand All @@ -337,7 +353,7 @@ public function assignStatic(
}
break;
default:
$compilationContext->backend->addStaticProperty(
$compilationContext->backend->$methodName(
$classEntry,
$property,
$variableVariable,
Expand Down
Loading

0 comments on commit 4f10143

Please sign in to comment.