Hoa central
Public Member Functions | Protected Attributes | List of all members
Hoa\Math\Bin\Calc Class Reference
Inheritance diagram for Hoa\Math\Bin\Calc:

Public Member Functions

 main ()
 
 usage ()
 
- Public Member Functions inherited from Hoa\Console\Dispatcher\Kit
 __construct (Router $router, Dispatcher $dispatcher, View\Viewable $view=null)
 
 getOption (&$optionValue, $short=null)
 
 setOptions (Array $options)
 
 makeUsageOptionsList (Array $definitions=[])
 
 resolveOptionAmbiguity (Array $solutions)
 
 status ($text, $status)
 
 readLine ($prefix=null)
 
 readPassword ($prefix=null)
 
- Public Member Functions inherited from Hoa\Dispatcher\Kit
 __construct (Router $router, Dispatcher $dispatcher, View\Viewable $view=null)
 
 construct ()
 

Protected Attributes

 $options
 
- Protected Attributes inherited from Hoa\Console\Dispatcher\Kit
 $options = null
 
 $_options = null
 

Additional Inherited Members

- Public Attributes inherited from Hoa\Console\Dispatcher\Kit
 $parser = null
 
- Public Attributes inherited from Hoa\Dispatcher\Kit
 $router = null
 
 $dispatcher = null
 
 $view = null
 
 $data = null
 

Detailed Description

Class .

A simple calculator.

Definition at line 52 of file Calc.php.

Member Function Documentation

Hoa\Math\Bin\Calc::main ( )

The entry method.

Returns
int

Definition at line 71 of file Calc.php.

72  {
73  while (false !== $c = $this->getOption($v)) {
74  switch ($c) {
75  case 'h':
76  case '?':
77  return $this->usage();
78 
79  case '__ambiguous':
80  $this->resolveOptionAmbiguity($v);
81 
82  break;
83  }
84  }
85 
86  $this->parser->listInputs($expression);
87 
88  $compiler = Compiler\Llk::load(
89  new File\Read('hoa://Library/Math/Arithmetic.pp')
90  );
91  $visitor = new Math\Visitor\Arithmetic();
92  $dump = new Compiler\Visitor\Dump();
93 
94  if (null !== $expression) {
95  $ast = $compiler->parse($expression);
96  echo $expression . ' = ' . $visitor->visit($ast), "\n";
97 
98  return;
99  }
100 
101  $readline = new Console\Readline();
102  $readline->setAutocompleter(
103  new Console\Readline\Autocompleter\Word(
104  array_merge(
105  array_keys($visitor->getConstants()->getArrayCopy()),
106  array_keys($visitor->getFunctions()->getArrayCopy())
107  )
108  )
109  );
110  $handle = null;
111  $expression = 'h';
112 
113  do {
114  switch ($expression) {
115  case 'h':
116  case 'help':
117  echo
118  'Usage:', "\n",
119  ' h[elp] to print this help;', "\n",
120  ' c[onstants] to print available constants;', "\n",
121  ' f[unctions] to print available functions;', "\n",
122  ' e[xpression] to print the current expression;', "\n",
123  ' d[ump] to dump the tree of the expression;', "\n",
124  ' q[uit] to quit.', "\n";
125 
126  break;
127 
128  case 'c':
129  case 'constants':
130  echo
131  implode(
132  ', ',
133  array_keys(
134  $visitor->getConstants()->getArrayCopy()
135  )
136  ),
137  "\n";
138 
139  break;
140 
141  case 'f':
142  case 'functions':
143  echo
144  implode(
145  ', ',
146  array_keys(
147  $visitor->getFunctions()->getArrayCopy()
148  )
149  ),
150  "\n";
151 
152  break;
153 
154  case 'e':
155  case 'expression':
156  echo $handle, "\n";
157 
158  break;
159 
160  case 'd':
161  case 'dump':
162  if (null === $handle) {
163  echo 'Type a valid expression before (“> 39 + 3”).', "\n";
164  } else {
165  echo $dump->visit($compiler->parse($handle)), "\n";
166  }
167 
168  break;
169 
170  case 'q':
171  case 'quit':
172  break 2;
173 
174  default:
175  if (null === $expression) {
176  break;
177  }
178 
179  try {
180  echo $visitor->visit($compiler->parse($expression)), "\n";
181  } catch (Compiler\Exception $e) {
182  echo $e->getMessage(), "\n";
183 
184  break;
185  }
186 
187  $handle = $expression;
188 
189  break;
190  }
191  } while (false !== $expression = $readline->readLine('> '));
192 
193  return;
194  }
getOption(&$optionValue, $short=null)
Definition: Kit.php:104
resolveOptionAmbiguity(Array $solutions)
Definition: Kit.php:190

Here is the call graph for this function:

Hoa\Math\Bin\Calc::usage ( )

The command usage.

Returns
int

Definition at line 201 of file Calc.php.

202  {
203  echo
204  'Usage : math:calc <options> [expression]', "\n",
205  'Options :', "\n",
206  $this->makeUsageOptionsList([
207  'help' => 'This help.'
208  ]), "\n";
209 
210  return;
211  }
makeUsageOptionsList(Array $definitions=[])
Definition: Kit.php:149

Here is the call graph for this function:

Here is the caller graph for this function:

Member Data Documentation

Hoa\Math\Bin\Calc::$options
protected
Initial value:

Definition at line 59 of file Calc.php.


The documentation for this class was generated from the following file: