Пример #1
0
int		convert_file(char *filename)
{
	t_function	*file;
	char		*line;
	int			ret;
	int			fd;
	int			test;

	g_begin = 0;
	g_lines_tot = 0;
	file = NULL;
	fd = open(filename, O_RDONLY);
	if (fd <= -1)
		return (cant_read_file(filename));
	while ((ret = get_next_line(fd, &line)) == 1)
	{
		if (!(is_valid_line(line, g_lines_tot + 1)))
			return (0);
		test = check_line(line);
		add_command(test, &file, line);
		g_lines_tot++;
	}
	if (ret == -1)
		return (error_while_reading(filename));
	check_file(&file);
	convert_to_bytecode(file, filename);
	return (0);
}
Пример #2
0
/*Validate the tokens in each line and make sure only valid tokens exist*/
int validate_tokens (char *buffer) {
	char tmp[10000];
	char line[500]; //max 500 characters each line
	int i; //buffer counter
	int j; //line pos counter
	int l = 0; //valid line counter
	int k = 0; //tmp char offset
	int ln = 0; //lines counter
	int noerror = 1;


	for (i = 0, j = 0; buffer[i] != '\0'; ++i) {
		if (j > 499) { //buffer overflow
			fputs("Error! Buffer overflow.", stderr);
			exit(-3);
		}
		else if (buffer[i] != ';') {
			line[j++] = buffer[i];
		}
		else {
			line[j] = '\0';
			j = 0;
			
			if (is_valid_line(line)) {
				++l;
				strcpy(&tmp[k], line);
				k += strlen(&tmp[k]);
				tmp[k++] = ';';
			}
			else {
				noerror = 0;
				//log the error to the global error buffer
				sprintf(&error_buffer[error_cnt], "%d: error: unrecognised token `%s`\n", ln + removed_lines, line);
				error_cnt += strlen(&error_buffer[error_cnt]);
			}
			++ln;
		}
	}
	
	removed_lines += ln - l;
	
	strcpy(buffer, tmp);
	return noerror;
}
Пример #3
0
/*Takes a line of valid code and extracts the token from it*/
token scan_one_token (char *line) {
	token tkn;
	char num[100];
	char op;
	int i;
	int s;
	
	if (!is_valid_line(line)) { //if validate_tokens function works as expected this should never happen, just in case
		tkn.type = invalid;
		return tkn;
	}

	for (s = 0; line[s] == ' '; ++s); //drop spaces on the start of line
	op = line[s]; //get the operator character
	
	switch (op) {
		case '+':
			tkn.operation = t_plus;
			break;
		case '-':
			tkn.operation = t_min;
			break;
		case '*':
			tkn.operation = t_mul;
			break;
		case '/':
			tkn.operation = t_div;
			break;
		case '%':
			tkn.operation = t_mod;
			break;
		case '=':
			tkn.operation = t_assign; //or t_end we will find out later
			break;
	}
	
	for (++s; line[s] == ' '; ++s); //drop spaces on the start of line
	
	if (line[s] == '\0') { //then we have a t_end, so...
		tkn.operation = t_end;
		tkn.type = eop;
		//eop has no data
	}
	else if (line[s] >= 'a' && line[s] <= 'z') { //then we have a type of variable
		//operation has been set in the switch
		tkn.type = variable;
		tkn.data.name = line[s];	
	}
	else if (line[s] >= '0' && line[s] <= '9') {
		for (i = 0; line[s] >= '0' && line[s] <= '9'; num[i] = line[s], ++i, ++s); //parse the number
		num[i] = '\0';
		
		//operation has been set in the switch
		tkn.type = literal;
		tkn.data.value = atoi(num);
	}
	else {
		//this should also never happen, just in case
		tkn.type = invalid;
	}
	
	return tkn;
}