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::write_stderr
void write_stderr(const std::string &text, const std::string &end="\n", bool flush=false)
Definition: cadabra2-cli.cc:316
Shell::colour_reset
const char * colour_reset
Definition: cadabra2-cli.hh:70
Shell::restart
void restart()
Definition: cadabra2-cli.cc:96
Shell::Flags::NoReadline
@ NoReadline
nevaluate.end
end
Definition: nevaluate.py:22
Shell::colour_success
const char * colour_success
Definition: cadabra2-cli.hh:69
Shell::str
std::string str(const pybind11::handle &obj)
Definition: cadabra2-cli.cc:363
Shell::Flags::None
@ None
ExitRequest::code
int code
Definition: cadabra2-cli.hh:83
Shell::set_histfile
void set_histfile()
Definition: cadabra2-cli.cc:328
Shell::Flags::NoColour
@ NoColour
ExitRequest::message
std::string message
Definition: cadabra2-cli.hh:84
Shell::Shell
Shell(Flags flags)
Definition: cadabra2-cli.cc:68
ExitRequest
Definition: cadabra2-cli.hh:74
Shell::execute_file
void execute_file(const std::string &filename, bool preprocess=true)
Definition: cadabra2-cli.cc:214
Shell::handle_error
void handle_error()
Definition: cadabra2-cli.cc:492
Shell::process_ps2
void process_ps2(const std::string &line)
Definition: cadabra2-cli.cc:443
Shell::collect
std::string collect
Definition: cadabra2-cli.hh:64
cadabra::rhs
Ex rhs(Ex_ptr ex)
Definition: py_ex.cc:318
Shell::set_completion_callback
void set_completion_callback(const char *buffer, std::vector< std::string > &completions)
Definition: cadabra2-cli.cc:456
operator&
bool operator&(Shell::Flags lhs, Shell::Flags rhs)
Definition: cadabra2-cli.cc:559
Shell::colour_error
const char * colour_error
Definition: cadabra2-cli.hh:66
Shell::interact_file
void interact_file(const std::string &filename, bool preprocess=true)
Definition: cadabra2-cli.cc:242
Shell::sanitize
std::string sanitize(std::string s)
Definition: cadabra2-cli.cc:378
Shell::Flags::IgnoreSemicolons
@ IgnoreSemicolons
Shell::Flags
Flags
Definition: cadabra2-cli.hh:20
ExitRequest::what
virtual const char * what() const noexcept override
Definition: cadabra2-cli.cc:541
Shell::repr
std::string repr(const pybind11::handle &obj)
Definition: cadabra2-cli.cc:368
Shell::globals
pybind11::dict globals
Definition: cadabra2-cli.hh:61
Shell::colour_warning
const char * colour_warning
Definition: cadabra2-cli.hh:67
Shell::flags
Flags flags
Definition: cadabra2-cli.hh:71
ExitRequest::ExitRequest
ExitRequest()
Definition: cadabra2-cli.cc:532
Shell::histfile
std::string histfile
Definition: cadabra2-cli.hh:44
Shell::colour_info
const char * colour_info
Definition: cadabra2-cli.hh:68
Shell::write_stdout
void write_stdout(const std::string &text, const std::string &end="\n", bool flush=false)
Definition: cadabra2-cli.cc:304
snoop::flush
Flush flush
Definition: Snoop.cc:63
Shell::py_stderr
pybind11::object py_stderr
Definition: cadabra2-cli.hh:63
Shell::get_ps2
std::string get_ps2()
Definition: cadabra2-cli.cc:484
Shell::~Shell
~Shell()
Definition: cadabra2-cli.cc:90
Shell::evaluate
pybind11::object evaluate(const std::string &code, const std::string &filename="<stdin>")
Definition: cadabra2-cli.cc:189
Shell::get_ps1
std::string get_ps1()
Definition: cadabra2-cli.cc:476
Shell::Flags::NoBanner
@ NoBanner
operator|
Shell::Flags operator|(Shell::Flags lhs, Shell::Flags rhs)
Definition: cadabra2-cli.cc:554
Shell::execute
void execute(const std::string &code, const std::string &filename="<stdin>")
Definition: cadabra2-cli.cc:202
Shell::py_stdout
pybind11::object py_stdout
Definition: cadabra2-cli.hh:63
operator|=
Shell::Flags & operator|=(Shell::Flags &lhs, Shell::Flags rhs)
Definition: cadabra2-cli.cc:546
cadabra::lhs
Ex lhs(Ex_ptr ex)
Definition: py_ex.cc:306
Shell::interact
void interact()
Definition: cadabra2-cli.cc:117
Shell::sys
pybind11::object sys
Definition: cadabra2-cli.hh:62
Shell::process_ps1
void process_ps1(const std::string &line)
Definition: cadabra2-cli.cc:384
Shell::site_path
std::string site_path
Definition: cadabra2-cli.hh:45
Shell
Definition: cadabra2-cli.hh:17