-
Notifications
You must be signed in to change notification settings - Fork 1
/
FACT_file.c
70 lines (60 loc) · 1.88 KB
/
FACT_file.c
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
/* This file is part of FACT.
*
* FACT is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FACT is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with FACT. If not, see <http://www.gnu.org/licenses/>.
*/
#include "FACT.h"
#include "FACT_vm.h"
#include "FACT_error.h"
#include "FACT_alloc.h"
#include "FACT_comp.h"
#include <stdio.h>
#include <stdlib.h>
int
FACT_load_file(const char *file_name) /* Load a file into the VM. */
{
int c;
FILE *fp;
char *file;
size_t i;
FACT_tree_t parsed;
FACT_lexed_t tokenized;
/* Allocate the entire file into memory. */
fp = fopen (file_name, "r"); /* Open the file for reading. */
if (fp == NULL)
FACT_throw_error (CURR_THIS, "could not open file %s", file_name);
for (file = NULL, i = 0; (c = getc (fp)) != EOF; i++) {
if (c == '\0') /* Put some more bounds here. */
i--;
else {
file = FACT_realloc (file, (i + 2));
file[i] = c;
}
}
if (file != NULL) {
/* NUL terminator. */
file[i] = '\0';
/* Tokenize, parse, compile and load. */
tokenized = FACT_lex_string (file);
tokenized.line = 1;
if (setjmp (tokenized.handle_err)) {
/* There was a parsing error, print it and return fail. */
printf ("=> Parsing error (%s:%zu): %s.\n", file_name, tokenized.line, tokenized.err);
return -1;
}
parsed = FACT_parse (&tokenized);
FACT_compile (parsed, file_name, false);
}
fclose (fp);
return 0;
}