-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
138 lines (120 loc) · 3.21 KB
/
Copy pathmain.c
File metadata and controls
138 lines (120 loc) · 3.21 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
/*
* PL/0 Compiler - Main Entry Point
*
* Usage: pl0c [-t] [source_file]
* -t Enable instruction trace
* file PL/0 source file (default: read from stdin)
*
* Compiles PL/0 source code to bytecode and executes it.
*/
#include "pl0.h"
#include <fcntl.h>
#include <unistd.h>
/*
* Read entire file into memory. Returns heap-allocated string.
* Caller must free the returned pointer.
*/
static char *read_file(const char *path)
{
FILE *fp;
char *content;
long size;
if (path == NULL || strcmp(path, "-") == 0) {
fp = stdin;
} else {
fp = fopen(path, "r");
if (fp == NULL) {
perror("fopen");
return NULL;
}
}
/* Get file size - only for regular files, not stdin */
if (fp == stdin) {
/* For stdin, read chunks until EOF */
size = 0;
size_t buf_size = 4096;
content = malloc(buf_size);
if (content == NULL) {
return NULL;
}
size_t total_read = 0;
char buf[256];
while (fgets(buf, sizeof(buf), fp) != NULL) {
size_t len = strlen(buf);
if (total_read + len >= buf_size) {
buf_size *= 2;
char *new_content = realloc(content, buf_size);
if (new_content == NULL) {
free(content);
return NULL;
}
content = new_content;
}
memcpy(content + total_read, buf, len);
total_read += len;
}
content[total_read] = '\0';
return content;
}
fseek(fp, 0, SEEK_END);
size = ftell(fp);
fseek(fp, 0, SEEK_SET);
/* Allocate buffer + null terminator */
content = malloc(size + 1);
if (content == NULL) {
perror("malloc");
fclose(fp);
return NULL;
}
/* Read entire file */
size_t read_size = fread(content, 1, size, fp);
content[read_size] = '\0';
fclose(fp);
return content;
}
/*
* Print usage information.
*/
static void usage(const char *prog)
{
printf("PL/0 Compiler v1.0\n");
printf("Usage: %s [-t] [source_file]\n", prog);
printf(" -t Enable instruction trace\n");
printf(" file PL/0 source file (default: read from stdin)\n");
}
int main(int argc, char *argv[])
{
const char *filename = NULL;
int trace = 0;
/* Parse arguments */
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-t") == 0) {
trace = 1;
} else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
usage(argv[0]);
return 0;
} else if (argv[i][0] != '-') {
filename = argv[i];
} else {
fprintf(stderr, "Unknown option: %s\n", argv[i]);
usage(argv[0]);
return 1;
}
}
/* Read source file */
char *source = read_file(filename);
if (source == NULL) {
return 1;
}
/* Compile */
Code code;
SymbolTable symbols;
int result = compile(source, &code, &symbols);
free(source);
if (result < 0) {
return 1;
}
printf("Compiled successfully (%d instructions)\n", code.code_count);
/* Run */
return run(&code, trace);
}