Interpreter

Intent
given a language, interpret sentences
Motivation
request (path) parser in a web application
Implementation
  • a class for each expression type, interpret method for each type
  • a class and object to store state (context)
  • each expression class is resposible for building it's own parse tree
Example
class tag {
  //ts is the current token stream
  //this is a variant of partial evaluation
  var $args;
  function __construct( &$ts) { 
    while( not_class($arg = next($ts)) ) {
      $this->args[] = $arg;
    }
  }
  //evaluate the 
  function run( &$context) { ... }
}

//example usage
$ts = array("tag","php","design patterns");
$op = current($ts);
$prog = new $op();
$prog->run( new context() );
notes
  • the expression types can be possibly parametrized if their run an construct methods follow, particular 'typical' shapes
  • not_class is a language dependent function