Minor changes

This commit is contained in:
Netkas 2023-03-05 13:57:57 -05:00
parent 05cc358eea
commit e9d033044c
27 changed files with 949 additions and 94 deletions

24
tests/pure_runner.php Normal file
View file

@ -0,0 +1,24 @@
<?php
use RTEX\Classes\InstructionBuilder;
use RTEX\Engine;
use RTEX\Objects\Program;
require 'ncc';
import('net.nosial.rtex', 'latest');
$program = new Program();
$program->getMain()->addInstruction(InstructionBuilder::set('foo', 'bar'));
$program->getMain()->addInstruction(InstructionBuilder::set('bar', 500));
$program->getMain()->addInstruction(InstructionBuilder::set('results',
InstructionBuilder::sum(
500,
InstructionBuilder::get('bar')
)
));
$engine = new Engine($program);
$engine->run();

35
tests/script_builder.php Normal file
View file

@ -0,0 +1,35 @@
<?php
use RTEX\Classes\InstructionBuilder;
use RTEX\Objects\Program;
require('ncc');
import('net.nosial.rtex', 'latest');
$program = new Program();
$program->getMain()->addInstruction(InstructionBuilder::abs(-2));
$program->getMain()->addInstruction(InstructionBuilder::div(2, 2));
$program->getMain()->addInstruction(InstructionBuilder::floor(2.5));
$program->getMain()->addInstruction(InstructionBuilder::mod(2, 2));
$program->getMain()->addInstruction(InstructionBuilder::mul(2, 2));
$program->getMain()->addInstruction(InstructionBuilder::pow(2, 2));
$program->getMain()->addInstruction(InstructionBuilder::round(2.5));
$program->getMain()->addInstruction(InstructionBuilder::sqrt(2));
$program->getMain()->addInstruction(InstructionBuilder::sub(2, 2));
$program->getMain()->addInstruction(InstructionBuilder::sum(2, 2));
$program->getMain()->addInstruction(InstructionBuilder::array_set(["test"=>["foo"=>"bar"]], 'test.foo', 'baz'));
$program->getMain()->addInstruction(InstructionBuilder::array_get(["test"=>["foo"=>"bar"]], 'test.foo'));
$program->getMain()->addInstruction(InstructionBuilder::set('test', 'foo'));
$program->getMain()->addInstruction(InstructionBuilder::get('test'));
$program->getMain()->addInstruction(InstructionBuilder::eq(2, 2));
$program->getMain()->addInstruction(InstructionBuilder::gt(2, 2));
$program->getMain()->addInstruction(InstructionBuilder::gte(2, 2));
$program->getMain()->addInstruction(InstructionBuilder::lt(2, 2));
$program->getMain()->addInstruction(InstructionBuilder::lte(2, 2));
$program->getMain()->addInstruction(InstructionBuilder::neq(2, 2));
print(json_encode($program->toArray(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL);
print((string)$program->getMain() . PHP_EOL);

72
tests/syntax_parse.php Normal file
View file

@ -0,0 +1,72 @@
<?php
function parseSyntax($syntax) {
// Initialize an empty array to hold the parsed instructions
$instructions = array();
// Split the syntax into an array of tokens
$tokens = preg_split('/[\s,()]+/', $syntax, -1, PREG_SPLIT_NO_EMPTY);
// Get the type of instruction
$type = array_shift($tokens);
$instructions['type'] = $type;
// Initialize an empty array to hold the instruction parameters
$params = array();
// Loop through the tokens
while (!empty($tokens)) {
// Get the next token
$token = array_shift($tokens);
// Check if the token is a key
if (preg_match('/^[a-zA-Z_][a-zA-Z0-9_]*$/', $token) && !empty($tokens) && $tokens[0] == ':') {
// Remove the colon
array_shift($tokens);
// Get the value of the key-value pair
$value = array_shift($tokens);
// Check if the value is a string
if (preg_match('/^["\'].*["\']$/', $value)) {
// Strip the quotes from the string value
$value = substr($value, 1, -1);
}
// Add the key-value pair to the instruction parameters
$params[$token] = $value;
} else {
// The token is not a key-value pair.
// Check if the token is a value for the previous key.
$last_key = array_key_last($params);
if ($last_key !== NULL) {
// The token is a value for the previous key.
// Add it to the instruction parameters as an array.
$params[$last_key] = array($params[$last_key], $token);
} else {
// The token is not a value for the previous key.
// Add it as a separate element to the instruction parameters.
$params[] = $token;
}
}
}
// Add the instruction parameters to the instructions array
$instructions['_'] = $params;
// Return the parsed instructions
return $instructions;
}
$syntax = 'equals(1, 2)';
$instructions = parseSyntax($syntax);
print_r($instructions);
$syntax = 'equals(1, get("foo"))';
$instructions = parseSyntax($syntax);
print_r($instructions);
$syntax = 'invoke(namespace: "std", method: "print", continue_on_error: false, params: {"value":"Hello World","second_value":{"type":"get","_":"foo"}})';
$instructions = parseSyntax($syntax);
print_r($instructions);