summaryrefslogtreecommitdiffhomepage
path: root/ast.c
blob: ab4f873ee7afc27791bd160ec8c576d610a65904 (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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
 * 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.
 */

#include "ast.h"
#include "lexer.h"
#include "parser.h"

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <math.h>
#include <libubox/utils.h>

struct ut_opcode *
ut_new_op(struct ut_state *s, int type, struct json_object *val, ...)
{
	struct ut_opcode *newop, *child;
	int n_op = 0;
	va_list ap;

	newop = calloc(1, sizeof(*newop));

	if (!newop) {
		fprintf(stderr, "Out of memory\n");
		exit(127);
	}

	newop->off = s->off;
	newop->type = type;
	newop->val = val;

	va_start(ap, val);

	while ((child = va_arg(ap, void *)) != (void *)1)
		if (n_op < sizeof(newop->operand) / sizeof(newop->operand[0]))
			newop->operand[n_op++] = child;

	va_end(ap);

	newop->next = s->pool;
	s->pool = newop;

	return newop;
}

struct ut_opcode *
ut_wrap_op(struct ut_opcode *parent, ...)
{
	struct ut_opcode *child;
	int n_op = 0;
	va_list ap;

	va_start(ap, parent);

	while ((child = va_arg(ap, void *)) != (void *)1)
		if (n_op < sizeof(parent->operand) / sizeof(parent->operand[0]))
			parent->operand[n_op++] = child;

	va_end(ap);

	return parent;
}

struct ut_opcode *
ut_append_op(struct ut_opcode *a, struct ut_opcode *b)
{
	struct ut_opcode *tail = a;

	while (tail->sibling)
		tail = tail->sibling;

	tail->sibling = b;

	return a;
}

static int
double_rounded_to_string(struct json_object *v, struct printbuf *pb, int level, int flags)
{
	double d = json_object_get_double(v);

	if (isnan(d))
		return sprintbuf(pb, level ? "\"NaN\"" : "NaN");

	if (d == INFINITY)
		return sprintbuf(pb, level ? "1e309" : "Infinity");

	if (d == -INFINITY)
		return sprintbuf(pb, level ? "-1e309" : "-Infinity");

	return sprintbuf(pb, "%g", d);
}

struct json_object *
json_object_new_double_rounded(double v)
{
	struct json_object *d = json_object_new_double(v);

	json_object_set_serializer(d, double_rounded_to_string, NULL, NULL);

	return d;
}

static int
null_obj_to_string(struct json_object *v, struct printbuf *pb, int level, int flags)
{
	return sprintbuf(pb, "null");
}

struct json_object *
json_object_new_null_obj(void) {
	struct json_object *d = json_object_new_boolean(false);

	json_object_set_serializer(d, null_obj_to_string, NULL, NULL);

	return d;
}

static int
func_to_string(struct json_object *v, struct printbuf *pb, int level, int flags)
{
	struct ut_opcode *op = json_object_get_userdata(v);
	struct ut_opcode *args = op ? op->operand[1] : NULL;
	struct json_object *name = (op && op->operand[0]) ? op->operand[0]->val : NULL;

	sprintbuf(pb, "%sfunction%s%s(",
		level ? "\"" : "",
		name ? " " : "",
		name ? json_object_get_string(name) : "");

	while (args) {
		sprintbuf(pb, "%s%s",
			(args != op->operand[1]) ? ", " : "",
			json_object_get_string(args->val));

		args = args->sibling;
	}

	return sprintbuf(pb, ") { ... }%s",
		level ? "\"" : "");
}

struct ut_opcode *
ut_new_func(struct ut_state *s, struct ut_opcode *name, struct ut_opcode *args, struct ut_opcode *body)
{
	struct ut_opcode *op = ut_new_op(s, T_FUNC, json_object_new_boolean(0), name, args, body, (void *)1);

	json_object_set_serializer(op->val, func_to_string, op, NULL);

	return op;
}

static void
ut_reset(struct ut_state *s)
{
	s->semicolon_emitted = false;
	s->start_tag_seen = false;
	s->blocktype = UT_BLOCK_NONE;
	s->off = 0;

	if (s->error.code == UT_ERROR_EXCEPTION)
		json_object_put(s->error.info.exception);

	memset(&s->error, 0, sizeof(s->error));
}

void
ut_free(struct ut_state *s)
{
	struct ut_opcode *op, *tmp;

	if (s) {
		while (s->stack.off > 0)
			json_object_put(s->stack.scope[--s->stack.off]);

		free(s->stack.scope);

		for (op = s->pool; op;) {
			tmp = op->next;

			if (op->val != (void *)1)
				json_object_put(op->val);

			free(op);
			op = tmp;
		}

		ut_reset(s);
	}

	free(s);
}

enum ut_error_type
ut_parse(struct ut_state *s, const char *expr)
{
	int len = strlen(expr);
	const char *ptr = expr;
	struct ut_opcode *op;
	void *pParser;
	int mlen = 0;

	if (!s)
		return UT_ERROR_OUT_OF_MEMORY;

	ut_reset(s);

	pParser = ParseAlloc(malloc);

	if (!pParser)
		return UT_ERROR_OUT_OF_MEMORY;

	while (len > 0) {
		op = ut_get_token(s, ptr, &mlen);

		if (mlen < 0) {
			s->error.code = -mlen;
			goto out;
		}

		if (op)
			Parse(pParser, op->type, op, s);

		if (s->error.code)
			goto out;

		len -= mlen;
		ptr += mlen;
	}

	Parse(pParser, 0, NULL, s);

out:
	ParseFree(pParser, free);

	return s->error.code;
}