diff options
| author | _ov4 <ov4@ov4.org> | 2026-10-04 18:09:43 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-10-04 18:09:43 +0000 |
| commit | fda4936771831605db13a3f7b5d85bb49db3998c (patch) | |
| tree | acca70a7848e9b0c5311d6fbab696ef152881ebb /src | |
| parent | 98f08f3a9511fba9d818bae3eb23ba03b525d749 (diff) | |
refactor eval
Diffstat (limited to 'src')
| -rw-r--r-- | src/eval/eval.cc | 61 | ||||
| -rw-r--r-- | src/eval/eval_exe.cc (renamed from src/eval.cc) | 87 | ||||
| -rw-r--r-- | src/eval/eval_tree_cd.cc | 56 |
3 files changed, 119 insertions, 85 deletions
diff --git a/src/eval/eval.cc b/src/eval/eval.cc new file mode 100644 index 0000000..83eccd7 --- /dev/null +++ b/src/eval/eval.cc @@ -0,0 +1,61 @@ +#include "eval.h" + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include <ctype.h> +#include <signal.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <errno.h> +#include <iostream> +#include <vector> +#include <string> + +#include "magic_enum/magic_enum.hpp" + +#include "error.h" +#include "lang_analysis/lexer_class.h" +#include "lang_analysis/ast.h" +#include "lang_analysis/token.h" +#include "handler.h" + +using namespace std; + +namespace ov4 +{ + +/* + * eval - Evaluate the command line that the user has just typed in + * + * If the user has requested a built-in command (quit, jobs, bg or fg) + * then execute it immediately. Otherwise, fork a child process and + * run the job in the context of the child. If the job is running in + * the foreground, wait for it to terminate and then return. Note: + * each child process must have a unique process group ID so that our + * background children don't receive SIGINT (SIGTSTP) from the kernel + * when we type ctrl-c (ctrl-z) at the keyboard. +*/ +void eval(char *raw_input) +{ + string s = raw_input; + T_lexer lexer_instance; + + try + { + lexer_instance.tokenizer(s); + lexer_instance.parse(0, lexer_instance.token.size(), lexer_instance.alloc_ast()); + } + catch(const T_error &e) + { + cerr << "Error: " << magic_enum::enum_name(e.code) << endl; + cerr << "what: " << e.what() << endl; + return; + } + + eval_tree_cd(0, lexer_instance, false); + return; +} + +} diff --git a/src/eval.cc b/src/eval/eval_exe.cc index 9db9b9d..534b878 100644 --- a/src/eval.cc +++ b/src/eval/eval_exe.cc @@ -1,20 +1,7 @@ #include "eval.h" -#include <stdio.h> -#include <stdlib.h> -#include <unistd.h> -#include <string.h> -#include <ctype.h> -#include <signal.h> -#include <sys/types.h> -#include <sys/wait.h> -#include <errno.h> -#include <iostream> -#include <vector> #include <string> -#include "magic_enum/magic_enum.hpp" - #include "error.h" #include "lang_analysis/lexer_class.h" #include "lang_analysis/ast.h" @@ -22,7 +9,7 @@ #include "lang_analysis/enum_type.h" #include "lang_analysis/symbol_property.h" #include "lang_analysis/parseline.h" -#include "handler.h" +#include "util/io.h" using namespace std; @@ -169,74 +156,4 @@ int eval_exe(const string &s, bool is_async, const T_lexer *lexer_instance, size return 0; } -int eval_tree_cd(size_t i, const T_lexer &lexer_instance, bool inside_subshell) -{ - // TODO: async - - int ret = -1; - - println("eval_tree_cd: processing {} subshell {}", i, (lexer_instance.ast[i].subshell ? "TRUE" : "FALSE")); - - // if it's a subshell, and current AST's subshell status is not subshell - // (subshell should be forkerd already, then set inside_subshell = true) - // fork itself - if (lexer_instance.ast[i].subshell && !inside_subshell) - { - eval_exe("", false, &lexer_instance, i); - return 0; - } - - if (lexer_instance.ast[i].token_type == TEXT) - { - ret = eval_exe(lexer_instance.ast[i].command_text, false); - return ret; - } - - if (lexer_instance.ast[i].left != string::npos) - { - ret = eval_tree_cd(lexer_instance.ast[i].left, lexer_instance, false); - } - - if (lexer_instance.ast[i].right != string::npos) - { - if (lexer_instance.ast[i].token_type == LOGIC_AND && ret != 0) return ret; - if (lexer_instance.ast[i].token_type == LOGIC_OR && ret == 0) return ret; - - ret = eval_tree_cd(lexer_instance.ast[i].right, lexer_instance, false); - } - return ret; -} - -/* - * eval - Evaluate the command line that the user has just typed in - * - * If the user has requested a built-in command (quit, jobs, bg or fg) - * then execute it immediately. Otherwise, fork a child process and - * run the job in the context of the child. If the job is running in - * the foreground, wait for it to terminate and then return. Note: - * each child process must have a unique process group ID so that our - * background children don't receive SIGINT (SIGTSTP) from the kernel - * when we type ctrl-c (ctrl-z) at the keyboard. -*/ -void eval(char *raw_input) -{ - string s = raw_input; - T_lexer lexer_instance; - - try - { - lexer_instance.tokenizer(s); - lexer_instance.parse(0, lexer_instance.token.size(), lexer_instance.alloc_ast()); - } - catch(const T_error &e) - { - cerr << "Error: " << magic_enum::enum_name(e.code) << endl; - cerr << "what: " << e.what() << endl; - return; - } - - eval_tree_cd(0, lexer_instance, false); - return; -} - -} +}
\ No newline at end of file diff --git a/src/eval/eval_tree_cd.cc b/src/eval/eval_tree_cd.cc new file mode 100644 index 0000000..7d7c1af --- /dev/null +++ b/src/eval/eval_tree_cd.cc @@ -0,0 +1,56 @@ +#include "eval.h" + +#include <string> + +#include "error.h" +#include "lang_analysis/lexer_class.h" +#include "lang_analysis/ast.h" +#include "lang_analysis/token.h" +#include "lang_analysis/enum_type.h" +#include "lang_analysis/symbol_property.h" +#include "util/io.h" + +using namespace std; + +namespace ov4 +{ + +int eval_tree_cd(size_t i, const T_lexer &lexer_instance, bool inside_subshell) +{ + // TODO: async + + int ret = -1; + + println("eval_tree_cd: processing {} subshell {}", i, (lexer_instance.ast[i].subshell ? "TRUE" : "FALSE")); + + // if it's a subshell, and current AST's subshell status is not subshell + // (subshell should be forkerd already, then set inside_subshell = true) + // fork itself + if (lexer_instance.ast[i].subshell && !inside_subshell) + { + eval_exe("", false, &lexer_instance, i); + return 0; + } + + if (lexer_instance.ast[i].token_type == TEXT) + { + ret = eval_exe(lexer_instance.ast[i].command_text, false); + return ret; + } + + if (lexer_instance.ast[i].left != string::npos) + { + ret = eval_tree_cd(lexer_instance.ast[i].left, lexer_instance, false); + } + + if (lexer_instance.ast[i].right != string::npos) + { + if (lexer_instance.ast[i].token_type == LOGIC_AND && ret != 0) return ret; + if (lexer_instance.ast[i].token_type == LOGIC_OR && ret == 0) return ret; + + ret = eval_tree_cd(lexer_instance.ast[i].right, lexer_instance, false); + } + return ret; +} + +}
\ No newline at end of file |
