aboutsummaryrefslogtreecommitdiff
path: root/include/parser/ast.h
blob: 75c4004f15ce684a198106798ee8b5b6c06e5f93 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

#pragma once

#include <string>

#include "parser/enum_type.h"
#include "parser/token.h"
#include "parser/shared.h"

using namespace std; // remove this

namespace ov4
{

class T_token;

class T_ast
{
public:
    T_ast() = default;

    enum_token_type token_type;
    string command_text;
    size_t left = string::npos;
    size_t right = string::npos;
};

size_t alloc_ast()
{
    ast.emplace_back();
    return ast_index++;
}

int get_pivot_order(enum_token_type x)
{
    switch(x)
    {
        case ASYNC:
        case SEMICOLON:
            return 1000;
        
        case LOGIC_AND:
        case LOGIC_OR:
            return 500;

        case PIPE:
            return 100;
            
        default:
            return 0;
    }
    return 0;
}

bool is_operator(enum_token_type x)
{
    switch(x)
    {
        case LOGIC_AND:
        case LOGIC_OR:
        case PIPE:
        case ASYNC:
            return true;
        default:
            return false;
    }
}

void parse(size_t cmd_begin, size_t cmd_end, size_t ast_vec)
{
    int current_depth = 0;
    int current_order = -1;
    size_t found_pos = string::npos;

    while (found_pos == string::npos)
    {
        // scan from right to left
        for (size_t i = cmd_end - 1; i < cmd_end; i--)
        {
            // if same depth
            // and higher order number
            if (token[i].bracket_depth == current_depth 
                && is_operator(token[i].token_type) 
                && get_pivot_order(token[i].token_type) > current_order)
            {
                current_order =  get_pivot_order(token[i].token_type);
                found_pos = i;
                println("Found pos: {} order: {} depth: {}", i, current_order, current_depth);
            }
        }
        current_depth++;
    }
}

}