|
@@ -13,26 +13,33 @@
|
|
|
|
|
|
|
|
#include "chunk.h"
|
|
#include "chunk.h"
|
|
|
#include "common.h"
|
|
#include "common.h"
|
|
|
|
|
+#include "table.h"
|
|
|
#include "value.h"
|
|
#include "value.h"
|
|
|
|
|
|
|
|
#define OBJ_TYPE(value) (AS_OBJ(value)->type)
|
|
#define OBJ_TYPE(value) (AS_OBJ(value)->type)
|
|
|
|
|
|
|
|
|
|
+#define IS_CLASS(value) isObjType(value, OBJ_CLASS)
|
|
|
#define IS_CLOSURE(value) isObjType(value, OBJ_CLOSURE)
|
|
#define IS_CLOSURE(value) isObjType(value, OBJ_CLOSURE)
|
|
|
#define IS_FUNCTION(value) isObjType(value, OBJ_FUNCTION)
|
|
#define IS_FUNCTION(value) isObjType(value, OBJ_FUNCTION)
|
|
|
|
|
+#define IS_INSTANCE(value) isObjType(value, OBJ_INSTANCE)
|
|
|
#define IS_NATIVE(value) isObjType(value, OBJ_NATIVE)
|
|
#define IS_NATIVE(value) isObjType(value, OBJ_NATIVE)
|
|
|
#define IS_STRING(value) isObjType(value, OBJ_STRING)
|
|
#define IS_STRING(value) isObjType(value, OBJ_STRING)
|
|
|
|
|
|
|
|
|
|
+#define AS_CLASS(value) ((ObjClass *) AS_OBJ(value))
|
|
|
#define AS_CLOSURE(value) ((ObjClosure *) AS_OBJ(value))
|
|
#define AS_CLOSURE(value) ((ObjClosure *) AS_OBJ(value))
|
|
|
#define AS_FUNCTION(value) ((ObjFunction *) AS_OBJ(value))
|
|
#define AS_FUNCTION(value) ((ObjFunction *) AS_OBJ(value))
|
|
|
|
|
+#define AS_INSTANCE(value) ((ObjInstance *) AS_OBJ(value))
|
|
|
#define AS_NATIVE(value) (((ObjNative *) AS_OBJ(value))->function)
|
|
#define AS_NATIVE(value) (((ObjNative *) AS_OBJ(value))->function)
|
|
|
#define AS_STRING(value) ((ObjString *) AS_OBJ(value))
|
|
#define AS_STRING(value) ((ObjString *) AS_OBJ(value))
|
|
|
#define AS_CSTRING(value) (((ObjString *) AS_OBJ(value))->chars)
|
|
#define AS_CSTRING(value) (((ObjString *) AS_OBJ(value))->chars)
|
|
|
|
|
|
|
|
typedef enum {
|
|
typedef enum {
|
|
|
- OBJ_STRING,
|
|
|
|
|
- OBJ_FUNCTION,
|
|
|
|
|
- OBJ_NATIVE,
|
|
|
|
|
|
|
+ OBJ_CLASS,
|
|
|
OBJ_CLOSURE,
|
|
OBJ_CLOSURE,
|
|
|
|
|
+ OBJ_NATIVE,
|
|
|
|
|
+ OBJ_FUNCTION,
|
|
|
|
|
+ OBJ_INSTANCE,
|
|
|
|
|
+ OBJ_STRING,
|
|
|
OBJ_UPVALUE,
|
|
OBJ_UPVALUE,
|
|
|
} ObjType;
|
|
} ObjType;
|
|
|
|
|
|
|
@@ -78,8 +85,21 @@ typedef struct {
|
|
|
int upvalueCount;
|
|
int upvalueCount;
|
|
|
} ObjClosure;
|
|
} ObjClosure;
|
|
|
|
|
|
|
|
|
|
+typedef struct {
|
|
|
|
|
+ struct Obj obj;
|
|
|
|
|
+ ObjString *name;
|
|
|
|
|
+} ObjClass;
|
|
|
|
|
+
|
|
|
|
|
+typedef struct {
|
|
|
|
|
+ struct Obj obj;
|
|
|
|
|
+ ObjClass *klass;
|
|
|
|
|
+ Table fields;
|
|
|
|
|
+} ObjInstance;
|
|
|
|
|
+
|
|
|
|
|
+ObjClass *newClass(ObjString *name);
|
|
|
ObjClosure *newClosure(ObjFunction *function);
|
|
ObjClosure *newClosure(ObjFunction *function);
|
|
|
ObjFunction *newFunction();
|
|
ObjFunction *newFunction();
|
|
|
|
|
+ObjInstance *newInstance(ObjClass *klass);
|
|
|
ObjNative *newNative(NativeFn function);
|
|
ObjNative *newNative(NativeFn function);
|
|
|
ObjString *takeString(char *chars, int length);
|
|
ObjString *takeString(char *chars, int length);
|
|
|
ObjString *copyString(const char *chars, int length);
|
|
ObjString *copyString(const char *chars, int length);
|