-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
582 lines (488 loc) · 16.2 KB
/
main.c
File metadata and controls
582 lines (488 loc) · 16.2 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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdint.h>
#include <stdbool.h>
#include "lexer/lexer.h"
#include "object/object.h"
#include "parser/parser.h"
#include "compiler/compiler.h"
#include "vm/vm.h"
#define BYTECODE_MARKER "MONKEY_BYTECODE"
#include "vm_stub_embed.h"
#define VERSION "0.1.0"
typedef struct {
unsigned char *data;
int length;
} SerializedBytecode;
typedef enum {
CMD_REPL,
CMD_RUN,
CMD_BUILD,
CMD_HELP,
CMD_VERSION,
CMD_INVALID
} CommandType;
typedef struct {
CommandType type;
char *input_file;
char *output_file;
char *error_message;
} ParsedArgs;
// --- Helper to read a file into memory ---
char *readFile(const char *filename) {
FILE *fp = fopen(filename, "rb");
if (!fp) {
return NULL;
}
if (fseek(fp, 0, SEEK_END) != 0) {
fclose(fp);
return NULL;
}
long size = ftell(fp);
if (size < 0) {
fclose(fp);
return NULL;
}
rewind(fp);
char *buffer = malloc(size + 1);
if (!buffer) {
fclose(fp);
return NULL;
}
size_t read_size = fread(buffer, 1, size, fp);
buffer[read_size] = '\0';
fclose(fp);
return buffer;
}
#define CONST_INTEGER 1
#define CONST_STRING 2
#define CONST_COMPILED_FUNCTION 3
#define CONST_BOOLEAN 4
#define CONST_NULL 5
#define CONST_ARRAY 6
#define CONST_HASH 7
// Little-endian encoding/decoding helpers
static void write_le32(unsigned char *buf, uint32_t val) {
buf[0] = val & 0xFF;
buf[1] = (val >> 8) & 0xFF;
buf[2] = (val >> 16) & 0xFF;
buf[3] = (val >> 24) & 0xFF;
}
static void write_le64(unsigned char *buf, uint64_t val) {
buf[0] = val & 0xFF;
buf[1] = (val >> 8) & 0xFF;
buf[2] = (val >> 16) & 0xFF;
buf[3] = (val >> 24) & 0xFF;
buf[4] = (val >> 32) & 0xFF;
buf[5] = (val >> 40) & 0xFF;
buf[6] = (val >> 48) & 0xFF;
buf[7] = (val >> 56) & 0xFF;
}
// Forward declaration
static size_t calculateObjectSize(Object *obj);
// Calculate size needed to serialize an object
static size_t calculateObjectSize(Object *obj) {
if (strcmp(obj->type, "Integer") == 0) {
return 1 + sizeof(int64_t); // tag + value
} else if (strcmp(obj->type, "String") == 0) {
return 1 + sizeof(int32_t) + strlen(obj->string->value); // tag + length + content
} else if (strcmp(obj->type, "Boolean") == 0) {
return 1 + 1; // tag + bool value (1 byte)
} else if (strcmp(obj->type, "Null") == 0) {
return 1; // just tag
} else if (strcmp(obj->type, "Array") == 0) {
size_t size = 1 + sizeof(int32_t); // tag + count
for (int i = 0; i < obj->array->count; i++) {
size += calculateObjectSize(&obj->array->elements[i]);
}
return size;
} else if (strcmp(obj->type, "Hash") == 0) {
size_t size = 1 + sizeof(int32_t); // tag + pair count
// Iterate through all buckets to count pairs
for (int i = 0; i < obj->hash->bucketCount; i++) {
HashEntry *entry = obj->hash->buckets[i];
while (entry != NULL) {
size += calculateObjectSize(&entry->key);
size += calculateObjectSize(&entry->value);
entry = entry->next;
}
}
return size;
} else if (strcmp(obj->type, "CompiledFunction") == 0) {
return 1 + sizeof(int32_t) + obj->compiledFunction->instructionCount + sizeof(int32_t) + sizeof(int32_t);
}
return 0; // unsupported type
}
// Forward declaration
static size_t serializeObject(Object *obj, unsigned char *buf, size_t offset);
// Serialize an object to buffer
static size_t serializeObject(Object *obj, unsigned char *buf, size_t offset) {
if (strcmp(obj->type, "Integer") == 0) {
uint8_t tag = CONST_INTEGER;
memcpy(buf + offset, &tag, 1);
offset += 1;
write_le64(buf + offset, obj->integer->value);
offset += sizeof(int64_t);
} else if (strcmp(obj->type, "String") == 0) {
uint8_t tag = CONST_STRING;
int32_t len = strlen(obj->string->value);
memcpy(buf + offset, &tag, 1);
offset += 1;
write_le32(buf + offset, len);
offset += sizeof(int32_t);
memcpy(buf + offset, obj->string->value, len);
offset += len;
} else if (strcmp(obj->type, "Boolean") == 0) {
uint8_t tag = CONST_BOOLEAN;
memcpy(buf + offset, &tag, 1);
offset += 1;
uint8_t val = obj->boolean->value ? 1 : 0;
memcpy(buf + offset, &val, 1);
offset += 1;
} else if (strcmp(obj->type, "Null") == 0) {
uint8_t tag = CONST_NULL;
memcpy(buf + offset, &tag, 1);
offset += 1;
} else if (strcmp(obj->type, "Array") == 0) {
uint8_t tag = CONST_ARRAY;
memcpy(buf + offset, &tag, 1);
offset += 1;
write_le32(buf + offset, obj->array->count);
offset += sizeof(int32_t);
for (int i = 0; i < obj->array->count; i++) {
offset = serializeObject(&obj->array->elements[i], buf, offset);
}
} else if (strcmp(obj->type, "Hash") == 0) {
uint8_t tag = CONST_HASH;
memcpy(buf + offset, &tag, 1);
offset += 1;
write_le32(buf + offset, obj->hash->size); // number of key-value pairs
offset += sizeof(int32_t);
// Serialize all key-value pairs
for (int i = 0; i < obj->hash->bucketCount; i++) {
HashEntry *entry = obj->hash->buckets[i];
while (entry != NULL) {
offset = serializeObject(&entry->key, buf, offset);
offset = serializeObject(&entry->value, buf, offset);
entry = entry->next;
}
}
} else if (strcmp(obj->type, "CompiledFunction") == 0) {
uint8_t tag = CONST_COMPILED_FUNCTION;
CompiledFunction *fn = obj->compiledFunction;
memcpy(buf + offset, &tag, 1);
offset += 1;
write_le32(buf + offset, fn->instructionCount);
offset += sizeof(int32_t);
memcpy(buf + offset, fn->instructions, fn->instructionCount);
offset += fn->instructionCount;
write_le32(buf + offset, fn->numLocals);
offset += sizeof(int32_t);
write_le32(buf + offset, fn->numParameters);
offset += sizeof(int32_t);
}
return offset;
}
SerializedBytecode serializeBytecode(ByteCode *bc) {
int instr_len = bc->instructionCount;
int const_count = bc->constantsCount;
size_t size = 0;
printf("📦 Preparing serialization...\n");
printf("📐 Instruction count: %d bytes\n", instr_len);
printf("📐 Constant count : %d\n", const_count);
size += sizeof(int32_t); // instruction length
size += instr_len; // instruction bytes
size += sizeof(int32_t); // constant count
for (int i = 0; i < const_count; i++) {
Object *obj = &bc->constants[i];
size_t obj_size = calculateObjectSize(obj);
if (obj_size == 0) {
fprintf(stderr, "⚠️ Skipping unsupported constant[%d] with type: %s\n", i, obj->type);
} else {
size += obj_size;
}
}
printf("🧮 Total size to serialize: %zu bytes\n", size);
unsigned char *buf = malloc(size);
if (!buf) {
fprintf(stderr, "❌ Failed to allocate %zu bytes\n", size);
exit(1);
}
size_t offset = 0;
write_le32(buf + offset, instr_len);
offset += sizeof(int32_t);
memcpy(buf + offset, bc->instructions, instr_len);
offset += instr_len;
write_le32(buf + offset, const_count);
offset += sizeof(int32_t);
for (int i = 0; i < const_count; i++) {
Object *obj = &bc->constants[i];
printf("🔍 Constant[%d] type = '%s'\n", i, obj->type);
size_t old_offset = offset;
offset = serializeObject(obj, buf, offset);
if (offset == old_offset) {
printf(" ⚠️ Unknown type, skipped.\n");
} else {
size_t obj_size = offset - old_offset;
if (strcmp(obj->type, "Integer") == 0) {
printf(" ↳ INTEGER value = %lld (%zu bytes)\n", obj->integer->value, obj_size);
} else if (strcmp(obj->type, "String") == 0) {
printf(" ↳ STRING length = %d, value = \"%s\" (%zu bytes)\n",
(int)strlen(obj->string->value), obj->string->value, obj_size);
} else if (strcmp(obj->type, "Boolean") == 0) {
printf(" ↳ BOOLEAN value = %s (%zu bytes)\n",
obj->boolean->value ? "true" : "false", obj_size);
} else if (strcmp(obj->type, "Null") == 0) {
printf(" ↳ NULL (%zu bytes)\n", obj_size);
} else if (strcmp(obj->type, "Array") == 0) {
printf(" ↳ ARRAY count = %d (%zu bytes)\n", obj->array->count, obj_size);
} else if (strcmp(obj->type, "Hash") == 0) {
printf(" ↳ HASH pairs = %d (%zu bytes)\n", obj->hash->size, obj_size);
} else if (strcmp(obj->type, "CompiledFunction") == 0) {
printf(" ↳ COMPILED_FUNCTION instructions=%d, locals=%d, params=%d (%zu bytes)\n",
obj->compiledFunction->instructionCount, obj->compiledFunction->numLocals,
obj->compiledFunction->numParameters, obj_size);
}
}
}
printf("✅ Final serialized size: %zu bytes\n", offset);
SerializedBytecode sb;
sb.data = buf;
sb.length = offset;
return sb;
}
void buildExecutable(const char *sourcePath, const char *outputPath) {
char *input = readFile(sourcePath);
if (!input) {
fprintf(stderr, "Failed to read %s\n", sourcePath);
exit(1);
}
Lexer *lexer = newLexer(input);
Parser *parser = newParser(lexer);
Program *program = parseProgram(parser);
Compiler *compiler = newCompiler();
compileProgram(compiler, program);
SerializedBytecode sb = serializeBytecode(getByteCode(compiler));
FILE *out = fopen(outputPath, "wb");
if (!out) {
fprintf(stderr, "Cannot open %s for writing\n", outputPath);
exit(1);
}
// Write embedded vm_stub to output binary
fwrite(bin_vm_stub, 1, bin_vm_stub_len, out);
// Write marker + length + bytecode
fwrite(BYTECODE_MARKER, 1, strlen(BYTECODE_MARKER), out);
// Write bytecode length in little-endian format
unsigned char len_bytes[4];
write_le32(len_bytes, sb.length);
fwrite(len_bytes, 1, 4, out);
fwrite(sb.data, 1, sb.length, out);
fclose(out);
chmod(outputPath, 0755);
printf("✅ Built %s\n", outputPath);
}
// --- Run in interpreter mode ---
void runSource(char *input) {
Lexer *lexer = newLexer(input);
Parser *parser = newParser(lexer);
Program *program = parseProgram(parser);
// Check for parser errors
if (parser->errors && parser->errorCount > 0) {
printf("Parser errors found:\n");
for (int i = 0; i < parser->errorCount; i++) {
printf(" %s\n", parser->errors[i]);
}
return;
}
Compiler *compiler = newCompiler();
compileProgram(compiler, program);
ByteCode *bytecode = getByteCode(compiler);
printf("Bytecode generated: %d instructions, %d constants\n",
bytecode->instructionCount, bytecode->constantsCount);
VM *vm = newVM(bytecode);
if (!vm) {
printf("Failed to create VM\n");
return;
}
printf("running vm...\n");
int result = run(vm);
printf("ran vm... (result: %d)\n", result);
printf("Stack pointer: %d\n", vm->sp);
Object *top = stackTop(vm);
if (top == NULL) {
printf("No result on stack\n");
} else {
char *buf = inspect(top);
if (buf == NULL) {
printf("inspect returned NULL\n");
} else {
printf("%s\n", buf);
}
}
}
void repl() {
char line[1024];
printf("Monkey REPL 🐵 — type 'exit' to quit\n");
while (1) {
printf(">> ");
if (!fgets(line, sizeof(line), stdin)) break;
if (strncmp(line, "exit", 4) == 0) break;
runSource(line);
}
}
// --- Print usage information ---
void printUsage(const char *program_name) {
printf("monkeyc programming language v%s\n\n", VERSION);
printf("USAGE:\n");
printf(" %s Start interactive REPL\n", program_name);
printf(" %s <file.mon> Run a MonkeyC script\n", program_name);
printf(" %s build <file.mon> [options] Compile to executable\n", program_name);
printf(" %s help Show this help message\n", program_name);
printf(" %s version Show version information\n\n", program_name);
printf("BUILD OPTIONS:\n");
printf(" -o <output> Specify output filename\n");
printf(" (default: input filename without extension)\n\n");
printf("EXAMPLES:\n");
printf(" %s # Start REPL\n", program_name);
printf(" %s hello.mon # Run hello.mon\n", program_name);
printf(" %s build hello.mon # Compile to 'hello'\n", program_name);
printf(" %s build hello.mon -o app # Compile to 'app'\n", program_name);
}
// --- Print version information ---
void printVersion() {
printf("MonkeyC v%s\n", VERSION);
printf("A fast, compiled programming language\n");
}
// --- Generate default output filename ---
char *getDefaultOutputName(const char *input_file) {
if (!input_file) return NULL;
char *output = malloc(strlen(input_file) + 1);
if (!output) return NULL;
strcpy(output, input_file);
// Remove .mon extension if present
char *dot = strrchr(output, '.');
if (dot && strcmp(dot, ".mon") == 0) {
*dot = '\0';
}
return output;
}
// --- Parse command line arguments ---
ParsedArgs parseArgs(int argc, char **argv) {
ParsedArgs args = {0};
if (argc == 1) {
args.type = CMD_REPL;
return args;
}
if (argc == 2) {
if (strcmp(argv[1], "help") == 0 || strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) {
args.type = CMD_HELP;
} else if (strcmp(argv[1], "version") == 0 || strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-v") == 0) {
args.type = CMD_VERSION;
} else {
// Assume it's a file to run
args.type = CMD_RUN;
args.input_file = argv[1];
}
return args;
}
if (argc >= 3 && strcmp(argv[1], "build") == 0) {
args.type = CMD_BUILD;
args.input_file = argv[2];
// Parse build options
for (int i = 3; i < argc; i++) {
if (strcmp(argv[i], "-o") == 0) {
if (i + 1 < argc) {
args.output_file = argv[i + 1];
i++; // Skip the next argument
} else {
args.type = CMD_INVALID;
args.error_message = "Error: -o option requires an output filename";
return args;
}
} else {
args.type = CMD_INVALID;
args.error_message = "Error: Unknown build option";
return args;
}
}
// Set default output name if not specified
if (!args.output_file) {
args.output_file = getDefaultOutputName(args.input_file);
}
return args;
}
args.type = CMD_INVALID;
args.error_message = "Error: Invalid command. Use 'monkeyc help' for usage information.";
return args;
}
// --- Validate file exists and is readable ---
bool validateInputFile(const char *filename) {
if (!filename) {
fprintf(stderr, "Error: No input file specified\n");
return false;
}
// Check if file exists and is readable
if (access(filename, R_OK) != 0) {
fprintf(stderr, "Error: Cannot read file '%s'\n", filename);
return false;
}
// Check if it's a .mon file (optional but good UX)
const char *ext = strrchr(filename, '.');
if (!ext || strcmp(ext, ".mon") != 0) {
fprintf(stderr, "Warning: '%s' doesn't have a .mon extension\n", filename);
}
return true;
}
int main(int argc, char **argv) {
ParsedArgs args = parseArgs(argc, argv);
switch (args.type) {
case CMD_REPL:
printf("MonkeyC v%s - Interactive REPL\n", VERSION);
printf("Type 'exit' or press Ctrl+C to quit\n\n");
repl();
break;
case CMD_RUN: {
if (!validateInputFile(args.input_file)) {
return 1;
}
char *input = readFile(args.input_file);
if (!input) {
fprintf(stderr, "Error: Failed to read file '%s'\n", args.input_file);
return 1;
}
printf("Running '%s'...\n", args.input_file);
runSource(input);
free(input);
break;
}
case CMD_BUILD: {
if (!validateInputFile(args.input_file)) {
if (args.output_file && args.output_file != args.input_file) {
free(args.output_file);
}
return 1;
}
printf("Building '%s' -> '%s'...\n", args.input_file, args.output_file);
buildExecutable(args.input_file, args.output_file);
printf("✅ Build completed successfully!\n");
if (args.output_file && args.output_file != args.input_file) {
free(args.output_file);
}
break;
}
case CMD_HELP:
printUsage(argv[0]);
break;
case CMD_VERSION:
printVersion();
break;
case CMD_INVALID:
fprintf(stderr, "%s\n\n", args.error_message);
printUsage(argv[0]);
return 1;
}
return 0;
}