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

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

// Supporting modules
#include "ls_internal.h"
#include "ls_expr.h"
#include "ls_kws.h"
#include "ls_lex.h"
#include "ls_goto.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 --------------------------------------------------------

void ls_kw_fun_GOTO(ls_context_t * ctx)
{
	// Allowed syntax:
	// GOTO ident
	// GOTO +num
	// GOTO -num
	ls_token_t tok;
	ls_lex(ctx, &tok);

	bool backward = false;

	switch (tok.ty)
	{
	case LS_TOK_WORD:
		ls_goto_ident(ctx, tok.body.word_val);
		break;
	case LS_TOK_OPERATOR:
		if (tok.body.oper_val == OPER_SUB)
			backward = true;
		else if (tok.body.oper_val != OPER_ADD)
			ls_throw_err(ctx, LS_SYNTAX_ERROR);

		ls_lex(ctx, &tok);
		if (tok.ty != LS_TOK_NUMBER)
			ls_throw_err(ctx, LS_SYNTAX_ERROR);

		if (tok.body.number_val > UINT16_MAX
			|| tok.body.number_val < 0)
			ls_throw_err(ctx, LS_SYNTAX_ERROR);

		ls_goto_num(ctx, backward, (uint16_t)(tok.body.number_val));
		break;
	default:
		ls_throw_err(ctx, LS_SYNTAX_ERROR);
	}
}

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