aboutsummaryrefslogtreecommitdiffstats
path: root/Bin/Shell.php
diff options
context:
space:
mode:
Diffstat (limited to 'Bin/Shell.php')
-rw-r--r--Bin/Shell.php301
1 files changed, 131 insertions, 170 deletions
diff --git a/Bin/Shell.php b/Bin/Shell.php
index 8b73dc6..8a338ec 100644
--- a/Bin/Shell.php
+++ b/Bin/Shell.php
@@ -8,7 +8,7 @@
*
* New BSD License
*
- * Copyright © 2007-2015, Ivan Enderlin. All rights reserved.
+ * Copyright © 2007-2015, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -34,224 +34,187 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
-namespace {
+namespace Hoa\Praspel\Bin;
-from('Hoa')
-
-/**
- * \Hoa\File\Read
- */
--> import('File.Read')
-
-/**
- * \Hoa\Compiler\Llk
- */
--> import('Compiler.Llk.~')
-
-/**
- * \Hoa\Praspel\Visitor\Interpreter
- */
--> import('Praspel.Visitor.Interpreter')
-
-/**
- * \Hoa\Praspel\Visitor\Compiler
- */
--> import('Praspel.Visitor.Compiler')
-
-/**
- * \Hoa\Realdom
- */
--> import('Realdom.~')
-
-/**
- * \Hoa\Math\Sampler\Random
- */
--> import('Math.Sampler.Random')
-
-/**
- * \Hoa\Console\Cursor
- */
--> import('Console.Cursor')
-
-/**
- * \Hoa\Console\Readline
- */
--> import('Console.Readline.~')
-
-/**
- * \Hoa\Console\Readline\Autocompleter\Word
- */
--> import('Console.Readline.Autocompleter.Word');
-
-}
-
-namespace Hoa\Praspel\Bin {
+use Hoa\Compiler;
+use Hoa\Console;
+use Hoa\Core;
+use Hoa\File;
+use Hoa\Math;
+use Hoa\Praspel;
+use Hoa\Realdom;
/**
* Class \Hoa\Praspel\Bin\Shell.
*
* Interactive Praspel shell.
*
- * @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
- * @copyright Copyright © 2007-2015 Ivan Enderlin.
+ * @copyright Copyright © 2007-2015 Hoa community
* @license New BSD License
*/
-
-class Shell extends \Hoa\Console\Dispatcher\Kit {
-
+class Shell extends Console\Dispatcher\Kit
+{
/**
* Options description.
*
- * @var \Hoa\Praspel\Bin\Shell array
+ * @var array
*/
- protected $options = array(
- array('help', \Hoa\Console\GetOption::NO_ARGUMENT, 'h'),
- array('help', \Hoa\Console\GetOption::NO_ARGUMENT, '?')
- );
+ protected $options = [
+ ['help', Console\GetOption::NO_ARGUMENT, 'h'],
+ ['help', Console\GetOption::NO_ARGUMENT, '?']
+ ];
/**
* The entry method.
*
- * @access public
* @return int
*/
- public function main ( ) {
+ public function main()
+ {
+ while (false !== $c = $this->getOption($v)) {
+ switch ($c) {
+ case 'h':
+ case '?':
+ return $this->usage();
- while(false !== $c = $this->getOption($v)) switch($c) {
+ case '__ambiguous':
+ $this->resolveOptionAmbiguity($v);
- case 'h':
- case '?':
- return $this->usage();
- break;
-
- case '__ambiguous':
- $this->resolveOptionAmbiguity($v);
- break;
+ break;
+ }
}
- \Hoa\Realdom::setDefaultSampler(new \Hoa\Math\Sampler\Random());
+ Realdom::setDefaultSampler(new Math\Sampler\Random());
- $compiler = \Hoa\Compiler\Llk::load(
- new \Hoa\File\Read('hoa://Library/Praspel/Grammar.pp')
+ $compiler = Compiler\Llk::load(
+ new File\Read('hoa://Library/Praspel/Grammar.pp')
);
- $interpreter = new \Hoa\Praspel\Visitor\Interpreter();
- $dump = new \Hoa\Praspel\Visitor\Compiler();
+ $interpreter = new Praspel\Visitor\Interpreter();
+ $dump = new Praspel\Visitor\Compiler();
$interpreter->visit($compiler->parse('@requires;'));
- $words = array();
+ $words = [];
from('Hoathis or Hoa')
- -> foreachImport('Realdom.*', function ( $classname ) use ( &$words ) {
-
+ -> foreachImport('Realdom.*', function ($classname) use (&$words) {
$class = new \ReflectionClass($classname);
- if($class->isSubclassOf('\Hoa\Realdom'))
+ if ($class->isSubclassOf('\Hoa\Realdom')) {
$words[] = $classname::NAME;
+ }
return;
});
- $readline = new \Hoa\Console\Readline();
+ $readline = new Console\Readline();
$readline->setAutocompleter(
- new \Hoa\Console\Readline\Autocompleter\Word($words)
+ new Console\Readline\Autocompleter\Word($words)
);
$expression = '.h';
- do { try {
-
- if('.' === $expression[0])
- @list($expression, $tail) = explode(' ', $expression);
-
- switch($expression) {
-
- case '.h':
- case '.help':
- echo 'Usage:', "\n",
- ' .h[elp] to print this help;', "\n",
- ' .c[lear] to clear the screen;', "\n",
- ' .v[ariables] to print all variables;', "\n",
- ' .s[ample] to sample a value of a variable;', "\n",
- ' .u[nset] to unset a variable;', "\n",
- ' .d[ump] to dump the tree of the expression;', "\n",
- ' .q[uit] to quit.', "\n";
- break;
-
- case '.c':
- case '.clear':
- \Hoa\Console\Cursor::clear('↕');
- break;
-
- case '.v':
- case '.variables':
- foreach($interpreter->getClause() as $variable)
- echo $variable->getName(), ': ',
- $variable->getHeld()->toPraspel(), "\n";
- break;
-
- case '.s':
- case '.sample':
- if(null === $tail) {
-
- echo 'You must precise a variable name.', "\n";
- break;
+ do {
+ try {
+ if ('.' === $expression[0]) {
+ @list($expression, $tail) = explode(' ', $expression);
}
- $_clause = $interpreter->getClause();
+ switch ($expression) {
+ case '.h':
+ case '.help':
+ echo
+ 'Usage:', "\n",
+ ' .h[elp] to print this help;', "\n",
+ ' .c[lear] to clear the screen;', "\n",
+ ' .v[ariables] to print all variables;', "\n",
+ ' .s[ample] to sample a value of a variable;', "\n",
+ ' .u[nset] to unset a variable;', "\n",
+ ' .d[ump] to dump the tree of the expression;', "\n",
+ ' .q[uit] to quit.', "\n";
- if(!isset($_clause[$tail])) {
+ break;
- echo 'Variable ', $tail, ' does not exist.', "\n";
- break;
- }
+ case '.c':
+ case '.clear':
+ Console\Cursor::clear('↕');
- $_variable = $_clause[$tail];
- var_export($_variable->sample());
- echo "\n";
- $_variable->reset();
- break;
+ break;
- case '.u':
- case '.unset':
- if(null === $tail) {
+ case '.v':
+ case '.variables':
+ foreach ($interpreter->getClause() as $variable) {
+ echo
+ $variable->getName(), ': ',
+ $variable->getHeld()->toPraspel(), "\n";
+ }
- echo 'You must precise a variable name.', "\n";
- break;
- }
+ break;
- $_clause = $interpreter->getClause();
- unset($_clause[$tail]);
- break;
+ case '.s':
+ case '.sample':
+ if (null === $tail) {
+ echo 'You must precise a variable name.', "\n";
- case '.d':
- case '.dump':
- echo $dump->visit($interpreter->getRoot());
- break;
+ break;
+ }
- case '.q':
- case '.quit':
- break 2;
+ $_clause = $interpreter->getClause();
- default:
- if(null === $expression)
- break;
+ if (!isset($_clause[$tail])) {
+ echo 'Variable ', $tail, ' does not exist.', "\n";
- $interpreter->visit(
- $compiler->parse($expression, 'expression')
- );
- break;
+ break;
+ }
- } }
+ $_variable = $_clause[$tail];
+ var_export($_variable->sample());
+ echo "\n";
+ $_variable->reset();
- catch ( \Exception $e ) {
+ break;
- echo $e->getMessage(), "\n";
- }
+ case '.u':
+ case '.unset':
+ if (null === $tail) {
+ echo 'You must precise a variable name.', "\n";
- echo "\n";
+ break;
+ }
- } while(false !== $expression = $readline->readLine('> '));
+ $_clause = $interpreter->getClause();
+ unset($_clause[$tail]);
+
+ break;
+
+ case '.d':
+ case '.dump':
+ echo $dump->visit($interpreter->getRoot());
+
+ break;
+
+ case '.q':
+ case '.quit':
+ break 2;
+
+ default:
+ if (null === $expression) {
+ break;
+ }
+
+ $interpreter->visit(
+ $compiler->parse($expression, 'expression')
+ );
+
+ break;
+
+ }
+ } catch (\Exception $e) {
+ echo $e->getMessage(), "\n";
+ }
+
+ echo "\n";
+ } while (false !== $expression = $readline->readLine('> '));
return;
}
@@ -259,22 +222,20 @@ class Shell extends \Hoa\Console\Dispatcher\Kit {
/**
* The command usage.
*
- * @access public
* @return int
*/
- public function usage ( ) {
-
- echo 'Usage : praspel:shell <options> [expression]', "\n",
- 'Options :', "\n",
- $this->makeUsageOptionsList(array(
- 'help' => 'This help.'
- )), "\n";
+ public function usage()
+ {
+ echo
+ 'Usage : praspel:shell <options> [expression]', "\n",
+ 'Options :', "\n",
+ $this->makeUsageOptionsList([
+ 'help' => 'This help.'
+ ]), "\n";
return;
}
}
-}
-
__halt_compiler();
Interactive Praspel shell.