class Token {

    static final int PROGRAM   = 0;
    static final int STRING    = 1;
    static final int TURN      = 2;
    static final int DEGREES   = 3;
    static final int FORWARD   = 4;
    static final int TIMES     = 5;
    static final int DO        = 6;
    static final int NUMBER    = 7;
    static final int BEGIN     = 8;
    static final int END       = 9;
    static final int SEMICOLON = 10;

    int tokenId;     // which token was it - see above list
    String string;   // string rendering of token
    int intValue;    // if token is NUMBER this gives number's value

    Token(String s) {  // Token constructor converts string to token
	string = s;
	if (s.equals("program"))      tokenId = PROGRAM;
	else if (s.equals("turn"))    tokenId = TURN;
	else if (s.equals("degrees")) tokenId = DEGREES;
	else if (s.equals("forward")) tokenId = FORWARD;
	else if (s.equals("times"))   tokenId = TIMES;
	else if (s.equals("do"))      tokenId = DO;
	else if (s.equals("begin"))   tokenId = BEGIN;
	else if (s.equals("end"))     tokenId = END;
	else if (s.equals(";"))       tokenId = SEMICOLON;
	else if (s.startsWith("\"") && s.endsWith("\""))
	    tokenId = STRING;
	else try {
		intValue = Integer.parseInt(s.trim());
		tokenId = NUMBER;
	    } catch (NumberFormatException nfe) {
		System.out.println("Unrecognised token "+s);
	    }
    }

    static String name(int tokenId) {
	switch(tokenId) {
	case PROGRAM:   return("program");
	case TURN:      return("turn");
	case DEGREES:   return("degrees");
	case FORWARD:   return("forward");
	case TIMES:     return ("times");
	case DO:        return ("do");
	case BEGIN:     return ("begin");
	case END:       return ("end");
	case SEMICOLON: return (";");
	case NUMBER:    return ("number");
	default: 
	    return ("UNRECOGNISED_TOKEN");
	}
    }
}
