aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 9467ace75c89a4dc709969c2aed8f091447fd5b2 (plain)
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
#include <stdio.h>
#include <program.h>
#include <lexer.h>
#include <string.h>
#include <parser.h>
#include <backend.h>
#include <print.h>



int main(int argc,char **argv)
{
	struct Options *options;
	struct Source *source;
	struct Program *program;
	struct Translation_Data *translation_data;
	struct AST_Translation_Unit* translation_unit;
	
	options=parse_command_line(argc,argv);
	if(options->src_name==NULL)
	{
		printf("No source file specified\n");
		return 0;
	}
	source=extract_source(strndup(options->src_name,100));
	translation_data=get_translation_data();

	if(options->target==OPTION_TARGET_TOKENS || options->target==OPTION_TARGET_AST || options->target==OPTION_TARGET_C)
	{
		lex(translation_data->tokens,source,translation_data);
		if(translation_data->errors->size>0)
		{
			printf("There was an error!\n");
			print_tokens(translation_data->tokens);
			return 1;
		}else if(options->target==OPTION_TARGET_TOKENS)
		{
			print_tokens(translation_data->tokens);
		}else if(options->target==OPTION_TARGET_AST || options->target==OPTION_TARGET_C) 
		{
			
			translation_unit=(struct AST_Translation_Unit*)parse_source(translation_data);
			if(has_new_errors(translation_data))
				{ print_errors(translation_data); return 1; }

			anotate_unchecked_states(translation_unit,translation_data);

			if(has_new_errors(translation_data))
				{ print_errors(translation_data); return 1; }
			if(options->target==OPTION_TARGET_AST)
			{
				print_ast((struct AST*)translation_unit);
			}else if(options->target==OPTION_TARGET_C)
			{
				ast_to_c(options->output_name,translation_unit,options);
			}
			delete_ast((struct AST*)translation_unit);
		}

	}else
	{
		assert(!"false");
	}

	delete_source(source);
	delete_options(options);
	return 0;
}