aboutsummaryrefslogtreecommitdiff
path: root/include/value.h
blob: b90b446128495435758fc28f7fcb7310315df568 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#ifndef TSL_VALUE_H
#define TSL_VALUE_H

#include "forward_decl.h"
#include <stddef.h>
#include <stdint.h>
#include "string_view.h"
#include "std_gc/list.h"

typedef enum {
    TSL_TYPE_NULL,
    TSL_TYPE_NUMBER,
    TSL_TYPE_STRING,
    TSL_TYPE_STRING_REF,
    TSL_TYPE_BOOL,
    TSL_TYPE_LIST,
    TSL_TYPE_MAP,
    TSL_TYPE_FUNCTION,
    TSL_TYPE_USERDATA
} TslType;

/* TODO: Support BigInt */
typedef double TslNumber;

typedef enum {
    TSL_FALSE,
    TSL_TRUE
} TslBool;

typedef struct {
    char *data;
    size_t size;
} TslString;

/* This is an index to the function */
typedef int TslFunction;

typedef struct TslHashMap TslHashMap;

struct TslValue {
    union {
        TslNumber number;
        TslString *string;
        /*
            TODO: Make this an int that refers to the index in the bytecode which contains the string view.
            Then the size of this union will be 8 bytes instead of 16 bytes on 64-bit systems.
        */
        TslStringView string_ref;
        TslBool boolean;
        TslBool null;
        TslList *list;
        TslHashMap *map;
        TslFunction function;
        void *userdata;
    } data;
    uint8_t type;
};

uint64_t tsl_value_hash(const TslValue *value);
/* Returns 1 if equal, otherwise returns 0 */
int tsl_value_equals(const TslValue *lhs, const TslValue *rhs);

/* This should be called before setting/modifying the data of the value */
void tsl_value_clear(TslValue *self);

int tsl_value_add(const TslValue *lhs_value, const TslValue *rhs_value, TslValue *result);

/* Returns NULL if index is out of bounds of the list */
TslValue* tsl_string_ref_get(const TslStringView *self, double index);

#endif /* TSL_VALUE_H */