summaryrefslogtreecommitdiff
path: root/src/parser
diff options
context:
space:
mode:
authorZuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian>2026-01-15 12:11:02 +0000
committerZuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian>2026-01-15 12:11:02 +0000
commit7411d71fbde5d652f04cc8851ed93bd15513968b (patch)
tree2fe1e8fa9a604dc558a0528d8f465bef8b597aab /src/parser
parent1925cf6f1a8c134cbfae728023ac325f03e4c14f (diff)
Some docs for plugins, among other things.
Diffstat (limited to 'src/parser')
-rw-r--r--src/parser/parser_stmt.c16
-rw-r--r--src/parser/parser_utils.c35
2 files changed, 50 insertions, 1 deletions
diff --git a/src/parser/parser_stmt.c b/src/parser/parser_stmt.c
index 227b5aa..24dfa10 100644
--- a/src/parser/parser_stmt.c
+++ b/src/parser/parser_stmt.c
@@ -3551,6 +3551,22 @@ ASTNode *parse_import(ParserContext *ctx, Lexer *l)
strncpy(plugin_name, plugin_tok.start + 1, name_len);
plugin_name[name_len] = '\0';
+ if (plugin_name[0] == '.' &&
+ (plugin_name[1] == '/' || (plugin_name[1] == '.' && plugin_name[2] == '/')))
+ {
+ char *current_dir = xstrdup(g_current_filename);
+ char *last_slash = strrchr(current_dir, '/');
+ if (last_slash)
+ {
+ *last_slash = 0;
+ char resolved_path[1024];
+ snprintf(resolved_path, sizeof(resolved_path), "%s/%s", current_dir, plugin_name);
+ free(plugin_name);
+ plugin_name = xstrdup(resolved_path);
+ }
+ free(current_dir);
+ }
+
// Check for optional "as alias"
char *alias = NULL;
Token as_tok = lexer_peek(l);
diff --git a/src/parser/parser_utils.c b/src/parser/parser_utils.c
index 490ea42..2e2fb5b 100644
--- a/src/parser/parser_utils.c
+++ b/src/parser/parser_utils.c
@@ -1,5 +1,6 @@
#include "../codegen/codegen.h"
+#include "../plugins/plugin_manager.h"
#include "parser.h"
#include <ctype.h>
#include <stdio.h>
@@ -2581,8 +2582,40 @@ char *find_similar_symbol(ParserContext *ctx, const char *name)
void register_plugin(ParserContext *ctx, const char *name, const char *alias)
{
+ // Try to find existing (built-in) or already loaded plugin
+ ZPlugin *plugin = zptr_find_plugin(name);
+
+ // If not found, try to load it dynamically
+ if (!plugin)
+ {
+ plugin = zptr_load_plugin(name);
+
+ if (!plugin)
+ {
+ char path[1024];
+ snprintf(path, sizeof(path), "%s.so", name);
+ plugin = zptr_load_plugin(path);
+ }
+
+ if (!plugin && !strchr(name, '/'))
+ {
+ char path[1024];
+ snprintf(path, sizeof(path), "./%s.so", name);
+ plugin = zptr_load_plugin(path);
+ }
+ }
+
+ if (!plugin)
+ {
+ fprintf(stderr,
+ COLOR_RED "Error:" COLOR_RESET " Could not load plugin '%s'\n"
+ " Tried built-ins and dynamic loading (.so)\n",
+ name);
+ exit(1);
+ }
+
ImportedPlugin *p = xmalloc(sizeof(ImportedPlugin));
- p->name = xstrdup(name);
+ p->name = xstrdup(plugin->name); // Use the plugin's internal name
p->alias = alias ? xstrdup(alias) : NULL;
p->next = ctx->imported_plugins;
ctx->imported_plugins = p;