Chapter 19.1 - 19.2
This commit is contained in:
parent
c10cbcdf99
commit
ba52787271
3 changed files with 41 additions and 0 deletions
|
@ -14,6 +14,7 @@ add_executable(Lox
|
|||
compiler.c
|
||||
scanner.h
|
||||
scanner.c
|
||||
object.h
|
||||
main.c)
|
||||
|
||||
install(TARGETS Lox)
|
||||
|
|
32
clox/src/object.h
Normal file
32
clox/src/object.h
Normal file
|
@ -0,0 +1,32 @@
|
|||
#ifndef OBJECT_H
|
||||
#define OBJECT_H
|
||||
|
||||
#include "common.h"
|
||||
#include "value.h"
|
||||
|
||||
#define OBJ_TYPE(value) (AS_OBJ(value)->type)
|
||||
|
||||
#define IS_STRING(value) isObjType(value, OBJ_STRING)
|
||||
|
||||
#define AS_STRING(value) ((ObjString *)AS_OBJ(value))
|
||||
#define AS_CSTRING(value) (((ObjString *)AS_OBJ(value))->chars)
|
||||
|
||||
typedef enum {
|
||||
OBJ_STRING,
|
||||
} ObjType;
|
||||
|
||||
struct Obj {
|
||||
ObjType type;
|
||||
};
|
||||
|
||||
struct ObjString {
|
||||
Obj obj;
|
||||
int length;
|
||||
char *chars;
|
||||
};
|
||||
|
||||
static inline bool isObjType(Value value, ObjType type) {
|
||||
return IS_OBJ(value) && AS_OBJ(value)->type == type;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -3,10 +3,14 @@
|
|||
|
||||
#include "common.h"
|
||||
|
||||
typedef struct Obj Obj;
|
||||
typedef struct ObjString ObjString;
|
||||
|
||||
typedef enum {
|
||||
VAL_BOOL,
|
||||
VAL_NIL,
|
||||
VAL_NUMBER,
|
||||
VAL_OBJ,
|
||||
} ValueType;
|
||||
|
||||
typedef struct {
|
||||
|
@ -14,19 +18,23 @@ typedef struct {
|
|||
union {
|
||||
bool boolean;
|
||||
double number;
|
||||
Obj *obj;
|
||||
} as;
|
||||
} Value;
|
||||
|
||||
#define IS_BOOL(value) ((value).type == VAL_BOOL)
|
||||
#define IS_NIL(value) ((value).type == VAL_NIL)
|
||||
#define IS_NUMBER(value) ((value).type == VAL_NUMBER)
|
||||
#define IS_OBJ(value) ((value).type == VAL_OBJ)
|
||||
|
||||
#define AS_OBJ(value) ((value).as.obj)
|
||||
#define AS_BOOL(value) ((value).as.boolean)
|
||||
#define AS_NUMBER(value) ((value).as.number)
|
||||
|
||||
#define BOOL_VAL(value) ((Value){VAL_BOOL, {.boolean = value}})
|
||||
#define NIL_VAL ((Value){VAL_NIL, {.number = 0}})
|
||||
#define NUMBER_VAL(value) ((Value){VAL_NUMBER, {.number = value}})
|
||||
#define OBJ_VAL(value) ((Value){VAL_OBJ, {.obj = (Obj *)object}})
|
||||
|
||||
typedef struct {
|
||||
int capacity;
|
||||
|
|
Loading…
Reference in a new issue