Cadabra
Computer algebra system for field theory problems
cadabra2-cli.hh
Go to the documentation of this file.
1 #include <string>
2 #include <memory>
3 
4 // Work around MSVC linking problem
5 #ifdef _DEBUG
6 #define CADABRA_CLI_DEBUG_MARKER
7 #undef _DEBUG
8 #endif
9 //#include <Python.h>
10 #include <pybind11/pybind11.h>
11 #include <pybind11/embed.h>
12 #ifdef CADABRA_CLI_DEBUG_MARKER
13 #define _DEBUG
14 #undef CADABRA_CLI_DEBUG_MARKER
15 #endif
16 
17 class Shell
18 {
19 public:
20  enum class Flags : unsigned int
21  {
22  None = 0x00,
23  NoBanner = 0x01,
24  IgnoreSemicolons = 0x02,
25  NoColour = 0x04,
26  NoReadline = 0x08,
27  };
28 
29  Shell(Flags flags);
30  ~Shell();
31 
32  void restart();
33  void interact();
34  pybind11::object evaluate(const std::string& code, const std::string& filename = "<stdin>");
35  void execute(const std::string& code, const std::string& filename = "<stdin>");
36  void execute_file(const std::string& filename, bool preprocess = true);
37  void interact_file(const std::string& filename, bool preprocess = true);
38 
39  void write_stdout(const std::string& text, const std::string& end = "\n", bool flush = false);
40  void write_stderr(const std::string& text, const std::string& end = "\n", bool flush = false);
41 
42 private:
43  void set_histfile();
44  std::string histfile;
45  std::string site_path;
46 
47  std::string str(const pybind11::handle& obj);
48  std::string repr(const pybind11::handle& obj);
49  std::string sanitize(std::string s);
50 
51  void process_ps1(const std::string& line);
52  void process_ps2(const std::string& line);
53  void set_completion_callback(const char* buffer, std::vector<std::string>& completions);
54 
55  std::string get_ps1();
56  std::string get_ps2();
57 
58  void handle_error();
59  void handle_error(pybind11::error_already_set& err);
60 
61  pybind11::dict globals;
62  pybind11::object sys;
63  pybind11::object py_stdout, py_stderr;
64  std::string collect;
65 
66  const char* colour_error;
67  const char* colour_warning;
68  const char* colour_info;
69  const char* colour_success;
70  const char* colour_reset;
72  };
73 
74 class ExitRequest : public std::exception
75 {
76 public:
77  ExitRequest();
78  ExitRequest(int code);
79  ExitRequest(const std::string& message);
80 
81  virtual const char* what() const noexcept override;
82 
83  int code;
84  std::string message;
85 };
86 
87 
Shell::Flags & operator|=(Shell::Flags &lhs, Shell::Flags rhs)
Definition: cadabra2-cli.cc:561
bool operator&(Shell::Flags lhs, Shell::Flags rhs)
Definition: cadabra2-cli.cc:574
Shell::Flags operator|(Shell::Flags lhs, Shell::Flags rhs)
Definition: cadabra2-cli.cc:569
Definition: cadabra2-cli.hh:75
virtual const char * what() const noexcept override
Definition: cadabra2-cli.cc:556
ExitRequest()
Definition: cadabra2-cli.cc:547
std::string message
Definition: cadabra2-cli.hh:84
int code
Definition: cadabra2-cli.hh:83
Definition: cadabra2-cli.hh:18
std::string str(const pybind11::handle &obj)
Definition: cadabra2-cli.cc:377
void interact()
Definition: cadabra2-cli.cc:131
std::string site_path
Definition: cadabra2-cli.hh:45
void restart()
Definition: cadabra2-cli.cc:99
void process_ps2(const std::string &line)
Definition: cadabra2-cli.cc:458
std::string sanitize(std::string s)
Definition: cadabra2-cli.cc:392
Flags flags
Definition: cadabra2-cli.hh:71
void set_completion_callback(const char *buffer, std::vector< std::string > &completions)
Definition: cadabra2-cli.cc:471
pybind11::object py_stderr
Definition: cadabra2-cli.hh:63
pybind11::object evaluate(const std::string &code, const std::string &filename="<stdin>")
Definition: cadabra2-cli.cc:203
std::string collect
Definition: cadabra2-cli.hh:64
const char * colour_reset
Definition: cadabra2-cli.hh:70
const char * colour_info
Definition: cadabra2-cli.hh:68
pybind11::object sys
Definition: cadabra2-cli.hh:62
void interact_file(const std::string &filename, bool preprocess=true)
Definition: cadabra2-cli.cc:256
std::string get_ps2()
Definition: cadabra2-cli.cc:499
std::string repr(const pybind11::handle &obj)
Definition: cadabra2-cli.cc:382
void set_histfile()
Definition: cadabra2-cli.cc:342
~Shell()
Definition: cadabra2-cli.cc:93
Flags
Definition: cadabra2-cli.hh:21
const char * colour_warning
Definition: cadabra2-cli.hh:67
void handle_error()
Definition: cadabra2-cli.cc:507
void write_stderr(const std::string &text, const std::string &end="\n", bool flush=false)
Definition: cadabra2-cli.cc:330
std::string histfile
Definition: cadabra2-cli.hh:44
void execute_file(const std::string &filename, bool preprocess=true)
Definition: cadabra2-cli.cc:228
const char * colour_success
Definition: cadabra2-cli.hh:69
pybind11::object py_stdout
Definition: cadabra2-cli.hh:63
void execute(const std::string &code, const std::string &filename="<stdin>")
Definition: cadabra2-cli.cc:216
const char * colour_error
Definition: cadabra2-cli.hh:66
void write_stdout(const std::string &text, const std::string &end="\n", bool flush=false)
Definition: cadabra2-cli.cc:318
pybind11::dict globals
Definition: cadabra2-cli.hh:61
std::string get_ps1()
Definition: cadabra2-cli.cc:491
void process_ps1(const std::string &line)
Definition: cadabra2-cli.cc:398
void handle_error(pybind11::error_already_set &err)
Shell(Flags flags)
Definition: cadabra2-cli.cc:73
Ex rhs(Ex_ptr ex)
Definition: py_ex.cc:391
Ex lhs(Ex_ptr ex)
Definition: py_ex.cc:379
end
Definition: nevaluate.py:22
Flush flush
Definition: Snoop.cc:63