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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
/*
* Copyright (C) 2020 Jo-Philipp Wich <jo@mein.io>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef __AST_H_
#define __AST_H_
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#ifdef JSONC
#include <json.h>
#else
#include <json-c/json.h>
#endif
#define UT_ERRMSG_OOM "Runtime error: Memory allocation failure"
enum ut_error_type {
UT_ERROR_NO_ERROR,
UT_ERROR_OUT_OF_MEMORY,
UT_ERROR_UNTERMINATED_COMMENT,
UT_ERROR_UNTERMINATED_STRING,
UT_ERROR_UNTERMINATED_BLOCK,
UT_ERROR_UNEXPECTED_TOKEN,
UT_ERROR_UNEXPECTED_CHAR,
UT_ERROR_OVERLONG_STRING,
UT_ERROR_INVALID_ESCAPE,
UT_ERROR_NESTED_BLOCKS,
UT_ERROR_EXCEPTION
};
enum ut_block_type {
UT_BLOCK_NONE,
UT_BLOCK_STATEMENT,
UT_BLOCK_EXPRESSION,
UT_BLOCK_COMMENT
};
struct ut_opcode {
int type;
struct json_object *val;
struct ut_opcode *operand[4], *next, *sibling;
uint32_t off;
};
struct ut_state {
struct ut_opcode *pool;
struct ut_opcode *main;
uint8_t semicolon_emitted:1;
uint8_t start_tag_seen:1;
uint8_t srand_called:1;
size_t off;
enum ut_block_type blocktype;
struct {
enum ut_error_type code;
union {
struct json_object *exception;
uint64_t tokens[2];
} info;
} error;
struct {
struct json_object **scope;
uint8_t size;
uint8_t off;
} stack;
};
struct ut_opcode *ut_new_op(struct ut_state *s, int type, struct json_object *val, ...);
struct ut_opcode *ut_wrap_op(struct ut_opcode *parent, ...);
struct ut_opcode *ut_append_op(struct ut_opcode *a, struct ut_opcode *b);
enum ut_error_type ut_parse(struct ut_state *s, const char *expr);
void ut_free(struct ut_state *s);
struct ut_opcode *ut_new_func(struct ut_state *s, struct ut_opcode *name, struct ut_opcode *args, struct ut_opcode *body);
struct json_object *json_object_new_double_rounded(double v);
struct json_object *json_object_new_null_obj(void);
void *ParseAlloc(void *(*mfunc)(size_t));
void Parse(void *pParser, int type, struct ut_opcode *op, struct ut_state *s);
void ParseFree(void *pParser, void (*ffunc)(void *));
static inline uint32_t getrefcnt(struct json_object *v) {
struct {
enum json_type o_type;
uint32_t _ref_count;
} *spy = (void *)v;
return spy ? spy->_ref_count : 0;
}
#endif /* __AST_H_ */
|