Beispiel #1
0
bool process_wire_statements(evl_wires &wires, evl_statement &s) {
	assert(s.type == evl_statement::WIRE);
	
	enum state_type {INIT, WIRE,IO, WIRES, WIRE_NAME, BUS, BUS_MSB, BUS_COLON, BUS_LSB, BUS_DONE, DONE};

	state_type state = INIT;
	int bus_width = 1;
	for (; !s.tokens.empty() && (state != DONE); s.tokens.pop_front()) {
		evl_token t = s.tokens.front();
		if (state == INIT) {
			
			if ((t.str == "INPUT") || (t.str == "OUTPUT")) {
				state = IO;
			}
			else {
				if (t.type == evl_token::NAME) {
					evl_wires::iterator it = wires.find(t.str);
					if(it != wires.end()) {
						std::cerr << "Wire '" << t.str << "' on line " << t.line_no << " is already defined" << std::endl;
						return false;
					}
					wires.insert(std::make_pair(t.str, bus_width));
					state = WIRE_NAME;
				}
			}
		}
		else if (state == IO) {
			if (t.type == evl_token::SINGLE) {
				if(t.str != "("){
					std::cerr << "Need a '(' but found '" << t.str << "' on line" << t.line_no << std::endl;
					return false;
				}
				state = WIRE;
			}
			else {
				std::cerr << "Need Input or Output designation but found '" << t.str << "' on line " << t.line_no << std::endl;
				return false;		
			}
		}
		else if (state == WIRE) {
			if (t.type == evl_token::NAME) {
				evl_wires::iterator it = wires.find(t.str);
				if(it != wires.end()) {
					std::cerr << "Wire '" << t.str << "' on line " << t.line_no << " is already defined" << std::endl;
					return false;
				}
				wires.insert(std::make_pair(t.str, bus_width));
				state = WIRE_NAME;
			}
			else {
				std::cerr << "Need 'wire' but found '" << t.str << "' on line " << t.line_no << std::endl;
				return false;
			}
		}
		else if (state == WIRE_NAME) {
			if ((t.str == ")") || (t.str == "=")) {
				state = DONE;
			}
			else {
				std::cerr << "Need ',' or ';' but found '" << t.str << "' on line " << t.line_no << std::endl;
				return false;
			}
		}
		else {
			assert(false); //never should get here
		}
	}
	if (!s.tokens.empty() || (state != DONE)) {
		std::cerr << "something wrong with the statement in wires" << std::endl;
		return false;
	}

	return true;
}
bool process_wire_statements(evl_wires &wires, evl_statement &s) {
	assert(s.type == evl_statement::WIRE);
	
	enum state_type {INIT, WIRE, WIRES, WIRE_NAME, BUS, BUS_MSB, BUS_COLON, BUS_LSB, BUS_DONE, DONE};

	state_type state = INIT;
	int bus_width = 1;
	for (; !s.tokens.empty() && (state != DONE); s.tokens.pop_front()) {
		evl_token t = s.tokens.front();
		if (state == INIT) {
			if (t.str == "wire") {
				state = WIRE;
			}
			else {
				std::cerr << "Need 'wire' but found '" << t.str << "' on line " << t.line_no << std::endl;
				return false;
			}
		}
		else if (state == WIRE) {
			if (t.type == evl_token::NAME) {
				evl_wires::iterator it = wires.find(t.str);
				if(it != wires.end()) {
					std::cerr << "Wire '" << t.str << "' on line " << t.line_no << " is already defined" << std::endl;
					return false;
				}
				wires.insert(std::make_pair(t.str, bus_width));
				state = WIRE_NAME;
			}
			else if (t.type == evl_token::SINGLE) {
				if(t.str != "["){
					std::cerr << "Need a '[' buf found '" << t.str << "' on line" << t.line_no << std::endl;
					return false;
				}
				state = BUS;
			}
			else {
				std::cerr << "Need 'wire' but found '" << t.str << "' on line " << t.line_no << std::endl;
				return false;
			}
		}
		else if (state == WIRES) {
				if (t.type == evl_token::NAME) {
					evl_wires::iterator it = wires.find(t.str);
					if(it != wires.end()) {
						std::cerr << "Wire '" << t.str << "' on line " << t.line_no << " is already defined" << std::endl;
						return false;
					}
					wires.insert(std::make_pair(t.str, bus_width));
					state = WIRE_NAME;
				}
			
		}
		else if (state == BUS){
			if(t.type == evl_token::NUMBER) {
				bus_width = atoi(t.str.c_str())+1;
				if(bus_width < 2) {
					std::cerr << "Bus width entered is " << bus_width << " but needs a bus width of at least 2. Line No: " << t.line_no << std::endl;
					return false;
				}
			}
			else {
				std::cerr << "Need 'number' but found '" << t.str << "' on line " << t.line_no << std::endl;
				return false;
			}
			state = BUS_MSB;
		}
		else if (state == BUS_MSB) {
			if(t.str != ":") {
				std::cerr << "Need a ':' buf found '" << t.str << "' on line " << t.line_no << std::endl;
				return false;
			}
			state = BUS_COLON;

		}
		else if (state == BUS_COLON) {
			if(t.type == evl_token::NUMBER) {
				if(t.str != "0"){
					std::cerr << "Need a '0' buf found '" << t.str << "' on line " << t.line_no << std::endl;
					return false;
				}
				state = BUS_LSB;
			}
		}
		else if (state == BUS_LSB) {
			if(t.type == evl_token::SINGLE) {
				if (t.str != "]") {
					std::cerr << "Need a ']' buf found '" << t.str << "' on line" << t.line_no << std::endl;
					return false;
				}
				state = BUS_DONE;
			}

		}
		else if (state == BUS_DONE) {
			wires.insert(std::make_pair(t.str, bus_width));
	
			state = WIRE_NAME;
		}


		else if (state == WIRE_NAME) {
			if (t.str == ",") {
				state = WIRES;
			}
			else if (t.str == ";") {
				state = DONE;
			}
			else {
				std::cerr << "Need ',' or ';' but found '" << t.str << "' on line " << t.line_no << std::endl;
				return false;
			}
		}
		else {
			assert(false); //never should get here
		}
	}
	if (!s.tokens.empty() || (state != DONE)) {
		std::cerr << "something wrong with the statement in wires" << std::endl;
		return false;
	}

	return true;
}