Skip to content

Commit

Permalink
added method to add predefined variables
Browse files Browse the repository at this point in the history
  • Loading branch information
bragle committed May 11, 2019
1 parent 8f4ee43 commit a765891
Showing 1 changed file with 62 additions and 8 deletions.
70 changes: 62 additions & 8 deletions Logica.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
class Logica {

private $vars = [];
private $staticVars = [];

private $parts = [];

private $output = '';
Expand Down Expand Up @@ -94,6 +96,44 @@ public function __construct(){

$this->line = $this->length;

},

'get' => function($stack){

if(count($stack) !== 1){

$this->error = 'get function requires one param';

return false;

}

$variable = $this->staticVars;

foreach(explode('.', $stack[0]) as $part){

if(!isset($variable[$part])){

$this->error = 'static variable ' . $part . ' does not exist';

return false;

}

$variable = &$variable[$part];

}

if(is_array($variable)){

$this->error = 'variable is an array, which is not currently supported';

return false;

}

return $variable;

}

];
Expand Down Expand Up @@ -130,7 +170,7 @@ public function __construct(){

}

private function split($string){
private function split(string $string){

$inString = false;

Expand Down Expand Up @@ -185,7 +225,7 @@ private function split($string){

}

private function validateCall($function, $paramCount){
private function validateCall($function, int $paramCount){

if(!is_callable($function)){

Expand Down Expand Up @@ -231,7 +271,7 @@ private function setVar(){

}

private function getVar($var){
private function getVar(string $var){

if(isset($this->vars[$var])){

Expand All @@ -255,7 +295,7 @@ private function getVar($var){

}

private function execute($string){
private function execute(string $string){

if(!$stack = $this->split(substr($string, 1, -1))){

Expand Down Expand Up @@ -283,7 +323,7 @@ private function execute($string){

}

private function test($string){
private function test(string $string){

if(!$stack = $this->split(substr($string, 1, -1))){

Expand Down Expand Up @@ -349,7 +389,7 @@ private function test($string){

}

private function return($string, $base = false){
private function return($string, bool $base = false){

if(is_array($string)){

Expand Down Expand Up @@ -397,6 +437,20 @@ private function return($string, $base = false){

}

public function addStatics($array){

if(!is_array($array)){

$this->error = 'Unable to import static variables';

return false;

}

$this->staticVars = $array;

}

public function error(){

if(!$this->error){
Expand All @@ -405,7 +459,7 @@ public function error(){

}

return "{$this->error} @ {$this->line}";
return "{$this->error} @ " . ($this->line - 1);

}

Expand All @@ -415,7 +469,7 @@ public function output(){

}

public function run($string){
public function run(string $string){

$this->parts = explode("\n", $string);

Expand Down

0 comments on commit a765891

Please sign in to comment.