summaryrefslogtreecommitdiff
path: root/src/lib/main.c
blob: bba93d3737fd062e3cfdcd2b29f20ec17bb8ddab (plain) (blame)
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
/* 
 * Copyright (C) 2026 Peter Mikkelsen <petermikkelsen10@gmail.com>
 *
 * This file is part of aplwc.
 *
 * Aplwc is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Aplwc is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with aplwc.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "aplwc_internal.h"
#include "boot.h"

struct aplwc *
aplwc_new(struct aplwc_fns fns, enum aplwc_rc *rcp)
{
	enum aplwc_rc rc;
	struct aplwc *aplwc;

	aplwc = NULL;
	if(fns.alloc == NULL){
		rc = APLWC_OUT_OF_MEMORY;
		goto end;
	}
	aplwc = fns.alloc(sizeof(*aplwc), NULL);
	if(aplwc == NULL){
		rc = APLWC_OUT_OF_MEMORY;
		goto end;
	}

	aplwc->modules = NULL;
	aplwc->fns = fns;
	memory_init(aplwc);

	rc = aplwc_load_module(aplwc, "Boot", (const char *)boot_src, boot_src_len);
end:
	if(rc != APLWC_OK){
		aplwc_destroy(aplwc);
		aplwc = NULL;
	}
	assert(rcp);
	*rcp = rc;
	return aplwc;
}

void
aplwc_destroy(struct aplwc *p)
{
	if(p == NULL)
		return;
	
	memory_destroy(p);
	if(p->fns.dealloc)
		p->fns.dealloc(p);
}

const char *
aplwc_errstr_rc(enum aplwc_rc rc)
{
	switch(rc){
	case APLWC_OK:
		return "";
	case APLWC_OUT_OF_MEMORY:
		return "Out of memory";
	case APLWC_NOT_YET_IMPLEMENTED:
		return "Not yet implemented";
	case APLWC_INVALID_UTF8:
		return "Invalid UTF-8";
	case APLWC_INVALID_SYNTAX:
		return "Invalid syntax";
	default:
		return "(unable to produce error string)";
	}
}

const char *
aplwc_errstr(struct aplwc *aplwc)
{
	enum { BUF_SIZE = 256 };

	const char *err;
	enum aplwc_rc rc;

	err = NULL;

	rc = aplwc ? aplwc->last_error : APLWC_RC_LAST+1;
	err = aplwc_errstr_rc(rc);

	return err;
}

enum aplwc_rc
aplwc_last_error(struct aplwc *aplwc)
{
	return aplwc->last_error;
}

void
aplwc_read(struct aplwc *aplwc, const char *linestr, AplwcLine *lineptr)
{
	struct string *text;
	struct token_list *tokens;
	struct aplwc_line *line;

	MEMPTRS(aplwc, 2);

	aplwc_clear_line(lineptr);

	if((text = chars_to_string(aplwc, linestr, -1)) == NULL)
		goto end;
	MEMPTR(0, text);

	if((tokens = scan_string(aplwc, text, 1)) == NULL)
		goto end;
	MEMPTR(1, tokens);

	if((aplwc->last_error = check_tokens(tokens)) != APLWC_OK)
		goto end;

	if((line = memory_alloc_aplwc_line(aplwc)) == NULL)
		goto end;
	
	line->text = text;
	line->tokens = tokens;

	memory_root_new(aplwc, line, &lineptr->ref);
end:
	MEMPTRS_EXIT(aplwc);
}

void
aplwc_free_line(AplwcLine *line)
{
	memory_root_free(&line->ref);
}

void
aplwc_eval(AplwcLine *line, AplwcValue *value)
{
	struct aplwc_line *l;
	struct aplwc_value *v;
	struct aplwc *aplwc;

	aplwc = line->ref.aplwc;

	aplwc_clear_value(value);
	l = memory_root_get(&line->ref);
	aplwc->last_error = APLWC_OK;
	v = eval_line(aplwc, l);
	if(v)
		memory_root_new(aplwc, v, &value->ref);
}

void
aplwc_free_value(AplwcValue *value)
{
	memory_root_free(&value->ref);
}

const char *
aplwc_value_string(AplwcValue *value)
{
	struct aplwc_value *v;
	assert(aplwc_valid_value(value));

	v = memory_root_get(&value->ref);
	switch(v->tag){
	default:
		return "???";
	}
}

enum aplwc_rc
aplwc_load_module(struct aplwc *aplwc, const char *name, const char *src, int srclen)
{
	struct string *text, *namestr;
	struct token_list *tokens;

	MEMPTRS(aplwc, 3);

	if((namestr = chars_to_string(aplwc, name, -1)) == NULL)
		goto end;
	MEMPTR(0, namestr);

	if((text = chars_to_string(aplwc, src, srclen)) == NULL)
		goto end;
	MEMPTR(1, text);

	if((tokens = scan_string(aplwc, text, 0)) == NULL)
		goto end;
	MEMPTR(2, tokens);

	aplwc->last_error = load_module(aplwc, namestr, tokens);
end:
	MEMPTRS_EXIT(aplwc);
	return aplwc_last_error(aplwc);
}

void
aplwc_clear_value(AplwcValue *v)
{
	clear_ref(&v->ref);
}

void
aplwc_clear_line(AplwcLine *l)
{
	clear_ref(&l->ref);
}

int
aplwc_valid_value(AplwcValue *v)
{
	return valid_ref(&v->ref);
}

int
aplwc_valid_line(AplwcLine *l)
{
	return valid_ref(&l->ref);
}

uint64_t aplwc_simulate_failure_at;
uint64_t aplwc_simulate_failure_count;

int
aplwc_simulate_failure(void)
{
	aplwc_simulate_failure_count++;
	if(aplwc_simulate_failure_at && (aplwc_simulate_failure_count >= aplwc_simulate_failure_at))
		return 1;
	else
		return 0;
}