|
@@ -17,16 +17,19 @@
|
|
|
|
|
|
|
|
#define OBJ_TYPE(value) (AS_OBJ(value)->type)
|
|
#define OBJ_TYPE(value) (AS_OBJ(value)->type)
|
|
|
|
|
|
|
|
-#define IS_FUNCTION(value) isObjType(value, OBJ_FUNCTION);
|
|
|
|
|
|
|
+#define IS_FUNCTION(value) isObjType(value, OBJ_FUNCTION)
|
|
|
|
|
+#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_FUNCTION(value) ((ObjFunction *) AS_OBJ(value))
|
|
#define AS_FUNCTION(value) ((ObjFunction *) AS_OBJ(value))
|
|
|
|
|
+#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_STRING,
|
|
|
OBJ_FUNCTION,
|
|
OBJ_FUNCTION,
|
|
|
|
|
+ OBJ_NATIVE,
|
|
|
} ObjType;
|
|
} ObjType;
|
|
|
|
|
|
|
|
struct Obj {
|
|
struct Obj {
|
|
@@ -41,6 +44,13 @@ typedef struct {
|
|
|
ObjString *name;
|
|
ObjString *name;
|
|
|
} ObjFunction;
|
|
} ObjFunction;
|
|
|
|
|
|
|
|
|
|
+typedef Value (*NativeFn)(int argCount, Value *args);
|
|
|
|
|
+
|
|
|
|
|
+typedef struct {
|
|
|
|
|
+ struct Obj obj;
|
|
|
|
|
+ NativeFn function;
|
|
|
|
|
+} ObjNative;
|
|
|
|
|
+
|
|
|
struct ObjString {
|
|
struct ObjString {
|
|
|
struct Obj obj;
|
|
struct Obj obj;
|
|
|
int length;
|
|
int length;
|
|
@@ -49,6 +59,7 @@ struct ObjString {
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
ObjFunction *newFunction();
|
|
ObjFunction *newFunction();
|
|
|
|
|
+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);
|
|
|
void printObject(Value value);
|
|
void printObject(Value value);
|