2022-12-25 18:54:14 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/** @noinspection PhpMissingFieldTypeInspection */
|
|
|
|
|
2022-12-30 00:46:25 -05:00
|
|
|
namespace RTEX\Objects\Program\Instructions\Arithmetic;
|
2022-12-25 18:54:14 -05:00
|
|
|
|
|
|
|
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-25 18:54:14 -05:00
|
|
|
use RTEX\Interfaces\InstructionInterface;
|
|
|
|
|
2022-12-30 00:46:25 -05:00
|
|
|
class Absolute implements InstructionInterface
|
2022-12-25 18:54:14 -05:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var mixed
|
|
|
|
*/
|
2022-12-30 00:46:25 -05:00
|
|
|
private $Value;
|
2022-12-25 18:54:14 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the type of instruction
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getType(): string
|
|
|
|
{
|
2022-12-30 00:46:25 -05:00
|
|
|
return InstructionType::Absolute;
|
2022-12-25 18:54:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array representation of the instruction
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function toArray(): array
|
|
|
|
{
|
|
|
|
return InstructionBuilder::toRaw(self::getType(), [
|
2022-12-30 00:46:25 -05:00
|
|
|
'value' => $this->Value,
|
2022-12-25 18:54:14 -05:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
2022-12-30 00:46:25 -05:00
|
|
|
$instruction->setValue($data['value'] ?? null);
|
2022-12-25 18:54:14 -05:00
|
|
|
return $instruction;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Engine $engine
|
2022-12-30 00:46:25 -05:00
|
|
|
* @return int|float
|
2022-12-26 16:28:55 -05:00
|
|
|
* @throws EvaluationException
|
2022-12-26 23:11:48 -05:00
|
|
|
* @throws TypeException
|
2022-12-25 18:54:14 -05:00
|
|
|
*/
|
2022-12-30 00:46:25 -05:00
|
|
|
public function eval(Engine $engine): int|float
|
2022-12-25 18:54:14 -05:00
|
|
|
{
|
2022-12-30 00:46:25 -05:00
|
|
|
$value = $engine->eval($this->Value);
|
2022-12-26 23:11:48 -05:00
|
|
|
|
2022-12-30 00:46:25 -05:00
|
|
|
if(!(is_int($value) || is_float($value) || is_double($value)))
|
|
|
|
throw new TypeException(sprintf('Cannot calculate the absolute value of a non-numeric value, got %s', Utilities::getType($value, true)));
|
2022-12-26 16:28:55 -05:00
|
|
|
|
2022-12-30 00:46:25 -05:00
|
|
|
return abs($value);
|
2022-12-25 18:54:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the string representation of the instruction
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function __toString(): string
|
|
|
|
{
|
|
|
|
return sprintf(
|
2022-12-30 00:46:25 -05:00
|
|
|
self::getType() . ' (%s+%s)',
|
|
|
|
Utilities::entityToString($this->Value),
|
2022-12-25 18:54:14 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the value of A
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2022-12-30 00:46:25 -05:00
|
|
|
public function getValue(): mixed
|
2022-12-25 18:54:14 -05:00
|
|
|
{
|
2022-12-30 00:46:25 -05:00
|
|
|
return $this->Value;
|
2022-12-25 18:54:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the value of A
|
|
|
|
*
|
2022-12-30 00:46:25 -05:00
|
|
|
* @param mixed $Value
|
2022-12-26 16:28:55 -05:00
|
|
|
* @throws InstructionException
|
2022-12-25 18:54:14 -05:00
|
|
|
*/
|
2022-12-30 00:46:25 -05:00
|
|
|
public function setValue(mixed $Value): void
|
2022-12-25 18:54:14 -05:00
|
|
|
{
|
2022-12-30 00:46:25 -05:00
|
|
|
$this->Value = InstructionBuilder::fromRaw($Value);
|
2022-12-25 18:54:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|