webgraphe/phlip

Embeddable scripts for PHP

v0.3.0 2018-03-07 22:07 UTC

This package is auto-updated.

Last update: 2024-11-05 07:06:02 UTC


README

Total Downloads Latest Stable Version License

Phlip (pronounced \ˈflip\) is an embeddable scripting language for PHP based on s-expressions.

How does it work?

A lexer tokenizes scripts and a parser assembles data structures. A script's behavior originates from data and code elements resolved from a controlled scope.

Integration is simpler with the Phlipy dialect.

Refer to Webgraphe\Phlip\Tests\Unit\ReadmeTest for the example below:

<?php

use Webgraphe\Phlip\Phlipy;
use Webgraphe\Phlip\Program;

// Tokenize and parse code into a program
$program = Program::parse('(lambda (x) (* x x))');
// Bootstrap a new scope with different level of Phlipy dialects
$scope = Phlipy::basic()->getScope();
// Execute program within said scope
$square = $program->execute($scope);

// In this case, return value is an anonymous function - a lambda - calculating the square of a number
// as per source code "(* x x)"
var_dump($square(M_PI)); // (double)9.8696044010894

How to install

Add a dependency on your projects with Composer, using composer require webgraphe/phlip. You may also install it globally with composer global require webgraphe/phlip.

Why use Phlip

  • Embeddable scripts mean data becomes code
  • Easy to use
  • Easy to learn
  • Build your own dialect!
  • Interoperable with PHP Classes
  • Create test suites with phlipunit (built on top of PHPUnit)
  • Ships with a literal REPL (loop (print (eval (read))))

DISCLAIMER

Lisp is a often considered a Homoiconic (code as data) language. Despite Phlip's usage of S-expressions and a somewhat successful attempt at reproducing McCarthy's eval, as the author I do not consider Phlip be an homoiconic language as it relies on PHP's internal data structures such as array, stdClass, native scalars, Closure and invokable classes for performance and convenience.

Phlip is embeddable in PHP and was built to allow transportation of code and data in a manner that can stay relatively secure, provided that functionalities interoperable with PHP stay in check.

For reference: Webgraphe\Phlip\Tests\System\LispTest::testMcCarthyEval()