goon

goon

https://git.tonybtw.com/goon.git git://git.tonybtw.com/goon.git
3,215 bytes raw
1
#ifndef GOON_H
2
#define GOON_H
3
4
#include <stddef.h>
5
#include <stdint.h>
6
#include <stdbool.h>
7
8
#define GOON_VERSION "0.1.0"
9
10
typedef enum {
11
    GOON_NIL,
12
    GOON_BOOL,
13
    GOON_INT,
14
    GOON_STRING,
15
    GOON_LIST,
16
    GOON_RECORD,
17
    GOON_BUILTIN,
18
    GOON_LAMBDA,
19
} Goon_Type;
20
21
typedef struct Goon_Value Goon_Value;
22
typedef struct Goon_Ctx Goon_Ctx;
23
typedef struct Goon_Record_Field Goon_Record_Field;
24
typedef struct Goon_Binding Goon_Binding;
25
26
typedef Goon_Value *(*Goon_Builtin_Fn)(Goon_Ctx *ctx, Goon_Value **args, size_t argc);
27
28
struct Goon_Record_Field {
29
    char *key;
30
    Goon_Value *value;
31
    Goon_Record_Field *next;
32
};
33
34
struct Goon_Value {
35
    Goon_Type type;
36
    struct Goon_Value *next_alloc;
37
    union {
38
        bool boolean;
39
        int64_t integer;
40
        char *string;
41
        struct {
42
            Goon_Value **items;
43
            size_t len;
44
            size_t cap;
45
        } list;
46
        struct {
47
            Goon_Record_Field *fields;
48
        } record;
49
        Goon_Builtin_Fn builtin;
50
        struct {
51
            char **params;
52
            size_t param_count;
53
            char *body;
54
            Goon_Binding *env;
55
        } lambda;
56
    } data;
57
};
58
59
typedef struct Goon_Binding {
60
    char *name;
61
    Goon_Value *value;
62
    struct Goon_Binding *next;
63
} Goon_Binding;
64
65
typedef struct {
66
    char *message;
67
    char *file;
68
    size_t line;
69
    size_t col;
70
    char *source_line;
71
} Goon_Error;
72
73
struct Goon_Ctx {
74
    Goon_Binding *env;
75
    Goon_Value *values;
76
    Goon_Record_Field *fields;
77
    Goon_Error error;
78
    char *base_path;
79
    void *userdata;
80
};
81
82
Goon_Ctx *goon_create(void);
83
void goon_destroy(Goon_Ctx *ctx);
84
85
void goon_set_userdata(Goon_Ctx *ctx, void *userdata);
86
void *goon_get_userdata(Goon_Ctx *ctx);
87
88
void goon_register(Goon_Ctx *ctx, const char *name, Goon_Builtin_Fn fn);
89
90
bool goon_load_file(Goon_Ctx *ctx, const char *path);
91
bool goon_load_string(Goon_Ctx *ctx, const char *source);
92
93
const char *goon_get_error(Goon_Ctx *ctx);
94
const Goon_Error *goon_get_error_info(Goon_Ctx *ctx);
95
void goon_error_print(const Goon_Error *err);
96
97
Goon_Value *goon_nil(Goon_Ctx *ctx);
98
Goon_Value *goon_bool(Goon_Ctx *ctx, bool val);
99
Goon_Value *goon_int(Goon_Ctx *ctx, int64_t val);
100
Goon_Value *goon_string(Goon_Ctx *ctx, const char *val);
101
Goon_Value *goon_list(Goon_Ctx *ctx);
102
Goon_Value *goon_record(Goon_Ctx *ctx);
103
104
bool goon_is_nil(Goon_Value *val);
105
bool goon_is_bool(Goon_Value *val);
106
bool goon_is_int(Goon_Value *val);
107
bool goon_is_string(Goon_Value *val);
108
bool goon_is_list(Goon_Value *val);
109
bool goon_is_record(Goon_Value *val);
110
111
bool goon_to_bool(Goon_Value *val);
112
int64_t goon_to_int(Goon_Value *val);
113
const char *goon_to_string(Goon_Value *val);
114
115
void goon_list_push(Goon_Ctx *ctx, Goon_Value *list, Goon_Value *item);
116
size_t goon_list_len(Goon_Value *list);
117
Goon_Value *goon_list_get(Goon_Value *list, size_t index);
118
119
void goon_record_set(Goon_Ctx *ctx, Goon_Value *record, const char *key, Goon_Value *value);
120
Goon_Value *goon_record_get(Goon_Value *record, const char *key);
121
Goon_Record_Field *goon_record_fields(Goon_Value *record);
122
123
Goon_Value *goon_eval_result(Goon_Ctx *ctx);
124
125
char *goon_to_json(Goon_Value *val);
126
char *goon_to_json_pretty(Goon_Value *val, int indent);
127
128
#endif