summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHombreLaser <buran@silosneeded.com>2024-07-15 22:52:10 -0600
committerHombreLaser <buran@silosneeded.com>2024-07-15 22:52:10 -0600
commitee906c84b866cd175b31b6d90778b3d8c19212c8 (patch)
treead80d5ba1acf88538f0a78a9c15b83c506443c5e
parent404826e78a56e15e20d3938aed80945295921745 (diff)
Add parserHEADmaster
-rw-r--r--src/include/parser.hpp8
-rw-r--r--src/parser.cpp63
2 files changed, 70 insertions, 1 deletions
diff --git a/src/include/parser.hpp b/src/include/parser.hpp
index 7bc7997..1399943 100644
--- a/src/include/parser.hpp
+++ b/src/include/parser.hpp
@@ -5,12 +5,18 @@
* PrimeTerm --> (*|/)Term | Null
* Factor --> -Expression | (Expression) | operand */
#pragma once
+#include <string>
#include "tokenizer.hpp"
#include "syntax_tree.hpp"
class Parser {
private:
+ // Private attributes.
+ Tokenizer tokenizer = Tokenizer();
+ Token **tokens;
+ Token *current_token;
+ // End of private attributes.
// Private methods.
void parseExpression();
void parsePrimeExpression();
@@ -20,5 +26,5 @@ private:
// End of private methods.
public:
Parser();
- const SyntaxTree *parse();
+ const SyntaxTree *parse(const std::string &input);
};
diff --git a/src/parser.cpp b/src/parser.cpp
new file mode 100644
index 0000000..68c2297
--- /dev/null
+++ b/src/parser.cpp
@@ -0,0 +1,63 @@
+#include <string>
+#include "include/tokenizer.hpp"
+#include "include/syntax_tree.hpp"
+#include "include/parser.hpp"
+
+const SyntaxTree *Parser::parse(const std::string &input) {
+ tokens = tokenizer.tokenize(input);
+ current_token = tokens[0];
+
+ return nullptr;
+}
+
+void Parser::parseExpression() {
+ parseTerm();
+ parsePrimeExpression();
+}
+
+void Parser::parsePrimeExpression() {
+ if(current_token->type == nil)
+ return;
+
+ if(current_token->type == sum || current_token->type == substraction) {
+ ++current_token;
+ parseExpression();
+ }
+}
+
+void Parser::parseTerm() {
+ parseFactor();
+ parsePrimeTerm();
+}
+
+void Parser::parsePrimeTerm() {
+ if(current_token->type == nil)
+ return;
+
+ if(current_token->type == multiplication ||
+ current_token->value == division) {
+ ++current_token;
+ parseTerm();
+ }
+}
+
+void Parser::parseFactor() {
+ switch(current_token->type) {
+ case substraction:
+ ++current_token;
+ parseExpression();
+ break;
+ case left_parens:
+ ++current_token;
+ parseExpression();
+ if(current_token->type != right_parens)
+ return;
+ ++current_token;
+ break;
+ case operand:
+ ++current_token;
+ break;
+ default:
+ return;
+ }
+}