<?php namespace xp\runtime;

use util\cmd\Console;
use lang\XPClass;

/**
 * Evaluates sourcecode
 *
 */
class Evaluate {
  
  /**
   * Main
   *
   * @param   string[] args
   */
  public static function main(array $args) {
    $argc= sizeof($args);

    // Read sourcecode from STDIN if no further argument is given
    if (0 === $argc) {
      $code= new Code(file_get_contents('php://stdin'));
    } else if ('--' === $args[0]) {
      $code= new Code(file_get_contents('php://stdin'));
    } else {
      $code= new Code($args[0]);
    }

    // Perform
    $argv= [XPClass::nameOf(__CLASS__)] + $args;
    return eval($code->head().$code->fragment());
  }
}

<?php namespace xp\runtime;

use util\cmd\Console;
use lang\XPClass;

/**
 * Evaluates sourcecode
 *
 */
class Evaluate {
  
  /**
   * Main
   *
   * @param   string[] args
   */
  public static function main(array $args) {
    $argc= sizeof($args);

    // Read sourcecode from STDIN if no further argument is given
    if (0 === $argc) {
      $code= new Code(file_get_contents('php://stdin'));
    } else if ('--' === $args[0]) {
      $code= new Code(file_get_contents('php://stdin'));
    } else if (is_file($args[0])) {
      $code= new Code(file_get_contents($args[0]));
    } else {
      $code= new Code($args[0]);
    }

    // Perform
    $argv= [XPClass::nameOf(__CLASS__)] + $args;
    return eval($code->head().$code->fragment());
  }
}

Enable running scripts from files
Enable running scripts from files

E.g. "xp -e AgeInDays.script.php", which is the equivalent of
"cat AgeInDays.script.php | xp -e"

Scripts do not have an enclosing class, but rather just start
with the code right away (after some optional "use" statements).
Example:

<?php
use util\{Date, DateUtil};
use util\cmd\Console;

$span= DateUtil::timespanBetween(new Date($argv[1]), Date::now());
Console::writeLine("Hey, you are ", $span->getDays(), " days old");
