aboutsummaryrefslogtreecommitdiff
path: root/src/ls_kw_impl_GOSUB_RETURN.c
blob: fa4b3dabbbea441689cb5d2f3d36ff5bf1f84d9b (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
// 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_kws.h"
#include "ls_expr.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_GOSUB(ls_t * self)
{
	self->callstack->body.sctx_call.pc = self->pc;

	ls_token_t tok;
	ls_lex(self, &tok);

	if (tok.ty != LS_TOK_WORD)
		ls_throw_err(self, LS_SYNTAX_ERROR);

	char ident[LS_IDENT_LEN];
	memcpy(ident, tok.body.word_val, LS_IDENT_LEN);

	ls_value_t * frame = ls_alloc(self);
	*frame = (ls_value_t) {
		.ty = LS_TY_SCTX_CALL,
		.prev = self->callstack,
		.next = NULL,
		.body.sctx_call = {
			.pc = LS_ADDR_NULL,
			.readptr = LS_ADDR_NULL,
		},
	};

	self->callstack = frame;
	self->callstack->body.sctx_call.pc = self->pc;

	ls_lex(self, &tok);
	if (tok.ty == LS_TOK_OPERATOR && tok.body.oper_val == OPER_LPAREN)
	{
		for (;;)
		{
			ls_lex(self, &tok);

			if (tok.ty == LS_TOK_WORD)
			{
				char arg_ident[LS_IDENT_LEN];
				memcpy(arg_ident, tok.body.word_val,
					LS_IDENT_LEN);

				ls_lex(self, &tok);
				if (!(tok.ty == LS_TOK_OPERATOR &&
					tok.body.oper_val == OPER_EQ))
					ls_throw_err(self, LS_SYNTAX_ERROR);

				ls_value_t val;
				ls_eval_expr(self, &val, NULL);

				ls_value_t * var = ls_new_var(self, arg_ident);
				ls_write_var(self, var, &val);
			}
			else if (tok.ty == LS_TOK_COMMA)
				continue;
			else if (tok.ty == LS_TOK_OPERATOR
				&& tok.body.oper_val == OPER_RPAREN)
				break;
			else
				ls_throw_err(self, LS_SYNTAX_ERROR);
		}
		ls_lex(self, &tok);
	}

	// AS is parsed by RETURN, just check that the line is actually valid
	if (tok.ty == LS_TOK_KEYWORD && tok.body.keyword_val == LS_KW_AS)
	{
		ls_lex(self, &tok);
		if (tok.ty != LS_TOK_WORD)
			ls_throw_err(self, LS_SYNTAX_ERROR);

		ls_lex(self, &tok);
	}

	if (tok.ty != LS_TOK_STATEMENT_SEP && tok.ty != LS_TOK_NONE)
		ls_throw_err(self, LS_SYNTAX_ERROR);

	ls_goto_ident(self, ident);
}

void ls_kw_fun_RETURN(ls_t * self)
{
	ls_token_t tok;
	ls_value_t val = {.ty = LS_TY_INT, .body.integer.value = 0};
	ls_lex(self, &tok);

	if (!LS_TOK_EOS(tok))
		ls_eval_expr(self, &val, &tok);

	// Free the scope
	ls_value_t * next;
	for (ls_value_t * i = self->callstack->next; i; i = next)
	{
		next = i->next;
		ls_free(self, i);
	}

	ls_value_t * frame = self->callstack->prev;
	ls_addr_t pc = self->callstack->body.sctx_call.pc;
	ls_free(self, self->callstack);
	self->callstack = frame;
	self->pc = pc;

	while (!LS_TOK_EOS(tok))
	{
		if (tok.ty == LS_TOK_KEYWORD
			&& tok.body.keyword_val == LS_KW_AS)
		{
			ls_lex(self, &tok);
			if (tok.ty != LS_TOK_WORD)
				// This was already checked!
				ls_throw_err(self, LS_INTERNAL_ERROR);
			ls_value_t * var = ls_find_var(self, tok.body.word_val);
			if (!var)
				var = ls_new_var(self, tok.body.word_val);
			ls_write_var(self, var, &val);
		}
		ls_lex(self, &tok);
	}
}

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