kuroko/vm.h

138 lines
3.4 KiB
C
Raw Normal View History

#pragma once
#include <stdarg.h>
2020-12-29 12:51:03 +03:00
#include <sys/types.h>
#include "kuroko.h"
#include "value.h"
#include "table.h"
#include "object.h"
#define FRAMES_MAX 64
typedef struct {
KrkClosure * closure;
uint8_t * ip;
size_t slots;
size_t outSlots;
} CallFrame;
typedef enum {
METHOD_INIT,
METHOD_STR,
METHOD_REPR,
2020-12-28 13:01:28 +03:00
METHOD_GET,
METHOD_SET,
METHOD_CLASS,
METHOD_NAME,
METHOD_FILE,
METHOD_INT,
METHOD_FLOAT,
METHOD_CHR,
METHOD_LEN,
2020-12-30 10:59:21 +03:00
METHOD_DOC,
METHOD_BASE,
METHOD_GETSLICE,
METHOD_LIST_INT,
METHOD_DICT_INT,
METHOD_INREPR,
METHOD_ORD,
2021-01-04 10:21:27 +03:00
METHOD_CALL,
2021-01-05 11:41:32 +03:00
METHOD_EQ,
METHOD__MAX,
} KrkSpecialMethods;
typedef struct {
CallFrame frames[FRAMES_MAX];
size_t frameCount;
size_t stackSize;
KrkValue * stack;
KrkValue * stackTop;
2020-12-26 10:53:15 +03:00
KrkTable globals;
KrkTable strings;
KrkTable modules;
KrkUpvalue * openUpvalues;
KrkObj * objects;
size_t bytesAllocated;
size_t nextGC;
size_t grayCount;
size_t grayCapacity;
ssize_t exitOnFrame;
KrkObj** grayStack;
KrkValue specialMethodNames[METHOD__MAX];
KrkClass * objectClass;
2020-12-28 13:01:28 +03:00
KrkInstance * builtins;
struct {
KrkClass * baseException;
KrkClass * typeError;
KrkClass * argumentError;
KrkClass * indexError;
KrkClass * keyError;
KrkClass * attributeError;
KrkClass * nameError;
KrkClass * importError;
KrkClass * ioError;
2021-01-04 05:27:33 +03:00
KrkClass * valueError;
} 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 */
KrkClass * tupleClass; /* Tuple */
2021-01-04 11:29:06 +03:00
/* Other useful stuff */
KrkClass * listiteratorClass;
KrkClass * rangeClass;
KrkClass * rangeiteratorClass;
2021-01-04 13:34:56 +03:00
KrkClass * striteratorClass;
} baseClasses;
2020-12-29 05:00:12 +03:00
KrkValue currentException;
int flags;
} KrkVM;
#define KRK_ENABLE_TRACING (1 << 0)
2021-01-02 06:21:11 +03:00
#define KRK_ENABLE_DISASSEMBLY (1 << 1)
#define KRK_ENABLE_SCAN_TRACING (1 << 2)
#define KRK_ENABLE_STRESS_GC (1 << 3)
#define KRK_GC_PAUSED (1 << 10)
2020-12-29 05:00:12 +03:00
#define KRK_HAS_EXCEPTION (1 << 11)
extern KrkVM krk_vm;
#define vm krk_vm
extern void krk_initVM(int flags);
extern void krk_freeVM(void);
2021-01-04 11:47:53 +03:00
extern void krk_resetStack(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 *);
extern void krk_push(KrkValue value);
extern KrkValue krk_pop(void);
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);
extern void krk_runtimeError(KrkClass * type, const char * fmt, ...);
#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
2021-01-01 15:07:42 +03:00
extern KrkInstance * krk_dictCreate(void);
extern KrkValue krk_runNext(void);
extern KrkValue krk_typeOf(int argc, KrkValue argv[]);
extern int krk_bindMethod(KrkClass * _class, KrkString * name);
extern int krk_callValue(KrkValue callee, int argCount, int extra);
extern KrkValue krk_dict_of(int argc, KrkValue argv[]);
2021-01-05 11:41:32 +03:00
extern KrkValue krk_callSimple(KrkValue value, int argCount, int isMethod);