aboutsummaryrefslogtreecommitdiff
path: root/src/ls_goto.c
blob: 3572be630c8152d84cc56e402db9258cf4118106 (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
// 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_goto.h"

// Supporting modules
#include "ls_internal.h"
#include "ls_lex.h"

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

// --- PRIVATE MACROS ----------------------------------------------------------
// --- PRIVATE DATATYPES -------------------------------------------------------
// --- PRIVATE CONSTANTS -------------------------------------------------------
// --- PRIVATE FUNCTION PROTOTYPES ---------------------------------------------

static bool _rewind_line(ls_t * self);

// --- PUBLIC VARIABLES --------------------------------------------------------
// --- PRIVATE VARIABLES -------------------------------------------------------
// --- PUBLIC FUNCTIONS --------------------------------------------------------

void ls_goto_num(ls_t * self, bool backward, uint16_t num)
{
	ls_addr_t target = LS_ADDR_NULL;

	for (size_t i = 0; i < LS_LABEL_CACHE_SIZE; i++)
	{
		if (self->label_cache[i].pc == LS_ADDR_NULL)
			continue;
		if (self->label_cache[i].num == num)
		{
			if ((self->label_cache[i].pc > self->pc && !backward) ||
				(self->label_cache[i].pc <= self->pc && backward))
			{
				target = self->label_cache[i].pc;
				break;
			}
		}
	}

	// In case we can't find the label, save pc to "unwind" for a
	// more helpful error later
	ls_addr_t pc = self->pc;

	if (target == LS_ADDR_NULL && !backward)
	{
		// Walk forward until we find the label
		for (;;)
		{
			ls_token_t tok;
			ls_lex(self, &tok);

			if (tok.ty == LS_TOK_NUM_LABEL)
			{
				if (tok.body.number_val == num)
				{
					target = self->pc;
					break;
				}
			}
			else if (tok.ty == LS_TOK_NONE)
				break;
		}
	}
	else if (target == LS_ADDR_NULL && backward)
	{
		// Walk backward until we find the label
		for (;;)
		{
			ls_token_t tok;

			if (!_rewind_line(self))
				goto throw;

			// Quickly saving and restoring our location avoids
			// a costly second rewind to undo the ls_lex().
			ls_addr_t bw_pc = self->pc++;
			ls_lex(self, &tok);
			if (tok.ty == LS_TOK_NUM_LABEL)
			{
				if (tok.body.number_val == num)
				{
					target = self->pc;
					break;
				}
			}

			self->pc = bw_pc;
		}
	}

	if (target == LS_ADDR_NULL)
	{
throw:
		self->pc = pc;
		ls_throw_err(self, LS_UNDEFINED_LABEL);
	}
	else
	{
		self->pc = target;
	}
}

void ls_goto_ident(ls_t * self, char const * ident)
{
	ls_value_t * label = NULL;

	for (ls_value_t * i = self->labels; i; i = i->next)
	{
		if (!strncmp(i->body.label.ident, ident, LS_IDENT_LEN))
		{
			label = i;
			break;
		}
	}

	if (!label)
	{
		for (;;)
		{
			ls_token_t tok;
			ls_lex(self, &tok);

			if (tok.ty == LS_TOK_STR_LABEL)
			{
				// New labels are always put at the head,
				// so test there. Don't just read the token
				// in case some processing was done.
				if (!strncmp(self->labels->body.label.ident,
					ident, LS_IDENT_LEN))
				{
					label = self->labels;
					break;
				}
			}
			else if (tok.ty == LS_TOK_NONE)
			{
				break;
			}
		}
	}

	if (!label)
		ls_throw_err(self, LS_UNDEFINED_LABEL);

	self->pc = label->body.label.pc;
}

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

static bool _rewind_line(ls_t * self)
{
	if (self->pc == 0)
		return false;

	ls_addr_t pc = (ls_addr_t)(self->pc - 1);

	for (;;)
	{
		unsigned char ch = ls_fetch(self, pc);

		if (ch == '\n')
		{
			self->pc = pc;
			return true;
		}

		if (pc == 0)
			return false;
		--pc;
	}
}