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
|
/*
* Copyright (C) 2026 Peter Mikkelsen <petermikkelsen10@gmail.com>
*
* This file is part of aplwc.
*
* Aplwc is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Aplwc is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with aplwc. If not, see <https://www.gnu.org/licenses/>.
*/
#include "aplwc_internal.h"
static int
read_keyword_decl(struct aplwc_module *module, size_t offset)
{
int len;
for(len = 1; (offset + len) < module->tokens->length; len++)
if(module->tokens->tokens[offset+len].tag == TOKEN_TAG_NEWLINE)
break;
return len;
}
static int
read_function_decl(struct aplwc_module *module, size_t offset)
{
int len, ok;
ok = 0;
for(len = 1; (offset + len) < module->tokens->length; len++){
if(module->tokens->tokens[offset+len].tag == TOKEN_TAG_DEL){
ok = 1;
break;
}
}
return ok ? len : -1;
}
static int
read_decl(struct aplwc_module *module, size_t offset)
{
switch(module->tokens->tokens[offset].tag){
case TOKEN_TAG_COMMENT:
case TOKEN_TAG_NEWLINE:
return 0;
case TOKEN_TAG_KEYWORD:
return read_keyword_decl(module, offset);
case TOKEN_TAG_DEL:
return read_function_decl(module, offset);
default:
return -1;
}
}
static enum aplwc_rc
read_decls(struct aplwc_module *module)
{
size_t offset;
int skip;
for(offset = 0; offset < module->tokens->length; offset++){
skip = read_decl(module, offset);
if(skip == -1)
return APLWC_INVALID_SYNTAX;
offset += skip;
}
return APLWC_OK;
}
enum aplwc_rc
load_module(struct aplwc *aplwc, struct string *name, struct token_list *tokens)
{
enum aplwc_rc rc;
struct aplwc_module *module;
MEMPTRS(aplwc, 2);
MEMPTR(0, name);
MEMPTR(1, tokens);
module = memory_alloc_aplwc_module(aplwc);
if(module == NULL){
rc = aplwc_last_error(aplwc);
goto end;
}
module->name = name;
module->tokens = tokens;
module->link = NULL;
rc = read_decls(module);
if(rc == APLWC_OK){
module->link = aplwc->modules;
aplwc->modules = module;
}else
aplwc->last_error = rc;
end:
MEMPTRS_EXIT(aplwc);
return rc;
}
|