18 AS_slice(outSlice)->start = (argc > 0) ? argv[0] : NONE_VAL();
19 AS_slice(outSlice)->end = (argc > 1) ? argv[1] : NONE_VAL();
20 AS_slice(outSlice)->step = (argc > 2) ? argv[2] : NONE_VAL();
25 static inline krk_integer_type _wrap(krk_integer_type count, krk_integer_type val) {
26 if (val < 0) val += count;
28 if (val > count) val = count;
32 static inline krk_integer_type _wrapn(krk_integer_type count, krk_integer_type val) {
33 if (val < 0) val += count;
34 if (val < -1) val = -1;
35 if (val > count) val = count;
39 int krk_extractSlicer(
const char * _method_name,
KrkValue slicerVal, krk_integer_type count, krk_integer_type *start, krk_integer_type *end, krk_integer_type *step) {
40 if (!(IS_slice(slicerVal))) {
41 TYPE_ERROR(slice, slicerVal);
45 struct KrkSlice * slicer = AS_slice(slicerVal);
51 if (!(IS_INTEGER(_start) || IS_NONE(_start))) {
52 TYPE_ERROR(
int or None, _start);
56 if (!(IS_INTEGER(_end) || IS_NONE(_end))) {
57 TYPE_ERROR(
int or None, _end);
61 if (!(IS_INTEGER(_step) || IS_NONE(_step))) {
62 TYPE_ERROR(
int or None, _step);
73 *step = IS_NONE(_step) ? 1 : AS_INTEGER(_step);
82 *start = _wrap(count, IS_NONE(_start) ? 0 : AS_INTEGER(_start));
83 *end = _wrap(count, IS_NONE(_end) ? count : AS_INTEGER(_end));
84 if (*end < *start) *end = *start;
86 *start = IS_NONE(_start) ? (count-1) : _wrap(count, AS_INTEGER(_start));
87 if (*start >= count) *start = count -1;
88 *end = IS_NONE(_end) ? -1 : _wrapn(count, AS_INTEGER(_end));
89 if (*end > *start) *end = *start;
95 #define CURRENT_CTYPE struct KrkSlice *
96 #define CURRENT_NAME self
98 KRK_Method(slice,__init__) {
99 METHOD_TAKES_AT_LEAST(1);
100 METHOD_TAKES_AT_MOST(3);
103 self->start = NONE_VAL();
105 self->step = NONE_VAL();
107 self->start = argv[1];
110 self->step = argv[3];
112 self->step = NONE_VAL();
119 KRK_Method(slice,__repr__) {
121 if (((
KrkObj*)
self)->
flags & KRK_OBJ_FLAGS_IN_REPR)
return OBJECT_VAL(
"slice(...)");
122 ((
KrkObj*)
self)->flags |= KRK_OBJ_FLAGS_IN_REPR;
124 pushStringBuilderStr(&sb,
"slice(",6);
133 if (IS_STRING(result)) pushStringBuilderStr(&sb, AS_STRING(result)->chars, AS_STRING(result)->length);
134 pushStringBuilderStr(&sb,
", ",2);
140 if (IS_STRING(result)) pushStringBuilderStr(&sb, AS_STRING(result)->chars, AS_STRING(result)->length);
141 pushStringBuilderStr(&sb,
", ",2);
147 if (IS_STRING(result)) pushStringBuilderStr(&sb, AS_STRING(result)->chars, AS_STRING(result)->length);
149 pushStringBuilder(&sb,
')');
150 ((
KrkObj*)
self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
151 return finishStringBuilder(&sb);
154 KRK_Method(slice,start) {
155 ATTRIBUTE_NOT_ASSIGNABLE();
159 KRK_Method(slice,end) {
160 ATTRIBUTE_NOT_ASSIGNABLE();
164 KRK_Method(slice,step) {
165 ATTRIBUTE_NOT_ASSIGNABLE();
170 void _createAndBind_sliceClass(
void) {
171 KrkClass * slice = ADD_BASE_CLASS(
vm.baseClasses->sliceClass,
"slice",
vm.baseClasses->objectClass);
174 slice->
obj.
flags |= KRK_OBJ_FLAGS_NO_INHERIT;
175 BIND_METHOD(slice,__init__);
176 BIND_METHOD(slice,__repr__);
177 BIND_PROP(slice,start);
178 BIND_PROP(slice,end);
179 BIND_PROP(slice,step);
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.
void krk_markValue(KrkValue value)
During a GC scan cycle, mark a value as used.
KrkCleanupCallback _ongcscan
C function to call when the garbage collector visits an instance of this class in the scan phase.
KrkObj * _reprer
__repr__ Called to create a reproducible string representation of an instance
size_t allocSize
Size to allocate when creating instances of this class.
KrkTable methods
General attributes table.
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.
The most basic object type.
uint16_t flags
General object flags, mostly related to garbage collection.
KrkValue krk_slice_of(int argc, const KrkValue argv[], int hasKw)
Create a slice object.
KrkNative * krk_defineNative(KrkTable *table, const char *name, NativeFn function)
Attach a native C function to an attribute table.
void krk_attachNamedValue(KrkTable *table, const char name[], KrkValue obj)
Attach a value to an attribute table.
Stack reference or primative value.
KrkClass * krk_getType(KrkValue value)
Get the class representing a value.
Inline flexible string array.
Utilities for creating native bindings.
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_callDirect(KrkObj *callable, int argCount)
Call a closure or native function with argCount arguments.