9 #include "opcode_enum.h"
20 array->
capacity = GROW_CAPACITY(old);
38 if (!IS_STRING(printable))
return;
39 fprintf(f,
"%s", AS_CSTRING(printable));
43 if (!IS_STRING(printable))
return;
44 fprintf(f,
"%s", AS_CSTRING(printable));
50 #define STRING_DEBUG_TRUNCATE 50
53 if (!IS_OBJECT(printable)) {
54 switch (KRK_VAL_TYPE(printable)) {
55 case KRK_VAL_INTEGER: fprintf(f, PRIkrk_int, AS_INTEGER(printable));
break;
56 case KRK_VAL_BOOLEAN: fprintf(f,
"%s", AS_BOOLEAN(printable) ?
"True" :
"False");
break;
57 case KRK_VAL_NONE: fprintf(f,
"None");
break;
59 switch (AS_HANDLER_TYPE(printable)) {
60 case OP_PUSH_TRY: fprintf(f,
"{try->%d}", (
int)AS_HANDLER_TARGET(printable));
break;
61 case OP_PUSH_WITH: fprintf(f,
"{with->%d}", (
int)AS_HANDLER_TARGET(printable));
break;
62 case OP_RAISE: fprintf(f,
"{raise<-%d}", (
int)AS_HANDLER_TARGET(printable));
break;
63 case OP_FILTER_EXCEPT: fprintf(f,
"{except<-%d}", (
int)AS_HANDLER_TARGET(printable));
break;
64 case OP_BEGIN_FINALLY: fprintf(f,
"{finally<-%d}", (
int)AS_HANDLER_TARGET(printable));
break;
65 case OP_RETURN: fprintf(f,
"{return<-%d}", (
int)AS_HANDLER_TARGET(printable));
break;
66 case OP_END_FINALLY: fprintf(f,
"{end<-%d}", (
int)AS_HANDLER_TARGET(printable));
break;
67 case OP_EXIT_LOOP: fprintf(f,
"{exit<-%d}", (
int)AS_HANDLER_TARGET(printable));
break;
68 case OP_RAISE_FROM: fprintf(f,
"{reraise<-%d}", (
int)AS_HANDLER_TARGET(printable));
break;
71 case KRK_VAL_KWARGS: {
72 if (AS_INTEGER(printable) == KWARGS_SINGLE) {
73 fprintf(f,
"{unpack single}");
74 }
else if (AS_INTEGER(printable) == KWARGS_LIST) {
75 fprintf(f,
"{unpack list}");
76 }
else if (AS_INTEGER(printable) == KWARGS_DICT) {
77 fprintf(f,
"{unpack dict}");
78 }
else if (AS_INTEGER(printable) == KWARGS_NIL) {
79 fprintf(f,
"{unpack nil}");
80 }
else if (AS_INTEGER(printable) == KWARGS_UNSET) {
81 fprintf(f,
"{unset default}");
83 fprintf(f,
"{sentinel=" PRIkrk_int
"}",AS_INTEGER(printable));
89 if (IS_FLOATING(printable)) fprintf(f,
"%.16g", AS_FLOATING(printable));
93 }
else if (IS_STRING(printable)) {
100 for (
size_t c = 0; c < AS_STRING(printable)->length && c < STRING_DEBUG_TRUNCATE; ++c) {
101 unsigned char byte = (
unsigned char)AS_CSTRING(printable)[c];
103 case '\\': fprintf(f,
"\\\\");
break;
104 case '\n': fprintf(f,
"\\n");
break;
105 case '\r': fprintf(f,
"\\r");
break;
106 case '\'': fprintf(f,
"\\'");
break;
108 if (byte < ' ' || byte >
'~') {
109 fprintf(f,
"\\x%02x",
byte);
111 fprintf(f,
"%c",
byte);
117 if (AS_STRING(printable)->length > STRING_DEBUG_TRUNCATE) {
122 switch (AS_OBJECT(printable)->type) {
123 case KRK_OBJ_CODEOBJECT: fprintf(f,
"<codeobject %s>", AS_codeobject(printable)->name ? AS_codeobject(printable)->name->chars :
"?");
break;
124 case KRK_OBJ_CLASS: fprintf(f,
"<class %s>", AS_CLASS(printable)->name ? AS_CLASS(printable)->name->chars :
"?");
break;
125 case KRK_OBJ_INSTANCE: fprintf(f,
"<instance of %s>", AS_INSTANCE(printable)->_class->name->chars);
break;
126 case KRK_OBJ_NATIVE: fprintf(f,
"<nativefn %s>", ((
KrkNative*)AS_OBJECT(printable))->name);
break;
127 case KRK_OBJ_CLOSURE: fprintf(f,
"<function %s>", AS_CLOSURE(printable)->function->name->chars);
break;
128 case KRK_OBJ_BYTES: fprintf(f,
"<bytes of len %ld>", (
long)AS_BYTES(printable)->length);
break;
129 case KRK_OBJ_TUPLE: {
131 for (
size_t i = 0; i < AS_TUPLE(printable)->values.count; ++i) {
133 if (i + 1 != AS_TUPLE(printable)->values.count) {
139 case KRK_OBJ_BOUND_METHOD: fprintf(f,
"<method %s>",
140 AS_BOUND_METHOD(printable)->method ? (
141 AS_BOUND_METHOD(printable)->method->type == KRK_OBJ_CLOSURE ? ((
KrkClosure*)AS_BOUND_METHOD(printable)->method)->function->name->chars :
142 (AS_BOUND_METHOD(printable)->method->type == KRK_OBJ_NATIVE ? ((
KrkNative*)AS_BOUND_METHOD(printable)->method)->name :
"(unknown)")) :
"(corrupt bound method)");
break;
143 default: fprintf(f,
"<%s>",
krk_typeName(printable));
break;
157 if (likely(type && type->
_eq)) {
162 if (IS_BOOLEAN(result))
return AS_BOOLEAN(result);
167 if (type && type->
_eq) {
172 if (IS_BOOLEAN(result))
return AS_BOOLEAN(result);
179 static inline int _krk_same_type_equivalence(uint16_t valtype,
KrkValue a,
KrkValue b) {
181 case KRK_VAL_BOOLEAN:
182 case KRK_VAL_INTEGER:
184 case KRK_VAL_NOTIMPL:
186 case KRK_VAL_HANDLER:
190 return _krk_method_equivalence(a,b);
194 static inline int _krk_same_type_equivalence_b(uint16_t valtype,
KrkValue a,
KrkValue b) {
196 case KRK_VAL_BOOLEAN:
197 case KRK_VAL_INTEGER:
199 case KRK_VAL_NOTIMPL:
201 case KRK_VAL_HANDLER:
205 return _krk_method_equivalence(a,b);
209 static inline int _krk_diff_type_equivalence(uint16_t val_a, uint16_t val_b,
KrkValue a,
KrkValue b) {
211 if (val_b == KRK_VAL_KWARGS || val_a == KRK_VAL_KWARGS)
return 0;
214 return _krk_method_equivalence(a,b);
219 if (a == b)
return 1;
220 uint16_t val_a = KRK_VAL_TYPE(a);
221 uint16_t val_b = KRK_VAL_TYPE(b);
222 return (val_a == val_b)
223 ? _krk_same_type_equivalence_b(val_a, a, b)
224 : _krk_diff_type_equivalence(val_a, val_b, a, b);
229 uint16_t val_a = KRK_VAL_TYPE(a);
230 uint16_t val_b = KRK_VAL_TYPE(b);
231 return (val_a == val_b)
232 ? _krk_same_type_equivalence(val_a,a,b)
233 : _krk_diff_type_equivalence(val_a,val_b,a,b);
Functions for dealing with garbage collection and memory allocation.
Struct definitions for core object types.
KrkObj * _tostr
__str__ Called to produce a string from an instance
KrkObj * _reprer
__repr__ Called to create a reproducible string representation of an instance
KrkObj * _eq
__eq__ Implementation for equality check (==)
Managed binding to a C function.
Flexible vector of stack references.
void krk_initValueArray(KrkValueArray *array)
Initialize a value array.
void krk_freeValueArray(KrkValueArray *array)
Release relesources used by a value array.
void krk_writeValueArray(KrkValueArray *array, KrkValue value)
Add a value to a value array.
Stack reference or primative value.
const char * krk_typeName(KrkValue value)
Get the name of the type of a value.
int krk_valuesEqual(KrkValue a, KrkValue b)
Compare two values for equality.
void krk_printValue(FILE *f, KrkValue value)
Print a string representation of a value.
int krk_valuesSame(KrkValue a, KrkValue b)
Compare two values by identity.
KrkClass * krk_getType(KrkValue value)
Get the class representing a value.
int krk_valuesSameOrEqual(KrkValue a, KrkValue b)
Compare two values by identity, then by equality.
void krk_printValueSafe(FILE *f, KrkValue value)
Print a value without calling the VM.
int krk_isFalsey(KrkValue value)
Determine the truth of a value.
Utilities for creating native bindings.
Definitions for primitive stack references.
Core API for the bytecode virtual machine.
threadLocal KrkThreadState krk_currentThread
Thread-local VM state.
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.