bcgen.c 1.72 KB
Newer Older
1
#include <ctype.h>
2
#include <stdlib.h>
3 4
#include <string.h>

5 6 7 8 9
#include "bcgen.h"
#include "bcgen_instructions.h"
#include "bcgen_instruction_table.h"
#include "parse_abc.h"
#include "util.h"
Erin van der Veen's avatar
Erin van der Veen committed
10

11
void parse_file(FILE *file) {
12 13
	char* line = NULL;
	size_t size = 0;
14
	int line_number = 0;
15

16
	code_next_module();
17

18
	while (getline(&line, &size, file) > 0) {
Camil Staps's avatar
Camil Staps committed
19
		line_number++;
Camil Staps's avatar
Camil Staps committed
20
		parse_line(line, line_number);
Erin van der Veen's avatar
Erin van der Veen committed
21
	}
22
	free(line);
Erin van der Veen's avatar
Erin van der Veen committed
23 24
}

25
void parse_files(FILE **abc_files, unsigned int nr_abc_files) {
Camil Staps's avatar
Camil Staps committed
26
	init_parser();
Erin van der Veen's avatar
Erin van der Veen committed
27 28 29 30
	init_instruction_table();
	load_instruction_table();

	// Parse every file
Erin van der Veen's avatar
Erin van der Veen committed
31 32
	unsigned int i;
	for(i = 0; i < nr_abc_files; i++) {
33
		parse_file(abc_files[i]);
34
		fclose(abc_files[i]);
Erin van der Veen's avatar
Erin van der Veen committed
35
	}
36 37

	free_instruction_table();
Erin van der Veen's avatar
Erin van der Veen committed
38 39
}

40
int main (int argc, char *argv[]) {
Erin van der Veen's avatar
Erin van der Veen committed
41 42 43 44 45
	if(argc < 2) {
		fprintf(stderr, "Error: No ABC file specified\n");
		return -1;
	}

Camil Staps's avatar
Camil Staps committed
46
	FILE **input_files = safe_malloc(sizeof(FILE*) * (argc-1));
47 48
	unsigned int nr_abc_files = 0;
	FILE *output_file = NULL;
Erin van der Veen's avatar
Erin van der Veen committed
49 50 51

	int i;
	for(i = 1; i < argc; i++) {
52 53 54 55
		if (!strcmp("-V",argv[i])) {
			fprintf(stderr,"%d\n",ABC_VERSION);
			return 0;
		} else if (!strcmp("-o",argv[i]) && i<=argc-1) {
56 57 58 59 60 61
			if((output_file = fopen(argv[i + 1], "wb")) == NULL) {
				fprintf(stderr, "Error: Could not open output file: %s\n", argv[i + 1]);
				return -1;
			}
			i++;
		} else {
Camil Staps's avatar
Camil Staps committed
62
			if((input_files[i-1] = fopen(argv[i], "r")) == NULL) {
63 64 65 66
				fprintf(stderr, "Error: Could not open abc file: %s\n", argv[i]);
				return -1;
			}
			nr_abc_files++;
Erin van der Veen's avatar
Erin van der Veen committed
67 68 69
		}
	}

70 71 72 73
	if (!output_file) {
		fprintf(stderr, "Usage: %s ABC [ABC ...] -o OUTPUT\n", argv[0]);
		return -1;
	}
Erin van der Veen's avatar
Erin van der Veen committed
74 75

	// List of lines per file
76
	initialize_code();
77

78
	parse_files(input_files, nr_abc_files);
Erin van der Veen's avatar
Erin van der Veen committed
79

80
	add_add_arg_labels();
81
	write_program(output_file);
82
	free_generated_program();
83

Erin van der Veen's avatar
Erin van der Veen committed
84 85
	return 0;
}