17 krk_integer_type step;
19 #define IS_range(o) (krk_isInstanceOf(o,KRK_BASE_CLASS(range)))
20 #define AS_range(o) ((struct Range*)AS_OBJECT(o))
26 krk_integer_type step;
28 #define IS_rangeiterator(o) (krk_isInstanceOf(o,KRK_BASE_CLASS(rangeiterator)))
29 #define AS_rangeiterator(o) ((struct RangeIterator*)AS_OBJECT(o))
31 FUNC_SIG(rangeiterator,__init__);
33 #define CURRENT_NAME self
34 #define CURRENT_CTYPE struct Range *
36 KRK_Method(range,__init__) {
37 METHOD_TAKES_AT_LEAST(1);
38 METHOD_TAKES_AT_MOST(3);
42 CHECK_ARG(1,
int,krk_integer_type,_max);
45 CHECK_ARG(1,
int,krk_integer_type,_min);
46 CHECK_ARG(2,
int,krk_integer_type,_max);
50 CHECK_ARG(3,
int,krk_integer_type,_step);
60 KRK_Method(range,__repr__) {
63 krk_pushStringBuilderFormat(&sb,
"range(%zd,%zd", (ssize_t)self->min, (ssize_t)self->max);
64 if (self->step != 1) krk_pushStringBuilderFormat(&sb,
",%zd", (ssize_t)self->step);
69 KRK_Method(range,__iter__) {
71 krk_integer_type min =
self->min;
72 krk_integer_type max =
self->max;
73 krk_integer_type step =
self->step;
76 FUNC_NAME(rangeiterator,__init__)(4, (
KrkValue[]){
krk_peek(0), INTEGER_VAL(min), INTEGER_VAL(max), INTEGER_VAL(step)},0);
79 return OBJECT_VAL(output);
82 KRK_Method(range,__contains__) {
84 if (!
krk_parseArgs(
".i", (
const char*[]){
"i"}, &i))
return NONE_VAL();
85 if (self->step == 1)
return BOOLEAN_VAL(i >= self->min && i < self->max);
86 if (self->step == -1)
return BOOLEAN_VAL(i <= self->min && i > self->max);
88 if (i >= self->max || i < self->min)
return BOOLEAN_VAL(0);
89 if ((i - self->min) % self->step)
return BOOLEAN_VAL(0);
90 return BOOLEAN_VAL(1);
92 if (i <= self->max || i > self->min)
return BOOLEAN_VAL(0);
93 if ((i - self->min) % -self->step)
return BOOLEAN_VAL(0);
94 return BOOLEAN_VAL(1);
99 #define CURRENT_CTYPE struct RangeIterator *
101 KRK_Method(rangeiterator,__init__) {
102 METHOD_TAKES_EXACTLY(3);
103 CHECK_ARG(1,
int,krk_integer_type,i);
104 CHECK_ARG(2,
int,krk_integer_type,max);
105 CHECK_ARG(3,
int,krk_integer_type,step);
112 KRK_Method(rangeiterator,__call__) {
114 krk_integer_type i =
self->i;
115 if (self->step > 0 ? (i >= self->max) : (i <= self->max)) {
118 self->i = i +
self->step;
119 return INTEGER_VAL(i);
124 void _createAndBind_rangeClass(
void) {
125 KrkClass * range = ADD_BASE_CLASS(
vm.baseClasses->rangeClass,
"range",
vm.baseClasses->objectClass);
127 range->
obj.
flags |= KRK_OBJ_FLAGS_NO_INHERIT;
128 KRK_DOC(BIND_METHOD(range,__init__),
129 "@brief Create an iterable that produces sequential numeric values.\n"
130 "@arguments [min,] max, [step]\n\n"
131 "With one argument, iteration will start at @c 0 and continue to @p max, exclusive. "
132 "With two arguments, iteration starts at @p min and continues to @p max, exclusive. "
133 "With three arguments, a @p step may also be included.");
134 BIND_METHOD(range,__iter__);
135 BIND_METHOD(range,__repr__);
136 BIND_METHOD(range,__contains__);
137 KRK_DOC(range,
"@brief Iterable object that produces sequential numeric values.");
140 KrkClass * rangeiterator = ADD_BASE_CLASS(
vm.baseClasses->rangeiteratorClass,
"rangeiterator",
vm.baseClasses->objectClass);
142 rangeiterator->
obj.
flags |= KRK_OBJ_FLAGS_NO_INHERIT;
143 BIND_METHOD(rangeiterator,__init__);
144 BIND_METHOD(rangeiterator,__call__);
KrkValue krk_runtimeError(KrkClass *type, const char *fmt,...)
Produce and raise an exception with a formatted message.
Functions for dealing with garbage collection and memory allocation.
size_t allocSize
Size to allocate when creating instances of this class.
void krk_finalizeClass(KrkClass *_class)
Finalize a class by collecting pointers to core methods.
KrkInstance * krk_newInstance(KrkClass *_class)
Create a new instance of the given class.
uint16_t flags
General object flags, mostly related to garbage collection.
Stack reference or primative value.
Inline flexible string array.
Utilities for creating native bindings.
#define krk_parseArgs(f, n,...)
Parse arguments to a function while accepting keyword arguments.
KrkValue krk_finishStringBuilder(struct StringBuilder *sb)
Finalize a string builder into a string object.
void krk_pushStringBuilder(struct StringBuilder *sb, char c)
Add a character to the end of a string builder.
#define KRK_DOC(thing, text)
Attach documentation to a thing of various types.
Definitions for primitive stack references.
Core API for the bytecode virtual machine.
#define vm
Convenience macro for namespacing.
KrkValue krk_pop(void)
Pop the top of the stack.
void krk_push(KrkValue value)
Push a stack value.
KrkValue krk_peek(int distance)
Peek down from the top of the stack.