├── set_iterator.h ├── dict_iterator.h ├── list_iterator.h ├── range_iterator.h ├── none.h ├── assert.h ├── slice.h ├── range.h ├── .gitignore ├── dict.h ├── set.h ├── set_iterator.cpp ├── list_iterator.cpp ├── dict_iterator.cpp ├── none.cpp ├── float.h ├── slice.cpp ├── Makefile ├── range_iterator.cpp ├── assert.cpp ├── str.h ├── range.cpp ├── int.h ├── bool.h ├── func.h ├── list.h ├── dict.cpp ├── float.cpp ├── set.cpp ├── int.cpp ├── generated.h ├── bool.cpp ├── str.cpp ├── type.h ├── list.cpp ├── main.cpp ├── generated.cpp └── func.cpp /set_iterator.h: -------------------------------------------------------------------------------- 1 | #include "type.h" 2 | 3 | value make$set_iterator(set_t *set); 4 | value __next__$set_iterator(value self); 5 | -------------------------------------------------------------------------------- /dict_iterator.h: -------------------------------------------------------------------------------- 1 | #include "type.h" 2 | 3 | value make$dict_iterator(dict_t *dict); 4 | value __next__$dict_iterator(value self); 5 | -------------------------------------------------------------------------------- /list_iterator.h: -------------------------------------------------------------------------------- 1 | #include "type.h" 2 | 3 | value make$list_iterator(list_t *lst); 4 | value __next__$list_iterator(value self); 5 | -------------------------------------------------------------------------------- /range_iterator.h: -------------------------------------------------------------------------------- 1 | #include "type.h" 2 | 3 | 4 | value make$range_iterator(range_t *rng); 5 | value __next__$range_iterator(value self); 6 | -------------------------------------------------------------------------------- /none.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "type.h" 4 | 5 | value make$none(); 6 | value __bool__$none(value self); 7 | value __eq__$none$none(value x, value y); 8 | value __ne__$none$none(value x, value y); 9 | -------------------------------------------------------------------------------- /assert.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifdef NDEBUG 3 | 4 | #define assert(v) if(!(v)) __builtin_unreachable() 5 | 6 | #else 7 | 8 | void my_assert(bool value, const char *code); 9 | #define assert(v) my_assert(v, #v) 10 | 11 | #endif -------------------------------------------------------------------------------- /slice.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "type.h" 4 | #include "int.h" 5 | 6 | value slice$int_(value stop); 7 | value slice$int_$int_(value start, value stop); 8 | value slice$int_$int_$int_(value start, value stop, value step); 9 | -------------------------------------------------------------------------------- /range.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "type.h" 4 | #include "int.h" 5 | 6 | value range$int_(value stop); 7 | value range$int_$int_(value start, value stop); 8 | value range$int_$int_$int_(value start, value stop, value step); 9 | value __iter__$range(value self); 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .gcda 2 | .gcno 3 | debug 4 | release 5 | 6 | # Compiled Object files 7 | *.slo 8 | *.lo 9 | *.o 10 | *.obj 11 | 12 | # Precompiled Headers 13 | *.gch 14 | *.pch 15 | 16 | # Compiled Dynamic libraries 17 | *.so 18 | *.dylib 19 | *.dll 20 | 21 | # Fortran module files 22 | *.mod 23 | 24 | # Compiled Static libraries 25 | *.lai 26 | *.la 27 | *.a 28 | *.lib 29 | 30 | # Executables 31 | *.exe 32 | *.out 33 | *.app 34 | >>>>>>> db8f910fc671488c9a6dd1097d3e05aca0eaddea 35 | -------------------------------------------------------------------------------- /dict.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "type.h" 4 | 5 | value make$dict(); 6 | value __getitem__$dict$(value self, value k); 7 | value __setitem__$dict$$(value self, value k, value v); 8 | value __contains__$dict$(value self, value k); 9 | value __delitem__$dict$(value self, value k); 10 | value __bool__$dict(value self); 11 | value __len__$dict(value self); 12 | value __eq__$dict$dict(value x, value y); 13 | value __ne__$dict$dict(value x, value y); 14 | value __iter__$dict(value self); 15 | 16 | -------------------------------------------------------------------------------- /set.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "type.h" 4 | 5 | value make$set(); 6 | value add$set$(value self, value v); 7 | value remove$set$(value self, value v); 8 | value __contains__$set$(value self, value v); 9 | value __bool__$set(value self); 10 | value __len__$set(value self); 11 | value __eq__$set$set(value x, value y); 12 | value __ne__$set$set(value x, value y); 13 | value __lt__$set$set(value x, value y); 14 | value __le__$set$set(value x, value y); 15 | value __ge__$set$set(value x, value y); 16 | value __gt__$set$set(value x, value y); 17 | value __iter__$set(value self); 18 | -------------------------------------------------------------------------------- /set_iterator.cpp: -------------------------------------------------------------------------------- 1 | #include "set_iterator.h" 2 | #include "assert.h" 3 | #include "none.h" 4 | #include "int.h" 5 | 6 | value make$set_iterator(set_t *set) { 7 | value ret; 8 | ret.type = value::SET_ITERATOR; 9 | ret.set_iteratorval = new set_iterator_t{set->cbegin(), set->cend()}; 10 | return ret; 11 | } 12 | 13 | value __next__$set_iterator(value self) { 14 | assert(self.type == value::SET_ITERATOR); 15 | auto iter = self.set_iteratorval; 16 | if (iter->cur != iter->end) { 17 | return *iter->cur++; 18 | } else { 19 | return make$none(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /list_iterator.cpp: -------------------------------------------------------------------------------- 1 | #include "list_iterator.h" 2 | #include "assert.h" 3 | #include "none.h" 4 | #include "int.h" 5 | 6 | value make$list_iterator(list_t *lst) { 7 | value ret; 8 | ret.type = value::LIST_ITERATOR; 9 | ret.list_iteratorval = new list_iterator_t{lst, 0}; 10 | return ret; 11 | } 12 | 13 | value __next__$list_iterator(value self) { 14 | assert(self.type == value::LIST_ITERATOR); 15 | auto iter = self.list_iteratorval; 16 | if (iter->index < iter->list->size()) { 17 | return (*iter->list)[iter->index++]; 18 | } else { 19 | return make$none(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /dict_iterator.cpp: -------------------------------------------------------------------------------- 1 | #include "dict_iterator.h" 2 | #include "assert.h" 3 | #include "none.h" 4 | #include "int.h" 5 | 6 | value make$dict_iterator(dict_t *dict) { 7 | value ret; 8 | ret.type = value::DICT_ITERATOR; 9 | ret.dict_iteratorval = new dict_iterator_t{dict->cbegin(), dict->cend()}; 10 | return ret; 11 | } 12 | 13 | value __next__$dict_iterator(value self) { 14 | assert(self.type == value::DICT_ITERATOR); 15 | auto iter = self.dict_iteratorval; 16 | if (iter->cur != iter->end) { 17 | return (iter->cur++)->first; 18 | } else { 19 | return make$none(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /none.cpp: -------------------------------------------------------------------------------- 1 | #include "none.h" 2 | #include "bool.h" 3 | #include "assert.h" 4 | 5 | value make$none() { 6 | value ret; 7 | ret.type = value::NONE; 8 | return ret; 9 | } 10 | 11 | value __bool__$none(value self) { 12 | assert(self.type == value::NONE); 13 | return make$bool_(false); 14 | } 15 | 16 | value __eq__$none$none(value x, value y) { 17 | assert(x.type == value::NONE && y.type == value::NONE); 18 | return make$bool_(true); 19 | } 20 | 21 | value __ne__$none$none(value x, value y) { 22 | assert(x.type == value::NONE && y.type == value::NONE); 23 | return make$bool_(false); 24 | } 25 | -------------------------------------------------------------------------------- /float.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "type.h" 4 | 5 | value make$float_(double v=0.0); 6 | 7 | value __add__$float_$float_(value x, value y); 8 | value __sub__$float_$float_(value x, value y); 9 | value __mul__$float_$float_(value x, value y); 10 | value __truediv__$float_$float_(value x, value y); 11 | value __mod__$float_$float_(value x, value y); 12 | value __eq__$float_$float_(value x, value y); 13 | value __ne__$float_$float_(value x, value y); 14 | value __lt__$float_$float_(value x, value y); 15 | value __le__$float_$float_(value x, value y); 16 | value __ge__$float_$float_(value x, value y); 17 | value __gt__$float_$float_(value x, value y); 18 | value __bool__$float_(value self); 19 | -------------------------------------------------------------------------------- /slice.cpp: -------------------------------------------------------------------------------- 1 | #include "slice.h" 2 | #include "assert.h" 3 | 4 | value slice$int_(value stop) { 5 | return slice$int_$int_$int_(make$int_(0), stop, make$int_(1)); 6 | } 7 | 8 | value slice$int_$int_(value start, value stop) { 9 | return slice$int_$int_$int_(start, stop, make$int_(1)); 10 | } 11 | 12 | value slice$int_$int_$int_(value start, value stop, value step) { 13 | assert(start.type == value::INT && stop.type == value::INT && 14 | step.type == value::INT); 15 | 16 | if (step.intval == 0) 17 | throw std::runtime_error("slice step must not be zero"); 18 | value ret; 19 | ret.type = value::SLICE; 20 | ret.sliceval = new slice_t{start.intval, stop.intval, step.intval}; 21 | return ret; 22 | } 23 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -std=c++11 -Wall -Wno-switch -Wno-logical-op-parentheses 2 | OBJS = assert.o list.o int.o float.o str.o func.o set.o none.o bool.o dict.o range.o range_iterator.o slice.o list_iterator.o set_iterator.o dict_iterator.o generated.o 3 | 4 | run: debug 5 | ./debug 6 | 7 | debug: CXXFLAGS += -O0 -g 8 | debug: $(OBJS) main.o 9 | $(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS) 10 | 11 | release: CXXFLAGS += -Ofast -march=native -flto -DNDEBUG 12 | release: $(OBJS) main.o 13 | $(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS) 14 | 15 | test: CXXFLAGS += -march=native -DNDEBUG 16 | test: $(OBJS) test.o 17 | $(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS) 18 | .PHONY: clean 19 | clean: 20 | - rm debug release *.o *.gcno *.gcda *.gcov 21 | -------------------------------------------------------------------------------- /range_iterator.cpp: -------------------------------------------------------------------------------- 1 | #include "range_iterator.h" 2 | #include "assert.h" 3 | #include "none.h" 4 | #include "int.h" 5 | 6 | value make$range_iterator(range_t *rng) { 7 | value ret; 8 | ret.type = value::RANGE_ITERATOR; 9 | ret.range_iteratorval = new range_iterator_t(*rng); 10 | return ret; 11 | } 12 | 13 | value __next__$range_iterator(value self) { 14 | assert(self.type == value::RANGE_ITERATOR); 15 | auto iter = self.range_iteratorval; 16 | if (iter->step >= 0 && iter->start < iter->stop || iter->step < 0 && iter->start > iter->stop) { 17 | value ret = make$int_(iter->start); 18 | iter->start += iter->step; 19 | return ret; 20 | } else { 21 | return make$none(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /assert.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void my_assert(bool value, const char *code) { 6 | if (value) return; 7 | fprintf(stderr, "Assertion %s failed\n", code); 8 | 9 | // storage array for stack trace address data 10 | void* addrlist[64]; 11 | 12 | // retrieve current stack addresses 13 | unsigned addrlen = backtrace(addrlist, sizeof( addrlist ) / sizeof( void* )); 14 | 15 | if ( addrlen == 0 ) 16 | { 17 | fprintf(stderr, " \n" ); 18 | return; 19 | } 20 | 21 | // create readable strings to each frame. 22 | char** symbollist = backtrace_symbols( addrlist, addrlen ); 23 | 24 | // print the stack trace. 25 | for (unsigned i = 4; i < addrlen; i++ ) 26 | fprintf(stderr, "%s\n", symbollist[i]); 27 | 28 | free(symbollist); 29 | abort(); 30 | } -------------------------------------------------------------------------------- /str.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "type.h" 4 | 5 | value make$str(const char *v=""); 6 | value __getitem__$str$(value self, value k); 7 | value __getitem__$str$int_(value self, value k); 8 | value __getitem__$str$slice(value self, value k); 9 | value __len__$str(value self); 10 | value __add__$str$str(value x, value y); 11 | value __mul__$str$int_(value self, value n); 12 | value __mul__$int_$str(value n, value self); 13 | value __mul__$str$bool_(value self, value n); 14 | value __mul__$bool_$str(value n, value self); 15 | value __eq__$str$str(value x, value y); 16 | value __ne__$str$str(value x, value y); 17 | value __lt__$str$str(value x, value y); 18 | value __le__$str$str(value x, value y); 19 | value __ge__$str$str(value x, value y); 20 | value __gt__$str$str(value x, value y); 21 | value __contains__$str$str(value self, value sub); 22 | value __bool__$str(value self); 23 | -------------------------------------------------------------------------------- /range.cpp: -------------------------------------------------------------------------------- 1 | #include "range.h" 2 | #include "assert.h" 3 | #include "range_iterator.h" 4 | 5 | value range$int_(value stop) { 6 | return range$int_$int_$int_(make$int_(0), stop, make$int_(1)); 7 | } 8 | 9 | value range$int_$int_(value start, value stop) { 10 | return range$int_$int_$int_(start, stop, make$int_(1)); 11 | } 12 | 13 | value range$int_$int_$int_(value start, value stop, value step) { 14 | assert(start.type == value::INT && stop.type == value::INT && step.type == value::INT); 15 | 16 | if (step.intval == 0) 17 | throw std::runtime_error("range step must not be zero"); 18 | value ret; 19 | ret.type = value::RANGE; 20 | ret.rangeval = new range_t{start.intval, stop.intval, step.intval}; 21 | return ret; 22 | } 23 | 24 | value __iter__$range(value self) { 25 | assert(self.type == value::RANGE); 26 | return make$range_iterator(self.rangeval); 27 | } 28 | -------------------------------------------------------------------------------- /int.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "type.h" 4 | 5 | value make$int_(long v=0); 6 | 7 | value __add__$int_$int_(value x, value y); 8 | value __sub__$int_$int_(value x, value y); 9 | value __mul__$int_$int_(value x, value y); 10 | value __truediv__$int_$int_(value x, value y); 11 | value __mod__$int_$int_(value x, value y); 12 | value __float__$int_(value self); 13 | value __eq__$int_$int_(value x, value y); 14 | value __ne__$int_$int_(value x, value y); 15 | value __lt__$int_$int_(value x, value y); 16 | value __le__$int_$int_(value x, value y); 17 | value __ge__$int_$int_(value x, value y); 18 | value __gt__$int_$int_(value x, value y); 19 | value __bool__$int_(value self); 20 | value __and__$int_$int_(value x, value y); 21 | value __or__$int_$int_(value x, value y); 22 | value __xor__$int_$int_(value x, value y); 23 | value __invert__$int_(value self); 24 | value __lshift__$int_$int_(value x, value y); 25 | value __rshift__$int_$int_(value x, value y); 26 | -------------------------------------------------------------------------------- /bool.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "type.h" 4 | #include "int.h" 5 | 6 | value make$bool_(bool v=false); 7 | 8 | value __eq__$bool_$bool_(value x, value y); 9 | value __ne__$bool_$bool_(value x, value y); 10 | value __lt__$bool_$bool_(value x, value y); 11 | value __le__$bool_$bool_(value x, value y); 12 | value __gt__$bool_$bool_(value x, value y); 13 | value __ge__$bool_$bool_(value x, value y); 14 | value __bool__$bool_(value self); 15 | value __int__$bool_(value self); 16 | value __float__$bool_(value self); 17 | value __invert__$bool_(value x); 18 | value __and__$bool_$bool_(value x, value y); 19 | value __or__$bool_$bool_(value x, value y); 20 | value __xor__$bool_$bool_(value x, value y); 21 | value __add__$bool_$bool_(value x, value y); 22 | value __sub__$bool_$bool_(value x, value y); 23 | value __mul__$bool_$bool_(value x, value y); 24 | value __truediv__$bool_$bool_(value x, value y); 25 | value __mod__$bool_$bool_(value x, value y); 26 | value __lshift__$bool_$bool_(value x, value y); 27 | value __rshift__$bool_$bool_(value x, value y); 28 | -------------------------------------------------------------------------------- /func.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | struct value; 4 | 5 | value print(value v); 6 | value __add__(value x, value y); 7 | value __sub__(value x, value y); 8 | value __mul__(value x, value y); 9 | value __truediv__(value x, value y); 10 | value __mod__(value x, value y); 11 | value __len__(value self); 12 | value __getitem__(value self, value k); 13 | value __contains__(value self, value v); 14 | value __setitem__(value self, value k, value v); 15 | value __delitem__(value self, value k); 16 | value __bool__(value self); 17 | value __eq__(value x, value y); 18 | value __ne__(value x, value y); 19 | value __ge__(value x, value y); 20 | value __gt__(value x, value y); 21 | value __lt__(value x, value y); 22 | value __le__(value x, value y); 23 | value __and__(value x, value y); 24 | value __or__(value x, value y); 25 | value __xor__(value x, value y); 26 | value __invert__(value self); 27 | value __lshift__(value x, value y); 28 | value __rshift__(value x, value y); 29 | value __iter__(value self); 30 | value __next__(value self); 31 | 32 | value append(value self, value v); 33 | value add(value self, value v); 34 | value remove(value self, value v); 35 | value pop(value self); -------------------------------------------------------------------------------- /list.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "type.h" 4 | 5 | value make$list(); 6 | value append$list$(value self, value v); 7 | value pop$list(value self); 8 | value __getitem__$list$(value self, value k); 9 | value __getitem__$list$int_(value self, value k); 10 | value __getitem__$list$slice(value self, value k); 11 | value __setitem__$list$$(value self, value k, value v); 12 | value __setitem__$list$int_$(value self, value k, value v); 13 | value __setitem__$list$slice$(value self, value k, value v); 14 | value __add__$list$list(value x, value y); 15 | value __mul__$list$int_(value self, value n); 16 | value __mul__$int_$list(value n, value self); 17 | value __mul__$list$bool_(value self, value n); 18 | value __mul__$bool_$list(value n, value self); 19 | value __len__$list(value self); 20 | value __eq__$list$list(value x, value y); 21 | value __ne__$list$list(value x, value y); 22 | value __lt__$list$list(value x, value y); 23 | value __le__$list$list(value x, value y); 24 | value __ge__$list$list(value x, value y); 25 | value __gt__$list$list(value x, value y); 26 | value __contains__$list$(value self, value v); 27 | value __delitem__$list$int_(value self, value k); 28 | value __delitem__$list$slice(value self, value k); 29 | value __bool__$list(value self); 30 | value __iter__$list(value self); 31 | 32 | -------------------------------------------------------------------------------- /dict.cpp: -------------------------------------------------------------------------------- 1 | #include "dict.h" 2 | #include "bool.h" 3 | #include "int.h" 4 | #include "assert.h" 5 | #include "none.h" 6 | #include "dict_iterator.h" 7 | 8 | value make$dict() { 9 | value ret; 10 | ret.type = value::DICT; 11 | ret.dictval = new dict_t(); 12 | return ret; 13 | } 14 | 15 | value __getitem__$dict$(value self, value k) { 16 | assert(self.type == value::DICT); 17 | auto it = self.dictval->find(k); 18 | if (it == self.dictval->end()) { 19 | throw std::runtime_error("key error"); 20 | } 21 | return it->second; 22 | } 23 | 24 | value __setitem__$dict$$(value self, value k, value v) { 25 | assert(self.type == value::DICT); 26 | (*self.dictval)[k] = v; 27 | return make$none(); 28 | } 29 | 30 | value __contains__$dict$(value self, value k) { 31 | assert(self.type == value::DICT); 32 | return make$bool_(self.dictval->find(k) != self.dictval->end()); 33 | } 34 | 35 | value __delitem__$dict$(value self, value k) { 36 | assert(self.type == value::DICT); 37 | self.dictval->erase(k); 38 | return make$none(); 39 | } 40 | 41 | value __bool__$dict(value self) { 42 | assert(self.type == value::DICT); 43 | return make$bool_(!self.dictval->empty()); 44 | } 45 | 46 | value __len__$dict(value self) { 47 | assert(self.type == value::DICT); 48 | return make$int_(self.dictval->size()); 49 | } 50 | 51 | value __eq__$dict$dict(value x, value y) { 52 | assert(x.type == value::DICT && y.type == value::DICT); 53 | return make$bool_(*x.dictval == *y.dictval); 54 | } 55 | 56 | value __ne__$dict$dict(value x, value y) { 57 | assert(x.type == value::DICT && y.type == value::DICT); 58 | return make$bool_(*x.dictval != *y.dictval); 59 | } 60 | 61 | value __iter__$dict(value self) { 62 | assert(self.type == value::DICT); 63 | return make$dict_iterator(self.dictval); 64 | } 65 | -------------------------------------------------------------------------------- /float.cpp: -------------------------------------------------------------------------------- 1 | #include "float.h" 2 | #include "bool.h" 3 | #include "assert.h" 4 | 5 | value make$float_(double v) { 6 | value ret; 7 | ret.type = value::FLOAT; 8 | ret.floatval = v; 9 | return ret; 10 | } 11 | 12 | value __add__$float_$float_(value x, value y) { 13 | assert(x.type == value::FLOAT && y.type == value::FLOAT); 14 | return make$float_(x.floatval + y.floatval); 15 | } 16 | 17 | value __sub__$float_$float_(value x, value y) { 18 | assert(x.type == value::FLOAT && y.type == value::FLOAT); 19 | return make$float_(x.floatval - y.floatval); 20 | } 21 | 22 | value __mul__$float_$float_(value x, value y) { 23 | assert(x.type == value::FLOAT && y.type == value::FLOAT); 24 | return make$float_(x.floatval * y.floatval); 25 | } 26 | 27 | value __truediv__$float_$float_(value x, value y) { 28 | assert(x.type == value::FLOAT && y.type == value::FLOAT); 29 | return make$float_(x.floatval / y.floatval); 30 | } 31 | 32 | value __mod__$float_$float_(value x, value y) { 33 | assert(x.type == value::FLOAT && y.type == value::FLOAT); 34 | return make$float_(fmod(x.floatval, y.floatval)); 35 | } 36 | 37 | value __eq__$float_$float_(value x, value y) { 38 | assert(x.type == value::FLOAT && y.type == value::FLOAT); 39 | return make$bool_(x.floatval == y.floatval); 40 | } 41 | 42 | value __ne__$float_$float_(value x, value y) { 43 | assert(x.type == value::FLOAT && y.type == value::FLOAT); 44 | return make$bool_(x.floatval != y.floatval); 45 | } 46 | 47 | value __lt__$float_$float_(value x, value y) { 48 | assert(x.type == value::FLOAT && y.type == value::FLOAT); 49 | return make$bool_(x.floatval < y.floatval); 50 | } 51 | 52 | value __le__$float_$float_(value x, value y) { 53 | assert(x.type == value::FLOAT && y.type == value::FLOAT); 54 | return make$bool_(x.floatval <= y.floatval); 55 | } 56 | 57 | value __ge__$float_$float_(value x, value y) { 58 | assert(x.type == value::FLOAT && y.type == value::FLOAT); 59 | return make$bool_(x.floatval >= y.floatval); 60 | } 61 | 62 | value __gt__$float_$float_(value x, value y) { 63 | assert(x.type == value::FLOAT && y.type == value::FLOAT); 64 | return make$bool_(x.floatval > y.floatval); 65 | } 66 | 67 | value __bool__$float_(value self) { 68 | assert(self.type == value::FLOAT); 69 | return make$bool_(self.floatval != 0.0); 70 | } 71 | -------------------------------------------------------------------------------- /set.cpp: -------------------------------------------------------------------------------- 1 | #include "set.h" 2 | #include "bool.h" 3 | #include "int.h" 4 | #include "assert.h" 5 | #include "none.h" 6 | #include "set_iterator.h" 7 | 8 | value make$set() { 9 | value ret; 10 | ret.type = value::SET; 11 | ret.setval = new set_t(); 12 | return ret; 13 | } 14 | 15 | value add$set$(value self, value v) { 16 | assert(self.type == value::SET); 17 | self.setval->insert(v); 18 | return make$set(); 19 | } 20 | 21 | value remove$set$(value self, value v) { 22 | assert(self.type == value::SET); 23 | auto it = self.setval->find(v); 24 | if (it == self.setval->end()) 25 | throw std::runtime_error("key error"); 26 | self.setval->erase(it); 27 | return make$set(); 28 | } 29 | 30 | value __contains__$set$(value self, value v) { 31 | assert(self.type == value::SET); 32 | return make$int_(self.setval->find(v) != self.setval->end()); 33 | } 34 | 35 | value __bool__$set(value self) { 36 | assert(self.type == value::SET); 37 | return make$int_(!self.setval->empty()); 38 | } 39 | 40 | value __len__$set(value self) { 41 | assert(self.type == value::SET); 42 | return make$int_(self.setval->size()); 43 | } 44 | 45 | value __eq__$set$set(value x, value y) { 46 | assert(x.type == value::SET && y.type == value::SET); 47 | return make$int_(*x.setval == *y.setval); 48 | } 49 | 50 | value __ne__$set$set(value x, value y) { 51 | assert(x.type == value::SET && y.type == value::SET); 52 | return make$int_(*x.setval != *y.setval); 53 | } 54 | 55 | value __lt__$set$set(value x, value y) { 56 | assert(x.type == value::SET && y.type == value::SET); 57 | 58 | if (x.setval->size() >= y.setval->size()) 59 | return make$int_(false); 60 | 61 | for (value v : *x.setval) { 62 | if (y.setval->find(v) == y.setval->end()) 63 | return make$int_(false); 64 | } 65 | return make$int_(true); 66 | } 67 | 68 | value __le__$set$set(value x, value y) { 69 | assert(x.type == value::SET && y.type == value::SET); 70 | 71 | if (x.setval->size() > y.setval->size()) 72 | return make$int_(false); 73 | 74 | for (value v : *x.setval) { 75 | if (y.setval->find(v) == y.setval->end()) 76 | return make$int_(false); 77 | } 78 | return make$int_(true); 79 | } 80 | 81 | value __ge__$set$set(value x, value y) { 82 | return __le__$set$set(y, x); 83 | } 84 | 85 | value __gt__$set$set(value x, value y) { 86 | return __lt__$set$set(y, x); 87 | } 88 | 89 | value __iter__$set(value self) { 90 | assert(self.type == value::SET); 91 | return make$set_iterator(self.setval); 92 | } 93 | -------------------------------------------------------------------------------- /int.cpp: -------------------------------------------------------------------------------- 1 | #include "int.h" 2 | #include "float.h" 3 | #include "bool.h" 4 | #include "assert.h" 5 | 6 | value make$int_(long v) { 7 | value ret; 8 | ret.type = value::INT; 9 | ret.intval = v; 10 | return ret; 11 | } 12 | 13 | value __add__$int_$int_(value x, value y) { 14 | assert(x.type == value::INT && y.type == value::INT); 15 | return make$int_(x.intval + y.intval); 16 | } 17 | 18 | value __sub__$int_$int_(value x, value y) { 19 | assert(x.type == value::INT && y.type == value::INT); 20 | return make$int_(x.intval - y.intval); 21 | } 22 | 23 | value __truediv__$int_$int_(value x, value y) { 24 | assert(x.type == value::INT && y.type == value::INT); 25 | return __truediv__$float_$float_(__float__$int_(x), __float__$int_(y)); 26 | } 27 | 28 | value __mul__$int_$int_(value x, value y) { 29 | assert(x.type == value::INT && y.type == value::INT); 30 | return make$int_(x.intval * y.intval); 31 | } 32 | 33 | value __mod__$int_$int_(value x, value y) { 34 | assert(x.type == value::INT && y.type == value::INT); 35 | return make$int_(x.intval % y.intval); 36 | } 37 | 38 | value __float__$int_(value self) { 39 | assert(self.type == value::INT); 40 | return make$float_(self.intval); 41 | } 42 | 43 | value __eq__$int_$int_(value x, value y) { 44 | assert(x.type == value::INT && y.type == value::INT); 45 | return make$bool_(x.intval == y.intval); 46 | } 47 | 48 | value __ne__$int_$int_(value x, value y) { 49 | assert(x.type == value::INT && y.type == value::INT); 50 | return make$bool_(x.intval != y.intval); 51 | } 52 | 53 | value __lt__$int_$int_(value x, value y) { 54 | assert(x.type == value::INT && y.type == value::INT); 55 | return make$bool_(x.intval < y.intval); 56 | } 57 | 58 | value __le__$int_$int_(value x, value y) { 59 | assert(x.type == value::INT && y.type == value::INT); 60 | return make$bool_(x.intval <= y.intval); 61 | } 62 | 63 | value __ge__$int_$int_(value x, value y) { 64 | assert(x.type == value::INT && y.type == value::INT); 65 | return make$bool_(x.intval >= y.intval); 66 | } 67 | 68 | value __gt__$int_$int_(value x, value y) { 69 | assert(x.type == value::INT && y.type == value::INT); 70 | return make$bool_(x.intval > y.intval); 71 | } 72 | 73 | value __bool__$int_(value self) { 74 | assert(self.type == value::INT); 75 | return make$bool_(self.intval != 0); 76 | } 77 | 78 | value __and__$int_$int_(value x, value y) { 79 | assert(x.type == value::INT && y.type == value::INT); 80 | return make$int_(x.intval & y.intval); 81 | } 82 | 83 | value __or__$int_$int_(value x, value y) { 84 | assert(x.type == value::INT && y.type == value::INT); 85 | return make$int_(x.intval | y.intval); 86 | } 87 | 88 | value __xor__$int_$int_(value x, value y) { 89 | assert(x.type == value::INT && y.type == value::INT); 90 | return make$int_(x.intval ^ y.intval); 91 | } 92 | 93 | value __invert__$int_(value self) { 94 | assert(self.type == value::INT); 95 | return make$int_(~self.intval); 96 | } 97 | 98 | value __lshift__$int_$int_(value x, value y) { 99 | assert(x.type == value::INT && y.type == value::INT); 100 | return make$int_(x.intval << y.intval); 101 | } 102 | 103 | value __rshift__$int_$int_(value x, value y) { 104 | assert(x.type == value::INT && y.type == value::INT); 105 | return make$int_(x.intval >> y.intval); 106 | } 107 | -------------------------------------------------------------------------------- /generated.h: -------------------------------------------------------------------------------- 1 | #include "type.h" 2 | value __add__$float_$int_(value x, value y); 3 | value __sub__$float_$int_(value x, value y); 4 | value __mul__$float_$int_(value x, value y); 5 | value __truediv__$float_$int_(value x, value y); 6 | value __lt__$float_$int_(value x, value y); 7 | value __le__$float_$int_(value x, value y); 8 | value __gt__$float_$int_(value x, value y); 9 | value __ge__$float_$int_(value x, value y); 10 | value __mod__$float_$int_(value x, value y); 11 | value __eq__$float_$int_(value x, value y); 12 | value __ne__$float_$int_(value x, value y); 13 | value __add__$float_$bool_(value x, value y); 14 | value __sub__$float_$bool_(value x, value y); 15 | value __mul__$float_$bool_(value x, value y); 16 | value __truediv__$float_$bool_(value x, value y); 17 | value __lt__$float_$bool_(value x, value y); 18 | value __le__$float_$bool_(value x, value y); 19 | value __gt__$float_$bool_(value x, value y); 20 | value __ge__$float_$bool_(value x, value y); 21 | value __mod__$float_$bool_(value x, value y); 22 | value __eq__$float_$bool_(value x, value y); 23 | value __ne__$float_$bool_(value x, value y); 24 | value __add__$int_$float_(value x, value y); 25 | value __sub__$int_$float_(value x, value y); 26 | value __mul__$int_$float_(value x, value y); 27 | value __truediv__$int_$float_(value x, value y); 28 | value __lt__$int_$float_(value x, value y); 29 | value __le__$int_$float_(value x, value y); 30 | value __gt__$int_$float_(value x, value y); 31 | value __ge__$int_$float_(value x, value y); 32 | value __mod__$int_$float_(value x, value y); 33 | value __eq__$int_$float_(value x, value y); 34 | value __ne__$int_$float_(value x, value y); 35 | value __add__$int_$bool_(value x, value y); 36 | value __sub__$int_$bool_(value x, value y); 37 | value __mul__$int_$bool_(value x, value y); 38 | value __truediv__$int_$bool_(value x, value y); 39 | value __lt__$int_$bool_(value x, value y); 40 | value __le__$int_$bool_(value x, value y); 41 | value __gt__$int_$bool_(value x, value y); 42 | value __ge__$int_$bool_(value x, value y); 43 | value __xor__$int_$bool_(value x, value y); 44 | value __and__$int_$bool_(value x, value y); 45 | value __or__$int_$bool_(value x, value y); 46 | value __mod__$int_$bool_(value x, value y); 47 | value __eq__$int_$bool_(value x, value y); 48 | value __ne__$int_$bool_(value x, value y); 49 | value __lshift__$int_$bool_(value x, value y); 50 | value __rshift__$int_$bool_(value x, value y); 51 | value __add__$bool_$float_(value x, value y); 52 | value __sub__$bool_$float_(value x, value y); 53 | value __mul__$bool_$float_(value x, value y); 54 | value __truediv__$bool_$float_(value x, value y); 55 | value __lt__$bool_$float_(value x, value y); 56 | value __le__$bool_$float_(value x, value y); 57 | value __gt__$bool_$float_(value x, value y); 58 | value __ge__$bool_$float_(value x, value y); 59 | value __mod__$bool_$float_(value x, value y); 60 | value __eq__$bool_$float_(value x, value y); 61 | value __ne__$bool_$float_(value x, value y); 62 | value __add__$bool_$int_(value x, value y); 63 | value __sub__$bool_$int_(value x, value y); 64 | value __mul__$bool_$int_(value x, value y); 65 | value __truediv__$bool_$int_(value x, value y); 66 | value __lt__$bool_$int_(value x, value y); 67 | value __le__$bool_$int_(value x, value y); 68 | value __gt__$bool_$int_(value x, value y); 69 | value __ge__$bool_$int_(value x, value y); 70 | value __xor__$bool_$int_(value x, value y); 71 | value __and__$bool_$int_(value x, value y); 72 | value __or__$bool_$int_(value x, value y); 73 | value __mod__$bool_$int_(value x, value y); 74 | value __eq__$bool_$int_(value x, value y); 75 | value __ne__$bool_$int_(value x, value y); 76 | value __lshift__$bool_$int_(value x, value y); 77 | value __rshift__$bool_$int_(value x, value y); 78 | -------------------------------------------------------------------------------- /bool.cpp: -------------------------------------------------------------------------------- 1 | #include "bool.h" 2 | #include "int.h" 3 | #include "float.h" 4 | #include "assert.h" 5 | #include "str.h" 6 | #include "list.h" 7 | 8 | value make$bool_(bool v) { 9 | value ret; 10 | ret.type = value::BOOL; 11 | ret.boolval = v; 12 | return ret; 13 | } 14 | 15 | value __eq__$bool_$bool_(value x, value y) { 16 | assert(x.type == value::BOOL && y.type == value::BOOL); 17 | return make$bool_(x.boolval == y.boolval); 18 | } 19 | 20 | value __ne__$bool_$bool_(value x, value y) { 21 | assert(x.type == value::BOOL && y.type == value::BOOL); 22 | return make$bool_(x.boolval != y.boolval); 23 | } 24 | 25 | value __lt__$bool_$bool_(value x, value y) { 26 | assert(x.type == value::BOOL && y.type == value::BOOL); 27 | return make$bool_(x.boolval < y.boolval); 28 | } 29 | 30 | value __le__$bool_$bool_(value x, value y) { 31 | assert(x.type == value::BOOL && y.type == value::BOOL); 32 | return make$bool_(x.boolval <= y.boolval); 33 | } 34 | 35 | value __ge__$bool_$bool_(value x, value y) { 36 | assert(x.type == value::BOOL && y.type == value::BOOL); 37 | return make$bool_(x.boolval >= y.boolval); 38 | } 39 | 40 | value __gt__$bool_$bool_(value x, value y) { 41 | assert(x.type == value::BOOL && y.type == value::BOOL); 42 | return make$bool_(x.boolval > y.boolval); 43 | } 44 | 45 | value __add__$bool_$bool_(value x, value y) { 46 | assert(x.type == value::BOOL && y.type == value::BOOL); 47 | return __add__$int_$int_(__int__$bool_(x), __int__$bool_(y)); 48 | } 49 | 50 | 51 | value __truediv__$bool_$bool_(value x, value y) { 52 | assert(x.type == value::BOOL && y.type == value::BOOL); 53 | return __truediv__$int_$int_(__int__$bool_(x), __int__$bool_(y)); 54 | } 55 | 56 | 57 | value __sub__$bool_$bool_(value x, value y) { 58 | assert(x.type == value::BOOL && y.type == value::BOOL); 59 | return __sub__$int_$int_(__int__$bool_(x), __int__$bool_(y)); 60 | } 61 | 62 | 63 | value __mul__$bool_$bool_(value x, value y) { 64 | assert(x.type == value::BOOL && y.type == value::BOOL); 65 | return __mul__$int_$int_(__int__$bool_(x), __int__$bool_(y)); 66 | } 67 | 68 | value __mod__$bool_$bool_(value x, value y) { 69 | assert(x.type == value::BOOL && y.type == value::BOOL); 70 | return __mod__$int_$int_(__int__$bool_(x), __int__$bool_(y)); 71 | } 72 | value __lshift__$bool_$bool_(value x, value y) { 73 | assert(x.type == value::BOOL && y.type == value::BOOL); 74 | return __lshift__$int_$int_(__int__$bool_(x), __int__$bool_(y)); 75 | } 76 | value __rshift__$bool_$bool_(value x, value y) { 77 | assert(x.type == value::BOOL && y.type == value::BOOL); 78 | return __rshift__$int_$int_(__int__$bool_(x), __int__$bool_(y)); 79 | } 80 | 81 | value __invert__$bool_(value x) { 82 | assert(x.type == value::BOOL); 83 | return __invert__$int_(__int__$bool_(x)); 84 | } 85 | 86 | value __bool__$bool_(value self) { 87 | assert(self.type == value::BOOL); 88 | return self; 89 | } 90 | 91 | value __int__$bool_(value self) { 92 | assert(self.type == value::BOOL); 93 | return make$int_(self.boolval? 1: 0); 94 | } 95 | 96 | value __float__$bool_(value self) { 97 | assert(self.type == value::BOOL); 98 | return make$float_(self.boolval? 1.0: 0.0); 99 | } 100 | 101 | value __and__$bool_$bool_(value x, value y) { 102 | assert(x.type == value::BOOL && y.type == value::BOOL); 103 | return make$bool_(x.boolval & y.boolval); 104 | } 105 | 106 | value __or__$bool_$bool_(value x, value y) { 107 | assert(x.type == value::BOOL && y.type == value::BOOL); 108 | return make$bool_(x.boolval | y.boolval); 109 | } 110 | 111 | value __xor__$bool_$bool_(value x, value y) { 112 | assert(x.type == value::BOOL && y.type == value::BOOL); 113 | return make$bool_(x.boolval ^ y.boolval); 114 | } 115 | -------------------------------------------------------------------------------- /str.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "str.h" 3 | #include "int.h" 4 | #include "bool.h" 5 | #include "assert.h" 6 | 7 | value make$str(const char *v) { 8 | value ret; 9 | ret.type = value::STR; 10 | ret.strval = new str_t(v); 11 | return ret; 12 | } 13 | 14 | value __getitem__$str$(value self, value k) { 15 | assert(self.type == value::STR); 16 | switch (k.type) { 17 | case value::INT: 18 | return __getitem__$str$int_(self, k); 19 | case value::SLICE: 20 | return __getitem__$str$slice(self, k); 21 | } 22 | throw std::runtime_error("Illegal argument type for str::__getitem__"); 23 | } 24 | 25 | value __getitem__$str$int_(value self, value k) { 26 | value val; 27 | val.type = value::STR; 28 | val.strval = new str_t(1, self.strval->at(k.intval)); 29 | return val; 30 | } 31 | 32 | value __getitem__$str$slice(value self, value k) { 33 | long start = k.sliceval->start; 34 | long stop = k.sliceval->stop; 35 | long step = k.sliceval->step; 36 | if (start < 0) 37 | start = (long)self.strval->size() + start; 38 | if (stop < 0) 39 | stop = (long)self.strval->size() + stop; 40 | 41 | str_t *ret = new str_t(); 42 | if (step > 0) { 43 | start = std::max(0L, start); 44 | stop = std::min((long)self.strval->size(), stop); 45 | for (long i = start; i < stop; i += step) { 46 | ret->push_back(self.strval->at(i)); 47 | } 48 | } else { 49 | start = std::min((long)self.strval->size() - 1L, start); 50 | stop = std::max(-1L, stop); 51 | for (long i = start; i > stop; i += step) { 52 | ret->push_back(self.strval->at(i)); 53 | } 54 | } 55 | 56 | value val; 57 | val.type = value::STR; 58 | val.strval = ret; 59 | return val; 60 | } 61 | 62 | value __len__$str(value self) { 63 | assert(self.type == value::STR); 64 | return make$int_(self.strval->size()); 65 | } 66 | 67 | value __add__$str$str(value x, value y) { 68 | assert(x.type == value::STR && y.type == value::STR); 69 | 70 | str_t *merged = new str_t(); 71 | merged->reserve(x.strval->size() + y.strval->size()); 72 | merged->insert(merged->end(), x.strval->begin(), x.strval->end()); 73 | merged->insert(merged->end(), y.strval->begin(), y.strval->end()); 74 | 75 | value ret; 76 | ret.type = value::STR; 77 | ret.strval = merged; 78 | return ret; 79 | } 80 | 81 | 82 | value __mul__$str$bool_(value self, value n) { 83 | assert(self.type == value::STR && n.type == value::BOOL); 84 | return __mul__$str$int_(self, __int__$bool_(n)); 85 | } 86 | value __mul__$bool_$str(value n, value self) { 87 | assert(self.type == value::STR && n.type == value::BOOL); 88 | return __mul__$int_$str(__int__$bool_(n), self); 89 | } 90 | 91 | value __mul__$int_$str(value n, value self) { 92 | assert(self.type == value::STR && n.type == value::INT); 93 | return __mul__$str$int_(self, n); 94 | } 95 | 96 | value __mul__$str$int_(value self, value n) { 97 | assert(self.type == value::STR && n.type == value::INT); 98 | 99 | str_t *duped = new str_t(); 100 | if (n.intval > 0) { 101 | duped->reserve(self.strval->size() * n.intval); 102 | for (long i = 0; i < n.intval; i++) 103 | duped->insert(duped->end(), self.strval->begin(), self.strval->end()); 104 | } 105 | 106 | value ret; 107 | ret.type = value::STR; 108 | ret.strval = duped; 109 | return ret; 110 | } 111 | 112 | value __eq__$str$str(value x, value y) { 113 | assert(x.type == value::STR && y.type == value::STR); 114 | return make$bool_(*x.strval == *y.strval); 115 | } 116 | 117 | value __ne__$str$str(value x, value y) { 118 | assert(x.type == value::STR && y.type == value::STR); 119 | return make$bool_(*x.strval != *y.strval); 120 | } 121 | 122 | value __lt__$str$str(value x, value y) { 123 | assert(x.type == value::STR && y.type == value::STR); 124 | return make$bool_(*x.strval < *y.strval); 125 | } 126 | 127 | value __le__$str$str(value x, value y) { 128 | assert(x.type == value::STR && y.type == value::STR); 129 | return make$bool_(*x.strval <= *y.strval); 130 | } 131 | 132 | value __ge__$str$str(value x, value y) { 133 | assert(x.type == value::STR && y.type == value::STR); 134 | return make$bool_(*x.strval >= *y.strval); 135 | } 136 | 137 | value __gt__$str$str(value x, value y) { 138 | assert(x.type == value::STR && y.type == value::STR); 139 | return make$bool_(*x.strval > *y.strval); 140 | } 141 | 142 | value __contains__$str$str(value self, value sub) { 143 | assert(self.type == value::STR && sub.type == value::STR); 144 | return make$bool_(self.strval->find(*sub.strval) != std::string::npos); 145 | } 146 | 147 | value __bool__$str(value self) { 148 | assert(self.type == value::STR); 149 | return make$bool_(!self.strval->empty()); 150 | } 151 | -------------------------------------------------------------------------------- /type.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "func.h" 8 | 9 | struct value; 10 | typedef std::vector list_t; 11 | typedef std::string str_t; 12 | typedef std::unordered_set set_t; 13 | typedef std::unordered_map dict_t; 14 | typedef struct { long start, stop, step; } range_t; 15 | typedef range_t range_iterator_t; 16 | typedef struct { long start, stop, step; } slice_t; 17 | typedef struct { list_t *list; size_t index; } list_iterator_t; 18 | struct set_iterator_t; 19 | struct dict_iterator_t; 20 | 21 | struct base_value { 22 | mutable size_t hash; 23 | base_value(): hash(0) { 24 | 25 | } 26 | }; 27 | 28 | struct value { 29 | mutable size_t hash; 30 | union { 31 | bool boolval; 32 | long intval; 33 | double floatval; 34 | str_t *strval; 35 | list_t *listval; 36 | set_t *setval; 37 | dict_t *dictval; 38 | range_t *rangeval; 39 | range_iterator_t *range_iteratorval; 40 | slice_t *sliceval; 41 | list_iterator_t *list_iteratorval; 42 | set_iterator_t *set_iteratorval; 43 | dict_iterator_t *dict_iteratorval; 44 | void* voidval; 45 | }; 46 | enum Type { NONE, BOOL, INT, FLOAT, STR, LIST, SET, DICT, RANGE, RANGE_ITERATOR, SLICE, LIST_ITERATOR, SET_ITERATOR, DICT_ITERATOR } type; 47 | value(): hash(0) { 48 | 49 | } 50 | 51 | bool operator ==(value other) const { 52 | return __eq__(*this, other).boolval; 53 | } 54 | 55 | bool operator !=(value other) const { 56 | return __ne__(*this, other).boolval; 57 | } 58 | 59 | bool operator <(value other) const { 60 | return __lt__(*this, other).boolval; 61 | } 62 | 63 | bool operator >(value other) const { 64 | return __gt__(*this, other).boolval; 65 | } 66 | 67 | bool operator <=(value other) const { 68 | return __le__(*this, other).boolval; 69 | } 70 | 71 | bool operator >=(value other) const { 72 | return __ge__(*this, other).boolval; 73 | } 74 | 75 | value operator +(value other) const { 76 | return __add__(*this, other); 77 | } 78 | 79 | value operator -(value other) const { 80 | return __sub__(*this, other); 81 | } 82 | 83 | value operator *(value other) const { 84 | return __mul__(*this, other); 85 | } 86 | 87 | value operator /(value other) const { 88 | return __truediv__(*this, other); 89 | } 90 | 91 | value operator %(value other) const { 92 | return __mod__(*this, other); 93 | } 94 | 95 | value operator &(value other) const { 96 | return __and__(*this, other); 97 | } 98 | 99 | value operator |(value other) const { 100 | return __or__(*this, other); 101 | } 102 | 103 | value operator ^(value other) const { 104 | return __xor__(*this, other); 105 | } 106 | 107 | value operator ~() const { 108 | return __invert__(*this); 109 | } 110 | 111 | value operator <<(value other) const { 112 | return __lshift__(*this, other); 113 | } 114 | 115 | value operator >>(value other) const { 116 | return __rshift__(*this, other); 117 | } 118 | 119 | operator bool() const { 120 | return __bool__(*this).boolval; 121 | } 122 | }; 123 | 124 | namespace std { 125 | template<> 126 | struct hash { 127 | size_t operator ()(const value& v) const { 128 | if (v.hash) { 129 | return v.hash; 130 | } 131 | printf("do calc\n"); 132 | switch (v.type) { 133 | case value::NONE: 134 | return v.hash = -9223372036577629359; 135 | case value::BOOL: 136 | return v.hash = std::hash()(v.boolval); 137 | case value::INT: 138 | return v.hash = std::hash()(v.intval); 139 | case value::FLOAT: 140 | if (v.floatval == (long)v.floatval) 141 | return v.hash = std::hash()((long)v.floatval); 142 | return v.hash = std::hash()(v.floatval); 143 | case value::STR: 144 | printf("%s\n", v.strval->c_str()); 145 | return v.hash = std::hash()(*v.strval); 146 | default: 147 | throw std::runtime_error("unhashable type"); 148 | } 149 | } 150 | }; 151 | } 152 | 153 | struct set_iterator_t { set_t::const_iterator cur, end; }; 154 | struct dict_iterator_t { dict_t::const_iterator cur, end; }; -------------------------------------------------------------------------------- /list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "list.h" 4 | #include "int.h" 5 | #include "bool.h" 6 | #include "func.h" 7 | #include "assert.h" 8 | #include "none.h" 9 | #include "list_iterator.h" 10 | 11 | value make$list() { 12 | value ret; 13 | ret.type = value::LIST; 14 | ret.listval = new list_t(); 15 | return ret; 16 | } 17 | 18 | value append$list$(value self, value v) { 19 | assert(self.type == value::LIST); 20 | self.listval->push_back(v); 21 | return make$none(); 22 | } 23 | 24 | value pop$list(value self) { 25 | assert(self.type == value::LIST); 26 | value ret = self.listval->back(); 27 | self.listval->pop_back(); 28 | return ret; 29 | } 30 | 31 | value __getitem__$list$(value self, value k) { 32 | assert(self.type == value::LIST); 33 | switch (k.type) { 34 | case value::INT: 35 | return __getitem__$list$int_(self, k); 36 | case value::SLICE: 37 | return __getitem__$list$slice(self, k); 38 | } 39 | throw std::runtime_error("Invalid argument type for list::__getitem__"); 40 | } 41 | 42 | value __setitem__$list$$(value self, value k, value v) { 43 | assert(self.type == value::LIST); 44 | switch (k.type) { 45 | case value::INT: 46 | return __setitem__$list$int_$(self, k, v); 47 | case value::SLICE: 48 | return __setitem__$list$slice$(self, k, v); 49 | } 50 | return make$none(); 51 | } 52 | 53 | value __add__$list$list(value x, value y) { 54 | assert(x.type == value::LIST && y.type == value::LIST); 55 | 56 | list_t *merged = new list_t(); 57 | merged->reserve(x.listval->size() + y.listval->size()); 58 | merged->insert(merged->end(), x.listval->begin(), x.listval->end()); 59 | merged->insert(merged->end(), y.listval->begin(), y.listval->end()); 60 | 61 | value ret; 62 | ret.type = value::LIST; 63 | ret.listval = merged; 64 | return ret; 65 | } 66 | value __mul__$list$bool_(value self, value n) { 67 | assert(self.type == value::LIST && n.type == value::BOOL); 68 | return __mul__$list$int_(self, __int__$bool_(n)); 69 | } 70 | value __mul__$bool_$list(value n, value self) { 71 | assert(self.type == value::LIST && n.type == value::BOOL); 72 | return __mul__$int_$list(__int__$bool_(n), self); 73 | } 74 | value __mul__$int_$list(value n, value self) { 75 | assert(self.type == value::LIST && n.type == value::INT); 76 | return __mul__$list$int_(self, n); 77 | } 78 | 79 | value __mul__$list$int_(value self, value n) { 80 | assert(self.type == value::LIST && n.type == value::INT); 81 | 82 | list_t *duped = new list_t(); 83 | if (n.intval > 0) { 84 | duped->reserve(self.listval->size() * n.intval); 85 | for (long i = 0; i < n.intval; i++) 86 | duped->insert(duped->end(), self.listval->begin(), self.listval->end()); 87 | } 88 | 89 | value ret; 90 | ret.type = value::LIST; 91 | ret.listval = duped; 92 | return ret; 93 | } 94 | 95 | value __len__$list(value self) { 96 | assert(self.type == value::LIST); 97 | return make$int_(self.listval->size()); 98 | } 99 | 100 | value __eq__$list$list(value x, value y) { 101 | assert(x.type == value::LIST && y.type == value::LIST); 102 | return make$bool_(*x.listval == *y.listval); 103 | } 104 | 105 | value __ne__$list$list(value x, value y) { 106 | assert(x.type == value::LIST && y.type == value::LIST); 107 | return make$bool_(*x.listval != *y.listval); 108 | } 109 | 110 | value __lt__$list$list(value x, value y) { 111 | assert(x.type == value::LIST && y.type == value::LIST); 112 | return make$bool_(*x.listval < *y.listval); 113 | } 114 | 115 | value __le__$list$list(value x, value y) { 116 | assert(x.type == value::LIST && y.type == value::LIST); 117 | return make$bool_(*x.listval <= *y.listval); 118 | } 119 | 120 | value __ge__$list$list(value x, value y) { 121 | assert(x.type == value::LIST && y.type == value::LIST); 122 | return make$bool_(*x.listval >= *y.listval); 123 | } 124 | 125 | value __gt__$list$list(value x, value y) { 126 | assert(x.type == value::LIST && y.type == value::LIST); 127 | return make$bool_(*x.listval > *y.listval); 128 | } 129 | 130 | value __contains__$list$(value self, value v) { 131 | assert(self.type == value::LIST); 132 | 133 | for (size_t i = 0; i < self.listval->size(); i++) 134 | if ((*self.listval)[i] == v) 135 | return make$bool_(true); 136 | return make$bool_(false); 137 | } 138 | 139 | value __delitem__$list$int_(value self, value k) { 140 | assert(self.type == value::LIST && k.type == value::INT); 141 | self.listval->erase(self.listval->begin() + k.intval); 142 | return make$none(); 143 | } 144 | 145 | value __delitem__$list$slice(value self, value k) { 146 | assert(false); 147 | return make$none(); 148 | } 149 | 150 | value __bool__$list(value self) { 151 | assert(self.type == value::LIST); 152 | return make$bool_(!self.listval->empty()); 153 | } 154 | 155 | 156 | value __getitem__$list$int_(value self, value k) { 157 | return self.listval->at(k.intval); 158 | } 159 | 160 | value __getitem__$list$slice(value self, value k) { 161 | long start = k.sliceval->start; 162 | long stop = k.sliceval->stop; 163 | long step = k.sliceval->step; 164 | if (start < 0) 165 | start = (long)self.listval->size() + start; 166 | if (stop < 0) 167 | stop = (long)self.listval->size() + stop; 168 | 169 | list_t *ret = new list_t(); 170 | if (step > 0) { 171 | start = std::max(0L, start); 172 | stop = std::min((long)self.listval->size(), stop); 173 | for (long i = start; i < stop; i += step) { 174 | ret->push_back(self.listval->at(i)); 175 | } 176 | } else { 177 | start = std::min((long)self.listval->size() - 1L, start); 178 | stop = std::max(-1L, stop); 179 | for (long i = start; i > stop; i += step) { 180 | ret->push_back(self.listval->at(i)); 181 | } 182 | } 183 | 184 | value val; 185 | val.type = value::LIST; 186 | val.listval = ret; 187 | return val; 188 | } 189 | 190 | value __setitem__$list$int_$(value self, value k, value v) { 191 | assert(self.type == value::LIST && k.type == value::INT); 192 | self.listval->at(k.intval) = v; 193 | return make$none(); 194 | } 195 | 196 | value __setitem__$list$slice$(value self, value k, value v) { 197 | assert(self.type == value::LIST && k.type == value::SLICE); 198 | if (v.type != value::LIST) 199 | throw std::runtime_error("list::__setitem__ must pass in a list"); 200 | long start = k.sliceval->start; 201 | long stop = k.sliceval->stop; 202 | long step = k.sliceval->step; 203 | if (start < 0) 204 | start = (long)self.listval->size() + start; 205 | if (stop < 0) 206 | stop = (long)self.listval->size() + stop; 207 | 208 | if (step > 0) { 209 | start = std::min((long)self.listval->size(), std::max(0L, start)); 210 | stop = std::max(start, std::min((long)self.listval->size(), stop)); 211 | if (step == 1) { 212 | self.listval->erase(self.listval->begin() + start, 213 | self.listval->begin() + stop); 214 | self.listval->insert(self.listval->begin() + start, v.listval->begin(), 215 | v.listval->end()); 216 | } else { 217 | long count = (stop - start + step - 1) / step; 218 | if (count != v.listval->size()) 219 | throw std::runtime_error( 220 | "list::__setitem__ slice length not match value length"); 221 | for (long i = 0; i < count; i++) { 222 | self.listval->at(start + step * i) = v.listval->at(i); 223 | } 224 | } 225 | } else { 226 | start = std::min((long)self.listval->size() - 1L, std::max(-1L, start)); 227 | stop = std::min(start, std::max(-1L, stop)); 228 | long count = (start - stop + -step - 1) / -step; 229 | if (count != v.listval->size()) 230 | throw std::runtime_error( 231 | "list::__setitem__ slice length not match value length"); 232 | for (long i = 0; i < count; i++) { 233 | self.listval->at(start + i * step) = v.listval->at(i); 234 | } 235 | } 236 | return make$none(); 237 | } 238 | 239 | value __iter__$list(value self) { 240 | assert(self.type == value::LIST); 241 | return make$list_iterator(self.listval); 242 | } 243 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "assert.h" 4 | #include "type.h" 5 | #include "list.h" 6 | #include "int.h" 7 | #include "float.h" 8 | #include "str.h" 9 | #include "dict.h" 10 | #include "bool.h" 11 | #include "set.h" 12 | #include "func.h" 13 | #include "none.h" 14 | #include "range.h" 15 | #include "range_iterator.h" 16 | #include "slice.h" 17 | 18 | void test_none() { 19 | assert(make$none() == make$none()); 20 | assert(__bool__(make$none()) == make$bool_(false)); 21 | } 22 | 23 | void test_bool() { 24 | assert(make$bool_(true) == make$int_(1)); 25 | assert(make$bool_(false) == make$float_(0)); 26 | assert(make$bool_(false) < make$bool_(true)); 27 | assert(!(make$bool_(false) < make$bool_(false))); 28 | assert(make$bool_(true) <= make$bool_(true)); 29 | assert(make$bool_(true) > make$int_(0)); 30 | assert(make$bool_(true) >= make$float_(0.5)); 31 | assert(__bool__(make$bool_(true))); 32 | assert(!__bool__(make$bool_(false))); 33 | assert(make$bool_(true) & make$bool_(true)); 34 | assert(!(make$bool_(false) | make$bool_(false))); 35 | assert(!(make$bool_(true) ^ make$bool_(true))); 36 | assert(~make$bool_(true) == make$int_(-2)); 37 | assert((make$bool_(true) << make$bool_(true)) == make$int_(2)); 38 | assert((make$int_(3) >> make$bool_(true)) == make$int_(1)); 39 | } 40 | 41 | void test_int() { 42 | assert(make$int_(1) + make$int_(2) == make$int_(3)); 43 | assert(make$int_(1) - make$int_(2) == make$int_(-1)); 44 | assert(make$int_(3) * make$int_(4) == make$int_(12)); 45 | assert(make$int_(10) / make$int_(3) == make$float_(10.0 / 3.0)); 46 | assert(make$int_(10) % make$int_(3) == make$int_(1)); 47 | assert(make$int_(3) + make$float_(4.5) == make$float_(7.5)); 48 | assert(make$int_(2) == make$float_(2.0)); 49 | assert(make$int_(2) < make$float_(2.1)); 50 | assert(__bool__(make$int_(1))); 51 | assert(!__bool__(make$int_(0))); 52 | assert((make$int_(123) & make$int_(456)) == make$int_(123 & 456)); 53 | assert((make$int_(-3) >> make$int_(1)) == make$int_(-2)); 54 | } 55 | 56 | void test_float() { 57 | assert(make$float_(1.1) + make$float_(2.2) == make$float_(1.1 + 2.2)); 58 | assert(make$float_(1.1) - make$float_(2.2) == make$float_(1.1 - 2.2)); 59 | assert(make$float_(1.1) * make$float_(2.2) == make$float_(1.1 * 2.2)); 60 | assert(make$float_(1.1) / make$float_(2.2) == make$float_(1.1 / 2.2)); 61 | assert(make$float_(10.75) % make$float_(5.25) == make$float_(0.25)); 62 | assert(make$float_(3.8) >= make$float_(3.8)); 63 | } 64 | 65 | void test_str() { 66 | assert(make$str("hello") == make$str("hello")); 67 | assert(__getitem__(make$str("hello"), make$int_(1)) == make$str("e")); 68 | assert(make$str("hello") + make$str("world") == make$str("helloworld")); 69 | assert(make$str("he") * make$int_(5) == make$str("hehehehehe")); 70 | assert(make$int_(-10) * make$str("whatever") == make$str("")); 71 | assert(__len__(make$str("12345")) == make$int_(5)); 72 | assert(__contains__(make$str("foobar"), make$str("oob")) == 73 | make$bool_(true)); 74 | assert(__contains__(make$str("foobar"), make$str("boo")) == 75 | make$bool_(false)); 76 | assert(__bool__(make$str("")) == make$bool_(false)); 77 | assert(__bool__(make$str("a")) == make$bool_(true)); 78 | assert(make$str("abc") < make$str("abcd")); 79 | assert(make$str("abd") >= make$str("abcd")); 80 | assert(__getitem__(make$str("0123456"), 81 | slice$int_$int_$int_(make$int_(1), make$int_(50), 82 | make$int_(2))) == make$str("135")); 83 | } 84 | 85 | value list1() { 86 | value l = make$list(); 87 | append(l, make$int_(2)); 88 | append(l, make$float_(3.0)); 89 | append(l, make$str("4")); 90 | return l; 91 | } 92 | 93 | value list2() { 94 | value l = make$list(); 95 | append(l, make$int_(5)); 96 | append(l, make$float_(6.0)); 97 | return l; 98 | } 99 | 100 | value list11() { 101 | value l = make$list(); 102 | append(l, make$int_(2)); 103 | append(l, make$str("w")); 104 | append(l, make$str("4")); 105 | return l; 106 | } 107 | 108 | value list12() { 109 | value l = make$list(); 110 | append(l, make$int_(2)); 111 | append(l, make$float_(3.0)); 112 | append(l, make$str("4")); 113 | append(l, make$int_(5)); 114 | append(l, make$float_(6.0)); 115 | return l; 116 | } 117 | 118 | value list13() { 119 | value l = make$list(); 120 | append(l, make$int_(2)); 121 | append(l, make$float_(3.0)); 122 | append(l, make$str("4")); 123 | append(l, make$int_(2)); 124 | append(l, make$float_(3.0)); 125 | append(l, make$str("4")); 126 | append(l, make$int_(2)); 127 | append(l, make$float_(3.0)); 128 | append(l, make$str("4")); 129 | return l; 130 | } 131 | 132 | value list1_del() { 133 | value l = make$list(); 134 | append(l, make$int_(2)); 135 | append(l, make$str("4")); 136 | return l; 137 | } 138 | 139 | value list1_pop() { 140 | value l = make$list(); 141 | append(l, make$int_(2)); 142 | append(l, make$float_(3.0)); 143 | return l; 144 | } 145 | 146 | value list1_slice() { 147 | value l = make$list(); 148 | append(l, make$str("4")); 149 | append(l, make$int_(2)); 150 | return l; 151 | } 152 | 153 | value list3() { 154 | value l = make$list(); 155 | append(l, make$int_(0)); 156 | append(l, make$int_(1)); 157 | append(l, make$int_(2)); 158 | append(l, make$int_(3)); 159 | append(l, make$int_(4)); 160 | return l; 161 | } 162 | 163 | value list4() { 164 | value l = make$list(); 165 | append(l, make$int_(50)); 166 | append(l, make$int_(51)); 167 | return l; 168 | } 169 | 170 | value list3_setslice1() { 171 | value l = make$list(); 172 | append(l, make$int_(0)); 173 | append(l, make$int_(51)); 174 | append(l, make$int_(2)); 175 | append(l, make$int_(50)); 176 | append(l, make$int_(4)); 177 | return l; 178 | } 179 | 180 | value list3_setslice2() { 181 | value l = make$list(); 182 | append(l, make$int_(0)); 183 | append(l, make$int_(1)); 184 | append(l, make$int_(50)); 185 | append(l, make$int_(51)); 186 | append(l, make$int_(3)); 187 | append(l, make$int_(4)); 188 | return l; 189 | } 190 | 191 | void test_list() { 192 | assert(list1() == list1()); 193 | assert(__len__(list1()) == make$int_(3)); 194 | assert(__getitem__(list1(), make$int_(1)) == make$float_(3.0)); 195 | assert(list1() + list2() == list12()); 196 | assert(make$int_(3) * list1() == list13()); 197 | assert(__contains__(list1(), make$str("4")) == make$bool_(true)); 198 | assert(__contains__(list1(), make$str("5")) == make$bool_(false)); 199 | assert(__bool__(make$list()) == make$bool_(false)); 200 | assert(__bool__(list1()) == make$bool_(true)); 201 | assert(list1() <= list12()); 202 | assert(__getitem__(list1(), slice$int_$int_$int_(make$int_(10), make$int_(-10), 203 | make$int_(-2))) == 204 | list1_slice()); 205 | 206 | value l = list1(); 207 | __setitem__(l, make$int_(1), make$str("w")); 208 | assert(l == list11()); 209 | 210 | l = list1(); 211 | __delitem__(l, make$int_(1)); 212 | assert(l == list1_del()); 213 | 214 | l = list1(); 215 | assert(make$str("4") == pop(l)); 216 | assert(l == list1_pop()); 217 | 218 | l = list3(); 219 | __setitem__(l, 220 | slice$int_$int_$int_(make$int_(3), make$int_(-50), make$int_(-2)), 221 | list4()); 222 | assert(l == list3_setslice1()); 223 | 224 | l = list3(); 225 | __setitem__(l, slice$int_$int_$int_(make$int_(2), make$int_(3), make$int_(1)), 226 | list4()); 227 | assert(l == list3_setslice2()); 228 | 229 | value iter = __iter__(list1()); 230 | assert(__next__(iter) == make$int_(2)); 231 | assert(__next__(iter) == make$float_(3.0)); 232 | assert(__next__(iter) == make$str("4")); 233 | assert(__next__(iter) == make$none()); 234 | } 235 | 236 | value set1() { 237 | value s = make$set(); 238 | add(s, make$int_(1)); 239 | add(s, make$float_(2.0)); 240 | add(s, make$str("3")); 241 | return s; 242 | } 243 | 244 | value set1_removed() { 245 | value s = make$set(); 246 | add(s, make$int_(1)); 247 | add(s, make$float_(2.0)); 248 | return s; 249 | } 250 | 251 | void test_set() { 252 | assert(set1() == set1()); 253 | assert(__len__(set1()) == make$int_(3)); 254 | assert(__contains__(set1(), make$int_(1))); 255 | assert(!__contains__(set1(), make$int_(4))); 256 | assert(set1() > set1_removed()); 257 | assert(!(set1() > set1())); 258 | assert(set1() >= set1()); 259 | 260 | value s = set1(); 261 | add(s, make$str("3")); 262 | assert(s == set1()); 263 | 264 | s = set1(); 265 | add(s, make$float_(1.0)); 266 | assert(s == set1()); 267 | 268 | s = set1(); 269 | remove(s, make$str("3")); 270 | assert(s == set1_removed()); 271 | 272 | value iter = __iter__(set1()); 273 | assert(__next__(iter) != make$none()); 274 | assert(__next__(iter) != make$none()); 275 | assert(__next__(iter) != make$none()); 276 | assert(__next__(iter) == make$none()); 277 | } 278 | 279 | value dict1() { 280 | value d = make$dict(); 281 | __setitem__(d, make$int_(1), make$int_(2)); 282 | __setitem__(d, make$str("a"), make$str("b")); 283 | return d; 284 | } 285 | 286 | value dict1_added() { 287 | value d = make$dict(); 288 | __setitem__(d, make$int_(1), make$str("foo")); 289 | __setitem__(d, make$str("a"), make$str("b")); 290 | return d; 291 | } 292 | 293 | value dict1_del() { 294 | value d = make$dict(); 295 | __setitem__(d, make$int_(1), make$int_(2)); 296 | return d; 297 | } 298 | 299 | void test_dict() { 300 | assert(dict1() == dict1()); 301 | assert(__len__(dict1()) == make$int_(2)); 302 | assert(__contains__(dict1(), make$int_(1))); 303 | assert(!__contains__(dict1(), make$int_(2))); 304 | assert(__getitem__(dict1(), make$int_(1)) == make$int_(2)); 305 | 306 | value d = dict1(); 307 | __setitem__(d, make$int_(1), make$str("foo")); 308 | assert(d == dict1_added()); 309 | assert(d != dict1()); 310 | 311 | d = dict1(); 312 | __delitem__(d, make$str("a")); 313 | assert(d == dict1_del()); 314 | 315 | value iter = __iter__(dict1()); 316 | assert(__next__(iter) != make$none()); 317 | assert(__next__(iter) != make$none()); 318 | assert(__next__(iter) == make$none()); 319 | } 320 | 321 | void test_range() { 322 | value r = range$int_(make$int_(3)); 323 | value iter = __iter__(r); 324 | assert(__next__(iter) == make$int_(0)); 325 | assert(__next__(iter) == make$int_(1)); 326 | assert(__next__(iter) == make$int_(2)); 327 | assert(__next__(iter) == make$none()); 328 | 329 | r = range$int_$int_$int_(make$int_(5), make$int_(1), make$int_(-2)); 330 | iter = __iter__(r); 331 | assert(__next__(iter) == make$int_(5)); 332 | assert(__next__(iter) == make$int_(3)); 333 | assert(__next__(iter) == make$none()); 334 | } 335 | 336 | int main() { 337 | test_none(); 338 | test_bool(); 339 | test_int(); 340 | test_float(); 341 | test_str(); 342 | test_list(); 343 | test_set(); 344 | test_dict(); 345 | test_range(); 346 | } -------------------------------------------------------------------------------- /generated.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "generated.h" 3 | #include "bool.h" 4 | #include "int.h" 5 | #include "float.h" 6 | #include "assert.h" 7 | #include "str.h" 8 | #include "list.h" 9 | 10 | value __add__$float_$int_(value x, value y) { 11 | assert(x.type == value::FLOAT && y.type == value::INT); 12 | return __add__$float_$float_(x, __float__$int_(y)); 13 | } 14 | value __sub__$float_$int_(value x, value y) { 15 | assert(x.type == value::FLOAT && y.type == value::INT); 16 | return __sub__$float_$float_(x, __float__$int_(y)); 17 | } 18 | value __mul__$float_$int_(value x, value y) { 19 | assert(x.type == value::FLOAT && y.type == value::INT); 20 | return __mul__$float_$float_(x, __float__$int_(y)); 21 | } 22 | value __truediv__$float_$int_(value x, value y) { 23 | assert(x.type == value::FLOAT && y.type == value::INT); 24 | return __truediv__$float_$float_(x, __float__$int_(y)); 25 | } 26 | value __lt__$float_$int_(value x, value y) { 27 | assert(x.type == value::FLOAT && y.type == value::INT); 28 | return __lt__$float_$float_(x, __float__$int_(y)); 29 | } 30 | value __le__$float_$int_(value x, value y) { 31 | assert(x.type == value::FLOAT && y.type == value::INT); 32 | return __le__$float_$float_(x, __float__$int_(y)); 33 | } 34 | value __gt__$float_$int_(value x, value y) { 35 | assert(x.type == value::FLOAT && y.type == value::INT); 36 | return __gt__$float_$float_(x, __float__$int_(y)); 37 | } 38 | value __ge__$float_$int_(value x, value y) { 39 | assert(x.type == value::FLOAT && y.type == value::INT); 40 | return __ge__$float_$float_(x, __float__$int_(y)); 41 | } 42 | value __mod__$float_$int_(value x, value y) { 43 | assert(x.type == value::FLOAT && y.type == value::INT); 44 | return __mod__$float_$float_(x, __float__$int_(y)); 45 | } 46 | value __eq__$float_$int_(value x, value y) { 47 | assert(x.type == value::FLOAT && y.type == value::INT); 48 | return __eq__$float_$float_(x, __float__$int_(y)); 49 | } 50 | value __ne__$float_$int_(value x, value y) { 51 | assert(x.type == value::FLOAT && y.type == value::INT); 52 | return __ne__$float_$float_(x, __float__$int_(y)); 53 | } 54 | value __add__$float_$bool_(value x, value y) { 55 | assert(x.type == value::FLOAT && y.type == value::BOOL); 56 | return __add__$float_$float_(x, __float__$bool_(y)); 57 | } 58 | value __sub__$float_$bool_(value x, value y) { 59 | assert(x.type == value::FLOAT && y.type == value::BOOL); 60 | return __sub__$float_$float_(x, __float__$bool_(y)); 61 | } 62 | value __mul__$float_$bool_(value x, value y) { 63 | assert(x.type == value::FLOAT && y.type == value::BOOL); 64 | return __mul__$float_$float_(x, __float__$bool_(y)); 65 | } 66 | value __truediv__$float_$bool_(value x, value y) { 67 | assert(x.type == value::FLOAT && y.type == value::BOOL); 68 | return __truediv__$float_$float_(x, __float__$bool_(y)); 69 | } 70 | value __lt__$float_$bool_(value x, value y) { 71 | assert(x.type == value::FLOAT && y.type == value::BOOL); 72 | return __lt__$float_$float_(x, __float__$bool_(y)); 73 | } 74 | value __le__$float_$bool_(value x, value y) { 75 | assert(x.type == value::FLOAT && y.type == value::BOOL); 76 | return __le__$float_$float_(x, __float__$bool_(y)); 77 | } 78 | value __gt__$float_$bool_(value x, value y) { 79 | assert(x.type == value::FLOAT && y.type == value::BOOL); 80 | return __gt__$float_$float_(x, __float__$bool_(y)); 81 | } 82 | value __ge__$float_$bool_(value x, value y) { 83 | assert(x.type == value::FLOAT && y.type == value::BOOL); 84 | return __ge__$float_$float_(x, __float__$bool_(y)); 85 | } 86 | value __mod__$float_$bool_(value x, value y) { 87 | assert(x.type == value::FLOAT && y.type == value::BOOL); 88 | return __mod__$float_$float_(x, __float__$bool_(y)); 89 | } 90 | value __eq__$float_$bool_(value x, value y) { 91 | assert(x.type == value::FLOAT && y.type == value::BOOL); 92 | return __eq__$float_$float_(x, __float__$bool_(y)); 93 | } 94 | value __ne__$float_$bool_(value x, value y) { 95 | assert(x.type == value::FLOAT && y.type == value::BOOL); 96 | return __ne__$float_$float_(x, __float__$bool_(y)); 97 | } 98 | value __add__$int_$float_(value x, value y) { 99 | assert(x.type == value::INT && y.type == value::FLOAT); 100 | return __add__$float_$float_(__float__$int_(x), y); 101 | } 102 | value __sub__$int_$float_(value x, value y) { 103 | assert(x.type == value::INT && y.type == value::FLOAT); 104 | return __sub__$float_$float_(__float__$int_(x), y); 105 | } 106 | value __mul__$int_$float_(value x, value y) { 107 | assert(x.type == value::INT && y.type == value::FLOAT); 108 | return __mul__$float_$float_(__float__$int_(x), y); 109 | } 110 | value __truediv__$int_$float_(value x, value y) { 111 | assert(x.type == value::INT && y.type == value::FLOAT); 112 | return __truediv__$float_$float_(__float__$int_(x), y); 113 | } 114 | value __lt__$int_$float_(value x, value y) { 115 | assert(x.type == value::INT && y.type == value::FLOAT); 116 | return __lt__$float_$float_(__float__$int_(x), y); 117 | } 118 | value __le__$int_$float_(value x, value y) { 119 | assert(x.type == value::INT && y.type == value::FLOAT); 120 | return __le__$float_$float_(__float__$int_(x), y); 121 | } 122 | value __gt__$int_$float_(value x, value y) { 123 | assert(x.type == value::INT && y.type == value::FLOAT); 124 | return __gt__$float_$float_(__float__$int_(x), y); 125 | } 126 | value __ge__$int_$float_(value x, value y) { 127 | assert(x.type == value::INT && y.type == value::FLOAT); 128 | return __ge__$float_$float_(__float__$int_(x), y); 129 | } 130 | value __mod__$int_$float_(value x, value y) { 131 | assert(x.type == value::INT && y.type == value::FLOAT); 132 | return __mod__$float_$float_(__float__$int_(x), y); 133 | } 134 | value __eq__$int_$float_(value x, value y) { 135 | assert(x.type == value::INT && y.type == value::FLOAT); 136 | return __eq__$float_$float_(__float__$int_(x), y); 137 | } 138 | value __ne__$int_$float_(value x, value y) { 139 | assert(x.type == value::INT && y.type == value::FLOAT); 140 | return __ne__$float_$float_(__float__$int_(x), y); 141 | } 142 | value __add__$int_$bool_(value x, value y) { 143 | assert(x.type == value::INT && y.type == value::BOOL); 144 | return __add__$int_$int_(x, __int__$bool_(y)); 145 | } 146 | value __sub__$int_$bool_(value x, value y) { 147 | assert(x.type == value::INT && y.type == value::BOOL); 148 | return __sub__$int_$int_(x, __int__$bool_(y)); 149 | } 150 | value __mul__$int_$bool_(value x, value y) { 151 | assert(x.type == value::INT && y.type == value::BOOL); 152 | return __mul__$int_$int_(x, __int__$bool_(y)); 153 | } 154 | value __truediv__$int_$bool_(value x, value y) { 155 | assert(x.type == value::INT && y.type == value::BOOL); 156 | return __truediv__$int_$int_(x, __int__$bool_(y)); 157 | } 158 | value __lt__$int_$bool_(value x, value y) { 159 | assert(x.type == value::INT && y.type == value::BOOL); 160 | return __lt__$int_$int_(x, __int__$bool_(y)); 161 | } 162 | value __le__$int_$bool_(value x, value y) { 163 | assert(x.type == value::INT && y.type == value::BOOL); 164 | return __le__$int_$int_(x, __int__$bool_(y)); 165 | } 166 | value __gt__$int_$bool_(value x, value y) { 167 | assert(x.type == value::INT && y.type == value::BOOL); 168 | return __gt__$int_$int_(x, __int__$bool_(y)); 169 | } 170 | value __ge__$int_$bool_(value x, value y) { 171 | assert(x.type == value::INT && y.type == value::BOOL); 172 | return __ge__$int_$int_(x, __int__$bool_(y)); 173 | } 174 | value __xor__$int_$bool_(value x, value y) { 175 | assert(x.type == value::INT && y.type == value::BOOL); 176 | return __xor__$int_$int_(x, __int__$bool_(y)); 177 | } 178 | value __and__$int_$bool_(value x, value y) { 179 | assert(x.type == value::INT && y.type == value::BOOL); 180 | return __and__$int_$int_(x, __int__$bool_(y)); 181 | } 182 | value __or__$int_$bool_(value x, value y) { 183 | assert(x.type == value::INT && y.type == value::BOOL); 184 | return __or__$int_$int_(x, __int__$bool_(y)); 185 | } 186 | value __mod__$int_$bool_(value x, value y) { 187 | assert(x.type == value::INT && y.type == value::BOOL); 188 | return __mod__$int_$int_(x, __int__$bool_(y)); 189 | } 190 | value __eq__$int_$bool_(value x, value y) { 191 | assert(x.type == value::INT && y.type == value::BOOL); 192 | return __eq__$int_$int_(x, __int__$bool_(y)); 193 | } 194 | value __ne__$int_$bool_(value x, value y) { 195 | assert(x.type == value::INT && y.type == value::BOOL); 196 | return __ne__$int_$int_(x, __int__$bool_(y)); 197 | } 198 | value __lshift__$int_$bool_(value x, value y) { 199 | assert(x.type == value::INT && y.type == value::BOOL); 200 | return __lshift__$int_$int_(x, __int__$bool_(y)); 201 | } 202 | value __rshift__$int_$bool_(value x, value y) { 203 | assert(x.type == value::INT && y.type == value::BOOL); 204 | return __rshift__$int_$int_(x, __int__$bool_(y)); 205 | } 206 | value __add__$bool_$float_(value x, value y) { 207 | assert(x.type == value::BOOL && y.type == value::FLOAT); 208 | return __add__$float_$float_(__float__$bool_(x), y); 209 | } 210 | value __sub__$bool_$float_(value x, value y) { 211 | assert(x.type == value::BOOL && y.type == value::FLOAT); 212 | return __sub__$float_$float_(__float__$bool_(x), y); 213 | } 214 | value __mul__$bool_$float_(value x, value y) { 215 | assert(x.type == value::BOOL && y.type == value::FLOAT); 216 | return __mul__$float_$float_(__float__$bool_(x), y); 217 | } 218 | value __truediv__$bool_$float_(value x, value y) { 219 | assert(x.type == value::BOOL && y.type == value::FLOAT); 220 | return __truediv__$float_$float_(__float__$bool_(x), y); 221 | } 222 | value __lt__$bool_$float_(value x, value y) { 223 | assert(x.type == value::BOOL && y.type == value::FLOAT); 224 | return __lt__$float_$float_(__float__$bool_(x), y); 225 | } 226 | value __le__$bool_$float_(value x, value y) { 227 | assert(x.type == value::BOOL && y.type == value::FLOAT); 228 | return __le__$float_$float_(__float__$bool_(x), y); 229 | } 230 | value __gt__$bool_$float_(value x, value y) { 231 | assert(x.type == value::BOOL && y.type == value::FLOAT); 232 | return __gt__$float_$float_(__float__$bool_(x), y); 233 | } 234 | value __ge__$bool_$float_(value x, value y) { 235 | assert(x.type == value::BOOL && y.type == value::FLOAT); 236 | return __ge__$float_$float_(__float__$bool_(x), y); 237 | } 238 | value __mod__$bool_$float_(value x, value y) { 239 | assert(x.type == value::BOOL && y.type == value::FLOAT); 240 | return __mod__$float_$float_(__float__$bool_(x), y); 241 | } 242 | value __eq__$bool_$float_(value x, value y) { 243 | assert(x.type == value::BOOL && y.type == value::FLOAT); 244 | return __eq__$float_$float_(__float__$bool_(x), y); 245 | } 246 | value __ne__$bool_$float_(value x, value y) { 247 | assert(x.type == value::BOOL && y.type == value::FLOAT); 248 | return __ne__$float_$float_(__float__$bool_(x), y); 249 | } 250 | value __add__$bool_$int_(value x, value y) { 251 | assert(x.type == value::BOOL && y.type == value::INT); 252 | return __add__$int_$int_(__int__$bool_(x), y); 253 | } 254 | value __sub__$bool_$int_(value x, value y) { 255 | assert(x.type == value::BOOL && y.type == value::INT); 256 | return __sub__$int_$int_(__int__$bool_(x), y); 257 | } 258 | value __mul__$bool_$int_(value x, value y) { 259 | assert(x.type == value::BOOL && y.type == value::INT); 260 | return __mul__$int_$int_(__int__$bool_(x), y); 261 | } 262 | value __truediv__$bool_$int_(value x, value y) { 263 | assert(x.type == value::BOOL && y.type == value::INT); 264 | return __truediv__$int_$int_(__int__$bool_(x), y); 265 | } 266 | value __lt__$bool_$int_(value x, value y) { 267 | assert(x.type == value::BOOL && y.type == value::INT); 268 | return __lt__$int_$int_(__int__$bool_(x), y); 269 | } 270 | value __le__$bool_$int_(value x, value y) { 271 | assert(x.type == value::BOOL && y.type == value::INT); 272 | return __le__$int_$int_(__int__$bool_(x), y); 273 | } 274 | value __gt__$bool_$int_(value x, value y) { 275 | assert(x.type == value::BOOL && y.type == value::INT); 276 | return __gt__$int_$int_(__int__$bool_(x), y); 277 | } 278 | value __ge__$bool_$int_(value x, value y) { 279 | assert(x.type == value::BOOL && y.type == value::INT); 280 | return __ge__$int_$int_(__int__$bool_(x), y); 281 | } 282 | value __xor__$bool_$int_(value x, value y) { 283 | assert(x.type == value::BOOL && y.type == value::INT); 284 | return __xor__$int_$int_(__int__$bool_(x), y); 285 | } 286 | value __and__$bool_$int_(value x, value y) { 287 | assert(x.type == value::BOOL && y.type == value::INT); 288 | return __and__$int_$int_(__int__$bool_(x), y); 289 | } 290 | value __or__$bool_$int_(value x, value y) { 291 | assert(x.type == value::BOOL && y.type == value::INT); 292 | return __or__$int_$int_(__int__$bool_(x), y); 293 | } 294 | value __mod__$bool_$int_(value x, value y) { 295 | assert(x.type == value::BOOL && y.type == value::INT); 296 | return __mod__$int_$int_(__int__$bool_(x), y); 297 | } 298 | value __eq__$bool_$int_(value x, value y) { 299 | assert(x.type == value::BOOL && y.type == value::INT); 300 | return __eq__$int_$int_(__int__$bool_(x), y); 301 | } 302 | value __ne__$bool_$int_(value x, value y) { 303 | assert(x.type == value::BOOL && y.type == value::INT); 304 | return __ne__$int_$int_(__int__$bool_(x), y); 305 | } 306 | value __lshift__$bool_$int_(value x, value y) { 307 | assert(x.type == value::BOOL && y.type == value::INT); 308 | return __lshift__$int_$int_(__int__$bool_(x), y); 309 | } 310 | value __rshift__$bool_$int_(value x, value y) { 311 | assert(x.type == value::BOOL && y.type == value::INT); 312 | return __rshift__$int_$int_(__int__$bool_(x), y); 313 | } 314 | -------------------------------------------------------------------------------- /func.cpp: -------------------------------------------------------------------------------- 1 | #include "func.h" 2 | #include "list.h" 3 | #include "int.h" 4 | #include "float.h" 5 | #include "str.h" 6 | #include "dict.h" 7 | #include "bool.h" 8 | #include "set.h" 9 | #include "none.h" 10 | #include "range.h" 11 | #include "range_iterator.h" 12 | #include "list_iterator.h" 13 | #include "set_iterator.h" 14 | #include "dict_iterator.h" 15 | #include "generated.h" 16 | 17 | value print(value v) { 18 | switch (v.type) { 19 | case value::NONE: 20 | printf(" None"); 21 | break; 22 | case value::BOOL: 23 | if (v.boolval) 24 | printf(" True"); 25 | else 26 | printf(" False"); 27 | break; 28 | case value::INT: 29 | printf(" %ld", v.intval); 30 | break; 31 | case value::FLOAT: 32 | printf(" %f", v.floatval); 33 | break; 34 | case value::LIST: 35 | printf("["); 36 | for (value v : *v.listval) { 37 | print(v); 38 | printf(","); 39 | } 40 | printf("]"); 41 | break; 42 | case value::STR: 43 | printf(" '%s'", v.strval->c_str()); 44 | break; 45 | case value::SET: 46 | printf("{"); 47 | for (auto it = v.setval->begin(); it != v.setval->end(); ++it) { 48 | print(*it); 49 | printf(","); 50 | } 51 | printf("}"); 52 | break; 53 | case value::DICT: 54 | printf("{"); 55 | for (auto it = v.dictval->begin(); it != v.dictval->end(); ++it) { 56 | print(it->first); 57 | printf(":"); 58 | print(it->second); 59 | printf(","); 60 | } 61 | printf("}"); 62 | break; 63 | default: 64 | throw std::runtime_error("invalid value to print"); 65 | } 66 | return make$none(); 67 | } 68 | 69 | value __add__(value x, value y) { 70 | switch (x.type) { 71 | case value::BOOL: 72 | switch (y.type) { 73 | case value::BOOL: 74 | return __add__$bool_$bool_(x, y); 75 | case value::INT: 76 | return __add__$bool_$int_(x, y); 77 | case value::FLOAT: 78 | return __add__$bool_$float_(x, y); 79 | } 80 | break; 81 | case value::INT: 82 | switch (y.type) { 83 | case value::BOOL: 84 | return __add__$int_$bool_(x, y); 85 | case value::INT: 86 | return __add__$int_$int_(x, y); 87 | case value::FLOAT: 88 | return __add__$int_$float_(x, y); 89 | } 90 | break; 91 | case value::FLOAT: 92 | switch (y.type) { 93 | case value::BOOL: 94 | return __add__$float_$bool_(x, y); 95 | case value::INT: 96 | return __add__$float_$int_(x, y); 97 | case value::FLOAT: 98 | return __add__$float_$float_(x, y); 99 | } 100 | break; 101 | case value::LIST: 102 | if (y.type == value::LIST) { 103 | return __add__$list$list(x, y); 104 | } 105 | break; 106 | case value::STR: 107 | if (y.type == value::STR) { 108 | return __add__$str$str(x, y); 109 | } 110 | } 111 | throw std::runtime_error("invalid argument type for +"); 112 | } 113 | 114 | value __sub__(value x, value y) { 115 | switch (x.type) { 116 | case value::BOOL: 117 | switch (y.type) { 118 | case value::BOOL: 119 | return __sub__$bool_$bool_(x, y); 120 | case value::INT: 121 | return __sub__$bool_$int_(x, y); 122 | case value::FLOAT: 123 | return __sub__$bool_$float_(x, y); 124 | } 125 | break; 126 | case value::INT: 127 | switch (y.type) { 128 | case value::BOOL: 129 | return __sub__$int_$bool_(x, y); 130 | case value::INT: 131 | return __sub__$int_$int_(x, y); 132 | case value::FLOAT: 133 | return __sub__$int_$float_(x, y); 134 | } 135 | break; 136 | case value::FLOAT: 137 | switch (y.type) { 138 | case value::BOOL: 139 | return __sub__$float_$bool_(x, y); 140 | case value::INT: 141 | return __sub__$float_$int_(x, y); 142 | case value::FLOAT: 143 | return __sub__$float_$float_(x, y); 144 | } 145 | } 146 | throw std::runtime_error("invalid argument type for -"); 147 | } 148 | 149 | value __mul__(value x, value y) { 150 | switch (x.type) { 151 | case value::BOOL: 152 | switch (y.type) { 153 | case value::BOOL: 154 | return __mul__$bool_$bool_(x, y); 155 | case value::INT: 156 | return __mul__$bool_$int_(x, y); 157 | case value::FLOAT: 158 | return __mul__$bool_$float_(x, y); 159 | case value::STR: 160 | return __mul__$bool_$str(x, y); 161 | case value::LIST: 162 | return __mul__$bool_$list(x, y); 163 | } 164 | break; 165 | case value::INT: 166 | switch (y.type) { 167 | case value::BOOL: 168 | return __mul__$int_$bool_(x, y); 169 | case value::INT: 170 | return __mul__$int_$int_(x, y); 171 | case value::FLOAT: 172 | return __mul__$int_$float_(x, y); 173 | case value::STR: 174 | return __mul__$int_$str(x, y); 175 | case value::LIST: 176 | return __mul__$int_$list(x, y); 177 | } 178 | break; 179 | case value::FLOAT: 180 | switch (y.type) { 181 | case value::BOOL: 182 | return __mul__$float_$bool_(x, y); 183 | case value::INT: 184 | return __mul__$float_$int_(x, y); 185 | case value::FLOAT: 186 | return __mul__$float_$float_(x, y); 187 | } 188 | break; 189 | case value::STR: 190 | switch (y.type) { 191 | case value::BOOL: 192 | return __mul__$str$bool_(x, y); 193 | case value::INT: 194 | return __mul__$str$int_(x, y); 195 | } 196 | break; 197 | case value::LIST: 198 | switch (y.type) { 199 | case value::BOOL: 200 | return __mul__$list$bool_(x, y); 201 | case value::INT: 202 | return __mul__$list$int_(x, y); 203 | } 204 | } 205 | throw std::runtime_error("invalid argument type for *"); 206 | } 207 | 208 | value __truediv__(value x, value y) { 209 | switch (x.type) { 210 | case value::BOOL: 211 | switch (y.type) { 212 | case value::BOOL: 213 | return __truediv__$bool_$bool_(x, y); 214 | case value::INT: 215 | return __truediv__$bool_$int_(x, y); 216 | case value::FLOAT: 217 | return __truediv__$bool_$float_(x, y); 218 | } 219 | break; 220 | case value::INT: 221 | switch (y.type) { 222 | case value::BOOL: 223 | return __truediv__$int_$bool_(x, y); 224 | case value::INT: 225 | return __truediv__$int_$int_(x, y); 226 | case value::FLOAT: 227 | return __truediv__$int_$float_(x, y); 228 | } 229 | break; 230 | case value::FLOAT: 231 | switch (y.type) { 232 | case value::BOOL: 233 | return __truediv__$float_$bool_(x, y); 234 | case value::INT: 235 | return __truediv__$float_$int_(x, y); 236 | case value::FLOAT: 237 | return __truediv__$float_$float_(x, y); 238 | } 239 | } 240 | throw std::runtime_error("invalid argument type for /"); 241 | } 242 | 243 | value __mod__(value x, value y) { 244 | switch (x.type) { 245 | case value::BOOL: 246 | switch (y.type) { 247 | case value::BOOL: 248 | return __mod__$bool_$bool_(x, y); 249 | case value::INT: 250 | return __mod__$bool_$int_(x, y); 251 | case value::FLOAT: 252 | return __mod__$bool_$float_(x, y); 253 | } 254 | break; 255 | case value::INT: 256 | switch (y.type) { 257 | case value::BOOL: 258 | return __mod__$int_$bool_(x, y); 259 | case value::INT: 260 | return __mod__$int_$int_(x, y); 261 | case value::FLOAT: 262 | return __mod__$int_$float_(x, y); 263 | } 264 | break; 265 | case value::FLOAT: 266 | switch (y.type) { 267 | case value::BOOL: 268 | return __mod__$float_$bool_(x, y); 269 | case value::INT: 270 | return __mod__$float_$int_(x, y); 271 | case value::FLOAT: 272 | return __mod__$float_$float_(x, y); 273 | } 274 | } 275 | throw std::runtime_error("invalid argument type for mod"); 276 | } 277 | 278 | value __len__(value self) { 279 | switch (self.type) { 280 | case value::LIST: 281 | return __len__$list(self); 282 | case value::STR: 283 | return __len__$str(self); 284 | case value::SET: 285 | return __len__$set(self); 286 | case value::DICT: 287 | return __len__$dict(self); 288 | default: 289 | throw std::runtime_error("invalid argument type for len"); 290 | } 291 | } 292 | 293 | value __getitem__(value self, value k) { 294 | switch (self.type) { 295 | case value::STR: 296 | switch (k.type) { 297 | case value::INT: 298 | return __getitem__$str$int_(self, k); 299 | case value::SLICE: 300 | return __getitem__$str$slice(self, k); 301 | } 302 | break; 303 | case value::LIST: 304 | switch (k.type) { 305 | case value::INT: 306 | return __getitem__$list$int_(self, k); 307 | case value::SLICE: 308 | return __getitem__$list$slice(self, k); 309 | } 310 | break; 311 | case value::DICT: 312 | switch (k.type) { 313 | case value::NONE: 314 | case value::BOOL: 315 | case value::INT: 316 | case value::FLOAT: 317 | case value::STR: 318 | return __getitem__$dict$(self, k); 319 | } 320 | } 321 | throw std::runtime_error("invalid argument type for []"); 322 | } 323 | 324 | value __contains__(value self, value v) { 325 | switch (self.type) { 326 | case value::STR: 327 | if (v.type == value::STR) 328 | return __contains__$str$str(self, v); 329 | break; 330 | case value::LIST: 331 | return __contains__$list$(self, v); 332 | case value::SET: 333 | return __contains__$set$(self, v); 334 | case value::DICT: 335 | switch (v.type) { 336 | case value::NONE: 337 | case value::BOOL: 338 | case value::INT: 339 | case value::FLOAT: 340 | case value::STR: 341 | return __contains__$dict$(self, v); 342 | } 343 | } 344 | throw std::runtime_error("invalid argument type for in"); 345 | } 346 | 347 | value __setitem__(value self, value k, value v) { 348 | switch (self.type) { 349 | case value::LIST: 350 | switch (k.type) { 351 | case value::INT: 352 | return __setitem__$list$int_$(self, k, v); 353 | case value::SLICE: 354 | return __setitem__$list$slice$(self, k, v); 355 | } 356 | break; 357 | case value::DICT: 358 | switch (k.type) { 359 | case value::NONE: 360 | case value::BOOL: 361 | case value::INT: 362 | case value::FLOAT: 363 | case value::STR: 364 | return __setitem__$dict$$(self, k, v); 365 | } 366 | } 367 | throw std::runtime_error("invalid argument type for []="); 368 | } 369 | 370 | value __delitem__(value self, value k) { 371 | switch (self.type) { 372 | case value::LIST: 373 | switch (k.type) { 374 | case value::INT: 375 | return __delitem__$list$int_(self, k); 376 | case value::SLICE: 377 | return __delitem__$list$slice(self, k); 378 | } 379 | break; 380 | case value::DICT: 381 | switch (k.type) { 382 | case value::NONE: 383 | case value::BOOL: 384 | case value::INT: 385 | case value::FLOAT: 386 | case value::STR: 387 | return __delitem__$dict$(self, k); 388 | } 389 | } 390 | throw std::runtime_error("invalid argument type for del"); 391 | } 392 | 393 | value __bool__(value self) { 394 | switch (self.type) { 395 | case value::NONE: 396 | return __bool__$none(self); 397 | case value::BOOL: 398 | return __bool__$bool_(self); 399 | case value::INT: 400 | return __bool__$int_(self); 401 | case value::FLOAT: 402 | return __bool__$float_(self); 403 | case value::STR: 404 | return __bool__$str(self); 405 | case value::LIST: 406 | return __bool__$list(self); 407 | case value::SET: 408 | return __bool__$set(self); 409 | case value::DICT: 410 | return __bool__$dict(self); 411 | } 412 | throw std::runtime_error("invalid argument type for bool()"); 413 | } 414 | 415 | value __eq__(value x, value y) { 416 | switch (x.type) { 417 | case value::NONE: 418 | if (y.type == value::NONE) 419 | return __eq__$none$none(x, y); 420 | break; 421 | case value::BOOL: 422 | switch (y.type) { 423 | case value::BOOL: 424 | return __eq__$bool_$bool_(x, y); 425 | case value::INT: 426 | return __eq__$bool_$int_(x, y); 427 | case value::FLOAT: 428 | return __eq__$bool_$float_(x, y); 429 | } 430 | break; 431 | case value::INT: 432 | switch (y.type) { 433 | case value::BOOL: 434 | return __eq__$int_$bool_(x, y); 435 | case value::INT: 436 | return __eq__$int_$int_(x, y); 437 | case value::FLOAT: 438 | return __eq__$int_$float_(x, y); 439 | } 440 | break; 441 | case value::FLOAT: 442 | switch (y.type) { 443 | case value::BOOL: 444 | return __eq__$float_$bool_(x, y); 445 | case value::INT: 446 | return __eq__$float_$int_(x, y); 447 | case value::FLOAT: 448 | return __eq__$float_$float_(x, y); 449 | } 450 | break; 451 | case value::STR: 452 | if (y.type == value::STR) 453 | return __eq__$str$str(x, y); 454 | break; 455 | case value::LIST: 456 | if (y.type == value::LIST) 457 | return __eq__$list$list(x, y); 458 | break; 459 | case value::SET: 460 | if (y.type == value::SET) 461 | return __eq__$set$set(x, y); 462 | break; 463 | case value::DICT: 464 | if (y.type == value::DICT) 465 | return __eq__$dict$dict(x, y); 466 | break; 467 | } 468 | return make$bool_(false); 469 | } 470 | 471 | value __ne__(value x, value y) { 472 | switch (x.type) { 473 | case value::NONE: 474 | if (y.type == value::NONE) 475 | return __ne__$none$none(x, y); 476 | break; 477 | case value::BOOL: 478 | switch (y.type) { 479 | case value::BOOL: 480 | return __ne__$bool_$bool_(x, y); 481 | case value::INT: 482 | return __ne__$bool_$int_(x, y); 483 | case value::FLOAT: 484 | return __ne__$bool_$float_(x, y); 485 | } 486 | break; 487 | case value::INT: 488 | switch (y.type) { 489 | case value::BOOL: 490 | return __ne__$int_$bool_(x, y); 491 | case value::INT: 492 | return __ne__$int_$int_(x, y); 493 | case value::FLOAT: 494 | return __ne__$int_$float_(x, y); 495 | } 496 | break; 497 | case value::FLOAT: 498 | switch (y.type) { 499 | case value::BOOL: 500 | return __ne__$float_$bool_(x, y); 501 | case value::INT: 502 | return __ne__$float_$int_(x, y); 503 | case value::FLOAT: 504 | return __ne__$float_$float_(x, y); 505 | } 506 | break; 507 | case value::STR: 508 | if (y.type == value::STR) 509 | return __ne__$str$str(x, y); 510 | break; 511 | case value::LIST: 512 | if (y.type == value::LIST) 513 | return __ne__$list$list(x, y); 514 | break; 515 | case value::SET: 516 | if (y.type == value::SET) 517 | return __ne__$set$set(x, y); 518 | break; 519 | case value::DICT: 520 | if (y.type == value::DICT) 521 | return __ne__$dict$dict(x, y); 522 | break; 523 | } 524 | return make$bool_(true); 525 | } 526 | 527 | value __lt__(value x, value y) { 528 | switch (x.type) { 529 | case value::BOOL: 530 | switch (y.type) { 531 | case value::BOOL: 532 | return __lt__$bool_$bool_(x, y); 533 | case value::INT: 534 | return __lt__$bool_$int_(x, y); 535 | case value::FLOAT: 536 | return __lt__$bool_$float_(x, y); 537 | } 538 | break; 539 | case value::INT: 540 | switch (y.type) { 541 | case value::BOOL: 542 | return __lt__$int_$bool_(x, y); 543 | case value::INT: 544 | return __lt__$int_$int_(x, y); 545 | case value::FLOAT: 546 | return __lt__$int_$float_(x, y); 547 | } 548 | break; 549 | case value::FLOAT: 550 | switch (y.type) { 551 | case value::BOOL: 552 | return __lt__$float_$bool_(x, y); 553 | case value::INT: 554 | return __lt__$float_$int_(x, y); 555 | case value::FLOAT: 556 | return __lt__$float_$float_(x, y); 557 | } 558 | break; 559 | case value::STR: 560 | if (y.type == value::STR) 561 | return __lt__$str$str(x, y); 562 | break; 563 | case value::LIST: 564 | if (y.type == value::LIST) 565 | return __lt__$list$list(x, y); 566 | break; 567 | case value::SET: 568 | if (y.type == value::SET) 569 | return __lt__$set$set(x, y); 570 | break; 571 | } 572 | throw std::runtime_error("invalid type for <"); 573 | } 574 | 575 | value __le__(value x, value y) { 576 | switch (x.type) { 577 | case value::BOOL: 578 | switch (y.type) { 579 | case value::BOOL: 580 | return __le__$bool_$bool_(x, y); 581 | case value::INT: 582 | return __le__$bool_$int_(x, y); 583 | case value::FLOAT: 584 | return __le__$bool_$float_(x, y); 585 | } 586 | break; 587 | case value::INT: 588 | switch (y.type) { 589 | case value::BOOL: 590 | return __le__$int_$bool_(x, y); 591 | case value::INT: 592 | return __le__$int_$int_(x, y); 593 | case value::FLOAT: 594 | return __le__$int_$float_(x, y); 595 | } 596 | break; 597 | case value::FLOAT: 598 | switch (y.type) { 599 | case value::BOOL: 600 | return __le__$float_$bool_(x, y); 601 | case value::INT: 602 | return __le__$float_$int_(x, y); 603 | case value::FLOAT: 604 | return __le__$float_$float_(x, y); 605 | } 606 | break; 607 | case value::STR: 608 | if (y.type == value::STR) 609 | return __le__$str$str(x, y); 610 | break; 611 | case value::LIST: 612 | if (y.type == value::LIST) 613 | return __le__$list$list(x, y); 614 | break; 615 | case value::SET: 616 | if (y.type == value::SET) 617 | return __le__$set$set(x, y); 618 | break; 619 | } 620 | throw std::runtime_error("invalid type for <="); 621 | } 622 | 623 | value __ge__(value x, value y) { 624 | switch (x.type) { 625 | case value::BOOL: 626 | switch (y.type) { 627 | case value::BOOL: 628 | return __ge__$bool_$bool_(x, y); 629 | case value::INT: 630 | return __ge__$bool_$int_(x, y); 631 | case value::FLOAT: 632 | return __ge__$bool_$float_(x, y); 633 | } 634 | break; 635 | case value::INT: 636 | switch (y.type) { 637 | case value::BOOL: 638 | return __ge__$int_$bool_(x, y); 639 | case value::INT: 640 | return __ge__$int_$int_(x, y); 641 | case value::FLOAT: 642 | return __ge__$int_$float_(x, y); 643 | } 644 | break; 645 | case value::FLOAT: 646 | switch (y.type) { 647 | case value::BOOL: 648 | return __ge__$float_$bool_(x, y); 649 | case value::INT: 650 | return __ge__$float_$int_(x, y); 651 | case value::FLOAT: 652 | return __ge__$float_$float_(x, y); 653 | } 654 | break; 655 | case value::STR: 656 | if (y.type == value::STR) 657 | return __ge__$str$str(x, y); 658 | break; 659 | case value::LIST: 660 | if (y.type == value::LIST) 661 | return __ge__$list$list(x, y); 662 | break; 663 | case value::SET: 664 | if (y.type == value::SET) 665 | return __ge__$set$set(x, y); 666 | break; 667 | } 668 | throw std::runtime_error("invalid type for >="); 669 | } 670 | 671 | value __gt__(value x, value y) { 672 | switch (x.type) { 673 | case value::BOOL: 674 | switch (y.type) { 675 | case value::BOOL: 676 | return __gt__$bool_$bool_(x, y); 677 | case value::INT: 678 | return __gt__$bool_$int_(x, y); 679 | case value::FLOAT: 680 | return __gt__$bool_$float_(x, y); 681 | } 682 | break; 683 | case value::INT: 684 | switch (y.type) { 685 | case value::BOOL: 686 | return __gt__$int_$bool_(x, y); 687 | case value::INT: 688 | return __gt__$int_$int_(x, y); 689 | case value::FLOAT: 690 | return __gt__$int_$float_(x, y); 691 | } 692 | break; 693 | case value::FLOAT: 694 | switch (y.type) { 695 | case value::BOOL: 696 | return __gt__$float_$bool_(x, y); 697 | case value::INT: 698 | return __gt__$float_$int_(x, y); 699 | case value::FLOAT: 700 | return __gt__$float_$float_(x, y); 701 | } 702 | break; 703 | case value::STR: 704 | if (y.type == value::STR) 705 | return __gt__$str$str(x, y); 706 | break; 707 | case value::LIST: 708 | if (y.type == value::LIST) 709 | return __gt__$list$list(x, y); 710 | break; 711 | case value::SET: 712 | if (y.type == value::SET) 713 | return __gt__$set$set(x, y); 714 | break; 715 | } 716 | throw std::runtime_error("invalid type for >"); 717 | } 718 | 719 | value __and__(value x, value y) { 720 | switch (x.type) { 721 | case value::BOOL: 722 | switch (y.type) { 723 | case value::BOOL: 724 | return __and__$bool_$bool_(x, y); 725 | case value::INT: 726 | return __and__$bool_$int_(x, y); 727 | } 728 | break; 729 | case value::INT: 730 | switch (y.type) { 731 | case value::BOOL: 732 | return __and__$int_$bool_(x, y); 733 | case value::INT: 734 | return __and__$int_$int_(x, y); 735 | } 736 | } 737 | throw std::runtime_error("invalid type for &"); 738 | } 739 | 740 | value __or__(value x, value y) { 741 | switch (x.type) { 742 | case value::BOOL: 743 | switch (y.type) { 744 | case value::BOOL: 745 | return __or__$bool_$bool_(x, y); 746 | case value::INT: 747 | return __or__$bool_$int_(x, y); 748 | } 749 | break; 750 | case value::INT: 751 | switch (y.type) { 752 | case value::BOOL: 753 | return __or__$int_$bool_(x, y); 754 | case value::INT: 755 | return __or__$int_$int_(x, y); 756 | } 757 | } 758 | throw std::runtime_error("invalid type for |"); 759 | } 760 | 761 | value __xor__(value x, value y) { 762 | switch (x.type) { 763 | case value::BOOL: 764 | switch (y.type) { 765 | case value::BOOL: 766 | return __xor__$bool_$bool_(x, y); 767 | case value::INT: 768 | return __xor__$bool_$int_(x, y); 769 | } 770 | break; 771 | case value::INT: 772 | switch (y.type) { 773 | case value::BOOL: 774 | return __xor__$int_$bool_(x, y); 775 | case value::INT: 776 | return __xor__$int_$int_(x, y); 777 | } 778 | } 779 | throw std::runtime_error("invalid type for ^"); 780 | } 781 | 782 | value __invert__(value self) { 783 | switch (self.type) { 784 | case value::BOOL: 785 | return __invert__$bool_(self); 786 | case value::INT: 787 | return __invert__$int_(self); 788 | default: 789 | throw std::runtime_error("invalid type for ~"); 790 | } 791 | } 792 | 793 | value __lshift__(value x, value y) { 794 | switch (x.type) { 795 | case value::BOOL: 796 | switch (y.type) { 797 | case value::BOOL: 798 | return __lshift__$bool_$bool_(x, y); 799 | case value::INT: 800 | return __lshift__$bool_$int_(x, y); 801 | } 802 | break; 803 | case value::INT: 804 | switch (y.type) { 805 | case value::BOOL: 806 | return __lshift__$int_$bool_(x, y); 807 | case value::INT: 808 | return __lshift__$int_$int_(x, y); 809 | } 810 | } 811 | throw std::runtime_error("invalid type for <<"); 812 | } 813 | 814 | value __rshift__(value x, value y) { 815 | switch (x.type) { 816 | case value::BOOL: 817 | switch (y.type) { 818 | case value::BOOL: 819 | return __rshift__$bool_$bool_(x, y); 820 | case value::INT: 821 | return __rshift__$bool_$int_(x, y); 822 | } 823 | break; 824 | case value::INT: 825 | switch (y.type) { 826 | case value::BOOL: 827 | return __rshift__$int_$bool_(x, y); 828 | case value::INT: 829 | return __rshift__$int_$int_(x, y); 830 | } 831 | } 832 | throw std::runtime_error("invalid type for >>"); 833 | } 834 | 835 | value __iter__(value self) { 836 | switch (self.type) { 837 | case value::RANGE: 838 | return __iter__$range(self); 839 | case value::LIST: 840 | return __iter__$list(self); 841 | case value::SET: 842 | return __iter__$set(self); 843 | case value::DICT: 844 | return __iter__$dict(self); 845 | } 846 | throw std::runtime_error("invalid type for __iter__"); 847 | } 848 | 849 | value __next__(value self) { 850 | switch (self.type) { 851 | case value::RANGE_ITERATOR: 852 | return __next__$range_iterator(self); 853 | case value::LIST_ITERATOR: 854 | return __next__$list_iterator(self); 855 | case value::SET_ITERATOR: 856 | return __next__$set_iterator(self); 857 | case value::DICT_ITERATOR: 858 | return __next__$dict_iterator(self); 859 | } 860 | throw std::runtime_error("invalid type for __next__"); 861 | } 862 | 863 | value append(value self, value v) { 864 | if (self.type == value::LIST) { 865 | return append$list$(self, v); 866 | } 867 | throw std::runtime_error("invalid type for append"); 868 | } 869 | 870 | value add(value self, value v) { 871 | if (self.type == value::SET) { 872 | return add$set$(self, v); 873 | } 874 | throw std::runtime_error("invalid type for add"); 875 | } 876 | 877 | value remove(value self, value v) { 878 | if (self.type == value::SET) { 879 | return remove$set$(self, v); 880 | } 881 | throw std::runtime_error("invalid type for remove"); 882 | } 883 | 884 | value pop(value self) { 885 | if (self.type == value::LIST) { 886 | return pop$list(self); 887 | } 888 | throw std::runtime_error("invalid type for pop"); 889 | } --------------------------------------------------------------------------------