From 036e7efcb9f4e80c8d76b9d1486c122915b702a1 Mon Sep 17 00:00:00 2001 From: _ov4 Date: Sat, 5 Sep 2026 15:13:01 +0000 Subject: parser working --- ast.cc | 11 +++++++++-- include/parser/ast.h | 44 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/ast.cc b/ast.cc index a0af720..66bd451 100644 --- a/ast.cc +++ b/ast.cc @@ -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 #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++; + } } } -- cgit v1.3.1-4-g156e