2020-12-26 03:32:21 +03:00
|
|
|
#pragma once
|
|
|
|
|
2020-12-29 09:26:45 +03:00
|
|
|
#include <stdarg.h>
|
2020-12-29 12:51:03 +03:00
|
|
|
#include <sys/types.h>
|
2020-12-26 03:32:21 +03:00
|
|
|
#include "kuroko.h"
|
|
|
|
#include "value.h"
|
2020-12-26 08:33:34 +03:00
|
|
|
#include "table.h"
|
2020-12-27 03:33:28 +03:00
|
|
|
#include "object.h"
|
|
|
|
|
|
|
|
#define FRAMES_MAX 64
|
2020-12-26 03:32:21 +03:00
|
|
|
|
|
|
|
typedef struct {
|
2020-12-27 07:02:26 +03:00
|
|
|
KrkClosure * closure;
|
2020-12-26 03:32:21 +03:00
|
|
|
uint8_t * ip;
|
2020-12-27 16:40:35 +03:00
|
|
|
size_t slots;
|
2020-12-30 16:30:09 +03:00
|
|
|
int isInlined;
|
2020-12-27 03:33:28 +03:00
|
|
|
} CallFrame;
|
|
|
|
|
2020-12-27 16:40:35 +03:00
|
|
|
typedef enum {
|
|
|
|
METHOD_INIT,
|
|
|
|
METHOD_STR,
|
2021-01-01 10:01:58 +03:00
|
|
|
METHOD_REPR,
|
2020-12-28 13:01:28 +03:00
|
|
|
METHOD_GET,
|
|
|
|
METHOD_SET,
|
|
|
|
METHOD_CLASS,
|
|
|
|
METHOD_NAME,
|
|
|
|
METHOD_FILE,
|
2020-12-29 10:40:42 +03:00
|
|
|
METHOD_INT,
|
|
|
|
METHOD_FLOAT,
|
|
|
|
METHOD_CHR,
|
2020-12-30 09:50:26 +03:00
|
|
|
METHOD_LEN,
|
2020-12-30 10:59:21 +03:00
|
|
|
METHOD_DOC,
|
2020-12-31 03:15:53 +03:00
|
|
|
METHOD_BASE,
|
2020-12-31 12:14:20 +03:00
|
|
|
METHOD_GETSLICE,
|
2021-01-01 14:52:18 +03:00
|
|
|
METHOD_LIST_INT,
|
|
|
|
METHOD_DICT_INT,
|
2020-12-27 16:40:35 +03:00
|
|
|
|
|
|
|
METHOD__MAX,
|
|
|
|
} KrkSpecialMethods;
|
|
|
|
|
2020-12-27 03:33:28 +03:00
|
|
|
typedef struct {
|
|
|
|
CallFrame frames[FRAMES_MAX];
|
|
|
|
size_t frameCount;
|
2020-12-26 03:32:21 +03:00
|
|
|
size_t stackSize;
|
|
|
|
KrkValue * stack;
|
|
|
|
KrkValue * stackTop;
|
2020-12-26 10:53:15 +03:00
|
|
|
KrkTable globals;
|
2020-12-26 08:33:34 +03:00
|
|
|
KrkTable strings;
|
2020-12-27 12:55:52 +03:00
|
|
|
KrkTable modules;
|
2020-12-27 07:02:26 +03:00
|
|
|
KrkUpvalue * openUpvalues;
|
2020-12-26 03:32:21 +03:00
|
|
|
KrkObj * objects;
|
2020-12-27 09:58:32 +03:00
|
|
|
size_t bytesAllocated;
|
|
|
|
size_t nextGC;
|
|
|
|
size_t grayCount;
|
|
|
|
size_t grayCapacity;
|
2020-12-29 07:19:22 +03:00
|
|
|
ssize_t exitOnFrame;
|
2020-12-27 09:58:32 +03:00
|
|
|
KrkObj** grayStack;
|
2020-12-27 16:40:35 +03:00
|
|
|
KrkValue specialMethodNames[METHOD__MAX];
|
2020-12-28 03:08:35 +03:00
|
|
|
|
2021-01-01 06:04:58 +03:00
|
|
|
KrkClass * objectClass;
|
2020-12-28 13:01:28 +03:00
|
|
|
KrkInstance * builtins;
|
|
|
|
|
2021-01-01 06:04:58 +03:00
|
|
|
struct {
|
|
|
|
KrkClass * baseException;
|
|
|
|
KrkClass * typeError;
|
|
|
|
KrkClass * argumentError;
|
|
|
|
KrkClass * indexError;
|
|
|
|
KrkClass * keyError;
|
|
|
|
KrkClass * attributeError;
|
|
|
|
KrkClass * nameError;
|
|
|
|
KrkClass * importError;
|
|
|
|
KrkClass * ioError;
|
|
|
|
} exceptions;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
KrkClass * typeClass; /* Class */
|
|
|
|
KrkClass * intClass; /* Integer */
|
|
|
|
KrkClass * floatClass; /* Floating */
|
|
|
|
KrkClass * boolClass; /* Boolean */
|
|
|
|
KrkClass * noneTypeClass; /* None */
|
|
|
|
KrkClass * strClass; /* String */
|
|
|
|
KrkClass * functionClass; /* Functions, Closures */
|
|
|
|
KrkClass * methodClass; /* BoundMethod */
|
2021-01-01 14:52:18 +03:00
|
|
|
|
|
|
|
KrkClass * baselist;
|
|
|
|
KrkClass * basedict;
|
2021-01-01 06:04:58 +03:00
|
|
|
} baseClasses;
|
|
|
|
|
2020-12-29 05:00:12 +03:00
|
|
|
KrkValue currentException;
|
2020-12-28 14:38:26 +03:00
|
|
|
int flags;
|
2020-12-26 03:32:21 +03:00
|
|
|
} KrkVM;
|
|
|
|
|
2020-12-28 14:38:26 +03:00
|
|
|
#define KRK_ENABLE_TRACING (1 << 0)
|
|
|
|
#define KRK_ENABLE_DEBUGGING (1 << 1)
|
|
|
|
#define KRK_ENABLE_SCAN_TRACING (1 << 2)
|
|
|
|
#define KRK_ENABLE_STRESS_GC (1 << 3)
|
|
|
|
|
2020-12-28 16:02:39 +03:00
|
|
|
#define KRK_GC_PAUSED (1 << 10)
|
2020-12-29 05:00:12 +03:00
|
|
|
#define KRK_HAS_EXCEPTION (1 << 11)
|
2020-12-28 16:02:39 +03:00
|
|
|
|
2020-12-31 08:46:02 +03:00
|
|
|
extern KrkVM krk_vm;
|
|
|
|
#define vm krk_vm
|
2020-12-26 03:32:21 +03:00
|
|
|
|
2020-12-28 14:38:26 +03:00
|
|
|
extern void krk_initVM(int flags);
|
2020-12-26 03:32:21 +03:00
|
|
|
extern void krk_freeVM(void);
|
2020-12-28 04:54:25 +03:00
|
|
|
extern KrkValue krk_interpret(const char * src, int newScope, char *, char *);
|
|
|
|
extern KrkValue krk_runfile(const char * fileName, int newScope, char *, char *);
|
2020-12-26 03:32:21 +03:00
|
|
|
extern void krk_push(KrkValue value);
|
|
|
|
extern KrkValue krk_pop(void);
|
2021-01-01 10:01:58 +03:00
|
|
|
extern KrkValue krk_peek(int distance);
|
2020-12-28 05:11:50 +03:00
|
|
|
extern const char * krk_typeName(KrkValue value);
|
2020-12-28 13:01:28 +03:00
|
|
|
extern void krk_defineNative(KrkTable * table, const char * name, NativeFn function);
|
2020-12-28 13:25:33 +03:00
|
|
|
extern void krk_attachNamedObject(KrkTable * table, const char name[], KrkObj * obj);
|
2020-12-31 09:48:39 +03:00
|
|
|
extern void krk_attachNamedValue(KrkTable * table, const char name[], KrkValue obj);
|
2021-01-01 06:04:58 +03:00
|
|
|
extern void krk_runtimeError(KrkClass * type, const char * fmt, ...);
|
2020-12-28 16:02:39 +03:00
|
|
|
|
|
|
|
#define KRK_PAUSE_GC() do { vm.flags |= KRK_GC_PAUSED; } while (0)
|
|
|
|
#define KRK_RESUME_GC() do { vm.flags &= ~(KRK_GC_PAUSED); } while (0)
|
2020-12-29 12:51:03 +03:00
|
|
|
|
|
|
|
extern KrkValue krk_dictGet(KrkValue dictClass, KrkInstance * dict, KrkValue key);
|
|
|
|
extern void krk_dictSet(KrkValue dictClass, KrkInstance * dict, KrkValue key, KrkValue value);
|
|
|
|
extern KrkInstance * krk_dictCreate(KrkValue * outClass);
|
2020-12-29 14:25:34 +03:00
|
|
|
extern KrkValue krk_runNext(void);
|
2021-01-01 10:01:58 +03:00
|
|
|
extern KrkValue krk_typeOf(int argc, KrkValue argv[]);
|
|
|
|
extern int krk_bindMethod(KrkClass * _class, KrkString * name);
|
|
|
|
extern int krk_callValue(KrkValue callee, int argCount);
|