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
|
compiler.c
|
||||||
scanner.h
|
scanner.h
|
||||||
scanner.c
|
scanner.c
|
||||||
|
object.h
|
||||||
main.c)
|
main.c)
|
||||||
|
|
||||||
install(TARGETS Lox)
|
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"
|
#include "common.h"
|
||||||
|
|
||||||
|
typedef struct Obj Obj;
|
||||||
|
typedef struct ObjString ObjString;
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
VAL_BOOL,
|
VAL_BOOL,
|
||||||
VAL_NIL,
|
VAL_NIL,
|
||||||
VAL_NUMBER,
|
VAL_NUMBER,
|
||||||
|
VAL_OBJ,
|
||||||
} ValueType;
|
} ValueType;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
@ -14,19 +18,23 @@ typedef struct {
|
||||||
union {
|
union {
|
||||||
bool boolean;
|
bool boolean;
|
||||||
double number;
|
double number;
|
||||||
|
Obj *obj;
|
||||||
} as;
|
} as;
|
||||||
} Value;
|
} Value;
|
||||||
|
|
||||||
#define IS_BOOL(value) ((value).type == VAL_BOOL)
|
#define IS_BOOL(value) ((value).type == VAL_BOOL)
|
||||||
#define IS_NIL(value) ((value).type == VAL_NIL)
|
#define IS_NIL(value) ((value).type == VAL_NIL)
|
||||||
#define IS_NUMBER(value) ((value).type == VAL_NUMBER)
|
#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_BOOL(value) ((value).as.boolean)
|
||||||
#define AS_NUMBER(value) ((value).as.number)
|
#define AS_NUMBER(value) ((value).as.number)
|
||||||
|
|
||||||
#define BOOL_VAL(value) ((Value){VAL_BOOL, {.boolean = value}})
|
#define BOOL_VAL(value) ((Value){VAL_BOOL, {.boolean = value}})
|
||||||
#define NIL_VAL ((Value){VAL_NIL, {.number = 0}})
|
#define NIL_VAL ((Value){VAL_NIL, {.number = 0}})
|
||||||
#define NUMBER_VAL(value) ((Value){VAL_NUMBER, {.number = value}})
|
#define NUMBER_VAL(value) ((Value){VAL_NUMBER, {.number = value}})
|
||||||
|
#define OBJ_VAL(value) ((Value){VAL_OBJ, {.obj = (Obj *)object}})
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int capacity;
|
int capacity;
|
||||||
|
|
Loading…
Reference in a new issue