aboutsummaryrefslogtreecommitdiff
path: root/src/ls.c
blob: 0f984fb72cf5f09a5ad14fdb53b4ec1edebf48ab (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
// This software disclaims copyright. Do what you want with it. Be gay, do
// crime. Originally written by Alexis Lockwood in 2021. Ⓐ

// --- DEPENDENCIES ------------------------------------------------------------

// This module
#include "ls.h"

// Supporting modules
#include "ls_kws.h"
#include "ls_expr.h"
#include "ls_internal.h"

// Standard headers
#include <stdbool.h>
#include <stddef.h>
#include <inttypes.h>
#include <string.h>

// --- PRIVATE MACROS ----------------------------------------------------------
// --- PRIVATE DATATYPES -------------------------------------------------------
// --- PRIVATE CONSTANTS -------------------------------------------------------
// --- PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// --- PUBLIC VARIABLES --------------------------------------------------------
// --- PRIVATE VARIABLES -------------------------------------------------------
// --- PUBLIC FUNCTIONS --------------------------------------------------------

ls_error_t ls_run(ls_context_t * ctx, ls_value_t * pool, size_t szpool)
{
	if (setjmp(ctx->error_jmp_buf))
	{
		return ctx->error;
	}

	ls_init_ctx(ctx, pool, szpool);

	while (!ctx->stop)
	{
		if (!ls_exec_line(ctx))
			return LS_OK;
	}

	return LS_STOPPED;
}

#define TO_POOL_N(p) (p ? "" : "----"), \
	(p ? 4 : 0), (p ? (unsigned)(p - first) : 0)
#define POOL_F "%s%.*u"
void ls_print_value(FILE * stream, ls_value_t * value, ls_value_t * first)
{
	char buf[16];

	fprintf(stream, "<"POOL_F"> p<"POOL_F"> n<"POOL_F"> ",
		TO_POOL_N(value), TO_POOL_N(value->prev), TO_POOL_N(value->next)
	);

	switch (value->ty)
	{
	case LS_TY_NOT_ALLOC:
		fprintf(stream, "[free    ]");
		break;
	case LS_TY_PRISTINE:
		fprintf(stream, "[pristine]");
		break;
	case LS_TY_INT:
		fprintf(stream, "[int     ] %"PRId32, value->body.integer.value);
		break;
	case LS_TY_STR:
		memcpy(buf, value->body.str.value, 4);
		buf[4] = 0;
		fprintf(stream,
			"[str     ] len = %"PRIu16", "
			"chunk = <"POOL_F">, val = %s",
			value->body.str.length,
			TO_POOL_N(value->body.str.chunk),
			buf
		);
		break;
	case LS_TY_LIST:
		fprintf(stream,
			"[list    ] len = %"PRIu16", first = <"POOL_F">, "
			"memo = %"PRIu16" @ <"POOL_F">",
			value->body.list.length,
			TO_POOL_N(value->body.list.first),
			value->body.list.memo_i,
			TO_POOL_N(value->body.list.memo)
		);
		break;
	case LS_TY_STR_CHUNK:
		memcpy(buf, value->body.str_chunk.value, 8);
		buf[8] = 0;
		fprintf(stream, "[strchunk] %s", buf);
		break;
	case LS_TY_INT_VAR:
		memcpy(buf, value->body.int_var.ident, LS_IDENT_LEN);
		buf[LS_IDENT_LEN] = 0;
		fprintf(stream, "[intvar  ] %s = %"PRId32,
			buf,
			value->body.int_var.value
		);
		break;
	case LS_TY_VAR:
		memcpy(buf, value->body.var.ident, LS_IDENT_LEN);
		buf[LS_IDENT_LEN] = 0;
		fprintf(stream, "[var     ] %s = <"POOL_F">",
			buf,
			TO_POOL_N(value->body.var.value)
		);
		break;
	case LS_TY_LABEL:
		memcpy(buf, value->body.label.ident, LS_IDENT_LEN);
		buf[LS_IDENT_LEN] = 0;
		fprintf(stream, "[label   ] %s = %"PRIu16,
			buf,
			value->body.label.pc
		);
		break;
	case LS_TY_SCTX_CALL:
		fprintf(stream, "[sctxCALL] pc = %"PRIu16", readptr = %"PRIu16,
			value->body.sctx_call.pc,
			value->body.sctx_call.readptr
		);
		break;
	case LS_TY_SCTX_FOR:
		fprintf(stream, "[sctxFOR ] term = %"PRId32", step = %"PRId32,
			value->body.sctx_for.term,
			value->body.sctx_for.step
		);
		break;
	case LS_TY_SCTX_WHILE:
		fprintf(stream, "[sctxWHIL] pc = %"PRIu16,
			value->body.sctx_while.while_pc
		);
		break;
	default:
		fprintf(stream, "[inval   ] ty = %u", value->ty);
		break;
	}
}

bool ls_translate_pc(ls_context_t * ctx, ls_addr_t pc, uint16_t * line,
	uint16_t * col)
{
	uint16_t n_line = 1, n_col = 1;
	bool last_was_nl = false;

	for (ls_addr_t i = 0; i <= pc; i++)
	{
		int f = ctx->fetcher(ctx->fetcher_ctx, i);

		if (f <= 0)
			return false;

		ls_uchar uch = (ls_uchar) f;

		if (last_was_nl)
		{
			n_line++;
			n_col = 1;
		}
		else
		{
			n_col++;
		}

		last_was_nl = (uch == (ls_uchar) '\n');
	}

	*line = n_line;
	*col = n_col;
	return true;
}

// --- PRIVATE FUNCTION DEFINITIONS --------------------------------------------