/
interp.h
322 lines (257 loc) · 10.3 KB
/
interp.h
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// interp.h
// Copyright (c) 2020, zhiayang
// Licensed under the Apache License Version 2.0.
#pragma once
#include <map>
#include <string>
#include <memory>
#include <complex>
#include "defs.h"
#include "perms.h"
namespace ikura
{
using complex = std::complex<double>;
}
namespace ikura::interp
{
struct Command;
struct Type : Serialisable
{
using Ptr = std::shared_ptr<const Type>;
static constexpr uint8_t T_VOID = 0;
static constexpr uint8_t T_INTEGER = 1;
static constexpr uint8_t T_DOUBLE = 2;
static constexpr uint8_t T_BOOLEAN = 3;
static constexpr uint8_t T_LIST = 4;
static constexpr uint8_t T_MAP = 5;
static constexpr uint8_t T_CHAR = 6;
static constexpr uint8_t T_FUNCTION = 7;
static constexpr uint8_t T_COMPLEX = 8;
static constexpr uint8_t T_VAR_LIST = 9;
static constexpr uint8_t T_GENERIC = 10;
uint8_t type_id() const { return this->_type; }
Ptr key_type() const { return this->_key_type; }
Ptr elm_type() const { return this->_elm_type; }
Ptr ret_type() const { return this->_elm_type; } // not a typo -- use elm_type to store ret_type also.
std::vector<Ptr> arg_types() const { return this->_arg_types; }
std::string generic_name() const { return this->_gen_name; }
bool is_map() const;
bool is_void() const;
bool is_bool() const;
bool is_list() const;
bool is_char() const;
bool is_string() const; // is_list && list->elm_type->is_char
bool is_double() const;
bool is_integer() const;
bool is_function() const;
bool is_complex() const;
bool is_variadic_list() const;
bool is_generic() const; // whether the type itself is a generic type
bool has_generics() const; // whether the type is generic in some way, eg. array of T.
bool is_same(Ptr other) const;
int get_cast_dist(Ptr to) const;
std::string str() const;
static Ptr get_void();
static Ptr get_bool();
static Ptr get_char();
static Ptr get_string();
static Ptr get_double();
static Ptr get_integer();
static Ptr get_complex();
static Ptr get_list(Ptr elm_type);
static Ptr get_variadic_list(Ptr elm_type);
static Ptr get_map(Ptr key_type, Ptr elm_type);
static Ptr get_macro_function();
static Ptr get_function(Ptr return_type, std::vector<Ptr> arg_types);
static Ptr get_generic(std::string name, int group);
virtual void serialise(Buffer& buf) const override;
static std::optional<std::shared_ptr<const Type>> deserialise(Span& buf);
Type(uint8_t t) : _type(t) { }
Type(uint8_t t, Ptr elm) : _type(t), _elm_type(elm) { }
Type(uint8_t t, Ptr key, Ptr elm) : _type(t), _key_type(key), _elm_type(elm) { }
Type(uint8_t t, std::vector<Ptr> args, Ptr ret) : _type(t), _elm_type(ret), _arg_types(args) { }
Type(uint8_t t, std::string g, uint64_t s) : _type(t), _gen_name(std::move(g)), _gen_group(s) { }
private:
uint8_t _type = T_VOID;
Ptr _key_type;
Ptr _elm_type; // elm type for lists, value type for map, return type for functions
std::vector<Ptr> _arg_types;
std::string _gen_name;
uint64_t _gen_group = 0;
};
struct Value : Serialisable
{
Value(Type::Ptr t) : _type(t) { }
~Value() { }
Type::Ptr type() const { return this->_type; }
bool is_map() const;
bool is_void() const;
bool is_bool() const;
bool is_list() const;
bool is_char() const;
bool is_string() const; // is_list && list->elm_type->is_char
bool is_double() const;
bool is_lvalue() const;
bool is_integer() const;
bool is_function() const;
bool is_complex() const;
bool is_same_type(const Value& other) const { return this->_type->is_same(other._type); }
bool get_bool() const;
uint32_t get_char() const;
double get_double() const;
Value* get_lvalue() const;
int64_t get_integer() const;
ikura::complex get_complex() const;
std::shared_ptr<Command> get_function() const;
std::vector<Value>& get_list();
const std::vector<Value>& get_list() const;
std::map<Value, Value>& get_map();
const std::map<Value, Value>& get_map() const;
std::string str() const;
std::string raw_str() const;
std::optional<Value> cast_to(Type::Ptr type) const;
static Value default_of(Type::Ptr type);
static Value of_void();
static Value of_bool(bool b);
static Value of_char(uint32_t c);
static Value of_double(double d);
static Value of_complex(const ikura::complex& c);
static Value of_complex(double re, double im);
static Value of_string(const char* s);
static Value of_string(ikura::str_view s);
static Value of_string(const std::string& s);
static Value of_integer(int64_t i);
static Value of_lvalue(Value* v);
static Value of_list(Type::Ptr, std::vector<Value> l);
static Value of_variadic_list(Type::Ptr, std::vector<Value> l);
static Value of_function(Command* function);
static Value of_function(std::shared_ptr<Command> function);
static Value of_map(Type::Ptr key_type, Type::Ptr value_type, std::map<Value, Value> m);
virtual void serialise(Buffer& buf) const override;
static std::optional<Value> deserialise(Span& buf);
static constexpr uint8_t TYPE_TAG = serialise::TAG_INTERP_VALUE;
// when converting this value to a message, send one message per list element
static constexpr uint8_t FLAG_DISMANTLE_LIST = 0x1;
uint8_t flags() const { return this->_flags; }
void set_flags(uint8_t f) { this->_flags = f; }
Value decay() const;
Value(const Value&) = default;
Value& operator = (const Value& other)
{
if(&other == this)
return *this;
auto copy = other;
return (*this = std::move(copy));
}
Value& operator = (Value&& rhs)
{
if(&rhs == this)
return *this;
if(rhs._type->is_void()) ;
else if(rhs._type->is_map()) this->v_map = std::move(rhs.v_map);
else if(rhs._type->is_bool()) this->v_bool = std::move(rhs.v_bool);
else if(rhs._type->is_list()) this->v_list = std::move(rhs.v_list);
else if(rhs._type->is_char()) this->v_char = std::move(rhs.v_char);
else if(rhs._type->is_double()) this->v_double = std::move(rhs.v_double);
else if(rhs._type->is_integer()) this->v_integer = std::move(rhs.v_integer);
else if(rhs._type->is_complex()) this->v_complex = std::move(rhs.v_complex);
else if(rhs.is_lvalue()) this->v_lvalue = std::move(rhs.v_lvalue);
else if(rhs.is_function()) this->v_function = rhs.v_function;
else assert(false);
this->_type = rhs._type;
this->_flags = rhs._flags;
this->v_is_lvalue = rhs.v_is_lvalue;
return *this;
}
bool operator == (const Value& other) const
{
if(!this->is_same_type(other) || this->is_lvalue() != other.is_lvalue())
return false;
if(this->_type->is_void()) return true;
else if(this->_type->is_map()) return this->v_map == other.v_map;
else if(this->_type->is_bool()) return this->v_bool == other.v_bool;
else if(this->_type->is_list()) return this->v_list == other.v_list;
else if(this->_type->is_char()) return this->v_char == other.v_char;
else if(this->_type->is_double()) return this->v_double == other.v_double;
else if(this->_type->is_integer()) return this->v_integer == other.v_integer;
else if(this->_type->is_complex()) return this->v_complex == other.v_complex;
else if(this->is_lvalue()) return this->v_lvalue == other.v_lvalue;
else return false;
}
bool operator < (const Value& rhs) const
{
if(!this->is_same_type(rhs) || this->is_lvalue() != rhs.is_lvalue())
return this->type()->type_id() < rhs.type()->type_id();
if(this->_type->is_void()) return false;
else if(this->_type->is_map()) return this->v_map < rhs.v_map;
else if(this->_type->is_bool()) return this->v_bool < rhs.v_bool;
else if(this->_type->is_list()) return this->v_list < rhs.v_list;
else if(this->_type->is_char()) return this->v_char < rhs.v_char;
else if(this->_type->is_double()) return this->v_double < rhs.v_double;
else if(this->_type->is_integer()) return this->v_integer < rhs.v_integer;
else if(this->_type->is_complex()) return std::norm(this->v_complex) < std::norm(rhs.v_complex);
else if(this->is_lvalue())
{
if(this->v_lvalue && rhs.v_lvalue)
return *this->v_lvalue < *rhs.v_lvalue;
return this->v_lvalue;
}
else return false;
}
private:
friend struct Hasher;
Type::Ptr _type;
uint8_t _flags = 0;
bool v_is_lvalue = false;
struct {
int64_t v_integer = 0;
double v_double = 0;
bool v_bool = false;
Value* v_lvalue = 0;
uint32_t v_char = 0;
std::shared_ptr<Command> v_function;
ikura::complex v_complex = 0;
std::vector<Value> v_list;
std::map<Value, Value> v_map;
};
static Value decay(const Value& v);
static std::vector<Value> decay(const std::vector<Value>& vs);
static std::map<Value, Value> decay(const std::map<Value, Value>& vs);
};
struct Command;
struct CmdContext
{
ikura::str_view callerid;
ikura::str_view callername;
const Channel* channel = nullptr;
uint64_t executionStart = 0;
// the arguments, split by spaces and Value::of_string-ed
std::vector<interp::Value> arguments;
std::string macro_args;
};
struct InterpState : Serialisable
{
// we need this to setup some global stuff.
InterpState();
ikura::string_map<Command*> commands;
ikura::string_map<std::string> aliases;
ikura::string_map<PermissionSet> builtinCommandPermissions;
std::shared_ptr<Command> findCommand(ikura::str_view name) const;
bool removeCommandOrAlias(ikura::str_view name);
std::pair<std::optional<interp::Value>, interp::Value*> resolveVariable(ikura::str_view name, CmdContext& cs);
Result<interp::Value> evaluateExpr(ikura::str_view expr, CmdContext& cs);
Result<bool> addGlobal(ikura::str_view name, interp::Value val);
Result<bool> removeGlobal(ikura::str_view name);
virtual void serialise(Buffer& buf) const override;
static std::optional<InterpState> deserialise(Span& buf);
static constexpr uint8_t TYPE_TAG = serialise::TAG_INTERP_STATE;
private:
ikura::string_map<interp::Value*> globals;
};
int getFunctionOverloadDistance(const std::vector<Type::Ptr>& target, const std::vector<Type::Ptr>& given);
Result<std::vector<Value>> coerceTypesForFunctionCall(ikura::str_view name, Type::Ptr signature, std::vector<Value> given);
}
namespace ikura
{
Synchronised<interp::InterpState>& interpreter();
}