Commit 14c093f9 authored by Rohan Asokan's avatar Rohan Asokan
Browse files

Updated Input manager methods to accomodate the use of binary mode

parent a4a0e87a
......@@ -6,6 +6,7 @@
#include "inputmanager.h"
#include "mathengine.h"
#include "historymanager.h"
#include <QDebug>
InputManager::InputManager()
{
}
......@@ -30,7 +31,7 @@ const QString &InputManager::result() const
return m_result;
}
void InputManager::append(const QString &subexpression)
void InputManager::append(const QString &subexpression, const bool isBinary)
{
// if expression was from result and input is numeric, clear expression
if(m_moveFromResult && subexpression.size() == 1)
......@@ -43,7 +44,13 @@ void InputManager::append(const QString &subexpression)
}
m_moveFromResult = false;
MathEngine::inst()->parse(m_expression + subexpression);
// Call the corresponding parsing call based on the type of expression.
MathEngine * engineInstance = MathEngine::inst();
if (isBinary) {
engineInstance->parseBinaryExpression(m_expression + subexpression);
} else {
engineInstance->parse(m_expression + subexpression);
}
if(!MathEngine::inst()->error())
{
m_stack.push_back(subexpression.size());
......
......@@ -17,7 +17,7 @@ public:
const QString &expression() const;
void setExpression(const QString &expression);
const QString &result() const;
Q_INVOKABLE void append(const QString &subexpression);
Q_INVOKABLE void append(const QString &subexpression, const bool isBinary=false);
Q_INVOKABLE void backspace();
Q_INVOKABLE void equal();
Q_INVOKABLE void clear();
......
......@@ -4,10 +4,30 @@
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include "mathengine.h"
#include <QDebug>
#include <QRegularExpression>
void MathEngine::parse(QString expr)
{
qDebug() << "Parse Numbers " << expr;
m_driver.parse(expr.toStdString());
m_result = QString::number(m_driver.result);
emit resultChanged();
}
void MathEngine::parseBinaryExpression(QString expr)
{
qDebug() << expr;
bool isNumberPresent = false;
// Match for the numbers
regexMatcher.setPattern(bitRegex);
match = regexMatcher.match(expr);
isNumberPresent = match.hasMatch();
qDebug() << (isNumberPresent ? "Bits Matched" : "No Bits");
// Match for operators
regexMatcher.setPattern(operatorRegex);
match = regexMatcher.match(expr);
qDebug() << (match.hasMatch() ? "Operator Matched" : "No operators");
}
/*
* SPDX-FileCopyrightText: 2020-2021 Han Young <hanyoung@protonmail.com>
* SPDX-FileCopyrightText: 2021-2022 Rohan Asokan <rohan.asokan@students.iiit.ac.in>
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
......@@ -7,6 +8,7 @@
#define MATHENGINE_H
#include "mathengine/driver.hh"
#include <QObject>
#include <QRegularExpression>
class MathEngine : public QObject
{
Q_OBJECT
......@@ -19,6 +21,7 @@ public:
return &singleton;
}
Q_INVOKABLE void parse(QString expr);
Q_INVOKABLE void parseBinaryExpression(QString expr);
QString result()
{
return m_result;
......@@ -34,6 +37,10 @@ private:
MathEngine(){};
driver m_driver;
QString m_result;
const QString bitRegex = QString("[01]+");
const QString operatorRegex = QString("[\\+\\-\\*\\/&\\|~\\^]|<{2}|>{2}");
QRegularExpression regexMatcher;
QRegularExpressionMatch match;
};
#endif
......@@ -116,7 +116,11 @@ Kirigami.Page {
// Uncomment next line for function overlay
// anchors.rightMargin: Kirigami.Units.gridUnit * 1.5
onPressed: {
inputManager.append(text);
if (text == "DEL") {
inputManager.backspace();
} else {
inputManager.append(text, true);
}
}
onClear: inputManager.clear()
}
......
......@@ -10,56 +10,32 @@ import QtQuick.Layouts 1.1
GridLayout {
signal pressed(string text)
signal clear()
columns: 5
columns: 4
rowSpacing: Kirigami.Units.smallSpacing
columnSpacing: Kirigami.Units.smallSpacing
// Features to add:
// 1) Left Shift and Right Shift operators
// 2) Ones and Twos complements
// 3) +, -, x, / operations
//
// Might have to look into adding a function overlay to seperate out uncommon functions (fractional, modulo, abs)
// Rwo Number from bottom: 6
NumberButton {text: ""; onClicked: pressed(text); special: true;}
NumberButton {text: "<<"; onClicked: pressed(text); special: true;}
NumberButton {text: ">>"; onClicked: pressed(text); special: true;}
NumberButton {text: "ONEC"; display: "ones"; onClicked: pressed(text); special: true;}
NumberButton {text: "TWOC"; display: "twos"; onClicked: pressed(text); special: true;}
// Row Number from bottom: 5
NumberButton {text: "(" ; onClicked: pressed(text); special: true;}
NumberButton {text: ")" ; onClicked: pressed(text); special: true;}
// Buttons are from left to right
// Row Number from bottom: 4
NumberButton {text: "<<"; onClicked: pressed(text);}
NumberButton {text: ">>"; onClicked: pressed(text);}
NumberButton {text: "CLEAR"; display: "CLR"; onClicked: clear(); special: true;}
NumberButton {text: "DEL"; display: ""; onClicked: pressed(text); onLongClicked: clear(); special: true;}
NumberButton {text: "XOR"; onClicked: pressed(text); special: true;}
// Row Number from bottom: 4
NumberButton {text: "C"; onClicked: pressed(text);}
NumberButton {text: "D"; onClicked: pressed(text);}
NumberButton {text: "E"; onClicked: pressed(text);}
NumberButton {text: "F"; onClicked: pressed(text);}
NumberButton {text: "NOT"; onClicked: pressed(text); special: true;}
// Row number from bottom: 3
NumberButton {text: "8"; onClicked: pressed(text);}
NumberButton {text: "9"; onClicked: pressed(text);}
NumberButton {text: "A"; onClicked: pressed(text);}
NumberButton {text: "B"; onClicked: pressed(text);}
NumberButton {text: "OR"; onClicked: pressed(text); special: true;}
NumberButton {text: "^"; onClicked: pressed(text); special: true;}
NumberButton {text: "~"; onClicked: pressed(text); special: true;}
NumberButton {text: "|"; onClicked: pressed(text); special: true;}
NumberButton {text: "&"; onClicked: pressed(text); special: true;}
// Row number from bottom: 2
NumberButton {text: "4"; onClicked: pressed(text);}
NumberButton {text: "5"; onClicked: pressed(text);}
NumberButton {text: "6"; onClicked: pressed(text);}
NumberButton {text: "7"; onClicked: pressed(text);}
NumberButton {text: "AND"; onClicked: pressed(text); special: true;}
NumberButton {text: "/"; display: "÷"; onClicked: pressed(text);}
NumberButton {text: "*"; display: "×"; onClicked: pressed(text);}
NumberButton {text: "-"; onClicked: pressed(text);}
NumberButton {text: "+"; onClicked: pressed(text);}
// Row number from bottom: 1
NumberButton {text: "0"; onClicked: pressed(text);}
NumberButton {text: "1"; onClicked: pressed(text);}
NumberButton {text: "2"; onClicked: pressed(text);}
NumberButton {text: "3"; onClicked: pressed(text);}
NumberButton {text: ""; onClicked: pressed(text);}
NumberButton {text: "="; onClicked: pressed(text); special: true;}
}
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