double Symbol_table::get_value(string s)
{
	for (int i = 0; i<vars.size(); ++i)
		if (vars[i].name == s) return vars[i].value;
	ts.end();
	error("get: undefined name ",s);
}
double declaration()
{
	Token t = ts.get();
	if (t.kind != VAR) {
		ts.end();
		error ("Valid name expected in declaration");
	}
	string name = t.name;
	Token t2 = ts.get();
	if (t2.kind != '=') {
		ts.end();
		error("'=' missing in declaration of " ,name);
	}
	double d = expression();
	variables.set_value(name, d);
	return d;
}
double primary()
{
	Token t = ts.get();
	switch (t.kind) {
	// Handles expressions in parentheses.
	case '(':
	{	double d = expression();
		t = ts.get();
		if (t.kind != ')') {
			ts.end();
			error("')' expected");
		}
		return d;
	}
	// This handles unary + or -
	case '-':
		return - primary();
	case '+':
		return + primary();
	case NUMBER:
		return t.value;
	case VAR:
		return variables.get_value(t.name);
	case SQRT:	{
		t=ts.get();
		if(t.kind!='(') {
			ts.end();
			error("sqrt: sqrt must be of the form sqrt(expression)");
		}
		ts.unget(t);
		double d = primary();
		if(d<0) {
			ts.end();
			error("sqrt: cannot take square root of a negative number");
		}
		return sqrt(d);
	}
	default:
		ts.end();
		error("primary expected");
	}
}
double term()
{
	double left = power();
	while(true) {
		Token t = ts.get();
		switch(t.kind) {
		case '*':
			left *= power();
			break;
		case '/':
		{	double d = power();
			if (d == 0) {
				ts.end();
				error("divide by zero");
			}
			left /= d;
			break;
		}
		default:
			ts.unget(t);
			return left;
		}
	}
}