2022-12-25 18:54:14 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/** @noinspection PhpMissingFieldTypeInspection */
|
|
|
|
|
|
|
|
namespace RTEX\Objects\Program\Instructions;
|
|
|
|
|
|
|
|
use RTEX\Abstracts\InstructionType;
|
|
|
|
use RTEX\Classes\InstructionBuilder;
|
|
|
|
use RTEX\Classes\Utilities;
|
|
|
|
use RTEX\Engine;
|
2022-12-26 16:28:55 -05:00
|
|
|
use RTEX\Exceptions\EvaluationException;
|
|
|
|
use RTEX\Exceptions\InstructionException;
|
2022-12-26 23:11:48 -05:00
|
|
|
use RTEX\Exceptions\Runtime\TypeException;
|
2022-12-26 16:28:55 -05:00
|
|
|
use RTEX\Exceptions\Runtime\ZeroDivisionException;
|
2022-12-25 18:54:14 -05:00
|
|
|
use RTEX\Interfaces\InstructionInterface;
|
|
|
|
|
|
|
|
class Divide implements InstructionInterface
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
private $A;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
private $B;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the type of instruction
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getType(): string
|
|
|
|
{
|
|
|
|
return InstructionType::Divide;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array representation of the instruction
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function toArray(): array
|
|
|
|
{
|
|
|
|
return InstructionBuilder::toRaw(self::getType(), [
|
|
|
|
'a' => $this->A,
|
|
|
|
'b' => $this->B
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a new instance of this class from an array representation
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @return InstructionInterface
|
2022-12-26 16:28:55 -05:00
|
|
|
* @throws InstructionException
|
2022-12-25 18:54:14 -05:00
|
|
|
*/
|
|
|
|
public static function fromArray(array $data): InstructionInterface
|
|
|
|
{
|
|
|
|
$instruction = new self();
|
|
|
|
$instruction->setA($data['a'] ?? null);
|
|
|
|
$instruction->setB($data['b'] ?? null);
|
|
|
|
return $instruction;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Engine $engine
|
|
|
|
* @return int
|
2022-12-26 16:28:55 -05:00
|
|
|
* @throws EvaluationException
|
2022-12-26 23:11:48 -05:00
|
|
|
* @throws TypeException
|
|
|
|
* @throws ZeroDivisionException
|
2022-12-25 18:54:14 -05:00
|
|
|
*/
|
|
|
|
public function eval(Engine $engine): int
|
|
|
|
{
|
2022-12-26 23:11:48 -05:00
|
|
|
$a = $engine->eval($this->A);
|
|
|
|
$b = $engine->eval($this->B);
|
|
|
|
|
|
|
|
if(!(is_int($a) || is_float($a) || is_double($a)))
|
|
|
|
throw new TypeException('Cannot divide a non-numeric value');
|
|
|
|
if(!(is_int($b) || is_float($b) || is_double($b)))
|
|
|
|
throw new TypeException('Cannot divide by a non-numeric value');
|
|
|
|
if ($b === 0)
|
2022-12-26 16:28:55 -05:00
|
|
|
throw new ZeroDivisionException(sprintf('Division by zero in %s', $this));
|
|
|
|
|
2022-12-26 23:11:48 -05:00
|
|
|
return (intval($a) / intval($b));
|
2022-12-25 18:54:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the string representation of the instruction
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function __toString(): string
|
|
|
|
{
|
|
|
|
return sprintf(
|
|
|
|
self::getType() . ' (%s/%s)',
|
|
|
|
Utilities::entityToString($this->A),
|
|
|
|
Utilities::entityToString($this->B)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the value of A
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getA(): mixed
|
|
|
|
{
|
|
|
|
return $this->A;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the value of A
|
|
|
|
*
|
|
|
|
* @param mixed $A
|
2022-12-26 16:28:55 -05:00
|
|
|
* @throws InstructionException
|
2022-12-25 18:54:14 -05:00
|
|
|
*/
|
|
|
|
public function setA(mixed $A): void
|
|
|
|
{
|
|
|
|
$this->A = InstructionBuilder::fromRaw($A);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the value of B
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getB(): mixed
|
|
|
|
{
|
|
|
|
return $this->B;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the value of B
|
|
|
|
*
|
|
|
|
* @param mixed $B
|
2022-12-26 16:28:55 -05:00
|
|
|
* @throws InstructionException
|
2022-12-25 18:54:14 -05:00
|
|
|
*/
|
|
|
|
public function setB(mixed $B): void
|
|
|
|
{
|
|
|
|
$this->B = InstructionBuilder::fromRaw($B);
|
|
|
|
}
|
|
|
|
}
|