From ee906c84b866cd175b31b6d90778b3d8c19212c8 Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Mon, 15 Jul 2024 22:52:10 -0600 Subject: Add parser --- src/parser.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/parser.cpp (limited to 'src/parser.cpp') 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 +#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; + } +} -- cgit v1.2.3