aboutsummaryrefslogtreecommitdiffstats
path: root/program.c
blob: c2140c16ff60655c53a558937bc98bd4b27afaa5 (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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#ifndef PROGRAM_C
#define PROGRAM_C
#include<program.h>

struct Source* extract_source(char *src_name)
{
	FILE *file;

	struct Source *ret;
	
	file=fopen(src_name,"r");
	if(file==NULL)
		return NULL;
	if(fseek(file,0L,SEEK_END)!=0)
		return NULL;

	ret=malloc(sizeof(struct Source));
	ret->src_size=ftell(file);
	ret->where_in_src=0;
	ret->src_name=src_name;
	ret->src=malloc(ret->src_size);
	ret->current_column=0;
	ret->current_row=0;

	fseek(file,0L,SEEK_SET);


	fread(ret->src,sizeof(char),ret->src_size,file);

	fclose(file);
	return ret;
}
struct Options* parse_command_line(char **argv)
{
	struct Options *ret;
	size_t i;

	ret=malloc(sizeof(struct Options));
	ret->print_tokens=1;
	ret->source=argv[1];
	return ret;
}
struct Translation_Data* get_translation_data()
{
	struct Translation_Data *ret;
	ret=malloc(sizeof(struct Translation_Data));
	ret->errors=malloc(sizeof(struct Queue));
	ret->tokens=malloc(sizeof(struct Queue));
	
	Queue_Init(ret->errors);
	Queue_Init(ret->tokens);

	ret->hold_number_of_errors=0;

	return ret;
}
struct Error* get_error(char *message,size_t row,size_t column)
{
	struct Error *ret;
	ret=malloc(sizeof(struct Error));
	ret->message=message;
	ret->row=row;
	ret->column=column;
}
void push_lexing_error(char *error_message,struct Source *src,struct Translation_Data *translation_data)
{
	Queue_Push(translation_data->errors,get_error(error_message,src->current_row,src->current_column));
}
void push_parsing_error(char *error_message,struct token *token ,struct Translation_Data *translation_data)
{
	Queue_Push(translation_data->errors,get_error(error_message,token->row,token->column));
}
char has_new_errors(struct Translation_Data *translation_data)
{
	if(translation_data->hold_number_of_errors!=translation_data->errors->size)
	{
		translation_data->hold_number_of_errors=translation_data->errors->size;
		return 1;
	}else
	{
		return 0;
	}
}

void delete_translation_data(struct Translation_Data *data)
{
	struct Error *hold_error;
	struct token *hold_token;

	while(data->tokens->size>0)
		delete_token(Queue_Pop(data->tokens));
	free(data->tokens);
	while(data->errors->size>0)
		delete_error(Queue_Pop(data->errors));
	free(data->errors);

	free(data);
}
void delete_source(struct Source *src)
{
	free(src->src_name);
	free(src->src);
	free(src);
}
void delete_options(struct Options *options)
{
	free(options);
}
void delete_error(struct Error *error)
{
	free(error->message);
	free(error);
}
#endif