bcgen.c 1.49 KB
Newer Older
1
#include <ctype.h>
Camil Staps's avatar
Camil Staps committed
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

Erin van der Veen's avatar
Erin van der Veen committed
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

Camil Staps's avatar
Camil Staps committed
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
	}
Camil Staps's avatar
Camil Staps committed
22
	free(line);
Erin van der Veen's avatar
Erin van der Veen committed
23 24
}

Erin van der Veen's avatar
Erin van der Veen committed
25
int main (int argc, char *argv[]) {
Erin van der Veen's avatar
Erin van der Veen committed
26 27 28 29 30
	if(argc < 2) {
		fprintf(stderr, "Error: No ABC file specified\n");
		return -1;
	}

31
	FILE *input_file=NULL;
32
	FILE *output_file = NULL;
Erin van der Veen's avatar
Erin van der Veen committed
33 34 35

	int i;
	for(i = 1; i < argc; i++) {
36 37 38 39
		if (!strcmp("-V",argv[i])) {
			fprintf(stderr,"%d\n",ABC_VERSION);
			return 0;
		} else if (!strcmp("-o",argv[i]) && i<=argc-1) {
40 41 42 43 44 45
			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 {
46 47 48 49 50
			if (input_file!=NULL){
				fprintf (stderr,"Error: At most one ABC file can be specified\n");
				return -1;
			} else if((input_file=fopen (argv[i],"r"))==NULL) {
				fprintf (stderr,"Error: Could not open abc file: %s\n",argv[i]);
51 52
				return -1;
			}
Erin van der Veen's avatar
Erin van der Veen committed
53 54 55
		}
	}

56
	if (!output_file) {
57
		fprintf(stderr, "Usage: %s ABC -o OUTPUT\n", argv[0]);
58 59
		return -1;
	}
Erin van der Veen's avatar
Erin van der Veen committed
60

61
	initialize_code();
62 63 64 65 66
	init_parser();
	init_instruction_table();
	load_instruction_table();
	parse_file (input_file);
	free_instruction_table();
Erin van der Veen's avatar
Erin van der Veen committed
67

68
	add_add_arg_labels();
69
	write_program(output_file);
70
	free_generated_program();
71

Erin van der Veen's avatar
Erin van der Veen committed
72 73
	return 0;
}