Commit 1b83af11

1b83af115121Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 8 months ago · 10 files · +5 added · ~5 modified

Description

Add --lib= search path for require, stub system, module dedup

  • --lib=DIR flag: resolve require "name" by searching DIR/name.rb
  • Recursive require resolution in required files
  • Deduplication: same file (by realpath) is loaded only once
  • Module dedup: reopened modules (module Lrama repeated 165 times) merge into single module_info instead of creating duplicates
  • stubs/ directory with placeholder stubs for forwardable, set, strscan, optparse, erb

lrama.rb (71 files, 14K lines) now fully parses with --lib=stubs/ and generates compilable C code.

Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]

Details

Commit
1b83af115121
Parent
5e41ec3c1a55

src/codegen.cmodifiedView file

@@ -664,17 +664,35 @@
664664 }
665665
666666 static void analyze_module(codegen_ctx_t *ctx, pm_module_node_t *node) {
667- module_info_t *mod = &ctx->modules[ctx->module_count++];
668- memset(mod, 0, sizeof(*mod));
669- mod->module_node = (pm_node_t *)node;
670-
667+ /* Extract module name first for dedup check */
668+ char mod_name[64] = "";
671669 if (PM_NODE_TYPE(node->constant_path) == PM_CONSTANT_READ_NODE) {
672670 pm_constant_read_node_t *cr = (pm_constant_read_node_t *)node->constant_path;
673671 char *name = cstr(ctx, cr->name);
674- snprintf(mod->name, sizeof(mod->name), "%s", name);
672+ snprintf(mod_name, sizeof(mod_name), "%s", name);
675673 free(name);
676674 }
677675
676+ /* Check if module already exists (e.g., "module Lrama" repeated across files) */
677+ module_info_t *mod = NULL;
678+ for (int i = 0; i < ctx->module_count; i++) {
679+ if (mod_name[0] && strcmp(ctx->modules[i].name, mod_name) == 0) {
680+ mod = &ctx->modules[i];
681+ break;
682+ }
683+ }
684+ if (!mod) {
685+ if (ctx->module_count >= MAX_MODULES) {
686+ fprintf(stderr, "Warning: too many modules (max %d), skipping '%s'\n",
687+ MAX_MODULES, mod_name);
688+ return;
689+ }
690+ mod = &ctx->modules[ctx->module_count++];
691+ memset(mod, 0, sizeof(*mod));
692+ snprintf(mod->name, sizeof(mod->name), "%s", mod_name);
693+ }
694+ mod->module_node = (pm_node_t *)node;
695+
678696 /* Analyze module body for methods and class ivars */
679697 if (!node->body) return;
680698 if (PM_NODE_TYPE(node->body) != PM_STATEMENTS_NODE) return;
@@ -1051,7 +1069,133 @@
10511069 return true;
10521070 }
10531071
1054-/* Process require_relative calls: parse required files and run analysis passes */
1072+/* Check if a node is a require "name" call */
1073+bool is_require(codegen_ctx_t *ctx, pm_node_t *node) {
1074+ if (PM_NODE_TYPE(node) != PM_CALL_NODE) return false;
1075+ pm_call_node_t *call = (pm_call_node_t *)node;
1076+ if (call->receiver) return false;
1077+ if (!ceq(ctx, call->name, "require")) return false;
1078+ if (!call->arguments || call->arguments->arguments.size != 1) return false;
1079+ if (PM_NODE_TYPE(call->arguments->arguments.nodes[0]) != PM_STRING_NODE) return false;
1080+ return true;
1081+}
1082+
1083+/* Try to resolve require "name" via lib search paths.
1084+ * Returns allocated full path string or NULL. */
1085+static char *resolve_require(codegen_ctx_t *ctx, const char *name) {
1086+ for (int i = 0; i < ctx->lib_path_count; i++) {
1087+ char path[PATH_MAX];
1088+ size_t nl = strlen(name);
1089+ if (nl >= 3 && strcmp(name + nl - 3, ".rb") == 0)
1090+ snprintf(path, sizeof(path), "%s/%s", ctx->lib_paths[i], name);
1091+ else
1092+ snprintf(path, sizeof(path), "%s/%s.rb", ctx->lib_paths[i], name);
1093+ FILE *f = fopen(path, "r");
1094+ if (f) {
1095+ fclose(f);
1096+ char *result = malloc(strlen(path) + 1);
1097+ strcpy(result, path);
1098+ return result;
1099+ }
1100+ }
1101+ return NULL;
1102+}
1103+
1104+/* Process a single file: parse, check errors, run class_analysis_pass.
1105+ * Returns index in required_files or -1 on failure. */
1106+static int process_one_require(codegen_ctx_t *ctx, const char *full_path) {
1107+ /* Deduplicate: skip if already required */
1108+ char real[PATH_MAX];
1109+ const char *canon = realpath(full_path, real) ? real : full_path;
1110+ for (int i = 0; i < ctx->required_file_count; i++) {
1111+ if (strcmp(ctx->required_files[i].path, canon) == 0)
1112+ return i; /* already loaded */
1113+ }
1114+
1115+ size_t req_len;
1116+ char *req_source = read_source_file(full_path, &req_len);
1117+ if (!req_source) return -1;
1118+
1119+ if (ctx->required_file_count >= MAX_REQUIRED_FILES) {
1120+ fprintf(stderr, "Warning: too many required files (max %d)\n", MAX_REQUIRED_FILES);
1121+ free(req_source);
1122+ return -1;
1123+ }
1124+
1125+ int idx = ctx->required_file_count++;
1126+ ctx->required_files[idx].source = req_source;
1127+ ctx->required_files[idx].path = xstrdup(canon);
1128+ pm_parser_init(&ctx->required_files[idx].parser,
1129+ (const uint8_t *)req_source, req_len, NULL);
1130+ ctx->required_files[idx].root = pm_parse(&ctx->required_files[idx].parser);
1131+
1132+ if (ctx->required_files[idx].parser.error_list.size > 0) {
1133+ fprintf(stderr, "Parse errors in '%s'\n", full_path);
1134+ ctx->required_file_count--;
1135+ pm_node_destroy(&ctx->required_files[idx].parser, ctx->required_files[idx].root);
1136+ pm_parser_free(&ctx->required_files[idx].parser);
1137+ free(req_source);
1138+ return -1;
1139+ }
1140+
1141+ pm_parser_t *saved_parser = ctx->parser;
1142+ const char *saved_source = ctx->source_path;
1143+ ctx->parser = &ctx->required_files[idx].parser;
1144+ ctx->source_path = full_path;
1145+
1146+ /* Recursively process require/require_relative in the required file */
1147+ pm_node_t *rroot = ctx->required_files[idx].root;
1148+ if (PM_NODE_TYPE(rroot) == PM_PROGRAM_NODE) {
1149+ pm_program_node_t *rprog = (pm_program_node_t *)rroot;
1150+ if (rprog->statements) {
1151+ for (size_t j = 0; j < rprog->statements->body.size; j++) {
1152+ pm_node_t *rs = rprog->statements->body.nodes[j];
1153+ if (is_require_relative(ctx, rs)) {
1154+ pm_call_node_t *rcall = (pm_call_node_t *)rs;
1155+ pm_string_node_t *rpath_node = (pm_string_node_t *)rcall->arguments->arguments.nodes[0];
1156+ const uint8_t *rrel_src = pm_string_source(&rpath_node->unescaped);
1157+ size_t rrel_len = pm_string_length(&rpath_node->unescaped);
1158+ char rrel_path[PATH_MAX];
1159+ snprintf(rrel_path, sizeof(rrel_path), "%.*s", (int)rrel_len, rrel_src);
1160+
1161+ char rdir_buf[PATH_MAX];
1162+ snprintf(rdir_buf, sizeof(rdir_buf), "%s", full_path);
1163+ char *rdir = dirname(rdir_buf);
1164+ char rfull[PATH_MAX];
1165+ size_t rrpl = strlen(rrel_path);
1166+ if (rrpl >= 3 && strcmp(rrel_path + rrpl - 3, ".rb") == 0)
1167+ snprintf(rfull, sizeof(rfull), "%s/%s", rdir, rrel_path);
1168+ else
1169+ snprintf(rfull, sizeof(rfull), "%s/%s.rb", rdir, rrel_path);
1170+ process_one_require(ctx, rfull);
1171+ } else if (is_require(ctx, rs)) {
1172+ pm_call_node_t *rcall = (pm_call_node_t *)rs;
1173+ pm_string_node_t *rpath_node = (pm_string_node_t *)rcall->arguments->arguments.nodes[0];
1174+ const uint8_t *rname_src = pm_string_source(&rpath_node->unescaped);
1175+ size_t rname_len = pm_string_length(&rpath_node->unescaped);
1176+ char rname[256];
1177+ snprintf(rname, sizeof(rname), "%.*s", (int)rname_len, rname_src);
1178+ char *resolved = resolve_require(ctx, rname);
1179+ if (resolved) {
1180+ process_one_require(ctx, resolved);
1181+ free(resolved);
1182+ } else {
1183+ fprintf(stderr, "spinel: warning: unsupported call Kernel#require (%s)\n", rname);
1184+ }
1185+ }
1186+ }
1187+ }
1188+ }
1189+
1190+ /* Run class analysis */
1191+ class_analysis_pass(ctx, ctx->required_files[idx].root);
1192+
1193+ ctx->parser = saved_parser;
1194+ ctx->source_path = saved_source;
1195+ return idx;
1196+}
1197+
1198+/* Process require_relative and require calls at top level */
10551199 static void process_require_relative(codegen_ctx_t *ctx, pm_node_t *root) {
10561200 assert(PM_NODE_TYPE(root) == PM_PROGRAM_NODE);
10571201 pm_program_node_t *prog = (pm_program_node_t *)root;
@@ -1060,72 +1204,49 @@
10601204
10611205 for (size_t i = 0; i < stmts->body.size; i++) {
10621206 pm_node_t *s = stmts->body.nodes[i];
1063- if (!is_require_relative(ctx, s)) continue;
10641207
1065- pm_call_node_t *call = (pm_call_node_t *)s;
1066- pm_string_node_t *path_node =
1067- (pm_string_node_t *)call->arguments->arguments.nodes[0];
1068- const uint8_t *rel_src = pm_string_source(&path_node->unescaped);
1069- size_t rel_len = pm_string_length(&path_node->unescaped);
1208+ if (is_require_relative(ctx, s)) {
1209+ pm_call_node_t *call = (pm_call_node_t *)s;
1210+ pm_string_node_t *path_node =
1211+ (pm_string_node_t *)call->arguments->arguments.nodes[0];
1212+ const uint8_t *rel_src = pm_string_source(&path_node->unescaped);
1213+ size_t rel_len = pm_string_length(&path_node->unescaped);
10701214
1071- /* Build relative path string */
1072- char rel_path[PATH_MAX];
1073- snprintf(rel_path, sizeof(rel_path), "%.*s", (int)rel_len, rel_src);
1215+ char rel_path[PATH_MAX];
1216+ snprintf(rel_path, sizeof(rel_path), "%.*s", (int)rel_len, rel_src);
10741217
1075- /* Resolve full path relative to source file directory */
1076- char dir_buf[PATH_MAX];
1077- snprintf(dir_buf, sizeof(dir_buf), "%s", ctx->source_path);
1078- char *dir = dirname(dir_buf);
1218+ char dir_buf[PATH_MAX];
1219+ snprintf(dir_buf, sizeof(dir_buf), "%s", ctx->source_path);
1220+ char *dir = dirname(dir_buf);
10791221
1080- char full_path[PATH_MAX];
1081- /* Append .rb if not already present */
1082- size_t rpl = strlen(rel_path);
1083- if (rpl >= 3 && strcmp(rel_path + rpl - 3, ".rb") == 0)
1084- snprintf(full_path, sizeof(full_path), "%s/%s", dir, rel_path);
1085- else
1086- snprintf(full_path, sizeof(full_path), "%s/%s.rb", dir, rel_path);
1222+ char full_path[PATH_MAX];
1223+ size_t rpl = strlen(rel_path);
1224+ if (rpl >= 3 && strcmp(rel_path + rpl - 3, ".rb") == 0)
1225+ snprintf(full_path, sizeof(full_path), "%s/%s", dir, rel_path);
1226+ else
1227+ snprintf(full_path, sizeof(full_path), "%s/%s.rb", dir, rel_path);
10871228
1088- /* Read and parse the required file */
1089- size_t req_len;
1090- char *req_source = read_source_file(full_path, &req_len);
1091- if (!req_source) {
1092- fprintf(stderr, "Warning: cannot open require_relative '%s' (%s)\n",
1093- rel_path, full_path);
1094- continue;
1095- }
1229+ if (process_one_require(ctx, full_path) < 0) {
1230+ fprintf(stderr, "Warning: cannot open require_relative '%s' (%s)\n",
1231+ rel_path, full_path);
1232+ }
1233+ } else if (is_require(ctx, s)) {
1234+ pm_call_node_t *call = (pm_call_node_t *)s;
1235+ pm_string_node_t *path_node =
1236+ (pm_string_node_t *)call->arguments->arguments.nodes[0];
1237+ const uint8_t *rel_src = pm_string_source(&path_node->unescaped);
1238+ size_t rel_len = pm_string_length(&path_node->unescaped);
10961239
1097- if (ctx->required_file_count >= MAX_REQUIRED_FILES) {
1098- fprintf(stderr, "Warning: too many require_relative files (max %d)\n",
1099- MAX_REQUIRED_FILES);
1100- free(req_source);
1101- continue;
1102- }
1103-
1104- int idx = ctx->required_file_count++;
1105- ctx->required_files[idx].source = req_source;
1106- pm_parser_init(&ctx->required_files[idx].parser,
1107- (const uint8_t *)req_source, req_len, NULL);
1108- ctx->required_files[idx].root =
1109- pm_parse(&ctx->required_files[idx].parser);
1240+ char name[256];
1241+ snprintf(name, sizeof(name), "%.*s", (int)rel_len, rel_src);
11101242
1111- /* Check for parse errors */
1112- if (ctx->required_files[idx].parser.error_list.size > 0) {
1113- fprintf(stderr, "Parse errors in '%s'\n", full_path);
1114- ctx->required_file_count--;
1115- pm_node_destroy(&ctx->required_files[idx].parser,
1116- ctx->required_files[idx].root);
1117- pm_parser_free(&ctx->required_files[idx].parser);
1118- free(req_source);
1119- continue;
1243+ char *resolved = resolve_require(ctx, name);
1244+ if (resolved) {
1245+ process_one_require(ctx, resolved);
1246+ free(resolved);
1247+ }
1248+ /* If not resolved, silently skip (the require call becomes a no-op) */
11201249 }
1121-
1122- /* Run class analysis on the required file (registers classes/modules/funcs).
1123- * We temporarily swap the parser so cstr/ceq use the required file's
1124- * constant pool. */
1125- pm_parser_t *saved_parser = ctx->parser;
1126- ctx->parser = &ctx->required_files[idx].parser;
1127- class_analysis_pass(ctx, ctx->required_files[idx].root);
1128- ctx->parser = saved_parser;
11291250 }
11301251 }
11311252