summaryrefslogtreecommitdiff
path: root/src/include
diff options
context:
space:
mode:
Diffstat (limited to 'src/include')
-rw-r--r--src/include/calculator.hpp6
-rw-r--r--src/include/operand.hpp10
-rw-r--r--src/include/parser.hpp24
-rw-r--r--src/include/product.hpp9
-rw-r--r--src/include/substraction.hpp8
-rw-r--r--src/include/sum.hpp8
-rw-r--r--src/include/syntax_tree.hpp12
-rw-r--r--src/include/tokenizer.hpp17
8 files changed, 86 insertions, 8 deletions
diff --git a/src/include/calculator.hpp b/src/include/calculator.hpp
index 8c5e97a..2362258 100644
--- a/src/include/calculator.hpp
+++ b/src/include/calculator.hpp
@@ -8,9 +8,11 @@
class Calculator {
private:
+ // Private attributes.
LCD_I2C *display;
Keypad *default_keypad;
Tokenizer tokenizer = Tokenizer();
+ Token **tokens;
char default_keypad_chars[4][4] = {
{'1', '2', '3', '+'},
{'4', '5', '6', '-'},
@@ -25,6 +27,10 @@ private:
};
uint keypad_col_pins[4] = {6, 7, 8, 9};
uint keypad_row_pins[4] = {16, 17, 18, 19};
+ // End of private attributes.
+ // Private methods.
+ void processInputs();
+ // End of private methods.
public:
Calculator();
~Calculator();
diff --git a/src/include/operand.hpp b/src/include/operand.hpp
new file mode 100644
index 0000000..cc638de
--- /dev/null
+++ b/src/include/operand.hpp
@@ -0,0 +1,10 @@
+#pragma once
+#include "syntax_tree.hpp"
+
+
+class Operand: SyntaxTree {
+ int value;
+public:
+ Operand(int value);
+ float eval() const;
+};
diff --git a/src/include/parser.hpp b/src/include/parser.hpp
new file mode 100644
index 0000000..7bc7997
--- /dev/null
+++ b/src/include/parser.hpp
@@ -0,0 +1,24 @@
+/* Grammar
+ * Expression --> Term PrimeExpression
+ * PrimeExpression --> (+|-)Expression | Null
+ * Term --> Factor PrimeTerm
+ * PrimeTerm --> (*|/)Term | Null
+ * Factor --> -Expression | (Expression) | operand */
+#pragma once
+#include "tokenizer.hpp"
+#include "syntax_tree.hpp"
+
+
+class Parser {
+private:
+ // Private methods.
+ void parseExpression();
+ void parsePrimeExpression();
+ void parseTerm();
+ void parsePrimeTerm();
+ void parseFactor();
+ // End of private methods.
+public:
+ Parser();
+ const SyntaxTree *parse();
+};
diff --git a/src/include/product.hpp b/src/include/product.hpp
new file mode 100644
index 0000000..1a87f80
--- /dev/null
+++ b/src/include/product.hpp
@@ -0,0 +1,9 @@
+#pragma once
+#include "syntax_tree.hpp"
+
+
+class Product: SyntaxTree {
+public:
+ Product(SyntaxTree *left, SyntaxTree *right);
+ float eval() const;
+};
diff --git a/src/include/substraction.hpp b/src/include/substraction.hpp
new file mode 100644
index 0000000..4a5a8c7
--- /dev/null
+++ b/src/include/substraction.hpp
@@ -0,0 +1,8 @@
+#pragma once
+#include "syntax_tree.hpp"
+
+class Substraction: SyntaxTree {
+public:
+ Substraction(SyntaxTree *left, SyntaxTree *right);
+ float eval() const;
+};
diff --git a/src/include/sum.hpp b/src/include/sum.hpp
new file mode 100644
index 0000000..35d6736
--- /dev/null
+++ b/src/include/sum.hpp
@@ -0,0 +1,8 @@
+#pragma once
+#include "syntax_tree.hpp"
+
+class Sum: SyntaxTree {
+public:
+ Sum(SyntaxTree *left, SyntaxTree *right);
+ float eval() const;
+};
diff --git a/src/include/syntax_tree.hpp b/src/include/syntax_tree.hpp
new file mode 100644
index 0000000..9edf23c
--- /dev/null
+++ b/src/include/syntax_tree.hpp
@@ -0,0 +1,12 @@
+#pragma once
+#include "tokenizer.hpp"
+
+class SyntaxTree {
+protected:
+ const SyntaxTree *left {};
+ const SyntaxTree *right {};
+public:
+ SyntaxTree(SyntaxTree *left = nullptr, SyntaxTree *right = nullptr);
+ virtual ~SyntaxTree();
+ virtual float eval() const = 0;
+};
diff --git a/src/include/tokenizer.hpp b/src/include/tokenizer.hpp
index a3f9998..7d00055 100644
--- a/src/include/tokenizer.hpp
+++ b/src/include/tokenizer.hpp
@@ -1,7 +1,7 @@
#pragma once
#include <regex>
#include <string>
-#include <array>
+#define TOKENS_ARRAY_LEN 16
enum Type {
operand,
@@ -11,7 +11,8 @@ enum Type {
division,
left_parens,
right_parens,
- equals
+ equals,
+ nil
};
struct Token {
@@ -22,14 +23,14 @@ struct Token {
class Tokenizer {
private:
// Private attributes
- std::array<Token *, 16> tokens;
+ Token *tokens[17];
const std::string *to_tokenize;
size_t current_char_index;
short tokens_head;
// Regexes
- std::regex operand_regex = std::regex("^0{1}|\\d+");
- std::regex operation_regex = std::regex("\\+|-|/|\\*");
- std::regex parens_regex = std::regex("(|)");
+ std::regex operand_regex = std::regex("^0{1}|^\\d+");
+ std::regex operation_regex = std::regex("^\\+|^-|/|^\\*");
+ std::regex parens_regex = std::regex("^\\(|^\\)");
// End of private attributes
// Private methods
void clearTokens();
@@ -42,6 +43,6 @@ private:
public:
Tokenizer();
~Tokenizer();
- const std::array<Token *, 16> &tokenize(const std::string &operation);
- const std::array<Token *, 16> &getTokens();
+ Token **tokenize(const std::string &operation);
+ Token **getTokens();
};