File: cadabra2-cli.hh

package info (click to toggle)
cadabra2 2.4.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 78,732 kB
  • sloc: ansic: 133,450; cpp: 92,064; python: 1,530; javascript: 203; sh: 184; xml: 182; objc: 53; makefile: 51
file content (90 lines) | stat: -rw-r--r-- 2,285 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <string>
#include <memory>

// Work around MSVC linking problem
#ifdef _DEBUG
#define CADABRA_CLI_DEBUG_MARKER
#undef _DEBUG
#endif
//#include <Python.h>
#include <pybind11/pybind11.h>
#include <pybind11/embed.h>
#ifdef CADABRA_CLI_DEBUG_MARKER
#define _DEBUG
#undef CADABRA_CLI_DEBUG_MARKER
#endif

class Shell
{
public:
	enum class Flags : unsigned int
	{
		None             = 0x00,
		NoBanner         = 0x01,
		IgnoreSemicolons = 0x02,
		NoColour         = 0x04,
		NoReadline       = 0x08,
	};

	Shell(Flags flags);
	~Shell();

	void restart();
	void interact();
	pybind11::object evaluate(const std::string& code, const std::string& filename = "<stdin>");
	void execute(const std::string& code, const std::string& filename = "<stdin>");
	void execute_file(const std::string& filename, bool preprocess = true);
	void interact_file(const std::string& filename, bool preprocess = true);

	void write_stdout(const std::string& text, const std::string& end = "\n", bool flush = false);
	void write_stderr(const std::string& text, const std::string& end = "\n", bool flush = false);

private:
	void set_histfile();
	std::string histfile;
	std::string site_path;

	std::string str(const pybind11::handle& obj);
	std::string repr(const pybind11::handle& obj);
	std::string sanitize(std::string s);

	void process_ps1(const std::string& line);
	void process_ps2(const std::string& line);
	void set_completion_callback(const char* buffer, std::vector<std::string>& completions);

	std::string get_ps1();
	std::string get_ps2();

	void handle_error();
	void handle_error(pybind11::error_already_set& err);

	pybind11::dict globals;
	pybind11::object sys;
	pybind11::object py_stdout, py_stderr;
	std::string collect;

	const char* colour_error;
	const char* colour_warning;
	const char* colour_info;
	const char* colour_success;
	const char* colour_reset;
	Flags flags;
	};

class ExitRequest : public std::exception
{
public:
	ExitRequest();
	ExitRequest(int code);
	ExitRequest(const std::string& message);

	virtual const char* what() const noexcept override;

	int code;
	std::string message;
};


Shell::Flags& operator |= (Shell::Flags& lhs, Shell::Flags rhs);
Shell::Flags operator | (Shell::Flags lhs, Shell::Flags rhs);
bool operator & (Shell::Flags lhs, Shell::Flags rhs);