OILS / mycpp / gc_builtins.h View on Github | oilshell.org

175 lines, 90 significant
1// gc_builtins.h: Statically typed Python builtins.
2//
3// Builtin types: tuples, NotImplementedError, AssertionError
4// Builtin functions: print(), repr(), ord()
5// Builtin operators: str_concat(), str_repeat(), list_repeat()
6
7#ifndef GC_BUILTINS_H
8#define GC_BUILTINS_H
9
10#include "mycpp/common.h"
11#include "mycpp/gc_obj.h"
12#include "mycpp/gc_str.h" // Str
13
14class BigStr;
15
16class _ExceptionOpaque {
17 public:
18 _ExceptionOpaque() {
19 }
20 static constexpr ObjHeader obj_header() {
21 return ObjHeader::ClassFixed(kZeroMask, sizeof(_ExceptionOpaque));
22 }
23};
24
25// mycpp removes constructor arguments
26class Exception : public _ExceptionOpaque {};
27
28class IndexError : public _ExceptionOpaque {};
29
30class KeyError : public _ExceptionOpaque {};
31
32class EOFError : public _ExceptionOpaque {};
33
34class ZeroDivisionError : public _ExceptionOpaque {};
35
36class KeyboardInterrupt : public _ExceptionOpaque {};
37
38class StopIteration : public _ExceptionOpaque {};
39
40class ValueError {
41 public:
42 ValueError() : message(nullptr) {
43 }
44 explicit ValueError(BigStr* message) : message(message) {
45 }
46
47 static constexpr ObjHeader obj_header() {
48 return ObjHeader::ClassFixed(field_mask(), sizeof(ValueError));
49 }
50
51 BigStr* message;
52
53 static constexpr uint32_t field_mask() {
54 return maskbit(offsetof(ValueError, message));
55 }
56};
57
58// Note these translations by mycpp:
59// - AssertionError -> assert(0);
60// - NotImplementedError -> FAIL(kNotImplemented);
61
62// libc::regex_match and other bindings raise RuntimeError
63class RuntimeError {
64 public:
65 explicit RuntimeError(BigStr* message) : message(message) {
66 }
67
68 static constexpr ObjHeader obj_header() {
69 return ObjHeader::ClassFixed(field_mask(), sizeof(RuntimeError));
70 }
71
72 BigStr* message;
73
74 static constexpr uint32_t field_mask() {
75 return maskbit(offsetof(RuntimeError, message));
76 }
77};
78
79// libc::wcswidth raises UnicodeError on invalid UTF-8
80class UnicodeError : public RuntimeError {
81 public:
82 explicit UnicodeError(BigStr* message) : RuntimeError(message) {
83 }
84};
85
86// Python 2 has a dubious distinction between IOError and OSError, so mycpp
87// generates this base class to catch both.
88class IOError_OSError : public _ExceptionOpaque {
89 public:
90 explicit IOError_OSError(int err_num) : _ExceptionOpaque(), errno_(err_num) {
91 }
92 int errno_;
93};
94
95class IOError : public IOError_OSError {
96 public:
97 explicit IOError(int err_num) : IOError_OSError(err_num) {
98 }
99};
100
101class OSError : public IOError_OSError {
102 public:
103 explicit OSError(int err_num) : IOError_OSError(err_num) {
104 }
105};
106
107class SystemExit : public _ExceptionOpaque {
108 public:
109 explicit SystemExit(int code) : _ExceptionOpaque(), code(code) {
110 }
111 int code;
112};
113
114void print(BigStr* s);
115
116inline void print(Str s) {
117 print(s.big_);
118}
119
120BigStr* repr(BigStr* s);
121
122BigStr* str(int i);
123
124BigStr* str(double d);
125
126BigStr* intern(BigStr* s);
127
128// Used by mark_sweep_heap and StrFormat
129bool StringToInt(const char* s, int len, int base, int* result);
130
131// Used by Oils integers
132bool StringToInt64(const char* s, int len, int base, int64_t* result);
133
134// String to integer, raising ValueError if invalid
135int to_int(BigStr* s, int base = 10);
136
137BigStr* chr(int i);
138int ord(BigStr* s);
139
140inline int to_int(bool b) {
141 return b;
142}
143
144bool to_bool(BigStr* s);
145
146// Used by division operator
147double to_float(int i);
148
149// Used for floating point flags like read -t 0.1
150double to_float(BigStr* s);
151
152inline bool to_bool(int i) {
153 return i != 0;
154}
155
156bool str_contains(BigStr* haystack, BigStr* needle);
157
158// Used by 'with str_switch(s)'
159bool str_equals_c(BigStr* s, const char* c_string, int c_len);
160
161// Only used by unit tests
162bool str_equals0(const char* c_string, BigStr* s);
163
164BigStr* str_concat(BigStr* a, BigStr* b); // a + b when a and b are strings
165BigStr* str_concat3(BigStr* a, BigStr* b, BigStr* c); // for os_path::join()
166BigStr* str_repeat(BigStr* s, int times); // e.g. ' ' * 3
167
168extern BigStr* kEmptyString;
169
170int hash(BigStr* s);
171
172int max(int a, int b);
173int min(int a, int b);
174
175#endif // GC_BUILTINS_H