diff --git a/docs/instructions/equals.md b/docs/instructions/comparators/equals.md similarity index 65% rename from docs/instructions/equals.md rename to docs/instructions/comparators/equals.md index 1317c6c..205623f 100644 --- a/docs/instructions/equals.md +++ b/docs/instructions/comparators/equals.md @@ -1,11 +1,11 @@ -# equals +# eq Returns true if the two values are equal, false otherwise. ## Parameters -* a (`integer`, `float`, `double`, `string`, `boolean`, `instruction`) - The first value to compare. -* b (`integer`, `float`, `double`, `string`, `boolean`, `instruction`) - The second value to compare. +* a (`integer`, `float`, `double`, `string`, `boolean`) - The first value to compare. +* b (`integer`, `float`, `double`, `string`, `boolean`) - The second value to compare. ## Return @@ -21,7 +21,7 @@ Returns true if the two values are equal, false otherwise. ```json { - "type": "equals", + "type": "eq", "_": { "a": "foo", "b": "foo" @@ -31,5 +31,5 @@ Returns true if the two values are equal, false otherwise. ### Last Updated -Monday, December 29th, 2022. +Monday, December 30th, 2022. Written by [Netkas](https://git.n64.cc/netkas) \ No newline at end of file diff --git a/docs/instructions/greater_than.md b/docs/instructions/comparators/greater_than.md similarity index 70% rename from docs/instructions/greater_than.md rename to docs/instructions/comparators/greater_than.md index b23bfd1..accf9ef 100644 --- a/docs/instructions/greater_than.md +++ b/docs/instructions/comparators/greater_than.md @@ -1,11 +1,11 @@ -# greater_than +# gt Returns true if the first argument is greater than the second argument. ## Parameters -* a (`integer`, `float`, `double`, `instruction`) - The first number. -* b (`integer`, `float`, `double`, `instruction`) - The second number. +* a (`integer`, `float`, `double`) - The first number. +* b (`integer`, `float`, `double`) - The second number. ## Return @@ -20,7 +20,7 @@ Returns true if the first argument is greater than the second argument. ```json { - "type": "greater_than", + "type": "gt", "_": { "a": 10, "b": 2 @@ -30,5 +30,5 @@ Returns true if the first argument is greater than the second argument. ### Last Updated -Monday, December 29th, 2022. +Monday, December 30th, 2022. Written by [Netkas](https://git.n64.cc/netkas) \ No newline at end of file diff --git a/docs/instructions/greater_than_or_equals.md b/docs/instructions/comparators/greater_than_or_equals.md similarity index 69% rename from docs/instructions/greater_than_or_equals.md rename to docs/instructions/comparators/greater_than_or_equals.md index 1af3225..fb48e26 100644 --- a/docs/instructions/greater_than_or_equals.md +++ b/docs/instructions/comparators/greater_than_or_equals.md @@ -1,11 +1,11 @@ -# greater_than_or_equals +# gte Returns true if the first argument is greater than or equal to the second argument. ## Parameters -* a (`integer`, `float`, `double`, `instruction`) - The first number. -* b (`integer`, `float`, `double`, `instruction`) - The second number. +* a (`integer`, `float`, `double`) - The first number. +* b (`integer`, `float`, `double`) - The second number. ## Return @@ -20,7 +20,7 @@ Returns true if the first argument is greater than or equal to the second argume ```json { - "type": "greater_than_or_equals", + "type": "gte", "_": { "a": 10, "b": 2 @@ -30,5 +30,5 @@ Returns true if the first argument is greater than or equal to the second argume ### Last Updated -Monday, December 29th, 2022. +Monday, December 30th, 2022. Written by [Netkas](https://git.n64.cc/netkas) \ No newline at end of file diff --git a/docs/instructions/comparators/less_than.md b/docs/instructions/comparators/less_than.md new file mode 100644 index 0000000..89c66bf --- /dev/null +++ b/docs/instructions/comparators/less_than.md @@ -0,0 +1,34 @@ +# lt + +Returns true if the first argument is less than the second argument. + +## Parameters + +* a (`integer`, `float`, `double`) - The first number. +* b (`integer`, `float`, `double`) - The second number. + +## Return + +(`boolean`) - True if the first argument is less than the second argument. + +## Exceptions + +* `EvaluationException` - If there was an error while evaluating one or more parameters. +* `TypeException` - If one or more parameters are not of the expected type. + +## Instruction Example + +```json +{ + "type": "lt", + "_": { + "a": 10, + "b": 2 + } +} +``` + +### Last Updated + +Monday, December 30th, 2022. +Written by [Netkas](https://git.n64.cc/netkas) \ No newline at end of file diff --git a/docs/instructions/comparators/less_than_or_equals.md b/docs/instructions/comparators/less_than_or_equals.md new file mode 100644 index 0000000..449721d --- /dev/null +++ b/docs/instructions/comparators/less_than_or_equals.md @@ -0,0 +1,34 @@ +# lte + +Returns true if the first argument is less than or equal to the second argument. + +## Parameters + +* a (`integer`, `float`, `double`) - The first number. +* b (`integer`, `float`, `double`) - The second number. + +## Return + +(`boolean`) - True if the first argument is less than or equal to the second argument. + +## Exceptions + +* `EvaluationException` - If there was an error while evaluating one or more parameters. +* `TypeException` - If one or more parameters are not of the expected type. + +## Instruction Example + +```json +{ + "type": "lte", + "_": { + "a": 10, + "b": 2 + } +} +``` + +### Last Updated + +Monday, December 30th, 2022. +Written by [Netkas](https://git.n64.cc/netkas) \ No newline at end of file diff --git a/docs/instructions/comparators/not_equals.md b/docs/instructions/comparators/not_equals.md new file mode 100644 index 0000000..41f9010 --- /dev/null +++ b/docs/instructions/comparators/not_equals.md @@ -0,0 +1,35 @@ +# neq + +Returns true if the first argument is not equal to the second argument. + +## Parameters + +* a (`integer`, `float`, `double`, `string`, `boolean`) - The first value to compare. +* b (`integer`, `float`, `double`, `string`, `boolean`) - The second value to compare. + +## Return + +(`boolean`) - True if the first argument is not equal to the second argument. + +## Exceptions + +* `EvaluationException` - If there was an error while evaluating one or more parameters. +* `TypeException` - If one or more parameters are not of the expected type. + + +## Instruction Example + +```json +{ + "type": "neq", + "_": { + "a": "foo", + "b": "foo" + } +} +``` + +### Last Updated + +Monday, December 30th, 2022. +Written by [Netkas](https://git.n64.cc/netkas) \ No newline at end of file diff --git a/src/RTEX/Objects/Program/Instructions/Equals.php b/src/RTEX/Objects/Program/Instructions/Comparators/Equals.php similarity index 96% rename from src/RTEX/Objects/Program/Instructions/Equals.php rename to src/RTEX/Objects/Program/Instructions/Comparators/Equals.php index 97a2ac5..483ce70 100644 --- a/src/RTEX/Objects/Program/Instructions/Equals.php +++ b/src/RTEX/Objects/Program/Instructions/Comparators/Equals.php @@ -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) ); diff --git a/src/RTEX/Objects/Program/Instructions/GreaterThan.php b/src/RTEX/Objects/Program/Instructions/Comparators/GreaterThan.php similarity index 82% rename from src/RTEX/Objects/Program/Instructions/GreaterThan.php rename to src/RTEX/Objects/Program/Instructions/Comparators/GreaterThan.php index a3ab6aa..6cb83a8 100644 --- a/src/RTEX/Objects/Program/Instructions/GreaterThan.php +++ b/src/RTEX/Objects/Program/Instructions/Comparators/GreaterThan.php @@ -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) ); diff --git a/src/RTEX/Objects/Program/Instructions/GreaterThanOrEquals.php b/src/RTEX/Objects/Program/Instructions/Comparators/GreaterThanOrEquals.php similarity index 82% rename from src/RTEX/Objects/Program/Instructions/GreaterThanOrEquals.php rename to src/RTEX/Objects/Program/Instructions/Comparators/GreaterThanOrEquals.php index cfa9bd8..222dccc 100644 --- a/src/RTEX/Objects/Program/Instructions/GreaterThanOrEquals.php +++ b/src/RTEX/Objects/Program/Instructions/Comparators/GreaterThanOrEquals.php @@ -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) ); diff --git a/src/RTEX/Objects/Program/Instructions/Comparators/LessThan.php b/src/RTEX/Objects/Program/Instructions/Comparators/LessThan.php new file mode 100644 index 0000000..55dd729 --- /dev/null +++ b/src/RTEX/Objects/Program/Instructions/Comparators/LessThan.php @@ -0,0 +1,141 @@ + $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); + } + } \ No newline at end of file diff --git a/src/RTEX/Objects/Program/Instructions/LessThanOrEquals.php b/src/RTEX/Objects/Program/Instructions/Comparators/LessThanOrEquals.php similarity index 69% rename from src/RTEX/Objects/Program/Instructions/LessThanOrEquals.php rename to src/RTEX/Objects/Program/Instructions/Comparators/LessThanOrEquals.php index 7a50de1..7a26f8a 100644 --- a/src/RTEX/Objects/Program/Instructions/LessThanOrEquals.php +++ b/src/RTEX/Objects/Program/Instructions/Comparators/LessThanOrEquals.php @@ -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 { diff --git a/src/RTEX/Objects/Program/Instructions/LessThan.php b/src/RTEX/Objects/Program/Instructions/Comparators/NotEquals.php similarity index 74% rename from src/RTEX/Objects/Program/Instructions/LessThan.php rename to src/RTEX/Objects/Program/Instructions/Comparators/NotEquals.php index 8ae09b8..1c2109f 100644 --- a/src/RTEX/Objects/Program/Instructions/LessThan.php +++ b/src/RTEX/Objects/Program/Instructions/Comparators/NotEquals.php @@ -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 {