summaryrefslogtreecommitdiff
path: root/src/lib/main.c
blob: cca1e681bc239dc359bc556d546738fc5c718550 (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
#include "aplwc_internal.h"

enum aplwc_rc
aplwc_new(struct aplwc **p, uint8_t *data, size_t size)
{
	struct memory m;

	m = memory_init(data, size);

	*p = memory_alloc_aplwc(&m);
	if(*p == NULL)
		return APLWC_OUT_OF_MEMORY;
	(*p)->memory = m;

	return APLWC_OK;
}

void
aplwc_destroy(struct aplwc *p)
{
	if(p)
		memory_free_aplwc(&p->memory, 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";
	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);
	if(text == NULL)
		return NULL;

	tokens = scan_line(aplwc, text);
	if(tokens == NULL)
		return NULL;

	ret = memory_alloc_aplwc_line(&aplwc->memory);
	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 == NULL)
		return;
	memory_free_aplwc_line(&line->aplwc->memory, line);
}

enum aplwc_rc
aplwc_eval(struct aplwc_line *line)
{
	line->aplwc->last_error = APLWC_OK;
	eval_line(line);
	return line->aplwc->last_error;
}