summaryrefslogtreecommitdiff
path: root/src/parser.cpp
blob: 68c2297ea56d5ae47db03532815be6bb2bd7a401 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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;
  }
}