Refactored all Arithmetic instructions
This commit is contained in:
parent
ba0dc302ff
commit
933f194726
10 changed files with 574 additions and 80 deletions
109
src/RTEX/Objects/Program/Instructions/Arithmetic/Absolute.php
Normal file
109
src/RTEX/Objects/Program/Instructions/Arithmetic/Absolute.php
Normal file
|
@ -0,0 +1,109 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/** @noinspection PhpMissingFieldTypeInspection */
|
||||||
|
|
||||||
|
namespace RTEX\Objects\Program\Instructions\Arithmetic;
|
||||||
|
|
||||||
|
use RTEX\Abstracts\InstructionType;
|
||||||
|
use RTEX\Classes\InstructionBuilder;
|
||||||
|
use RTEX\Classes\Utilities;
|
||||||
|
use RTEX\Engine;
|
||||||
|
use RTEX\Exceptions\EvaluationException;
|
||||||
|
use RTEX\Exceptions\InstructionException;
|
||||||
|
use RTEX\Exceptions\Runtime\TypeException;
|
||||||
|
use RTEX\Interfaces\InstructionInterface;
|
||||||
|
|
||||||
|
class Absolute implements InstructionInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var mixed
|
||||||
|
*/
|
||||||
|
private $Value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the type of instruction
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getType(): string
|
||||||
|
{
|
||||||
|
return InstructionType::Absolute;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an array representation of the instruction
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function toArray(): array
|
||||||
|
{
|
||||||
|
return InstructionBuilder::toRaw(self::getType(), [
|
||||||
|
'value' => $this->Value,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new instance of this class from an array representation
|
||||||
|
*
|
||||||
|
* @param array $data
|
||||||
|
* @return InstructionInterface
|
||||||
|
* @throws InstructionException
|
||||||
|
*/
|
||||||
|
public static function fromArray(array $data): InstructionInterface
|
||||||
|
{
|
||||||
|
$instruction = new self();
|
||||||
|
$instruction->setValue($data['value'] ?? null);
|
||||||
|
return $instruction;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Engine $engine
|
||||||
|
* @return int|float
|
||||||
|
* @throws EvaluationException
|
||||||
|
* @throws TypeException
|
||||||
|
*/
|
||||||
|
public function eval(Engine $engine): int|float
|
||||||
|
{
|
||||||
|
$value = $engine->eval($this->Value);
|
||||||
|
|
||||||
|
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)));
|
||||||
|
|
||||||
|
return abs($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the string representation of the instruction
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function __toString(): string
|
||||||
|
{
|
||||||
|
return sprintf(
|
||||||
|
self::getType() . ' (%s+%s)',
|
||||||
|
Utilities::entityToString($this->Value),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the value of A
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getValue(): mixed
|
||||||
|
{
|
||||||
|
return $this->Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the value of A
|
||||||
|
*
|
||||||
|
* @param mixed $Value
|
||||||
|
* @throws InstructionException
|
||||||
|
*/
|
||||||
|
public function setValue(mixed $Value): void
|
||||||
|
{
|
||||||
|
$this->Value = InstructionBuilder::fromRaw($Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
/** @noinspection PhpMissingFieldTypeInspection */
|
/** @noinspection PhpMissingFieldTypeInspection */
|
||||||
|
|
||||||
namespace RTEX\Objects\Program\Instructions;
|
namespace RTEX\Objects\Program\Instructions\Arithmetic;
|
||||||
|
|
||||||
use RTEX\Abstracts\InstructionType;
|
use RTEX\Abstracts\InstructionType;
|
||||||
use RTEX\Classes\InstructionBuilder;
|
use RTEX\Classes\InstructionBuilder;
|
||||||
|
@ -66,20 +66,20 @@
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Engine $engine
|
* @param Engine $engine
|
||||||
* @return int
|
* @return int|float
|
||||||
* @throws EvaluationException
|
* @throws EvaluationException
|
||||||
* @throws TypeException
|
* @throws TypeException
|
||||||
* @throws ZeroDivisionException
|
* @throws ZeroDivisionException
|
||||||
*/
|
*/
|
||||||
public function eval(Engine $engine): int
|
public function eval(Engine $engine): int|float
|
||||||
{
|
{
|
||||||
$a = $engine->eval($this->A);
|
$a = $engine->eval($this->A);
|
||||||
$b = $engine->eval($this->B);
|
$b = $engine->eval($this->B);
|
||||||
|
|
||||||
if(!(is_int($a) || is_float($a) || is_double($a)))
|
if(!(is_int($a) || is_float($a) || is_double($a)))
|
||||||
throw new TypeException('Cannot divide a non-numeric value');
|
throw new TypeException(sprintf('Cannot divide a non-numeric value \'A\' of type %s', Utilities::getType($a, true)));
|
||||||
if(!(is_int($b) || is_float($b) || is_double($b)))
|
if(!(is_int($b) || is_float($b) || is_double($b)))
|
||||||
throw new TypeException('Cannot divide by a non-numeric value');
|
throw new TypeException(sprintf('Cannot divide a non-numeric value \'B\' of type %s', Utilities::getType($b, true)));
|
||||||
if ($b === 0)
|
if ($b === 0)
|
||||||
throw new ZeroDivisionException(sprintf('Division by zero in %s', $this));
|
throw new ZeroDivisionException(sprintf('Division by zero in %s', $this));
|
||||||
|
|
109
src/RTEX/Objects/Program/Instructions/Arithmetic/Floor.php
Normal file
109
src/RTEX/Objects/Program/Instructions/Arithmetic/Floor.php
Normal file
|
@ -0,0 +1,109 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/** @noinspection PhpMissingFieldTypeInspection */
|
||||||
|
|
||||||
|
namespace RTEX\Objects\Program\Instructions\Arithmetic;
|
||||||
|
|
||||||
|
use RTEX\Abstracts\InstructionType;
|
||||||
|
use RTEX\Classes\InstructionBuilder;
|
||||||
|
use RTEX\Classes\Utilities;
|
||||||
|
use RTEX\Engine;
|
||||||
|
use RTEX\Exceptions\EvaluationException;
|
||||||
|
use RTEX\Exceptions\InstructionException;
|
||||||
|
use RTEX\Exceptions\Runtime\TypeException;
|
||||||
|
use RTEX\Interfaces\InstructionInterface;
|
||||||
|
|
||||||
|
class Floor implements InstructionInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var mixed
|
||||||
|
*/
|
||||||
|
private $Value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the type of instruction
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getType(): string
|
||||||
|
{
|
||||||
|
return InstructionType::Floor;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an array representation of the instruction
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function toArray(): array
|
||||||
|
{
|
||||||
|
return InstructionBuilder::toRaw(self::getType(), [
|
||||||
|
'value' => $this->Value,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new instance of this class from an array representation
|
||||||
|
*
|
||||||
|
* @param array $data
|
||||||
|
* @return InstructionInterface
|
||||||
|
* @throws InstructionException
|
||||||
|
*/
|
||||||
|
public static function fromArray(array $data): InstructionInterface
|
||||||
|
{
|
||||||
|
$instruction = new self();
|
||||||
|
$instruction->setValue($data['value'] ?? null);
|
||||||
|
return $instruction;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Engine $engine
|
||||||
|
* @return int|float
|
||||||
|
* @throws EvaluationException
|
||||||
|
* @throws TypeException
|
||||||
|
*/
|
||||||
|
public function eval(Engine $engine): int|float
|
||||||
|
{
|
||||||
|
$value = $engine->eval($this->Value);
|
||||||
|
|
||||||
|
if(!(is_int($value) || is_float($value) || is_double($value)))
|
||||||
|
throw new TypeException(sprintf('Cannot calculate the floor value of a non-numeric value, got %s', Utilities::getType($value, true)));
|
||||||
|
|
||||||
|
return floor($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the string representation of the instruction
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function __toString(): string
|
||||||
|
{
|
||||||
|
return sprintf(
|
||||||
|
self::getType() . ' (%s+%s)',
|
||||||
|
Utilities::entityToString($this->Value),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the value of A
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getValue(): mixed
|
||||||
|
{
|
||||||
|
return $this->Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the value of A
|
||||||
|
*
|
||||||
|
* @param mixed $Value
|
||||||
|
* @throws InstructionException
|
||||||
|
*/
|
||||||
|
public function setValue(mixed $Value): void
|
||||||
|
{
|
||||||
|
$this->Value = InstructionBuilder::fromRaw($Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -2,14 +2,15 @@
|
||||||
|
|
||||||
/** @noinspection PhpMissingFieldTypeInspection */
|
/** @noinspection PhpMissingFieldTypeInspection */
|
||||||
|
|
||||||
namespace RTEX\Objects\Program\Instructions;
|
namespace RTEX\Objects\Program\Instructions\Arithmetic;
|
||||||
|
|
||||||
use RTEX\Abstracts\InstructionType;
|
use RTEX\Abstracts\InstructionType;
|
||||||
use RTEX\Classes\InstructionBuilder;
|
use RTEX\Classes\InstructionBuilder;
|
||||||
use RTEX\Classes\Utilities;
|
use RTEX\Classes\Utilities;
|
||||||
use RTEX\Engine;
|
use RTEX\Engine;
|
||||||
use RTEX\Exceptions\Core\MalformedInstructionException;
|
use RTEX\Exceptions\EvaluationException;
|
||||||
use RTEX\Exceptions\Core\UnsupportedVariableType;
|
use RTEX\Exceptions\InstructionException;
|
||||||
|
use RTEX\Exceptions\Runtime\TypeException;
|
||||||
use RTEX\Interfaces\InstructionInterface;
|
use RTEX\Interfaces\InstructionInterface;
|
||||||
|
|
||||||
class Modulo implements InstructionInterface
|
class Modulo implements InstructionInterface
|
||||||
|
@ -38,7 +39,6 @@
|
||||||
* Returns an array representation of the instruction
|
* Returns an array representation of the instruction
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
* @throws UnsupportedVariableType
|
|
||||||
*/
|
*/
|
||||||
public function toArray(): array
|
public function toArray(): array
|
||||||
{
|
{
|
||||||
|
@ -53,8 +53,7 @@
|
||||||
*
|
*
|
||||||
* @param array $data
|
* @param array $data
|
||||||
* @return InstructionInterface
|
* @return InstructionInterface
|
||||||
* @throws MalformedInstructionException
|
* @throws InstructionException
|
||||||
* @throws UnsupportedVariableType
|
|
||||||
*/
|
*/
|
||||||
public static function fromArray(array $data): InstructionInterface
|
public static function fromArray(array $data): InstructionInterface
|
||||||
{
|
{
|
||||||
|
@ -66,19 +65,27 @@
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Engine $engine
|
* @param Engine $engine
|
||||||
* @return int
|
* @return int|float
|
||||||
* @throws UnsupportedVariableType
|
* @throws EvaluationException
|
||||||
|
* @throws TypeException
|
||||||
*/
|
*/
|
||||||
public function eval(Engine $engine): int
|
public function eval(Engine $engine): int|float
|
||||||
{
|
{
|
||||||
return (intval($engine->eval($this->A)) % intval($engine->eval($this->B)));
|
$a = $engine->eval($this->A);
|
||||||
|
$b = $engine->eval($this->B);
|
||||||
|
|
||||||
|
if(!(is_int($a) || is_float($a) || is_double($a)))
|
||||||
|
throw new TypeException(sprintf('Cannot perform modulo operation on non-numeric value \'A\' of type %s', Utilities::getType($a, true)));
|
||||||
|
if(!(is_int($b) || is_float($b) || is_double($b)))
|
||||||
|
throw new TypeException(sprintf('Cannot perform modulo operation on non-numeric value \'B\' of type %s', Utilities::getType($b. true)));
|
||||||
|
|
||||||
|
return ($a % $b);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the string representation of the instruction
|
* Returns the string representation of the instruction
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
* @throws UnsupportedVariableType
|
|
||||||
*/
|
*/
|
||||||
public function __toString(): string
|
public function __toString(): string
|
||||||
{
|
{
|
||||||
|
@ -103,8 +110,7 @@
|
||||||
* Sets the value of A
|
* Sets the value of A
|
||||||
*
|
*
|
||||||
* @param mixed $A
|
* @param mixed $A
|
||||||
* @throws UnsupportedVariableType
|
* @throws InstructionException
|
||||||
* @throws MalformedInstructionException
|
|
||||||
*/
|
*/
|
||||||
public function setA(mixed $A): void
|
public function setA(mixed $A): void
|
||||||
{
|
{
|
||||||
|
@ -125,8 +131,7 @@
|
||||||
* Sets the value of B
|
* Sets the value of B
|
||||||
*
|
*
|
||||||
* @param mixed $B
|
* @param mixed $B
|
||||||
* @throws MalformedInstructionException
|
* @throws InstructionException
|
||||||
* @throws UnsupportedVariableType
|
|
||||||
*/
|
*/
|
||||||
public function setB(mixed $B): void
|
public function setB(mixed $B): void
|
||||||
{
|
{
|
|
@ -2,14 +2,15 @@
|
||||||
|
|
||||||
/** @noinspection PhpMissingFieldTypeInspection */
|
/** @noinspection PhpMissingFieldTypeInspection */
|
||||||
|
|
||||||
namespace RTEX\Objects\Program\Instructions;
|
namespace RTEX\Objects\Program\Instructions\Arithmetic;
|
||||||
|
|
||||||
use RTEX\Abstracts\InstructionType;
|
use RTEX\Abstracts\InstructionType;
|
||||||
use RTEX\Classes\InstructionBuilder;
|
use RTEX\Classes\InstructionBuilder;
|
||||||
use RTEX\Classes\Utilities;
|
use RTEX\Classes\Utilities;
|
||||||
use RTEX\Engine;
|
use RTEX\Engine;
|
||||||
use RTEX\Exceptions\Core\MalformedInstructionException;
|
use RTEX\Exceptions\EvaluationException;
|
||||||
use RTEX\Exceptions\Core\UnsupportedVariableType;
|
use RTEX\Exceptions\InstructionException;
|
||||||
|
use RTEX\Exceptions\Runtime\TypeException;
|
||||||
use RTEX\Interfaces\InstructionInterface;
|
use RTEX\Interfaces\InstructionInterface;
|
||||||
|
|
||||||
class Multiply implements InstructionInterface
|
class Multiply implements InstructionInterface
|
||||||
|
@ -38,7 +39,6 @@
|
||||||
* Returns an array representation of the instruction
|
* Returns an array representation of the instruction
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
* @throws UnsupportedVariableType
|
|
||||||
*/
|
*/
|
||||||
public function toArray(): array
|
public function toArray(): array
|
||||||
{
|
{
|
||||||
|
@ -53,8 +53,7 @@
|
||||||
*
|
*
|
||||||
* @param array $data
|
* @param array $data
|
||||||
* @return InstructionInterface
|
* @return InstructionInterface
|
||||||
* @throws MalformedInstructionException
|
* @throws InstructionException
|
||||||
* @throws UnsupportedVariableType
|
|
||||||
*/
|
*/
|
||||||
public static function fromArray(array $data): InstructionInterface
|
public static function fromArray(array $data): InstructionInterface
|
||||||
{
|
{
|
||||||
|
@ -66,19 +65,27 @@
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Engine $engine
|
* @param Engine $engine
|
||||||
* @return int
|
* @return int|float
|
||||||
* @throws UnsupportedVariableType
|
* @throws EvaluationException
|
||||||
|
* @throws TypeException
|
||||||
*/
|
*/
|
||||||
public function eval(Engine $engine): int
|
public function eval(Engine $engine): int|float
|
||||||
{
|
{
|
||||||
return (intval($engine->eval($this->A)) * intval($engine->eval($this->B)));
|
$a = $engine->eval($this->A);
|
||||||
|
$b = $engine->eval($this->B);
|
||||||
|
|
||||||
|
if(!(is_int($a) || is_float($a) || is_double($a)))
|
||||||
|
throw new TypeException(sprintf('Cannot multiply a non-numeric value \'A\' of type \'%s\'', Utilities::getType($a, true)));
|
||||||
|
if(!(is_int($b) || is_float($b) || is_double($b)))
|
||||||
|
throw new TypeException(sprintf('Cannot multiply a non-numeric value \'B\' of type \'%s\'', Utilities::getType($b, true)));
|
||||||
|
|
||||||
|
return ($a * $b);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the string representation of the instruction
|
* Returns the string representation of the instruction
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
* @throws UnsupportedVariableType
|
|
||||||
*/
|
*/
|
||||||
public function __toString(): string
|
public function __toString(): string
|
||||||
{
|
{
|
||||||
|
@ -103,8 +110,7 @@
|
||||||
* Sets the value of A
|
* Sets the value of A
|
||||||
*
|
*
|
||||||
* @param mixed $A
|
* @param mixed $A
|
||||||
* @throws UnsupportedVariableType
|
* @throws InstructionException
|
||||||
* @throws MalformedInstructionException
|
|
||||||
*/
|
*/
|
||||||
public function setA(mixed $A): void
|
public function setA(mixed $A): void
|
||||||
{
|
{
|
||||||
|
@ -125,8 +131,7 @@
|
||||||
* Sets the value of B
|
* Sets the value of B
|
||||||
*
|
*
|
||||||
* @param mixed $B
|
* @param mixed $B
|
||||||
* @throws MalformedInstructionException
|
* @throws InstructionException
|
||||||
* @throws UnsupportedVariableType
|
|
||||||
*/
|
*/
|
||||||
public function setB(mixed $B): void
|
public function setB(mixed $B): void
|
||||||
{
|
{
|
|
@ -2,14 +2,15 @@
|
||||||
|
|
||||||
/** @noinspection PhpMissingFieldTypeInspection */
|
/** @noinspection PhpMissingFieldTypeInspection */
|
||||||
|
|
||||||
namespace RTEX\Objects\Program\Instructions;
|
namespace RTEX\Objects\Program\Instructions\Arithmetic;
|
||||||
|
|
||||||
use RTEX\Abstracts\InstructionType;
|
use RTEX\Abstracts\InstructionType;
|
||||||
use RTEX\Classes\InstructionBuilder;
|
use RTEX\Classes\InstructionBuilder;
|
||||||
use RTEX\Classes\Utilities;
|
use RTEX\Classes\Utilities;
|
||||||
use RTEX\Engine;
|
use RTEX\Engine;
|
||||||
use RTEX\Exceptions\Core\MalformedInstructionException;
|
use RTEX\Exceptions\EvaluationException;
|
||||||
use RTEX\Exceptions\Core\UnsupportedVariableType;
|
use RTEX\Exceptions\InstructionException;
|
||||||
|
use RTEX\Exceptions\Runtime\TypeException;
|
||||||
use RTEX\Interfaces\InstructionInterface;
|
use RTEX\Interfaces\InstructionInterface;
|
||||||
|
|
||||||
class Power implements InstructionInterface
|
class Power implements InstructionInterface
|
||||||
|
@ -38,7 +39,6 @@
|
||||||
* Returns an array representation of the instruction
|
* Returns an array representation of the instruction
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
* @throws UnsupportedVariableType
|
|
||||||
*/
|
*/
|
||||||
public function toArray(): array
|
public function toArray(): array
|
||||||
{
|
{
|
||||||
|
@ -53,8 +53,7 @@
|
||||||
*
|
*
|
||||||
* @param array $data
|
* @param array $data
|
||||||
* @return InstructionInterface
|
* @return InstructionInterface
|
||||||
* @throws MalformedInstructionException
|
* @throws InstructionException
|
||||||
* @throws UnsupportedVariableType
|
|
||||||
*/
|
*/
|
||||||
public static function fromArray(array $data): InstructionInterface
|
public static function fromArray(array $data): InstructionInterface
|
||||||
{
|
{
|
||||||
|
@ -66,19 +65,27 @@
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Engine $engine
|
* @param Engine $engine
|
||||||
* @return int
|
* @return int|float
|
||||||
* @throws UnsupportedVariableType
|
* @throws TypeException
|
||||||
|
* @throws EvaluationException
|
||||||
*/
|
*/
|
||||||
public function eval(Engine $engine): int
|
public function eval(Engine $engine): int|float
|
||||||
{
|
{
|
||||||
return (intval($engine->eval($this->A)) ** intval($engine->eval($this->B)));
|
$a = $engine->eval($this->A);
|
||||||
|
$b = $engine->eval($this->B);
|
||||||
|
|
||||||
|
if(!(is_int($a) || is_float($a) || is_double($a)))
|
||||||
|
throw new TypeException(sprintf('Cannot raise \'A\' to the power of \'B\' because \'A\' is not a number (\'%s\')', Utilities::getType($a, true)));
|
||||||
|
if(!(is_int($b) || is_float($b) || is_double($b)))
|
||||||
|
throw new TypeException(sprintf('Cannot raise \'A\' to the power of \'B\' because \'B\' is not a number (\'%s\')', Utilities::getType($b, true)));
|
||||||
|
|
||||||
|
return ($a ** $b);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the string representation of the instruction
|
* Returns the string representation of the instruction
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
* @throws UnsupportedVariableType
|
|
||||||
*/
|
*/
|
||||||
public function __toString(): string
|
public function __toString(): string
|
||||||
{
|
{
|
||||||
|
@ -103,8 +110,7 @@
|
||||||
* Sets the value of A
|
* Sets the value of A
|
||||||
*
|
*
|
||||||
* @param mixed $A
|
* @param mixed $A
|
||||||
* @throws UnsupportedVariableType
|
* @throws InstructionException
|
||||||
* @throws MalformedInstructionException
|
|
||||||
*/
|
*/
|
||||||
public function setA(mixed $A): void
|
public function setA(mixed $A): void
|
||||||
{
|
{
|
||||||
|
@ -125,8 +131,7 @@
|
||||||
* Sets the value of B
|
* Sets the value of B
|
||||||
*
|
*
|
||||||
* @param mixed $B
|
* @param mixed $B
|
||||||
* @throws MalformedInstructionException
|
* @throws InstructionException
|
||||||
* @throws UnsupportedVariableType
|
|
||||||
*/
|
*/
|
||||||
public function setB(mixed $B): void
|
public function setB(mixed $B): void
|
||||||
{
|
{
|
142
src/RTEX/Objects/Program/Instructions/Arithmetic/Round.php
Normal file
142
src/RTEX/Objects/Program/Instructions/Arithmetic/Round.php
Normal file
|
@ -0,0 +1,142 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/** @noinspection PhpMissingFieldTypeInspection */
|
||||||
|
|
||||||
|
namespace RTEX\Objects\Program\Instructions\Arithmetic;
|
||||||
|
|
||||||
|
use RTEX\Abstracts\InstructionType;
|
||||||
|
use RTEX\Classes\InstructionBuilder;
|
||||||
|
use RTEX\Classes\Utilities;
|
||||||
|
use RTEX\Engine;
|
||||||
|
use RTEX\Exceptions\EvaluationException;
|
||||||
|
use RTEX\Exceptions\InstructionException;
|
||||||
|
use RTEX\Exceptions\Runtime\TypeException;
|
||||||
|
use RTEX\Interfaces\InstructionInterface;
|
||||||
|
|
||||||
|
class Round implements InstructionInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var mixed
|
||||||
|
*/
|
||||||
|
private $Value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The number of decimal places to round to
|
||||||
|
*
|
||||||
|
* @var mixed
|
||||||
|
*/
|
||||||
|
private $Precision;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the type of instruction
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getType(): string
|
||||||
|
{
|
||||||
|
return InstructionType::Round;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an array representation of the instruction
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function toArray(): array
|
||||||
|
{
|
||||||
|
return InstructionBuilder::toRaw(self::getType(), [
|
||||||
|
'value' => $this->Value,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new instance of this class from an array representation
|
||||||
|
*
|
||||||
|
* @param array $data
|
||||||
|
* @return InstructionInterface
|
||||||
|
* @throws InstructionException
|
||||||
|
*/
|
||||||
|
public static function fromArray(array $data): InstructionInterface
|
||||||
|
{
|
||||||
|
$instruction = new self();
|
||||||
|
$instruction->setValue($data['value'] ?? null);
|
||||||
|
return $instruction;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Engine $engine
|
||||||
|
* @return float|int
|
||||||
|
* @throws EvaluationException
|
||||||
|
* @throws TypeException
|
||||||
|
*/
|
||||||
|
public function eval(Engine $engine): int|float
|
||||||
|
{
|
||||||
|
$value = $engine->eval($this->Value);
|
||||||
|
$precision = $engine->eval($this->Precision);
|
||||||
|
|
||||||
|
if(!(is_int($value) || is_float($value) || is_double($value)))
|
||||||
|
throw new TypeException(sprintf('Cannot calculate the round value of a non-numeric value, got %s', Utilities::getType($value, true)));
|
||||||
|
|
||||||
|
if(is_null($precision))
|
||||||
|
return round($value);
|
||||||
|
|
||||||
|
if(!(is_int($precision) || is_float($precision) || is_double($precision)))
|
||||||
|
throw new TypeException(sprintf('Cannot calculate the round value of a non-numeric precision, got %s', Utilities::getType($precision, true)));
|
||||||
|
|
||||||
|
return round($value, $precision);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the string representation of the instruction
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function __toString(): string
|
||||||
|
{
|
||||||
|
return sprintf(
|
||||||
|
self::getType() . ' (%s+%s)',
|
||||||
|
Utilities::entityToString($this->Value),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the value of A
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getValue(): mixed
|
||||||
|
{
|
||||||
|
return $this->Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the value of A
|
||||||
|
*
|
||||||
|
* @param mixed $Value
|
||||||
|
* @throws InstructionException
|
||||||
|
*/
|
||||||
|
public function setValue(mixed $Value): void
|
||||||
|
{
|
||||||
|
$this->Value = InstructionBuilder::fromRaw($Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
* @noinspection PhpUnused
|
||||||
|
*/
|
||||||
|
public function getPrecision(): mixed
|
||||||
|
{
|
||||||
|
return $this->Precision;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $Precision
|
||||||
|
* @noinspection PhpUnused
|
||||||
|
*/
|
||||||
|
public function setPrecision(mixed $Precision): void
|
||||||
|
{
|
||||||
|
$this->Precision = $Precision;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
109
src/RTEX/Objects/Program/Instructions/Arithmetic/SquareRoot.php
Normal file
109
src/RTEX/Objects/Program/Instructions/Arithmetic/SquareRoot.php
Normal file
|
@ -0,0 +1,109 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/** @noinspection PhpMissingFieldTypeInspection */
|
||||||
|
|
||||||
|
namespace RTEX\Objects\Program\Instructions\Arithmetic;
|
||||||
|
|
||||||
|
use RTEX\Abstracts\InstructionType;
|
||||||
|
use RTEX\Classes\InstructionBuilder;
|
||||||
|
use RTEX\Classes\Utilities;
|
||||||
|
use RTEX\Engine;
|
||||||
|
use RTEX\Exceptions\EvaluationException;
|
||||||
|
use RTEX\Exceptions\InstructionException;
|
||||||
|
use RTEX\Exceptions\Runtime\TypeException;
|
||||||
|
use RTEX\Interfaces\InstructionInterface;
|
||||||
|
|
||||||
|
class SquareRoot implements InstructionInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var mixed
|
||||||
|
*/
|
||||||
|
private $Value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the type of instruction
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getType(): string
|
||||||
|
{
|
||||||
|
return InstructionType::SquareRoot;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an array representation of the instruction
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function toArray(): array
|
||||||
|
{
|
||||||
|
return InstructionBuilder::toRaw(self::getType(), [
|
||||||
|
'value' => $this->Value,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new instance of this class from an array representation
|
||||||
|
*
|
||||||
|
* @param array $data
|
||||||
|
* @return InstructionInterface
|
||||||
|
* @throws InstructionException
|
||||||
|
*/
|
||||||
|
public static function fromArray(array $data): InstructionInterface
|
||||||
|
{
|
||||||
|
$instruction = new self();
|
||||||
|
$instruction->setValue($data['value'] ?? null);
|
||||||
|
return $instruction;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Engine $engine
|
||||||
|
* @return int|float
|
||||||
|
* @throws EvaluationException
|
||||||
|
* @throws TypeException
|
||||||
|
*/
|
||||||
|
public function eval(Engine $engine): int|float
|
||||||
|
{
|
||||||
|
$value = $engine->eval($this->Value);
|
||||||
|
|
||||||
|
if(!(is_int($value) || is_float($value) || is_double($value)))
|
||||||
|
throw new TypeException(sprintf('Cannot calculate the square root of a non-numeric \'Value\', got type %s', Utilities::getType($value, true)));
|
||||||
|
|
||||||
|
return sqrt($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the string representation of the instruction
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function __toString(): string
|
||||||
|
{
|
||||||
|
return sprintf(
|
||||||
|
self::getType() . ' (%s+%s)',
|
||||||
|
Utilities::entityToString($this->Value),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the value of A
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getValue(): mixed
|
||||||
|
{
|
||||||
|
return $this->Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the value of A
|
||||||
|
*
|
||||||
|
* @param mixed $Value
|
||||||
|
* @throws InstructionException
|
||||||
|
*/
|
||||||
|
public function setValue(mixed $Value): void
|
||||||
|
{
|
||||||
|
$this->Value = InstructionBuilder::fromRaw($Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -2,14 +2,15 @@
|
||||||
|
|
||||||
/** @noinspection PhpMissingFieldTypeInspection */
|
/** @noinspection PhpMissingFieldTypeInspection */
|
||||||
|
|
||||||
namespace RTEX\Objects\Program\Instructions;
|
namespace RTEX\Objects\Program\Instructions\Arithmetic;
|
||||||
|
|
||||||
use RTEX\Abstracts\InstructionType;
|
use RTEX\Abstracts\InstructionType;
|
||||||
use RTEX\Classes\InstructionBuilder;
|
use RTEX\Classes\InstructionBuilder;
|
||||||
use RTEX\Classes\Utilities;
|
use RTEX\Classes\Utilities;
|
||||||
use RTEX\Engine;
|
use RTEX\Engine;
|
||||||
use RTEX\Exceptions\Core\MalformedInstructionException;
|
use RTEX\Exceptions\EvaluationException;
|
||||||
use RTEX\Exceptions\Core\UnsupportedVariableType;
|
use RTEX\Exceptions\InstructionException;
|
||||||
|
use RTEX\Exceptions\Runtime\TypeException;
|
||||||
use RTEX\Interfaces\InstructionInterface;
|
use RTEX\Interfaces\InstructionInterface;
|
||||||
|
|
||||||
class Subtract implements InstructionInterface
|
class Subtract implements InstructionInterface
|
||||||
|
@ -38,7 +39,6 @@
|
||||||
* Returns an array representation of the instruction
|
* Returns an array representation of the instruction
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
* @throws UnsupportedVariableType
|
|
||||||
*/
|
*/
|
||||||
public function toArray(): array
|
public function toArray(): array
|
||||||
{
|
{
|
||||||
|
@ -53,8 +53,7 @@
|
||||||
*
|
*
|
||||||
* @param array $data
|
* @param array $data
|
||||||
* @return InstructionInterface
|
* @return InstructionInterface
|
||||||
* @throws MalformedInstructionException
|
* @throws InstructionException
|
||||||
* @throws UnsupportedVariableType
|
|
||||||
*/
|
*/
|
||||||
public static function fromArray(array $data): InstructionInterface
|
public static function fromArray(array $data): InstructionInterface
|
||||||
{
|
{
|
||||||
|
@ -66,19 +65,27 @@
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Engine $engine
|
* @param Engine $engine
|
||||||
* @return int
|
* @return int|float
|
||||||
* @throws UnsupportedVariableType
|
* @throws EvaluationException
|
||||||
|
* @throws TypeException
|
||||||
*/
|
*/
|
||||||
public function eval(Engine $engine): int
|
public function eval(Engine $engine): int|float
|
||||||
{
|
{
|
||||||
return (intval($engine->eval($this->A)) - intval($engine->eval($this->B)));
|
$a = $engine->eval($this->A);
|
||||||
|
$b = $engine->eval($this->B);
|
||||||
|
|
||||||
|
if(!(is_int($a) || is_float($a) || is_double($a)))
|
||||||
|
throw new TypeException(sprintf('Cannot subtract a non-numeric value of \'A\' of type \'%s\'', Utilities::getType($a, true)));
|
||||||
|
if(!(is_int($b) || is_float($b) || is_double($b)))
|
||||||
|
throw new TypeException(sprintf('Cannot subtract a non-numeric value of \'B\' of type \'%s\'', Utilities::getType($b, true)));
|
||||||
|
|
||||||
|
return ($a - $b);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the string representation of the instruction
|
* Returns the string representation of the instruction
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
* @throws UnsupportedVariableType
|
|
||||||
*/
|
*/
|
||||||
public function __toString(): string
|
public function __toString(): string
|
||||||
{
|
{
|
||||||
|
@ -103,8 +110,7 @@
|
||||||
* Sets the value of A
|
* Sets the value of A
|
||||||
*
|
*
|
||||||
* @param mixed $A
|
* @param mixed $A
|
||||||
* @throws UnsupportedVariableType
|
* @throws InstructionException
|
||||||
* @throws MalformedInstructionException
|
|
||||||
*/
|
*/
|
||||||
public function setA(mixed $A): void
|
public function setA(mixed $A): void
|
||||||
{
|
{
|
||||||
|
@ -125,8 +131,7 @@
|
||||||
* Sets the value of B
|
* Sets the value of B
|
||||||
*
|
*
|
||||||
* @param mixed $B
|
* @param mixed $B
|
||||||
* @throws MalformedInstructionException
|
* @throws InstructionException
|
||||||
* @throws UnsupportedVariableType
|
|
||||||
*/
|
*/
|
||||||
public function setB(mixed $B): void
|
public function setB(mixed $B): void
|
||||||
{
|
{
|
|
@ -2,14 +2,15 @@
|
||||||
|
|
||||||
/** @noinspection PhpMissingFieldTypeInspection */
|
/** @noinspection PhpMissingFieldTypeInspection */
|
||||||
|
|
||||||
namespace RTEX\Objects\Program\Instructions;
|
namespace RTEX\Objects\Program\Instructions\Arithmetic;
|
||||||
|
|
||||||
use RTEX\Abstracts\InstructionType;
|
use RTEX\Abstracts\InstructionType;
|
||||||
use RTEX\Classes\InstructionBuilder;
|
use RTEX\Classes\InstructionBuilder;
|
||||||
use RTEX\Classes\Utilities;
|
use RTEX\Classes\Utilities;
|
||||||
use RTEX\Engine;
|
use RTEX\Engine;
|
||||||
use RTEX\Exceptions\Core\MalformedInstructionException;
|
use RTEX\Exceptions\EvaluationException;
|
||||||
use RTEX\Exceptions\Core\UnsupportedVariableType;
|
use RTEX\Exceptions\InstructionException;
|
||||||
|
use RTEX\Exceptions\Runtime\TypeException;
|
||||||
use RTEX\Interfaces\InstructionInterface;
|
use RTEX\Interfaces\InstructionInterface;
|
||||||
|
|
||||||
class Sum implements InstructionInterface
|
class Sum implements InstructionInterface
|
||||||
|
@ -38,7 +39,6 @@
|
||||||
* Returns an array representation of the instruction
|
* Returns an array representation of the instruction
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
* @throws UnsupportedVariableType
|
|
||||||
*/
|
*/
|
||||||
public function toArray(): array
|
public function toArray(): array
|
||||||
{
|
{
|
||||||
|
@ -53,8 +53,7 @@
|
||||||
*
|
*
|
||||||
* @param array $data
|
* @param array $data
|
||||||
* @return InstructionInterface
|
* @return InstructionInterface
|
||||||
* @throws MalformedInstructionException
|
* @throws InstructionException
|
||||||
* @throws UnsupportedVariableType
|
|
||||||
*/
|
*/
|
||||||
public static function fromArray(array $data): InstructionInterface
|
public static function fromArray(array $data): InstructionInterface
|
||||||
{
|
{
|
||||||
|
@ -66,19 +65,27 @@
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Engine $engine
|
* @param Engine $engine
|
||||||
* @return int
|
* @return int|float
|
||||||
* @throws UnsupportedVariableType
|
* @throws EvaluationException
|
||||||
|
* @throws TypeException
|
||||||
*/
|
*/
|
||||||
public function eval(Engine $engine): int
|
public function eval(Engine $engine): int|float
|
||||||
{
|
{
|
||||||
return (intval($engine->eval($this->A)) + intval($engine->eval($this->B)));
|
$a = $engine->eval($this->A);
|
||||||
|
$b = $engine->eval($this->B);
|
||||||
|
|
||||||
|
if(!(is_int($a) || is_float($a) || is_double($a)))
|
||||||
|
throw new TypeException(sprintf('Cannot sum a non-numeric value of \'A\' of type %s', Utilities::getType($a, true)));
|
||||||
|
if(!(is_int($b) || is_float($b) || is_double($b)))
|
||||||
|
throw new TypeException(sprintf('Cannot sum a non-numeric value of \'B\' of type %s', Utilities::getType($b, true)));
|
||||||
|
|
||||||
|
return ($a + $b);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the string representation of the instruction
|
* Returns the string representation of the instruction
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
* @throws UnsupportedVariableType
|
|
||||||
*/
|
*/
|
||||||
public function __toString(): string
|
public function __toString(): string
|
||||||
{
|
{
|
||||||
|
@ -103,8 +110,7 @@
|
||||||
* Sets the value of A
|
* Sets the value of A
|
||||||
*
|
*
|
||||||
* @param mixed $A
|
* @param mixed $A
|
||||||
* @throws UnsupportedVariableType
|
* @throws InstructionException
|
||||||
* @throws MalformedInstructionException
|
|
||||||
*/
|
*/
|
||||||
public function setA(mixed $A): void
|
public function setA(mixed $A): void
|
||||||
{
|
{
|
||||||
|
@ -125,8 +131,7 @@
|
||||||
* Sets the value of B
|
* Sets the value of B
|
||||||
*
|
*
|
||||||
* @param mixed $B
|
* @param mixed $B
|
||||||
* @throws MalformedInstructionException
|
* @throws InstructionException
|
||||||
* @throws UnsupportedVariableType
|
|
||||||
*/
|
*/
|
||||||
public function setB(mixed $B): void
|
public function setB(mixed $B): void
|
||||||
{
|
{
|
Loading…
Add table
Reference in a new issue