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
|
#include "../includes/compiler.h"
#include "../includes/test.h"
#include <malloc.h>
#include <stdio.h>
#include <string.h>
string convert_filename(string filename)
{
size_t sz_filename = strnlen(filename, 1024);
int cur = sz_filename - 3;
string new_filename = malloc(sizeof(*new_filename) * (sz_filename + 2));
strncpy(new_filename, filename, sz_filename - 3);
strncpy(new_filename + cur, ".html", 5);
return new_filename;
}
int main(int argc, char *argv[])
{
if (argc > 1)
{ // Test or file
if (strcmp(argv[1], "--test") == 0)
{
fprintf(stderr, "Running tests\n");
exit(0);
}
bool free_output = false;
string filename;
string output;
if (argc >= 3)
{
filename = argv[1];
output = argv[2];
}
else if (argc == 2)
{
filename = argv[1];
output = convert_filename(filename);
free_output = true;
}
else
{
fputs("No inputs given for files\n", stderr);
exit(1);
}
FILE *fp = fopen(filename, "r");
if (fp != NULL)
{
int sz_lines = nlines(fp);
printf("Lines: %d\n", sz_lines);
string *compiled_lines = compile_file(fp, filename, sz_lines);
puts("Compiled document");
fclose(fp);
fp = fopen(output, "w");
for (int line = 0; line < sz_lines; ++line)
{
fprintf(fp, "%s\n", compiled_lines[line]);
free(compiled_lines[line]);
}
fclose(fp);
printf("Written to file (%s)\n", output);
if (free_output)
free(output);
free(compiled_lines);
}
else
{
fprintf(stderr, "%s is not a file\n", filename);
exit(1);
}
}
else
{ // Repl
while (true)
{
string input, compiled_input;
input = malloc(sizeof(*input) * 1024);
fputs("> ", stdout);
fgets(input, 1024, stdin);
compiled_input = compile_line(input, strlen(input), "<stdin>");
fputs(compiled_input, stdout);
free(input);
free(compiled_input);
}
}
}
|