PHPackages                             didix16/php-interpreter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. didix16/php-interpreter

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

didix16/php-interpreter
=======================

A simple interpreter made in PHP. It allows to parse an abstract language tokens and do whatever you whant

1.0.4(4y ago)19.0k1MITPHP

Since Aug 5Pushed 4y ago1 watchersCompare

[ Source](https://github.com/didix16/php-interpreter)[ Packagist](https://packagist.org/packages/didix16/php-interpreter)[ RSS](/packages/didix16-php-interpreter/feed)WikiDiscussions main Synced 6d ago

READMEChangelog (1)Dependencies (2)Versions (6)Used By (1)

PHP Interpreter
===============

[](#php-interpreter)

A simple interpreter made in PHP. It allows to parse an abstract language tokens and do whatever you want

Content
-------

[](#content)

- [What is an Interpreter](#what-is-a-token)
- [Installation](#installation)
- [Usage](#usage)
- [Check also](#check-also)

### What is an Interpreter

[](#what-is-an-interpreter)

An interpreter is a piece of software which allows parse an array of tokens of any language and give them a "significance". For example you could process a function, call a service, do something funny...

As an example, imagine we have an APILexer and APIParser, which understands a metalanguage to script some instructions related with REST APIS:

```
$script = "
GET https://some-awesome-api.com/endpoint?token=TOKEN
PIPETO $mySuperService
SAVEDB localhost:10000
";

$lexer = new APILexer($script);
$parser = new APIParser($lexer);

$tokens = $parser->parse();

// var_dump($tokens) should be:

array(12) {
  [0]=>
  object(Token)#4 (2) {
    ["value":protected]=>
    string(3) "GET"
    ["type":protected]=>
    int(2)
  }
  [1]=>
  object(Token)#2 (2) {
    ["value":protected]=>
    string(1) " "
    ["type":protected]=>
    int(3)
  }
  [2]=>
  object(Token)#5 (2) {
    ["value":protected]=>
    string(49) "https://some-awesome-api.com/endpoint?token=TOKEN"
    ["type":protected]=>
    int(4)
  }
  [3]=>
  object(Token)#6 (2) {
    ["value":protected]=>
    string(1) "
"
    ["type":protected]=>
    int(5)
  }
  [4]=>
  object(Token)#7 (2) {
    ["value":protected]=>
    string(6) "PIPETO"
    ["type":protected]=>
    int(2)
  }
  [5]=>
  object(Token)#8 (2) {
    ["value":protected]=>
    string(1) " "
    ["type":protected]=>
    int(3)
  }
  [6]=>
  object(Token)#9 (2) {
    ["value":protected]=>
    string(15) "$mySuperService"
    ["type":protected]=>
    int(4)
  }
  [7]=>
  object(Token)#10 (2) {
    ["value":protected]=>
    string(1) "
"
    ["type":protected]=>
    int(5)
  }
  [8]=>
  object(Token)#11 (2) {
    ["value":protected]=>
    string(6) "SAVEDB"
    ["type":protected]=>
    int(2)
  }
  [9]=>
  object(Token)#12 (2) {
    ["value":protected]=>
    string(1) " "
    ["type":protected]=>
    int(3)
  }
  [10]=>
  object(Token)#13 (2) {
    ["value":protected]=>
    string(15) "localhost:10000"
    ["type":protected]=>
    int(4)
  }
  [11]=>
  object(Token)#14 (2) {
    ["value":protected]=>
    string(5) ""
    ["type":protected]=>
    int(1)
  }
}
```

With these tokens we can do some kind of magic and create an Interpreter which process them. For example, we can call it: APIInterpreter.

```
public class APIInterpreter extends Interpreter {

    public function __construct(APIParser $parser)
    {
        parent::__construct($parser, null);

        $this
            // Allows make GET using an http client or whatever...
            ->loadFunction(new APIGetFunction(...))
            // Allows to interact with DDBB and store values
            ->loadFunction(new DBFunction(...))
            // ...whatever
    }

    protected function consume(): TokenInterface {

        $token = parent::consume();
        if(
            $token->getValue() !== Lexer::EOF_VALUE &&
            $token->getType() === YOUR_TYPE)
            //do something

        return $token;
    }

    /**
     * Execute this interpreter using the parsed tokens
     */
    public function run()
    {
        // All your tokens processing logic goes here
        $token = $this->lookahead();

        // do something with GET token and whitespace token, or whatever...

        do {
            //...
        }while(($token = $this->lookahead()));
    }

    // Write your auxiliar functions here

    /**
     * this one could parse each starting command line:
     * GET, PIPETO, SAVEDB, etc...
     */
    public function processCommand()
    {
        ...
    }

    /**
     * This one could resolve some service from PIPETO command...
     */
    public function processService()
    {
        ...
    }
}
```

Well that is a super simple example. You have to left your imagination work and do something special :)

### Installation

[](#installation)

```
composer require didix16/php-interpreter
```

### Usage

[](#usage)

Using the example above, I'm gonna show a simple usage to instruct the APIInterpeter how it should be acting when reading our great APIScript:

A simple script would have a structure like this:

```
GET https://some-awesome-api.com/endpoint?token=TOKEN
PIPETO $mySuperService
SAVEDB localhost:10000

```

We can identify lines, which each line is an action. Each action has a keyname, a space and a param ( it could be a list of params)

So lets say we have to identify tokens like:

T\_ACTION, T\_WHITESPACE, T\_ARG and T\_NEWLINE

```
