/* getword: get next word from input, return first character or EOF */
int getword(char *word, int lim)
{
	int c, getch();
	void ungetch(int c);
	char *w = word;

	while (isspace(c = getch()))
		;
	*w++ = c;
	if (issingle(c) ) {
		*w = '\0';
		return c;
	}
	for ( ; --lim > 0; w++) {
		if (isspace(*w = getch())) {
			ungetch(*w);
			break;
		}
	}
	*w = '\0';
	return word[0];
}
Example #2
0
bool extract_tokens_from_line(std::string line, int line_no, evl_tokens &tokens) { // use reference to modify it
	for (size_t i = 0; i < line.size();) {
		// comments
		if (line[i] == '/') {
			++i;
			if ((i == line.size()) || (line[i] != '/')) {
				std::cerr << "LINE " << line_no << ": a single / is not allowed" << std::endl;
				return false;
			}
			break; // skip the rest of the line by exiting the loop
		}
		// comments part 2
		else if (line[i] == '#') {
			break; //Comment Detected, skip to next line
		}

		// spaces
		else if (isspace(line[i]))
		{
			++i; // skip this space character
			continue; // skip the rest of the iteration
		}

		// SINGLE
		else if (issingle(line[i])) {
			evl_token token;
			token.line_no = line_no;
			token.type = evl_token::SINGLE;
			token.str = std::string(1, line[i]);
			tokens.push_back(token);
			++i;										// we consumed this character
			continue;									 // skip the rest of the iteration
		}

		// NAME
		else if (isname(line[i])) {
			size_t name_begin = i;
			for (++i; i < line.size(); ++i)
			{
				if (!(isname(line[i]) || isdigit(line[i]))) {
					break;
				}
			}
			evl_token token;
			token.line_no = line_no;
			token.type = evl_token::NAME;
			token.str = std::string(line.substr(name_begin, i-name_begin));
			tokens.push_back(token);
		}
			
		// NUMBER
		else if (isdigit(line[i])){
			size_t number_begin = i;
			for(++i; i < line.size(); ++i)
			{
				if (isname(line[i])){
					std::cerr << "LINE " << line_no << ": invalid character" << std::endl;
					return false;
				}
				else if (issingle(line[i]) || !((line[i]) || isname(line[i]))) {
					break;
				}
			}
			evl_token token;
			token.line_no = line_no;
			token.type = evl_token::NUMBER;
			token.str = std::string(line.substr(number_begin, i-number_begin));
			tokens.push_back(token);
		}
		else
		{
			std::cerr << "LINE " << line_no << ": invalid character" << std::endl;
			return false;
		}
	}
	return true;
}