Skip to content

shaunxcode/php-quasiquote-preprocessor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 

Repository files navigation

php quasiquote preprocessor does what it says on the tin. In theory you would use this in a framework which would keep track of the preprocessed source files but it can also be used at the command line. 

This obviously would conflict with the use of ` for shell commands, but really if you are of such a persuasion "these aren't the quotes you are looking for".

Beyond standard quasiquote/unquote/unquote-splicing functionality some other sugar is provided. i.e. dict creation:

$people = 
  `(shaun: 
     (class: person
      pets: 
        (tofu: (class: cat color: white)
	 abby: (class: dog color: (black white))
	 sschnitzel: (class: dog color: (brown black))))
    daena: 
      (class: person
       pets: 
         (cordelia: (class: cat color: black)
	  margret: (class: cat color: orange))))

yields:

$people = array(
  'shaun' => array(
    'class' => 'person',
    'pets' => array(
      'tofu' => array('class' => 'cat', 'color' => 'white'),
      'abby' => array('class' => 'dog', 'color' => array('black', 'white')),
      'schnitzel' => array('class' => 'dog', 'color' => array('brown', 'black')))),
  'daena' => array(
    'class' => 'person', 
    'pets' => array(
      'cordelia' => array('class' => 'cat', 'color' => 'black'),
      'margret' => array('class' => 'cat', 'color' => 'orange'))));

as you can infer an assumption is made that any "bare word" becomes a string. Floats/numbers are treated numerically and strings are also allowed i.e. 

$names = `('timmy mcolours' 67.50 sammy peter 300 'walter jensen' "normal string $interpolation works");

yields:

$names = array('timmy mcolours', 67.50, 'sammy', 'peter', 300, 'walter jensen', "normal string $interpolation works");

The real fun starts when you start unquoting (variables prefixed with a comma (,)) and unquotesplicing (variables/expressions prefixed with a comma and then an at sign (,@)):

$x = `(a b c);
$z = 300;
$q = `(1 2 3 ,@x ,z ,$z ,@$x);

yields:

$x = array('a', 'b', 'c');
$z = 300;
$q = arraySplice(arraySplice(array('1', '2', '3', null, $z, $z, null), 6, 1, $x), 3, 1, $x);

Obviously not something you would want to write yourself (if you value your sanity) - and I feel it is the cause of a lot of bad php code out there in the form of looping over arrays to change their structure rather than using array_splice expressions. Wait array_splice can't be used functionally because it expects a reference thus the arraySplice wrapper function that allows life to carry on. Also demonstrated above is the fact that unquote/unquotesplice can handle variables with or without a $ prefix. 

Native php expressions can be unquoted via { }. Any php expression that returns a value is valid within the brackets. This means function calls etc. but not things like loops.

$x = `(1 2 {1 + 2} ,@{$obj->method($arg, `(sub quasi expression))});

Variable keywords work as expected:

$y = 'key';
$x = `(,y: 300 {'key' . 'built' . funCall(1,2)}: (1 2 3));

yields:

$y = 'key';
$x = array($y => 300, ('key' . 'built' . funCall(1,2)) => array(1, 2, 3));

Also the magical stuff php allows is still there:

$x = `(1 2 key: val 3 4 5);

yields: 

$x = array(1, 2, 'key' => 'val', 3, 4, 5);

Oh yeah I am also toying with the idea of allowing a specification of the array class you use instead of the native array so you could so things like:

`(1 2 3)->map(function($x) { 
	echo $x;
});

I should also probably consider pretty printing the code so it is doesn't turn into gnarly one-liners in the case that someone is hand editing the generated code (which is not a use case I intend, but still..)

While I'm in there trying to defoul things I may as well allow:

$key = 'x';
echo `(x: 60 y: 70)[$key];

yields:

$key = 'x';
echo arrayAt(array('x' => 60, 'y' => 70), $key);

You can also call a quasiquoted expression right after a function call i.e. 

$x = functionCall`(arg1 arg2 ,x ,y);

$z = funcionCall2`(key: ,x key2: ,y);

yields:

$x = functionCall(array('arg1', 'arg2', $x, $y));

$z = functionCalls(array('key' => $x, 'key2' => $y));

About

php quasiquote preprocessor PQP

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages