#pragma once // SleelaTerminal(TM) shell -- L1 core model. // // Original SLeeLa work (see NOTICE): a clean-room shell, not derived from Bash. // This header defines the shared value/token/AST types and the runtime // Environment. Everything above depends on these; this file depends on nothing // in the project. #include #include #include #include #include namespace sleela::sh { struct Value { std::string text; Value() = default; Value(std::string s) : text(std::move(s)) {} long asInt() const noexcept; static Value fromInt(long v); }; enum class Tok { Word, Assignment, Pipe, Semi, Newline, AndIf, OrIf, Less, Great, DGreat, DLess, DLessDash, LParen, RParen, LBrace, RBrace, If, Then, Elif, Else, Fi, While, Until, Do, Done, For, In, Case, Esac, Amp, Bang, HeredocBody, Eof }; struct Token { Tok kind = Tok::Eof; std::string text; int line = 0; int col = 0; }; enum class RedirOp { In, Out, Append, Heredoc }; struct Redirection { RedirOp op = RedirOp::Out; int fd = -1; std::string target; std::string body; bool expand_body = true; }; enum class NodeKind { Simple, Pipeline, AndOr, List, If, While, Until, For, Case, FunctionDef }; struct Node; using NodePtr = std::unique_ptr; struct CaseItem { std::vector patterns; NodePtr body; }; struct Assignment { std::string name; std::string value; }; enum class AndOrOp { And, Or }; struct Node { NodeKind kind; std::vector assigns; std::vector words; std::vector redirs; std::vector children; std::vector child_async; bool negated = false; std::vector andor_ops; std::vector if_conds; std::vector if_bodies; NodePtr else_body; NodePtr while_cond; NodePtr while_body; std::string for_var; std::vector for_words; NodePtr for_body; std::string case_subject; std::vector case_items; std::string func_name; NodePtr func_body; explicit Node(NodeKind k) : kind(k) {} }; class Environment { public: Environment(); std::string get(const std::string& name) const; bool has(const std::string& name) const; void set(const std::string& name, const std::string& value); void unset(const std::string& name); void exportVar(const std::string& name); bool isExported(const std::string& name) const; std::vector exportedEnviron() const; Environment scopedCopy() const; void defineFunction(const std::string& name, std::shared_ptr body); std::shared_ptr lookupFunction(const std::string& name) const; bool hasFunction(const std::string& name) const; void setPositionals(std::vector args); std::vector positionals() const { return positionals_; } std::string getPositional(std::size_t n) const; std::size_t positionalCount() const { return positionals_.size(); } int lastStatus() const noexcept { return last_status_; } void setLastStatus(int s) noexcept { last_status_ = s; } bool shouldExit() const noexcept { return should_exit_; } int exitCode() const noexcept { return exit_code_; } void requestExit(int code) noexcept { should_exit_ = true; exit_code_ = code; } const std::string& prompt() const noexcept { return prompt_; } void setPrompt(std::string prompt) { prompt_ = std::move(prompt); } void requestBreak(int levels) noexcept { loop_break_ = levels > 0 ? levels : 1; } void requestContinue(int levels) noexcept { loop_continue_ = levels > 0 ? levels : 1; } int breakLevels() const noexcept { return loop_break_; } int continueLevels() const noexcept { return loop_continue_; } bool loopSignal() const noexcept { return loop_break_ > 0 || loop_continue_ > 0; } char consumeLoopSignal() noexcept { if (loop_break_ > 0) { if (--loop_break_ > 0) return 'B'; return 'B'; } if (loop_continue_ > 0) { if (--loop_continue_ > 0) return 'B'; return 'C'; } return 0; } struct Job { int id = 0; long pid = 0; std::string command; bool running = true; }; int addJob(long pid, const std::string& command); std::vector& jobs() noexcept { return jobs_; } const std::vector& jobs() const noexcept { return jobs_; } Job* findJob(int id) noexcept; void removeJob(int id) noexcept; private: struct Var { std::string value; bool exported = false; }; std::map vars_; std::map> functions_; std::vector positionals_; std::vector jobs_; std::string prompt_ = "sleela$ "; int next_job_id_ = 1; int last_status_ = 0; bool should_exit_ = false; int exit_code_ = 0; int loop_break_ = 0; int loop_continue_ = 0; }; } // namespace sleela::sh