Added base files, with the basic ability to add/remove variables from the Runtime.

This commit is contained in:
Netkas 2022-12-23 00:10:02 -05:00
parent aef02c4bff
commit 7e5207c45a
19 changed files with 906 additions and 0 deletions

View file

@ -0,0 +1,43 @@
<?php
namespace RTEX\Classes;
use RTEX\Exceptions\Core\UnsupportedVariableType;
use RTEX\Interfaces\InstructionInterface;
use RTEX\Objects\Program\Instructions\GetVariable;
use RTEX\Objects\Program\Instructions\SetVariable;
class InstructionBuilder
{
/**
* Constructs a new get variable instruction
*
* @param $name
* @return InstructionInterface
* @throws UnsupportedVariableType
*/
public static function getVariable($name): InstructionInterface
{
$instruction = new GetVariable();
$instruction->setVariable($name);
return $instruction;
}
/**
* Constructs a new set variable instruction
*
* @param $name
* @param $value
* @return InstructionInterface
* @throws UnsupportedVariableType
*/
public static function setVariable($name, $value): InstructionInterface
{
$instruction = new SetVariable();
$instruction->setVariable($name);
$instruction->setValue($value);
return $instruction;
}
}

View file

@ -0,0 +1,81 @@
<?php
namespace RTEX\Classes;
use RTEX\Abstracts\InstructionType;
use RTEX\Abstracts\VariableTypes;
use RTEX\Exceptions\Core\MalformedInstructionException;
use RTEX\Exceptions\Core\UnsupportedVariableType;
use RTEX\Interfaces\InstructionInterface;
use RTEX\Objects\Program\Instructions\GetVariable;
use RTEX\Objects\Program\Instructions\SetVariable;
class Utilities
{
/**
* Determines the type of variable, throws an exception if the type is not supported
*
* @param $input
* @return string
* @throws UnsupportedVariableType
*/
public static function determineType($input): string
{
if ($input instanceof InstructionInterface)
return VariableTypes::Instruction;
if (is_string($input))
return VariableTypes::String;
if (is_int($input))
return VariableTypes::Integer;
if (is_float($input))
return VariableTypes::Float;
if (is_bool($input))
return VariableTypes::Boolean;
if (is_null($input))
return VariableTypes::Null;
throw new UnsupportedVariableType(gettype($input));
}
/**
* Returns a supported variable type to an array representation
*
* @param $input
* @return array|mixed
* @throws UnsupportedVariableType
*/
public static function toArray($input)
{
return match (self::determineType($input))
{
VariableTypes::Instruction => $input->toArray(),
default => $input,
};
}
/**
* Constructs an instruction from an array representation
*
* @param array $array
* @return InstructionInterface
* @throws MalformedInstructionException
* @throws UnsupportedVariableType
*/
public static function constructInstruction(array $array): InstructionInterface
{
if(!isset($array['type']))
throw new MalformedInstructionException(sprintf('Instruction type not specified'));
if(!isset($array['_']))
throw new MalformedInstructionException(sprintf('Instruction data not specified'));
switch($array['type'])
{
case InstructionType::GetVariable:
return GetVariable::fromArray($array['_']);
case InstructionType::SetVariable:
return SetVariable::fromArray($array['_']);
default:
throw new MalformedInstructionException(sprintf('Instruction type "%s" not supported', $array['type']));
}
}
}

View file

@ -0,0 +1,31 @@
<?php
namespace RTEX\Classes;
use Exception;
use RTEX\Abstracts\VariableTypes;
use RTEX\Interfaces\InstructionInterface;
class Validate
{
/**
* Determines if the input is a supported variable type
*
* @param $type
* @return bool
*/
public static function supportedVariableType($type): bool
{
try
{
Utilities::determineType($type);
}
catch(Exception $e)
{
unset($e);
return false;
}
return true;
}
}