Beispiel #1
0
void aff(std::string const& str)
{
	put_in_array(str);
}
Command *parser(char *path, int *size_com, int *num_str, int *err)
{
	Command *commands;
	Label *labels;
	int point_com = 0, point_lbl = 0;
	FILE *input;
	*num_str = 0;
	input = fopen(path, "r");
	commands = (Command *)malloc(sizeof(Command) * SIZE_COMMAND);
	labels = (Label *)malloc(sizeof(Label) * SIZE_LABEL);
	while(!feof(input))
	{
		char str[SIZE_STRING], *lexeme;
		int point_str = 0;
		str[0] = 0;
		++(*num_str);
		if ((point_com - 1) % SIZE_COMMAND == 0)
			commands = (Command *)realloc(commands, sizeof(Command) * (point_com + SIZE_COMMAND));
                if ((point_lbl - 1) % SIZE_LABEL == 0)
			labels = (Label *)realloc(labels, sizeof(Label) * (point_lbl + SIZE_LABEL));
		fgets(str, SIZE_STRING - 1, input);
		lexeme = read_word(str, &point_str, err);
		if (*err != 0)
		{
			clean_arrays(commands, point_com, labels, point_lbl);	
			return NULL;
		}
		commands[point_com].opcode = is_command(lexeme);
		if (commands[point_com].opcode == LDC) 
			commands[point_com].number = read_number(str, &point_str, err);
		else if ((commands[point_com].opcode == LD) || (commands[point_com].opcode == ST))
			commands[point_com].address = read_address(str, &point_str, err);
		else if ((commands[point_com].opcode == BR) || (commands[point_com].opcode == JMP))
		{
			commands[point_com].label = read_word(str, &point_str, err);
			if (*err == 1) *err = 5;
			if (*err == 0)
			{
				if (is_command(commands[point_com].label) == LBL)
					put_in_array(labels, &point_lbl, commands[point_com].label, -1, *num_str, err);	
				else *err = 5; 
			}
		}
		else if (commands[point_com].opcode == LBL)
		{
			while ((str[point_str] == ' ') || (str[point_str] == 9)) ++point_str;
			if (str[point_str++] == ':')
				put_in_array(labels, &point_lbl, lexeme, point_com, *num_str, err);
			else *err = 6;
		}
		if (*err != 0)
		{
			clean_arrays(commands, point_com + 1, labels, point_lbl);
			return NULL;
		}
		while ((str[point_str] == ' ') || (str[point_str] == 9)) ++point_str;
		if ((str[point_str] != ';') && (str[point_str] != 0) && (str[point_str] != '\n'))
		{
			*err = 8;
			clean_arrays(commands, point_com + 1, labels, point_lbl);
			return NULL;
		}
		if ((commands[point_com].opcode != LBL) && (commands[point_com].opcode != NUL)) 
		{
			commands[point_com].num_str = *num_str;
			++point_com;
		}
	}  
	replacement(commands, point_com, labels, point_lbl, num_str, err);
	if (*err != 0)
	{
		clean_arrays(commands, point_com, labels, point_lbl);
		return NULL;
	}	
	else 
	{
		*size_com = point_com;
		return commands;
	}	
}