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
|
#include "aplwc_internal.h"
#include "boot.h"
enum aplwc_rc
aplwc_new(struct aplwc **p, uint8_t *data, size_t size)
{
struct aplwc tmp_aplwc;
memory_init(&tmp_aplwc, data, size);
*p = memory_alloc_aplwc(&tmp_aplwc);
if(*p == NULL)
return tmp_aplwc.last_error;
memcpy(*p, &tmp_aplwc, sizeof(tmp_aplwc));
return aplwc_load_module(*p, "Boot", (const char *)boot_src, boot_src_len);
}
void
aplwc_destroy(struct aplwc *p)
{
UNUSED_PARAMETER(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;
}
struct aplwc_line *
aplwc_read(struct aplwc *aplwc, const char *line)
{
struct string *text;
struct token_list *tokens;
struct aplwc_line *ret;
text = chars_to_string(aplwc, line, -1);
if(text == NULL)
return NULL;
tokens = scan_string(aplwc, text, 1);
if(tokens == NULL)
return NULL;
aplwc->last_error = check_tokens(tokens);
if(aplwc->last_error != APLWC_OK)
return NULL;
ret = memory_alloc_aplwc_line(aplwc);
if(ret == NULL)
return NULL;
ret->aplwc = aplwc;
ret->text = text;
ret->tokens = tokens;
return ret;
}
void
aplwc_free_line(struct aplwc_line *line)
{
if(line)
memory_free_root(line->aplwc, line);
}
struct aplwc_value *
aplwc_eval(struct aplwc_line *line)
{
line->aplwc->last_error = APLWC_OK;
return eval_line(line);
}
void
aplwc_free_value(struct aplwc_value *value)
{
if(value)
memory_free_root(value->aplwc, value);
}
const char *
aplwc_value_string(struct aplwc_value *value)
{
assert(value != NULL);
switch(value->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;
if((namestr = chars_to_string(aplwc, name, -1)) == NULL)
goto error;
if((text = chars_to_string(aplwc, src, srclen)) == NULL)
goto error;
if((tokens = scan_string(aplwc, text, 0)) == NULL)
goto error;
if(load_module(aplwc, namestr, tokens) != APLWC_OK){
error:
return aplwc_last_error(aplwc);
}
return APLWC_OK;
}
|