/* * Copyright (C) 2026 Peter Mikkelsen * * 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 . */ #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; for(len = 1; (offset + len) < module->tokens->length; len++) if(module->tokens->tokens[offset+len].tag == TOKEN_TAG_DEL) break; return len; } static int read_decl(struct aplwc_module *module, size_t offset) { switch(module->tokens->tokens[offset].tag){ 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; } struct aplwc_module * module_lookup(struct aplwc *aplwc, struct string *name) { struct aplwc_module *m; for(m = aplwc->modules; m; m = m->link){ if(string_compare(m->name, name) == 0) break; } return m; }