Introduces various instruction additions to the base engine.
Created \RTEX\Objects\Program\Instructions > Sum Created \RTEX\Objects\Program\Instructions > Subtract Created \RTEX\Objects\Program\Instructions > Power Created \RTEX\Objects\Program\Instructions > Multiply Created \RTEX\Objects\Program\Instructions > Modulo Created \RTEX\Objects\Program\Instructions > LessThanOrEquals Created \RTEX\Objects\Program\Instructions > LessThan Created \RTEX\Objects\Program\Instructions > Invoke Created \RTEX\Objects\Program\Instructions > GreaterThanOrEquals Created \RTEX\Objects\Program\Instructions > GreaterThan Created \RTEX\Objects\Program\Instructions > Equals Created \RTEX\Objects\Program\Instructions > Divide Created \RTEX\Objects\Program\Instructions > ArrayGet Refactored \RTEX\Objects\Program\Instructions > SetVariable Refactored \RTEX\Objects\Program\Instructions > GetVariable
This commit is contained in:
parent
68b50a1a21
commit
08c4b3079e
15 changed files with 1965 additions and 90 deletions
150
src/RTEX/Objects/Program/Instructions/ArrayGet.php
Normal file
150
src/RTEX/Objects/Program/Instructions/ArrayGet.php
Normal file
|
@ -0,0 +1,150 @@
|
|||
<?php
|
||||
|
||||
/** @noinspection PhpMissingFieldTypeInspection */
|
||||
|
||||
namespace RTEX\Objects\Program\Instructions;
|
||||
|
||||
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\Interfaces\InstructionInterface;
|
||||
|
||||
class ArrayGet implements InstructionInterface
|
||||
{
|
||||
/**
|
||||
* The array to read from
|
||||
*
|
||||
* @var mixed
|
||||
*/
|
||||
private $Array;
|
||||
|
||||
/**
|
||||
* The query to use to read from the array
|
||||
*
|
||||
* @var mixed
|
||||
*/
|
||||
private $Value;
|
||||
|
||||
/**
|
||||
* The name of the variable to set
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getType(): string
|
||||
{
|
||||
return InstructionType::ArrayGet;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @noinspection PhpMissingReturnTypeInspection
|
||||
* @noinspection PhpUnused
|
||||
*/
|
||||
public function getArray()
|
||||
{
|
||||
return $this->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)
|
||||
);
|
||||
}
|
||||
}
|
135
src/RTEX/Objects/Program/Instructions/Divide.php
Normal file
135
src/RTEX/Objects/Program/Instructions/Divide.php
Normal file
|
@ -0,0 +1,135 @@
|
|||
<?php
|
||||
|
||||
/** @noinspection PhpMissingFieldTypeInspection */
|
||||
|
||||
namespace RTEX\Objects\Program\Instructions;
|
||||
|
||||
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\Interfaces\InstructionInterface;
|
||||
|
||||
class Divide implements InstructionInterface
|
||||
{
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
private $A;
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
private $B;
|
||||
|
||||
/**
|
||||
* Returns the type of instruction
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getType(): string
|
||||
{
|
||||
return InstructionType::Divide;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array representation of the instruction
|
||||
*
|
||||
* @return array
|
||||
* @throws UnsupportedVariableType
|
||||
*/
|
||||
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 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);
|
||||
}
|
||||
}
|
135
src/RTEX/Objects/Program/Instructions/Equals.php
Normal file
135
src/RTEX/Objects/Program/Instructions/Equals.php
Normal file
|
@ -0,0 +1,135 @@
|
|||
<?php
|
||||
|
||||
/** @noinspection PhpMissingFieldTypeInspection */
|
||||
|
||||
namespace RTEX\Objects\Program\Instructions;
|
||||
|
||||
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\Interfaces\InstructionInterface;
|
||||
|
||||
class Equals implements InstructionInterface
|
||||
{
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
private $A;
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
private $B;
|
||||
|
||||
/**
|
||||
* Returns the type of instruction
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getType(): string
|
||||
{
|
||||
return InstructionType::Equals;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array representation of the instruction
|
||||
*
|
||||
* @return array
|
||||
* @throws UnsupportedVariableType
|
||||
*/
|
||||
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 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);
|
||||
}
|
||||
}
|
|
@ -1,10 +1,14 @@
|
|||
<?php
|
||||
|
||||
/** @noinspection PhpMissingFieldTypeInspection */
|
||||
|
||||
namespace RTEX\Objects\Program\Instructions;
|
||||
|
||||
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\Interfaces\InstructionInterface;
|
||||
|
||||
|
@ -13,7 +17,7 @@
|
|||
/**
|
||||
* The name of the variable to select
|
||||
*
|
||||
* @var string|integer|boolean|float|null|InstructionInterface|InstructionInterface[]
|
||||
* @var mixed
|
||||
*/
|
||||
private $Variable;
|
||||
|
||||
|
@ -28,27 +32,12 @@
|
|||
return InstructionType::GetVariable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array representation of the object
|
||||
*
|
||||
* @return array
|
||||
* @throws UnsupportedVariableType
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'type' => $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)
|
||||
);
|
||||
}
|
||||
}
|
135
src/RTEX/Objects/Program/Instructions/GreaterThan.php
Normal file
135
src/RTEX/Objects/Program/Instructions/GreaterThan.php
Normal file
|
@ -0,0 +1,135 @@
|
|||
<?php
|
||||
|
||||
/** @noinspection PhpMissingFieldTypeInspection */
|
||||
|
||||
namespace RTEX\Objects\Program\Instructions;
|
||||
|
||||
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\Interfaces\InstructionInterface;
|
||||
|
||||
class GreaterThan implements InstructionInterface
|
||||
{
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
private $A;
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
private $B;
|
||||
|
||||
/**
|
||||
* Returns the type of instruction
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getType(): string
|
||||
{
|
||||
return InstructionType::GreaterThan;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array representation of the instruction
|
||||
*
|
||||
* @return array
|
||||
* @throws UnsupportedVariableType
|
||||
*/
|
||||
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 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);
|
||||
}
|
||||
}
|
135
src/RTEX/Objects/Program/Instructions/GreaterThanOrEquals.php
Normal file
135
src/RTEX/Objects/Program/Instructions/GreaterThanOrEquals.php
Normal file
|
@ -0,0 +1,135 @@
|
|||
<?php
|
||||
|
||||
/** @noinspection PhpMissingFieldTypeInspection */
|
||||
|
||||
namespace RTEX\Objects\Program\Instructions;
|
||||
|
||||
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\Interfaces\InstructionInterface;
|
||||
|
||||
class GreaterThanOrEquals implements InstructionInterface
|
||||
{
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
private $A;
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
private $B;
|
||||
|
||||
/**
|
||||
* Returns the type of instruction
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getType(): string
|
||||
{
|
||||
return InstructionType::GreaterThanOrEquals;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array representation of the instruction
|
||||
*
|
||||
* @return array
|
||||
* @throws UnsupportedVariableType
|
||||
*/
|
||||
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 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);
|
||||
}
|
||||
}
|
204
src/RTEX/Objects/Program/Instructions/Invoke.php
Normal file
204
src/RTEX/Objects/Program/Instructions/Invoke.php
Normal file
|
@ -0,0 +1,204 @@
|
|||
<?php
|
||||
|
||||
/** @noinspection PhpMissingFieldTypeInspection */
|
||||
|
||||
namespace RTEX\Objects\Program\Instructions;
|
||||
|
||||
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\Interfaces\InstructionInterface;
|
||||
|
||||
class Invoke implements InstructionInterface
|
||||
{
|
||||
/**
|
||||
* The namespace of the method to invoke
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $Namespace;
|
||||
|
||||
/**
|
||||
* The name of the method to invoke
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $Method;
|
||||
|
||||
/**
|
||||
* The parameters to pass to the method
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $Parameters;
|
||||
|
||||
/**
|
||||
* If the execution should be stopped after the method has raised an exception
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $FailOnError;
|
||||
|
||||
/**
|
||||
* Returns the type of instruction
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getType(): string
|
||||
{
|
||||
return InstructionType::Invoke;
|
||||
}
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->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;
|
||||
}
|
||||
}
|
135
src/RTEX/Objects/Program/Instructions/LessThan.php
Normal file
135
src/RTEX/Objects/Program/Instructions/LessThan.php
Normal file
|
@ -0,0 +1,135 @@
|
|||
<?php
|
||||
|
||||
/** @noinspection PhpMissingFieldTypeInspection */
|
||||
|
||||
namespace RTEX\Objects\Program\Instructions;
|
||||
|
||||
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\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
|
||||
* @throws UnsupportedVariableType
|
||||
*/
|
||||
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 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);
|
||||
}
|
||||
}
|
135
src/RTEX/Objects/Program/Instructions/LessThanOrEquals.php
Normal file
135
src/RTEX/Objects/Program/Instructions/LessThanOrEquals.php
Normal file
|
@ -0,0 +1,135 @@
|
|||
<?php
|
||||
|
||||
/** @noinspection PhpMissingFieldTypeInspection */
|
||||
|
||||
namespace RTEX\Objects\Program\Instructions;
|
||||
|
||||
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\Interfaces\InstructionInterface;
|
||||
|
||||
class LessThanOrEquals implements InstructionInterface
|
||||
{
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
private $A;
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
private $B;
|
||||
|
||||
/**
|
||||
* Returns the type of instruction
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getType(): string
|
||||
{
|
||||
return InstructionType::Divide;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array representation of the instruction
|
||||
*
|
||||
* @return array
|
||||
* @throws UnsupportedVariableType
|
||||
*/
|
||||
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 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);
|
||||
}
|
||||
}
|
135
src/RTEX/Objects/Program/Instructions/Modulo.php
Normal file
135
src/RTEX/Objects/Program/Instructions/Modulo.php
Normal file
|
@ -0,0 +1,135 @@
|
|||
<?php
|
||||
|
||||
/** @noinspection PhpMissingFieldTypeInspection */
|
||||
|
||||
namespace RTEX\Objects\Program\Instructions;
|
||||
|
||||
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\Interfaces\InstructionInterface;
|
||||
|
||||
class Modulo implements InstructionInterface
|
||||
{
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
private $A;
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
private $B;
|
||||
|
||||
/**
|
||||
* Returns the type of instruction
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getType(): string
|
||||
{
|
||||
return InstructionType::Modulo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array representation of the instruction
|
||||
*
|
||||
* @return array
|
||||
* @throws UnsupportedVariableType
|
||||
*/
|
||||
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 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);
|
||||
}
|
||||
}
|
135
src/RTEX/Objects/Program/Instructions/Multiply.php
Normal file
135
src/RTEX/Objects/Program/Instructions/Multiply.php
Normal file
|
@ -0,0 +1,135 @@
|
|||
<?php
|
||||
|
||||
/** @noinspection PhpMissingFieldTypeInspection */
|
||||
|
||||
namespace RTEX\Objects\Program\Instructions;
|
||||
|
||||
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\Interfaces\InstructionInterface;
|
||||
|
||||
class Multiply implements InstructionInterface
|
||||
{
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
private $A;
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
private $B;
|
||||
|
||||
/**
|
||||
* Returns the type of instruction
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getType(): string
|
||||
{
|
||||
return InstructionType::Multiply;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array representation of the instruction
|
||||
*
|
||||
* @return array
|
||||
* @throws UnsupportedVariableType
|
||||
*/
|
||||
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 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);
|
||||
}
|
||||
}
|
135
src/RTEX/Objects/Program/Instructions/Power.php
Normal file
135
src/RTEX/Objects/Program/Instructions/Power.php
Normal file
|
@ -0,0 +1,135 @@
|
|||
<?php
|
||||
|
||||
/** @noinspection PhpMissingFieldTypeInspection */
|
||||
|
||||
namespace RTEX\Objects\Program\Instructions;
|
||||
|
||||
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\Interfaces\InstructionInterface;
|
||||
|
||||
class Power implements InstructionInterface
|
||||
{
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
private $A;
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
private $B;
|
||||
|
||||
/**
|
||||
* Returns the type of instruction
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getType(): string
|
||||
{
|
||||
return InstructionType::Power;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array representation of the instruction
|
||||
*
|
||||
* @return array
|
||||
* @throws UnsupportedVariableType
|
||||
*/
|
||||
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 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);
|
||||
}
|
||||
}
|
|
@ -1,10 +1,14 @@
|
|||
<?php /** @noinspection PhpReturnDocTypeMismatchInspection */
|
||||
<?php
|
||||
|
||||
namespace RTEX\Objects\Program\Instructions;
|
||||
/** @noinspection PhpMissingFieldTypeInspection */
|
||||
|
||||
namespace RTEX\Objects\Program\Instructions;
|
||||
|
||||
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\Interfaces\InstructionInterface;
|
||||
|
||||
|
@ -13,14 +17,14 @@ namespace RTEX\Objects\Program\Instructions;
|
|||
/**
|
||||
* The name of the variable to set
|
||||
*
|
||||
* @var string|integer|boolean|float|null|InstructionInterface
|
||||
* @var mixed
|
||||
*/
|
||||
private $Variable;
|
||||
|
||||
/**
|
||||
* The value to set the variable to
|
||||
*
|
||||
* @var string|integer|boolean|float|null|InstructionInterface
|
||||
* @var mixed
|
||||
*/
|
||||
private $Value;
|
||||
|
||||
|
@ -34,6 +38,48 @@ namespace RTEX\Objects\Program\Instructions;
|
|||
return InstructionType::SetVariable;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @noinspection PhpMissingReturnTypeInspection
|
||||
* @noinspection PhpUnused
|
||||
*/
|
||||
public function getVariable()
|
||||
{
|
||||
return $this->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)
|
||||
);
|
||||
}
|
||||
}
|
135
src/RTEX/Objects/Program/Instructions/Subtract.php
Normal file
135
src/RTEX/Objects/Program/Instructions/Subtract.php
Normal file
|
@ -0,0 +1,135 @@
|
|||
<?php
|
||||
|
||||
/** @noinspection PhpMissingFieldTypeInspection */
|
||||
|
||||
namespace RTEX\Objects\Program\Instructions;
|
||||
|
||||
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\Interfaces\InstructionInterface;
|
||||
|
||||
class Subtract implements InstructionInterface
|
||||
{
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
private $A;
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
private $B;
|
||||
|
||||
/**
|
||||
* Returns the type of instruction
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getType(): string
|
||||
{
|
||||
return InstructionType::Subtract;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array representation of the instruction
|
||||
*
|
||||
* @return array
|
||||
* @throws UnsupportedVariableType
|
||||
*/
|
||||
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 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);
|
||||
}
|
||||
}
|
135
src/RTEX/Objects/Program/Instructions/Sum.php
Normal file
135
src/RTEX/Objects/Program/Instructions/Sum.php
Normal file
|
@ -0,0 +1,135 @@
|
|||
<?php
|
||||
|
||||
/** @noinspection PhpMissingFieldTypeInspection */
|
||||
|
||||
namespace RTEX\Objects\Program\Instructions;
|
||||
|
||||
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\Interfaces\InstructionInterface;
|
||||
|
||||
class Sum implements InstructionInterface
|
||||
{
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
private $A;
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
private $B;
|
||||
|
||||
/**
|
||||
* Returns the type of instruction
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getType(): string
|
||||
{
|
||||
return InstructionType::Sum;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array representation of the instruction
|
||||
*
|
||||
* @return array
|
||||
* @throws UnsupportedVariableType
|
||||
*/
|
||||
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 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);
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue