Refactored Comparators instructions and added documentation
This commit is contained in:
parent
f071d1dc72
commit
671958063c
12 changed files with 321 additions and 71 deletions
|
@ -2,7 +2,7 @@
|
|||
|
||||
/** @noinspection PhpMissingFieldTypeInspection */
|
||||
|
||||
namespace RTEX\Objects\Program\Instructions;
|
||||
namespace RTEX\Objects\Program\Instructions\Comparators;
|
||||
|
||||
use RTEX\Abstracts\InstructionType;
|
||||
use RTEX\Classes\InstructionBuilder;
|
||||
|
@ -83,7 +83,7 @@
|
|||
public function __toString(): string
|
||||
{
|
||||
return sprintf(
|
||||
self::getType() . ' (%s==%s)',
|
||||
self::getType() . ' %s == %s',
|
||||
Utilities::entityToString($this->A),
|
||||
Utilities::entityToString($this->B)
|
||||
);
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
/** @noinspection PhpMissingFieldTypeInspection */
|
||||
|
||||
namespace RTEX\Objects\Program\Instructions;
|
||||
namespace RTEX\Objects\Program\Instructions\Comparators;
|
||||
|
||||
use RTEX\Abstracts\InstructionType;
|
||||
use RTEX\Classes\InstructionBuilder;
|
||||
|
@ -65,21 +65,22 @@
|
|||
|
||||
/**
|
||||
* @param Engine $engine
|
||||
* @return int
|
||||
* @return bool
|
||||
* @throws EvaluationException
|
||||
* @throws TypeException
|
||||
*/
|
||||
public function eval(Engine $engine): int
|
||||
public function eval(Engine $engine): bool
|
||||
{
|
||||
/** @noinspection DuplicatedCode */
|
||||
$a = $engine->eval($this->A);
|
||||
$b = $engine->eval($this->B);
|
||||
|
||||
if (!is_numeric($a))
|
||||
throw new TypeException(sprintf('Parameter "a" must be numeric, %s given', Utilities::getType($a)));
|
||||
if (!is_numeric($b))
|
||||
throw new TypeException(sprintf('Parameter "b" must be numeric, %s given', Utilities::getType($b)));
|
||||
if (!(is_int($a) || is_float($a)) || is_double($a))
|
||||
throw new TypeException(sprintf('Cannot compare 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 compare a non-numeric value \'B\' of type \'%s\'', Utilities::getType($b, true)));
|
||||
|
||||
return (intval($engine->eval($this->A)) > intval($engine->eval($this->B)));
|
||||
return ($a > $b);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -90,7 +91,7 @@
|
|||
public function __toString(): string
|
||||
{
|
||||
return sprintf(
|
||||
self::getType() . ' (%s>%s)',
|
||||
self::getType() . ' %s > %s',
|
||||
Utilities::entityToString($this->A),
|
||||
Utilities::entityToString($this->B)
|
||||
);
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
/** @noinspection PhpMissingFieldTypeInspection */
|
||||
|
||||
namespace RTEX\Objects\Program\Instructions;
|
||||
namespace RTEX\Objects\Program\Instructions\Comparators;
|
||||
|
||||
use RTEX\Abstracts\InstructionType;
|
||||
use RTEX\Classes\InstructionBuilder;
|
||||
|
@ -65,21 +65,22 @@
|
|||
|
||||
/**
|
||||
* @param Engine $engine
|
||||
* @return int
|
||||
* @throws TypeException
|
||||
* @return bool
|
||||
* @throws EvaluationException
|
||||
* @throws TypeException
|
||||
*/
|
||||
public function eval(Engine $engine): int
|
||||
public function eval(Engine $engine): bool
|
||||
{
|
||||
/** @noinspection DuplicatedCode */
|
||||
$a = $engine->eval($this->A);
|
||||
$b = $engine->eval($this->B);
|
||||
|
||||
if (!is_numeric($a))
|
||||
throw new TypeException(sprintf('Parameter "a" must be numeric, %s given', Utilities::getType($a)));
|
||||
if (!is_numeric($b))
|
||||
throw new TypeException(sprintf('Parameter "b" must be numeric, %s given', Utilities::getType($b)));
|
||||
if (!(is_int($a) || is_float($a)) || is_double($a))
|
||||
throw new TypeException(sprintf('Cannot compare 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 compare a non-numeric value \'B\' of type \'%s\'', Utilities::getType($b, true)));
|
||||
|
||||
return (intval($engine->eval($this->A)) >= intval($engine->eval($this->B)));
|
||||
return ($a >= $b);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -90,7 +91,7 @@
|
|||
public function __toString(): string
|
||||
{
|
||||
return sprintf(
|
||||
self::getType() . ' (%s>=%s)',
|
||||
self::getType() . ' %s >= %s',
|
||||
Utilities::entityToString($this->A),
|
||||
Utilities::entityToString($this->B)
|
||||
);
|
141
src/RTEX/Objects/Program/Instructions/Comparators/LessThan.php
Normal file
141
src/RTEX/Objects/Program/Instructions/Comparators/LessThan.php
Normal file
|
@ -0,0 +1,141 @@
|
|||
<?php
|
||||
|
||||
/** @noinspection PhpMissingFieldTypeInspection */
|
||||
|
||||
namespace RTEX\Objects\Program\Instructions\Comparators;
|
||||
|
||||
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 LessThan implements InstructionInterface
|
||||
{
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
private $A;
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
private $B;
|
||||
|
||||
/**
|
||||
* Returns the type of instruction
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getType(): string
|
||||
{
|
||||
return InstructionType::LessThan;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @throws InstructionException
|
||||
*/
|
||||
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 bool
|
||||
* @throws EvaluationException
|
||||
* @throws TypeException
|
||||
*/
|
||||
public function eval(Engine $engine): bool
|
||||
{
|
||||
/** @noinspection DuplicatedCode */
|
||||
$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 compare 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 compare a non-numeric value \'B\' of type \'%s\'', Utilities::getType($b, true)));
|
||||
|
||||
return ($a < $b);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @throws InstructionException
|
||||
*/
|
||||
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
|
||||
* @throws InstructionException
|
||||
*/
|
||||
public function setB(mixed $B): void
|
||||
{
|
||||
$this->B = InstructionBuilder::fromRaw($B);
|
||||
}
|
||||
}
|
|
@ -2,14 +2,15 @@
|
|||
|
||||
/** @noinspection PhpMissingFieldTypeInspection */
|
||||
|
||||
namespace RTEX\Objects\Program\Instructions;
|
||||
namespace RTEX\Objects\Program\Instructions\Comparators;
|
||||
|
||||
use RTEX\Abstracts\InstructionType;
|
||||
use RTEX\Classes\InstructionBuilder;
|
||||
use RTEX\Classes\Utilities;
|
||||
use RTEX\Engine;
|
||||
use RTEX\Exceptions\Core\MalformedInstructionException;
|
||||
use RTEX\Exceptions\Core\UnsupportedVariableType;
|
||||
use RTEX\Exceptions\EvaluationException;
|
||||
use RTEX\Exceptions\InstructionException;
|
||||
use RTEX\Exceptions\Runtime\TypeException;
|
||||
use RTEX\Interfaces\InstructionInterface;
|
||||
|
||||
class LessThanOrEquals implements InstructionInterface
|
||||
|
@ -31,14 +32,13 @@
|
|||
*/
|
||||
public function getType(): string
|
||||
{
|
||||
return InstructionType::Divide;
|
||||
return InstructionType::LessThanOrEquals;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array representation of the instruction
|
||||
*
|
||||
* @return array
|
||||
* @throws UnsupportedVariableType
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
|
@ -53,8 +53,7 @@
|
|||
*
|
||||
* @param array $data
|
||||
* @return InstructionInterface
|
||||
* @throws MalformedInstructionException
|
||||
* @throws UnsupportedVariableType
|
||||
* @throws InstructionException
|
||||
*/
|
||||
public static function fromArray(array $data): InstructionInterface
|
||||
{
|
||||
|
@ -66,24 +65,33 @@
|
|||
|
||||
/**
|
||||
* @param Engine $engine
|
||||
* @return int
|
||||
* @throws UnsupportedVariableType
|
||||
* @return bool
|
||||
* @throws EvaluationException
|
||||
* @throws TypeException
|
||||
*/
|
||||
public function eval(Engine $engine): int
|
||||
public function eval(Engine $engine): bool
|
||||
{
|
||||
return (intval($engine->eval($this->A)) <= intval($engine->eval($this->B)));
|
||||
/** @noinspection DuplicatedCode */
|
||||
$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 compare 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 compare a non-numeric value \'B\' of type \'%s\'', Utilities::getType($b, true)));
|
||||
|
||||
return ($a <= $b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the string representation of the instruction
|
||||
*
|
||||
* @return string
|
||||
* @throws UnsupportedVariableType
|
||||
*/
|
||||
public function __toString(): string
|
||||
{
|
||||
return sprintf(
|
||||
self::getType() . ' (%s<=%s)',
|
||||
self::getType() . ' %s <= %s',
|
||||
Utilities::entityToString($this->A),
|
||||
Utilities::entityToString($this->B)
|
||||
);
|
||||
|
@ -103,8 +111,7 @@
|
|||
* Sets the value of A
|
||||
*
|
||||
* @param mixed $A
|
||||
* @throws UnsupportedVariableType
|
||||
* @throws MalformedInstructionException
|
||||
* @throws InstructionException
|
||||
*/
|
||||
public function setA(mixed $A): void
|
||||
{
|
||||
|
@ -125,8 +132,7 @@
|
|||
* Sets the value of B
|
||||
*
|
||||
* @param mixed $B
|
||||
* @throws MalformedInstructionException
|
||||
* @throws UnsupportedVariableType
|
||||
* @throws InstructionException
|
||||
*/
|
||||
public function setB(mixed $B): void
|
||||
{
|
|
@ -2,17 +2,17 @@
|
|||
|
||||
/** @noinspection PhpMissingFieldTypeInspection */
|
||||
|
||||
namespace RTEX\Objects\Program\Instructions;
|
||||
namespace RTEX\Objects\Program\Instructions\Comparators;
|
||||
|
||||
use RTEX\Abstracts\InstructionType;
|
||||
use RTEX\Classes\InstructionBuilder;
|
||||
use RTEX\Classes\Utilities;
|
||||
use RTEX\Engine;
|
||||
use RTEX\Exceptions\Core\MalformedInstructionException;
|
||||
use RTEX\Exceptions\Core\UnsupportedVariableType;
|
||||
use RTEX\Exceptions\EvaluationException;
|
||||
use RTEX\Exceptions\InstructionException;
|
||||
use RTEX\Interfaces\InstructionInterface;
|
||||
|
||||
class LessThan implements InstructionInterface
|
||||
class NotEquals implements InstructionInterface
|
||||
{
|
||||
/**
|
||||
* @var mixed
|
||||
|
@ -31,14 +31,13 @@
|
|||
*/
|
||||
public function getType(): string
|
||||
{
|
||||
return InstructionType::LessThan;
|
||||
return InstructionType::NotEquals;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array representation of the instruction
|
||||
*
|
||||
* @return array
|
||||
* @throws UnsupportedVariableType
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
|
@ -53,8 +52,7 @@
|
|||
*
|
||||
* @param array $data
|
||||
* @return InstructionInterface
|
||||
* @throws MalformedInstructionException
|
||||
* @throws UnsupportedVariableType
|
||||
* @throws InstructionException
|
||||
*/
|
||||
public static function fromArray(array $data): InstructionInterface
|
||||
{
|
||||
|
@ -66,24 +64,26 @@
|
|||
|
||||
/**
|
||||
* @param Engine $engine
|
||||
* @return int
|
||||
* @throws UnsupportedVariableType
|
||||
* @return bool
|
||||
* @throws EvaluationException
|
||||
*/
|
||||
public function eval(Engine $engine): int
|
||||
public function eval(Engine $engine): bool
|
||||
{
|
||||
return (intval($engine->eval($this->A)) < intval($engine->eval($this->B)));
|
||||
$a = $engine->eval($this->A);
|
||||
$b = $engine->eval($this->B);
|
||||
|
||||
return ($a !== $b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the string representation of the instruction
|
||||
*
|
||||
* @return string
|
||||
* @throws UnsupportedVariableType
|
||||
*/
|
||||
public function __toString(): string
|
||||
{
|
||||
return sprintf(
|
||||
self::getType() . ' (%s<%s)',
|
||||
self::getType() . ' $s !== $s',
|
||||
Utilities::entityToString($this->A),
|
||||
Utilities::entityToString($this->B)
|
||||
);
|
||||
|
@ -103,8 +103,7 @@
|
|||
* Sets the value of A
|
||||
*
|
||||
* @param mixed $A
|
||||
* @throws UnsupportedVariableType
|
||||
* @throws MalformedInstructionException
|
||||
* @throws InstructionException
|
||||
*/
|
||||
public function setA(mixed $A): void
|
||||
{
|
||||
|
@ -125,8 +124,7 @@
|
|||
* Sets the value of B
|
||||
*
|
||||
* @param mixed $B
|
||||
* @throws MalformedInstructionException
|
||||
* @throws UnsupportedVariableType
|
||||
* @throws InstructionException
|
||||
*/
|
||||
public function setB(mixed $B): void
|
||||
{
|
Loading…
Add table
Add a link
Reference in a new issue