Commit e7c9e56f authored by LONGMAN's avatar LONGMAN

Improved calc and whoami commands

parent 449740ed
...@@ -19,7 +19,8 @@ ...@@ -19,7 +19,8 @@
], ],
"require": { "require": {
"php": ">=5.3.0", "php": ">=5.3.0",
"ext-pdo": "*" "ext-pdo": "*",
"hoa/math": "~0.0"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
......
...@@ -33,16 +33,27 @@ class CalcCommand extends Command ...@@ -33,16 +33,27 @@ class CalcCommand extends Command
$data['reply_to_message_id'] = $message_id; $data['reply_to_message_id'] = $message_id;
$data['text'] = $this->compute($text); $data['text'] = $this->compute($text);
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
} }
protected function compute($text) { protected function compute($expression) {
$text = preg_replace('/[^0-9\+\-\*\/\(\) ]/i', '', trim($text));
$compute = create_function('', 'return (' . trim($text) . ');' ); // Load the compiler
$result = 0 + $compute(); $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; return $result;
} }
......
...@@ -32,8 +32,9 @@ class WhoamiCommand extends Command ...@@ -32,8 +32,9 @@ class WhoamiCommand extends Command
$data = array(); $data = array();
$data['chat_id'] = $chat_id; $data['chat_id'] = $chat_id;
$data['reply_to_message_id'] = $message_id; $data['reply_to_message_id'] = $message_id;
$data['text'] = 'Your name is: ' . $message->getFrom()->getFirstName().' '.$message->getFrom()->getLastName(); $data['text'] = 'Your Id: ' . $message->getFrom()->getId();
$data['text'] = "\n".'Username: ' . $message->getFrom()->getUsername(); $data['text'] .= "\n".'Name: ' . $message->getFrom()->getFirstName().' '.$message->getFrom()->getLastName();
$data['text'] .= "\n".'Username: ' . $message->getFrom()->getUsername();
$result = Request::sendMessage($data); $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