diff options
| author | _ov4 <ov4@ov4.org> | 2026-09-05 15:13:01 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-09-05 15:13:01 +0000 |
| commit | 036e7efcb9f4e80c8d76b9d1486c122915b702a1 (patch) | |
| tree | d5379b5248a6b980922e663e9da61a75c3318c41 | |
| parent | 16a3288af9d214d11feb0c1c3bdb7ecaca2bfdd0 (diff) | |
parser working
| -rw-r--r-- | ast.cc | 11 | ||||
| -rw-r--r-- | include/parser/ast.h | 44 |
2 files changed, 50 insertions, 5 deletions
@@ -21,7 +21,7 @@ constexpr int MAXLINE = 1024; int main() { - int test_case = 3; + int test_case = 4; string s; switch (test_case) @@ -38,6 +38,9 @@ int main() case 3: s = "hi (hello || foo)"; break; + case 4: + s = "alpha || beta (gamma && (delta || epsilon & ) )"; + break; default: return 0; } @@ -45,7 +48,7 @@ int main() tokenizer(s); - cout << "----------" << endl; + cout << "---------- tokenizer ----------" << endl; for (auto c : token) { if (c.token_type == LOGIC_AND) @@ -63,5 +66,9 @@ int main() else if (c.token_type == TEXT) println("{} TEXT {}", c.bracket_depth, c.text); } + + cout << "---------- parse ----------" << endl; + + parse(0, token.size(), alloc_ast()); return 0; } diff --git a/include/parser/ast.h b/include/parser/ast.h index 151afb0..75c4004 100644 --- a/include/parser/ast.h +++ b/include/parser/ast.h @@ -3,6 +3,7 @@ #include <string> #include "parser/enum_type.h" +#include "parser/token.h" #include "parser/shared.h" using namespace std; // remove this @@ -10,6 +11,8 @@ using namespace std; // remove this namespace ov4 { +class T_token; + class T_ast { public: @@ -17,8 +20,8 @@ public: enum_token_type token_type; string command_text; - size_t left; - size_t right; + size_t left = string::npos; + size_t right = string::npos; }; size_t alloc_ast() @@ -48,9 +51,44 @@ int get_pivot_order(enum_token_type x) 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++; + } } } |
