diff --git a/src/RTEX/Objects/Program/Instructions/ArrayGet.php b/src/RTEX/Objects/Program/Instructions/ArrayGet.php new file mode 100644 index 0000000..21622e6 --- /dev/null +++ b/src/RTEX/Objects/Program/Instructions/ArrayGet.php @@ -0,0 +1,150 @@ +Array; + } + + /** + * @param mixed $variable + * @throws UnsupportedVariableType + * @throws MalformedInstructionException + * @noinspection PhpMissingParamTypeInspection + */ + public function setArray($variable): void + { + $this->Array = InstructionBuilder::fromRaw($variable); + } + + /** + * @return mixed + * @noinspection PhpMissingReturnTypeInspection + */ + public function getValue() + { + return $this->Value; + } + + /** + * @param mixed $value + * @throws MalformedInstructionException + * @throws UnsupportedVariableType + * @noinspection PhpMissingParamTypeInspection + */ + public function setValue($value): void + { + $this->Value = InstructionBuilder::fromRaw($value); + } + + /** + * Returns an array representation of the object + * + * @return array + * @throws UnsupportedVariableType + */ + public function toArray(): array + { + return InstructionBuilder::toRaw(self::getType(), [ + 'array' => $this->Array, + 'value' => $this->Value + ]); + } + + /** + * @param array $data + * @return InstructionInterface + * @throws MalformedInstructionException + * @throws UnsupportedVariableType + */ + public static function fromArray(array $data): InstructionInterface + { + $instruction = new self(); + $instruction->setArray($data['array'] ?? null); + $instruction->setValue($data['value'] ?? null); + + return $instruction; + } + + /** + * @param Engine $engine + * @return void + * @throws UnsupportedVariableType + */ + public function eval(Engine $engine) + { + + $queryParts = explode('.', $engine->eval($this->Value)); + $value = $engine->getEnvironment()->getRuntimeVariable($engine->eval($this->Array)); + + foreach ($queryParts as $queryPart) + { + if (is_array($value) && array_key_exists($queryPart, $value)) + { + $value = $value[$queryPart]; + } + else + { + return null; + } + } + + return $value; + } + + /** + * @inheritDoc + * @throws UnsupportedVariableType + */ + public function __toString(): string + { + return sprintf( + self::getType() . ' (array: %s, value: %s)', + Utilities::entityToString($this->Array), + Utilities::entityToString($this->Value) + ); + } + } \ No newline at end of file diff --git a/src/RTEX/Objects/Program/Instructions/Divide.php b/src/RTEX/Objects/Program/Instructions/Divide.php new file mode 100644 index 0000000..9ec79ba --- /dev/null +++ b/src/RTEX/Objects/Program/Instructions/Divide.php @@ -0,0 +1,135 @@ + $this->A, + 'b' => $this->B + ]); + } + + /** + * Constructs a new instance of this class from an array representation + * + * @param array $data + * @return InstructionInterface + * @throws MalformedInstructionException + * @throws UnsupportedVariableType + */ + 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 + * @throws UnsupportedVariableType + */ + public function eval(Engine $engine): int + { + return (intval($engine->eval($this->A)) / intval($engine->eval($this->B))); + } + + /** + * Returns the string representation of the instruction + * + * @return string + * @throws UnsupportedVariableType + */ + 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 UnsupportedVariableType + * @throws MalformedInstructionException + */ + 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 MalformedInstructionException + * @throws UnsupportedVariableType + */ + 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/Equals.php b/src/RTEX/Objects/Program/Instructions/Equals.php new file mode 100644 index 0000000..afc0f7e --- /dev/null +++ b/src/RTEX/Objects/Program/Instructions/Equals.php @@ -0,0 +1,135 @@ + $this->A, + 'b' => $this->B + ]); + } + + /** + * Constructs a new instance of this class from an array representation + * + * @param array $data + * @return InstructionInterface + * @throws MalformedInstructionException + * @throws UnsupportedVariableType + */ + 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 UnsupportedVariableType + */ + public function eval(Engine $engine): bool + { + return (intval($engine->eval($this->A)) == intval($engine->eval($this->B))); + } + + /** + * Returns the string representation of the instruction + * + * @return string + * @throws UnsupportedVariableType + */ + 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 UnsupportedVariableType + * @throws MalformedInstructionException + */ + 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 MalformedInstructionException + * @throws UnsupportedVariableType + */ + 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/GetVariable.php b/src/RTEX/Objects/Program/Instructions/GetVariable.php index e7b3e0c..f7583ca 100644 --- a/src/RTEX/Objects/Program/Instructions/GetVariable.php +++ b/src/RTEX/Objects/Program/Instructions/GetVariable.php @@ -1,10 +1,14 @@ $this->getType(), - '_' => [ - 'variable' => Utilities::toArray($this->Variable) - ] - ]; - } - /** * Returns * - * @return bool|float|int|InstructionInterface|string|null + * @return mixed * @noinspection PhpMissingReturnTypeInspection + * @noinspection PhpUnused */ public function getVariable() { @@ -56,34 +45,19 @@ } /** - * @param bool|float|int|InstructionInterface|string|null $variable + * @param mixed $variable + * @throws MalformedInstructionException * @throws UnsupportedVariableType + * @noinspection PhpMissingParamTypeInspection */ public function setVariable($variable): void { - switch(Utilities::determineType($variable)) - { - - - default: - $this->Variable = $variable; - } - } - - /** - * Constructs a new GetVariable instruction from an array representation - * - * @throws UnsupportedVariableType - */ - public static function fromArray(array $data): InstructionInterface - { - $instruction = new GetVariable(); - $instruction->setVariable($data['variable'] ?? null); - return $instruction; + $this->Variable = InstructionBuilder::fromRaw($variable); } /** * @inheritDoc + * @throws UnsupportedVariableType */ public function eval(Engine $engine) { @@ -91,4 +65,45 @@ $engine->eval($this->Variable) ); } + + /** + * Returns an array representation of the object + * + * @return array + * @throws UnsupportedVariableType + */ + public function toArray(): array + { + return InstructionBuilder::toRaw(self::getType(), [ + 'variable' => $this->Variable + ]); + } + + /** + * Constructs a new GetVariable instruction from an array representation + * + * @param array $data + * @return InstructionInterface + * @throws MalformedInstructionException + * @throws UnsupportedVariableType + */ + public static function fromArray(array $data): InstructionInterface + { + $instruction = new self(); + $instruction->setVariable($data['variable'] ?? null); + + return $instruction; + } + + /** + * @inheritDoc + * @throws UnsupportedVariableType + */ + public function __toString(): string + { + return sprintf( + self::getType() . ' (variable: %s)', + Utilities::entityToString($this->Variable) + ); + } } \ No newline at end of file diff --git a/src/RTEX/Objects/Program/Instructions/GreaterThan.php b/src/RTEX/Objects/Program/Instructions/GreaterThan.php new file mode 100644 index 0000000..382dd51 --- /dev/null +++ b/src/RTEX/Objects/Program/Instructions/GreaterThan.php @@ -0,0 +1,135 @@ + $this->A, + 'b' => $this->B + ]); + } + + /** + * Constructs a new instance of this class from an array representation + * + * @param array $data + * @return InstructionInterface + * @throws MalformedInstructionException + * @throws UnsupportedVariableType + */ + 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 + * @throws UnsupportedVariableType + */ + public function eval(Engine $engine): int + { + return (intval($engine->eval($this->A)) > intval($engine->eval($this->B))); + } + + /** + * Returns the string representation of the instruction + * + * @return string + * @throws UnsupportedVariableType + */ + 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 UnsupportedVariableType + * @throws MalformedInstructionException + */ + 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 MalformedInstructionException + * @throws UnsupportedVariableType + */ + 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/GreaterThanOrEquals.php b/src/RTEX/Objects/Program/Instructions/GreaterThanOrEquals.php new file mode 100644 index 0000000..898cd8c --- /dev/null +++ b/src/RTEX/Objects/Program/Instructions/GreaterThanOrEquals.php @@ -0,0 +1,135 @@ + $this->A, + 'b' => $this->B + ]); + } + + /** + * Constructs a new instance of this class from an array representation + * + * @param array $data + * @return InstructionInterface + * @throws MalformedInstructionException + * @throws UnsupportedVariableType + */ + 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 + * @throws UnsupportedVariableType + */ + public function eval(Engine $engine): int + { + return (intval($engine->eval($this->A)) >= intval($engine->eval($this->B))); + } + + /** + * Returns the string representation of the instruction + * + * @return string + * @throws UnsupportedVariableType + */ + 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 UnsupportedVariableType + * @throws MalformedInstructionException + */ + 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 MalformedInstructionException + * @throws UnsupportedVariableType + */ + 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/Invoke.php b/src/RTEX/Objects/Program/Instructions/Invoke.php new file mode 100644 index 0000000..35d98de --- /dev/null +++ b/src/RTEX/Objects/Program/Instructions/Invoke.php @@ -0,0 +1,204 @@ +Parameters = []; + $this->FailOnError = false; + } + + /** + * @return array + * @throws UnsupportedVariableType + */ + public function toArray(): array + { + return InstructionBuilder::toRaw(self::getType(), [ + 'namespace' => $this->Namespace, + 'method' => $this->Method, + 'parameters' => $this->Parameters, + 'fail_on_error' => $this->FailOnError, + ]); + } + + /** + * Constructs an instruction from an array + * + * @param array $data + * @return InstructionInterface + * @throws MalformedInstructionException + * @throws UnsupportedVariableType + */ + public static function fromArray(array $data): InstructionInterface + { + $instruction = new self(); + $instruction->setNamespace($data['namespace'] ?? null); + $instruction->setMethod($data['method'] ?? null); + $instruction->setParameters($data['parameters'] ?? []); + $instruction->setFailOnError($data['fail_on_error'] ?? false); + + return $instruction; + } + + /** + * Invokes the method and returns the result + * + * @param Engine $engine + * @return mixed + * @throws UnsupportedVariableType + * @noinspection PhpMissingReturnTypeInspection + */ + public function eval(Engine $engine) + { + $parameters = []; + foreach($this->Parameters as $key => $value) + $parameters[$key] = $engine->eval($value); + + return $engine->callMethod( + $engine->eval($this->Namespace), $engine->eval($this->Method), + $parameters + ); + } + + /** + * @return string + * @noinspection PhpUnused + */ + public function getNamespace(): string + { + return $this->Namespace; + } + + /** + * @param string $Namespace + */ + public function setNamespace(string $Namespace): void + { + $this->Namespace = $Namespace; + } + + /** + * @return string + * @noinspection PhpUnused + */ + public function getMethod(): string + { + return $this->Method; + } + + /** + * @param string $Method + */ + public function setMethod(string $Method): void + { + $this->Method = $Method; + } + + /** + * @return array + * @noinspection PhpUnused + */ + public function getParameters(): array + { + return $this->Parameters; + } + + /** + * @param array $Parameters + * @throws MalformedInstructionException + * @throws UnsupportedVariableType + */ + public function setParameters(array $Parameters): void + { + $this->Parameters = InstructionBuilder::fromRaw($Parameters); + } + + /** + * @return bool + * @noinspection PhpUnused + */ + public function isFailOnError(): bool + { + return $this->FailOnError; + } + + /** + * @param bool $FailOnError + */ + public function setFailOnError(bool $FailOnError): void + { + $this->FailOnError = $FailOnError; + } + + /** + * @inheritDoc + * @throws UnsupportedVariableType + */ + public function __toString(): string + { + $parameters = []; + foreach ($this->Parameters as $key => $value) + $parameters[] = $key . ': ' . Utilities::entityToString($value); + + $results = sprintf( + self::getType() . ' %s::%s(%s)', + $this->Namespace, $this->Method, implode(', ', $parameters) + ); + + if(!$this->FailOnError) + $results .= ' #FOE'; + + return $results; + } + } \ No newline at end of file diff --git a/src/RTEX/Objects/Program/Instructions/LessThan.php b/src/RTEX/Objects/Program/Instructions/LessThan.php new file mode 100644 index 0000000..8ae09b8 --- /dev/null +++ b/src/RTEX/Objects/Program/Instructions/LessThan.php @@ -0,0 +1,135 @@ + $this->A, + 'b' => $this->B + ]); + } + + /** + * Constructs a new instance of this class from an array representation + * + * @param array $data + * @return InstructionInterface + * @throws MalformedInstructionException + * @throws UnsupportedVariableType + */ + 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 + * @throws UnsupportedVariableType + */ + public function eval(Engine $engine): int + { + return (intval($engine->eval($this->A)) < intval($engine->eval($this->B))); + } + + /** + * Returns the string representation of the instruction + * + * @return string + * @throws UnsupportedVariableType + */ + 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 UnsupportedVariableType + * @throws MalformedInstructionException + */ + 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 MalformedInstructionException + * @throws UnsupportedVariableType + */ + 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/LessThanOrEquals.php new file mode 100644 index 0000000..7a50de1 --- /dev/null +++ b/src/RTEX/Objects/Program/Instructions/LessThanOrEquals.php @@ -0,0 +1,135 @@ + $this->A, + 'b' => $this->B + ]); + } + + /** + * Constructs a new instance of this class from an array representation + * + * @param array $data + * @return InstructionInterface + * @throws MalformedInstructionException + * @throws UnsupportedVariableType + */ + 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 + * @throws UnsupportedVariableType + */ + public function eval(Engine $engine): int + { + return (intval($engine->eval($this->A)) <= intval($engine->eval($this->B))); + } + + /** + * Returns the string representation of the instruction + * + * @return string + * @throws UnsupportedVariableType + */ + 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 UnsupportedVariableType + * @throws MalformedInstructionException + */ + 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 MalformedInstructionException + * @throws UnsupportedVariableType + */ + 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/Modulo.php b/src/RTEX/Objects/Program/Instructions/Modulo.php new file mode 100644 index 0000000..fbdab59 --- /dev/null +++ b/src/RTEX/Objects/Program/Instructions/Modulo.php @@ -0,0 +1,135 @@ + $this->A, + 'b' => $this->B + ]); + } + + /** + * Constructs a new instance of this class from an array representation + * + * @param array $data + * @return InstructionInterface + * @throws MalformedInstructionException + * @throws UnsupportedVariableType + */ + 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 + * @throws UnsupportedVariableType + */ + public function eval(Engine $engine): int + { + return (intval($engine->eval($this->A)) % intval($engine->eval($this->B))); + } + + /** + * Returns the string representation of the instruction + * + * @return string + * @throws UnsupportedVariableType + */ + 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 UnsupportedVariableType + * @throws MalformedInstructionException + */ + 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 MalformedInstructionException + * @throws UnsupportedVariableType + */ + 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/Multiply.php b/src/RTEX/Objects/Program/Instructions/Multiply.php new file mode 100644 index 0000000..83c2a68 --- /dev/null +++ b/src/RTEX/Objects/Program/Instructions/Multiply.php @@ -0,0 +1,135 @@ + $this->A, + 'b' => $this->B + ]); + } + + /** + * Constructs a new instance of this class from an array representation + * + * @param array $data + * @return InstructionInterface + * @throws MalformedInstructionException + * @throws UnsupportedVariableType + */ + 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 + * @throws UnsupportedVariableType + */ + public function eval(Engine $engine): int + { + return (intval($engine->eval($this->A)) * intval($engine->eval($this->B))); + } + + /** + * Returns the string representation of the instruction + * + * @return string + * @throws UnsupportedVariableType + */ + 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 UnsupportedVariableType + * @throws MalformedInstructionException + */ + 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 MalformedInstructionException + * @throws UnsupportedVariableType + */ + 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/Power.php b/src/RTEX/Objects/Program/Instructions/Power.php new file mode 100644 index 0000000..9855be0 --- /dev/null +++ b/src/RTEX/Objects/Program/Instructions/Power.php @@ -0,0 +1,135 @@ + $this->A, + 'b' => $this->B + ]); + } + + /** + * Constructs a new instance of this class from an array representation + * + * @param array $data + * @return InstructionInterface + * @throws MalformedInstructionException + * @throws UnsupportedVariableType + */ + 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 + * @throws UnsupportedVariableType + */ + public function eval(Engine $engine): int + { + return (intval($engine->eval($this->A)) ** intval($engine->eval($this->B))); + } + + /** + * Returns the string representation of the instruction + * + * @return string + * @throws UnsupportedVariableType + */ + 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 UnsupportedVariableType + * @throws MalformedInstructionException + */ + 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 MalformedInstructionException + * @throws UnsupportedVariableType + */ + 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/SetVariable.php b/src/RTEX/Objects/Program/Instructions/SetVariable.php index 091350b..2f11f4b 100644 --- a/src/RTEX/Objects/Program/Instructions/SetVariable.php +++ b/src/RTEX/Objects/Program/Instructions/SetVariable.php @@ -1,10 +1,14 @@ -Variable; + } + + /** + * @param mixed $variable + * @throws UnsupportedVariableType + * @throws MalformedInstructionException + * @noinspection PhpMissingParamTypeInspection + */ + public function setVariable($variable): void + { + $this->Variable = InstructionBuilder::fromRaw($variable); + } + + /** + * @return mixed + * @noinspection PhpMissingReturnTypeInspection + */ + public function getValue() + { + return $this->Value; + } + + /** + * @param mixed $value + * @throws MalformedInstructionException + * @throws UnsupportedVariableType + * @noinspection PhpMissingParamTypeInspection + */ + public function setValue($value): void + { + $this->Value = InstructionBuilder::fromRaw($value); + } + /** * Returns an array representation of the object * @@ -42,17 +88,17 @@ namespace RTEX\Objects\Program\Instructions; */ public function toArray(): array { - return [ - 'type' => $this->getType(), - '_' => [ - 'variable' => Utilities::toArray($this->Variable), - 'value' => Utilities::toArray($this->Value) - ] - ]; + return InstructionBuilder::toRaw(self::getType(), [ + 'variable' => $this->Variable, + 'value' => $this->Value + ]); } /** - * @inheritDoc + * @param array $data + * @return InstructionInterface + * @throws MalformedInstructionException + * @throws UnsupportedVariableType */ public static function fromArray(array $data): InstructionInterface { @@ -63,54 +109,29 @@ namespace RTEX\Objects\Program\Instructions; return $instruction; } - /** - * @return bool|float|int|InstructionInterface|InstructionInterface[]|string|null - */ - public function getVariable() - { - return $this->Variable; - } - - /** - * @param bool|float|int|InstructionInterface|InstructionInterface|string|null $variable - * @throws UnsupportedVariableType - * @noinspection PhpMissingParamTypeInspection - */ - public function setVariable($variable): void - { - Utilities::determineType($variable); - $this->Variable = $variable; - } - - /** - * @return bool|float|int|InstructionInterface|InstructionInterface[]|string|null - */ - public function getValue() - { - return $this->Value; - } - - /** - * @param bool|float|int|InstructionInterface|InstructionInterface|string|null $value - * @throws UnsupportedVariableType - * @noinspection PhpMissingParamTypeInspection - */ - public function setValue($value): void - { - Utilities::determineType($value); - $this->Value = $value; - } - /** * @param Engine $engine - * @return mixed|void + * @return void * @throws UnsupportedVariableType */ - public function eval(Engine $engine) + public function eval(Engine $engine): void { $engine->getEnvironment()->setRuntimeVariable( $engine->eval($this->Variable), $engine->eval($this->Value) ); } + + /** + * @inheritDoc + * @throws UnsupportedVariableType + */ + public function __toString(): string + { + return sprintf( + self::getType() . ' (variable: %s, value: %s)', + Utilities::entityToString($this->Variable), + Utilities::entityToString($this->Value) + ); + } } \ No newline at end of file diff --git a/src/RTEX/Objects/Program/Instructions/Subtract.php b/src/RTEX/Objects/Program/Instructions/Subtract.php new file mode 100644 index 0000000..5030c3f --- /dev/null +++ b/src/RTEX/Objects/Program/Instructions/Subtract.php @@ -0,0 +1,135 @@ + $this->A, + 'b' => $this->B + ]); + } + + /** + * Constructs a new instance of this class from an array representation + * + * @param array $data + * @return InstructionInterface + * @throws MalformedInstructionException + * @throws UnsupportedVariableType + */ + 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 + * @throws UnsupportedVariableType + */ + public function eval(Engine $engine): int + { + return (intval($engine->eval($this->A)) - intval($engine->eval($this->B))); + } + + /** + * Returns the string representation of the instruction + * + * @return string + * @throws UnsupportedVariableType + */ + 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 UnsupportedVariableType + * @throws MalformedInstructionException + */ + 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 MalformedInstructionException + * @throws UnsupportedVariableType + */ + 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/Sum.php b/src/RTEX/Objects/Program/Instructions/Sum.php new file mode 100644 index 0000000..8f2df93 --- /dev/null +++ b/src/RTEX/Objects/Program/Instructions/Sum.php @@ -0,0 +1,135 @@ + $this->A, + 'b' => $this->B + ]); + } + + /** + * Constructs a new instance of this class from an array representation + * + * @param array $data + * @return InstructionInterface + * @throws MalformedInstructionException + * @throws UnsupportedVariableType + */ + 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 + * @throws UnsupportedVariableType + */ + public function eval(Engine $engine): int + { + return (intval($engine->eval($this->A)) + intval($engine->eval($this->B))); + } + + /** + * Returns the string representation of the instruction + * + * @return string + * @throws UnsupportedVariableType + */ + 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 UnsupportedVariableType + * @throws MalformedInstructionException + */ + 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 MalformedInstructionException + * @throws UnsupportedVariableType + */ + public function setB(mixed $B): void + { + $this->B = InstructionBuilder::fromRaw($B); + } + } \ No newline at end of file