Commit e7c9e56f authored by LONGMAN's avatar LONGMAN

Improved calc and whoami commands

parent 449740ed
......@@ -19,7 +19,8 @@
],
"require": {
"php": ">=5.3.0",
"ext-pdo": "*"
"ext-pdo": "*",
"hoa/math": "~0.0"
},
"autoload": {
"psr-4": {
......
......@@ -33,16 +33,27 @@ class CalcCommand extends Command
$data['reply_to_message_id'] = $message_id;
$data['text'] = $this->compute($text);
$result = Request::sendMessage($data);
}
protected function compute($text) {
$text = preg_replace('/[^0-9\+\-\*\/\(\) ]/i', '', trim($text));
$compute = create_function('', 'return (' . trim($text) . ');' );
$result = 0 + $compute();
protected function compute($expression) {
// Load the compiler
$compiler = \Hoa\Compiler\Llk::load(
new \Hoa\File\Read('hoa://Library/Math/Arithmetic.pp')
);
// Load the visitor, aka the "evaluator"
$visitor = new \Hoa\Math\Visitor\Arithmetic();
// Parse the expression
$ast = $compiler->parse($expression);
// Evaluate
$result = $visitor->visit($ast);
return $result;
}
......
......@@ -32,8 +32,9 @@ class WhoamiCommand extends Command
$data = array();
$data['chat_id'] = $chat_id;
$data['reply_to_message_id'] = $message_id;
$data['text'] = 'Your name is: ' . $message->getFrom()->getFirstName().' '.$message->getFrom()->getLastName();
$data['text'] = "\n".'Username: ' . $message->getFrom()->getUsername();
$data['text'] = 'Your Id: ' . $message->getFrom()->getId();
$data['text'] .= "\n".'Name: ' . $message->getFrom()->getFirstName().' '.$message->getFrom()->getLastName();
$data['text'] .= "\n".'Username: ' . $message->getFrom()->getUsername();
$result = Request::sendMessage($data);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment