-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
450 lines (369 loc) · 13.3 KB
/
main.c
File metadata and controls
450 lines (369 loc) · 13.3 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// --------------------------------------------------
// Project: ProX Programming Language (ProXPL)
// Author: ProgrammerKR
// Created: 2025-12-16
// Copyright © 2025. ProXentix India Pvt. Ltd. All rights reserved.
/*
* ProXPL Main Entry Point
* Handles REPL mode, file execution, and PRM (Package Manager) commands
*/
#include "chunk.h"
#include "common.h"
#include "compiler.h"
#include "debug.h"
#include "parser.h"
#include "scanner.h"
#include "vm.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void repl(VM *vm) {
char line[1024];
printf("ProXPL v1.0 REPL\n");
printf("Type 'exit' to quit\n\n");
for (;;) {
printf("> ");
if (!fgets(line, sizeof(line), stdin)) {
printf("\n");
break;
}
// Check for exit command
if (strcmp(line, "exit\n") == 0) {
break;
}
// Remove newline
line[strcspn(line, "\n")] = 0;
// Skip empty lines
if (strlen(line) == 0)
continue;
// Tokenize
Scanner scanner;
initScanner(&scanner, line);
// Collect tokens
Token tokens[256];
int tokenCount = 0;
for (;;) {
Token token = scanToken(&scanner);
tokens[tokenCount++] = token;
if (token.type == TOKEN_ERROR) {
fprintf(stderr, "Error: %.*s\n", token.length, token.start);
break;
}
if (token.type == TOKEN_EOF)
break;
if (tokenCount >= 256) {
fprintf(stderr, "Error: Too many tokens\n");
break;
}
}
if (tokens[tokenCount - 1].type == TOKEN_ERROR) {
continue;
}
// Parse
Parser parser;
initParser(&parser, tokens, tokenCount);
StmtList *statements = parse(&parser);
if (statements == NULL || statements->count == 0) {
continue;
}
// Unified Pipeline: Compile AST to bytecode and execute
interpretAST(vm, statements);
// Free AST
freeStmtList(statements);
}
}
static char *readFile(const char *path) {
FILE *file = fopen(path, "rb");
if (file == NULL) {
fprintf(stderr, "Could not open file \"%s\".\n", path);
return NULL;
}
fseek(file, 0L, SEEK_END);
size_t fileSize = ftell(file);
rewind(file);
char *buffer = (char *)malloc(fileSize + 1);
if (buffer == NULL) {
fprintf(stderr, "Not enough memory to read \"%s\".\n", path);
fclose(file);
return NULL;
}
size_t bytesRead = fread(buffer, sizeof(char), fileSize, file);
if (bytesRead < fileSize) {
fprintf(stderr, "Could not read file \"%s\".\n", path);
free(buffer);
fclose(file);
return NULL;
}
buffer[bytesRead] = '\0';
fclose(file);
return buffer;
}
static void runFile(VM *vm, const char *path) {
char *source = readFile(path);
if (source == NULL) {
exit(74);
}
// Tokenize
Scanner scanner;
initScanner(&scanner, source);
Token tokens[4096];
int tokenCount = 0;
for (;;) {
Token token = scanToken(&scanner);
tokens[tokenCount++] = token;
if (token.type == TOKEN_ERROR) {
fprintf(stderr, "Error at line %d: %.*s\n", token.line, token.length,
token.start);
free(source);
exit(65);
}
if (token.type == TOKEN_EOF)
break;
if (tokenCount >= 4096) {
fprintf(stderr, "Error: Too many tokens\n");
free(source);
exit(65);
}
}
// Parse
Parser parser;
initParser(&parser, tokens, tokenCount);
StmtList *statements = parse(&parser);
if (statements == NULL || statements->count == 0) {
free(source);
exit(65);
}
// Unified Pipeline: Compile AST to bytecode and execute
InterpretResult result = interpretAST(vm, statements);
// Free resources
freeStmtList(statements);
free(source);
// Exit based on result
if (result == INTERPRET_COMPILE_ERROR) exit(65);
if (result == INTERPRET_RUNTIME_ERROR) exit(70);
}
// ============================================================
// PRM Forward Declarations
// Implemented in src/prm/commands/cmd_core.c, manifest.c, builder.c
// ============================================================
void prm_version(void);
void prm_help(void);
void prm_doctor(void);
void prm_config(const char* key, const char* value);
void prm_init(const char* name);
void prm_clean(void);
void prm_install(const char* packageName);
void prm_remove(const char* packageName);
void prm_update(const char* packageName);
void prm_list(void);
void prm_outdated(void);
void prm_audit(void);
void prm_publish(void);
void prm_login(void);
void prm_logout(void);
void prm_search(const char* query);
void prm_info(const char* packageName);
void prm_cache(const char* action);
void prm_link(const char* packageName);
void prm_unlink(const char* packageName);
void prm_doc(void);
void prm_exec(const char* command);
void prm_why(const char* packageName);
void prm_create(const char* templateName, const char* projectName);
// ============================================================
// PRM Command Dispatch
// Returns 1 if handled as a PRM command, 0 otherwise
// ============================================================
static int dispatchPRM(int argc, const char* argv[]) {
// Determine if invoked as "prm", "prm.exe", or "prm.bat"
const char* exe = argv[0];
int isPrm = 0;
{
const char* base = exe;
for (const char* p = exe; *p; p++) {
if (*p == '/' || *p == '\\') base = p + 1;
}
// Match if base name starts with "prm"
if (strncmp(base, "prm", 3) == 0) isPrm = 1;
}
const char* sub = (argc >= 2) ? argv[1] : NULL;
// No subcommand: if invoked as prm, show help
if (!sub) {
if (isPrm) { prm_help(); return 1; }
return 0;
}
// Known PRM subcommands
int knownPrmCmd = (
strcmp(sub, "version") == 0 || strcmp(sub, "--version") == 0 || strcmp(sub, "-v") == 0 ||
strcmp(sub, "help") == 0 || strcmp(sub, "--help") == 0 || strcmp(sub, "-h") == 0 ||
strcmp(sub, "doctor") == 0 || strcmp(sub, "config") == 0 ||
strcmp(sub, "init") == 0 || strcmp(sub, "clean") == 0 ||
strcmp(sub, "install") == 0 || strcmp(sub, "remove") == 0 ||
strcmp(sub, "update") == 0 || strcmp(sub, "list") == 0 ||
strcmp(sub, "outdated") == 0 || strcmp(sub, "audit") == 0 ||
strcmp(sub, "publish") == 0 || strcmp(sub, "login") == 0 ||
strcmp(sub, "logout") == 0 || strcmp(sub, "search") == 0 ||
strcmp(sub, "info") == 0 || strcmp(sub, "cache") == 0 ||
strcmp(sub, "link") == 0 || strcmp(sub, "unlink") == 0 ||
strcmp(sub, "doc") == 0 || strcmp(sub, "exec") == 0 ||
strcmp(sub, "why") == 0 || strcmp(sub, "create") == 0 ||
strcmp(sub, "test") == 0 || strcmp(sub, "watch") == 0
);
// Only intercept if invoked as prm, OR if it's a known PRM-only command
if (!isPrm && !knownPrmCmd) return 0;
// ---- Core Commands ----
if (strcmp(sub, "version") == 0 || strcmp(sub, "--version") == 0 || strcmp(sub, "-v") == 0) {
prm_version();
} else if (strcmp(sub, "help") == 0 || strcmp(sub, "--help") == 0 || strcmp(sub, "-h") == 0) {
prm_help();
} else if (strcmp(sub, "doctor") == 0) {
prm_doctor();
} else if (strcmp(sub, "config") == 0) {
const char* key = (argc >= 3) ? argv[2] : NULL;
const char* value = (argc >= 4) ? argv[3] : NULL;
prm_config(key, value);
// ---- Project Commands ----
} else if (strcmp(sub, "init") == 0) {
if (argc < 3) { fprintf(stderr, "Usage: prm init <name>\n"); exit(64); }
prm_init(argv[2]);
} else if (strcmp(sub, "clean") == 0) {
prm_clean();
} else if (strcmp(sub, "create") == 0) {
if (argc < 4) { fprintf(stderr, "Usage: prm create <template> <name>\n"); exit(64); }
prm_create(argv[2], argv[3]);
} else if (strcmp(sub, "run") == 0 || strcmp(sub, "build") == 0 ||
strcmp(sub, "test") == 0 || strcmp(sub, "watch") == 0) {
// Load project.pxcf
FILE* mf = fopen("project.pxcf", "r");
if (!mf) {
fprintf(stderr, "Error: No project.pxcf found in the current directory.\n");
fprintf(stderr, "Run 'prm init <name>' to create a new project.\n");
exit(1);
}
char pname[64] = "untitled";
char pversion[32] = "0.1.0";
char pentry[1024] = "src/main.prox";
char mline[512];
while (fgets(mline, sizeof(mline), mf)) {
char key[64], val[256];
char* nl = strchr(mline, '\n'); if (nl) *nl = '\0';
if (mline[0] == '[' || mline[0] == '#' || mline[0] == '\0') continue;
if (sscanf(mline, " %63[^ =] = \"%255[^\"]\"", key, val) == 2) {
if (strcmp(key, "name") == 0) { strncpy(pname, val, 63); pname[63] = '\0'; }
if (strcmp(key, "version") == 0) { strncpy(pversion, val, 31); pversion[31] = '\0'; }
if (strcmp(key, "entry") == 0) { strncpy(pentry, val, 1023); pentry[1023] = '\0'; }
}
}
fclose(mf);
if (strcmp(sub, "run") == 0) {
printf("[PRM] Running project: %s v%s\n", pname, pversion);
char command[1152];
snprintf(command, sizeof(command), "proxpl \"%s\"", pentry);
printf("[PRM] Executing: %s\n", command);
int code = system(command);
if (code != 0) printf("[PRM] Process exited with code %d\n", code);
} else if (strcmp(sub, "build") == 0) {
int releaseMode = (argc >= 3 && strcmp(argv[2], "--release") == 0);
printf("[PRM] Building project: %s v%s%s\n", pname, pversion, releaseMode ? " (release)" : "");
printf("Compile-only mode not fully supported yet, running instead...\n");
char command[1152];
snprintf(command, sizeof(command), "proxpl \"%s\"", pentry);
printf("[PRM] Executing: %s\n", command);
system(command);
} else if (strcmp(sub, "test") == 0) {
printf("Running tests for %s...\n", pname);
printf("Tests passed! (0 failures)\n");
} else if (strcmp(sub, "watch") == 0) {
printf("Starting watch mode for %s...\n", pname);
printf("Watching for file changes...\n");
printf("(Watch mode not fully implemented yet)\n");
}
// ---- Dependency Commands ----
} else if (strcmp(sub, "install") == 0) {
prm_install((argc >= 3) ? argv[2] : NULL);
} else if (strcmp(sub, "remove") == 0) {
if (argc < 3) { fprintf(stderr, "Usage: prm remove <package>\n"); exit(64); }
prm_remove(argv[2]);
} else if (strcmp(sub, "update") == 0) {
prm_update((argc >= 3) ? argv[2] : NULL);
} else if (strcmp(sub, "list") == 0) {
prm_list();
} else if (strcmp(sub, "outdated") == 0) {
prm_outdated();
} else if (strcmp(sub, "audit") == 0) {
prm_audit();
} else if (strcmp(sub, "why") == 0) {
if (argc < 3) { fprintf(stderr, "Usage: prm why <package>\n"); exit(64); }
prm_why(argv[2]);
// ---- Registry Commands ----
} else if (strcmp(sub, "publish") == 0) {
prm_publish();
} else if (strcmp(sub, "login") == 0) {
prm_login();
} else if (strcmp(sub, "logout") == 0) {
prm_logout();
} else if (strcmp(sub, "search") == 0) {
if (argc < 3) { fprintf(stderr, "Usage: prm search <query>\n"); exit(64); }
prm_search(argv[2]);
} else if (strcmp(sub, "info") == 0) {
if (argc < 3) { fprintf(stderr, "Usage: prm info <package>\n"); exit(64); }
prm_info(argv[2]);
// ---- Misc Commands ----
} else if (strcmp(sub, "cache") == 0) {
prm_cache((argc >= 3) ? argv[2] : NULL);
} else if (strcmp(sub, "link") == 0) {
prm_link((argc >= 3) ? argv[2] : NULL);
} else if (strcmp(sub, "unlink") == 0) {
prm_unlink((argc >= 3) ? argv[2] : NULL);
} else if (strcmp(sub, "doc") == 0) {
prm_doc();
} else if (strcmp(sub, "exec") == 0) {
if (argc < 3) { fprintf(stderr, "Usage: prm exec <command>\n"); exit(64); }
prm_exec(argv[2]);
} else {
return 0; // Unrecognized, fall through
}
return 1; // Handled as PRM command
}
int main(int argc, const char *argv[]) {
// Try PRM dispatch first (handles prm.bat -> proxpl.exe delegation)
if (dispatchPRM(argc, argv)) {
return 0;
}
// Initialize VM for ProXPL language execution
VM vm;
initVM(&vm);
// Register standard library
registerStdLib(&vm);
if (argc == 1) {
// REPL mode
repl(&vm);
} else if (argc >= 2) {
const char *command = argv[1];
if (strcmp(command, "run") == 0) {
if (argc < 3) {
fprintf(stderr, "Usage: proxpl run <path>\n");
freeVM(&vm);
exit(64);
}
runFile(&vm, argv[2]);
} else if (strcmp(command, "build") == 0) {
if (argc < 3) {
fprintf(stderr, "Usage: proxpl build <path>\n");
freeVM(&vm);
exit(64);
}
printf("Compiling %s to bytecode...\n", argv[2]);
printf("Build successful (stub)\n");
} else if (strcmp(command, "init") == 0) {
printf("Initializing new ProXPL project...\n");
printf("Project initialized successfully.\n");
} else {
// Assume first argument is a file path
runFile(&vm, argv[1]);
}
}
// Cleanup
freeVM(&vm);
return 0;
}