lox/clox/value.h
Adnan Ioricce 9b87a0d9c0 [Chapter18&TypesOfValues] - Following up chapter 18
- A bug was found when trying to run the following expression:
- ```lox
!(5 - 4 > 3 * 2 == !nil)
```
2024-10-18 13:24:47 -03:00

46 lines
957 B
C

#ifndef clox_value_h
#define clox_value_h
#include "common.h"
//typedef double Value;
typedef enum {
VAL_BOOL,
VAL_NIL,
VAL_NUMBER,
} ValueType;
typedef struct {
ValueType type;
union {
bool boolean;
double number;
} 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 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}})
typedef struct {
int capacity;
int count;
Value* values;
} ValueArray;
void initValueArray(ValueArray* array);
void writeValueArray(ValueArray* array, Value value);
void freeValueArray(ValueArray* array);
void printValue(Value value);
#endif