Compiler Framework

Design note

如何给语言添加IO功能

private char skipWhitespace(char ch) {
    while (true) {
        if (ch == ' ' || ch == '\t' || ch == '\n')
            ch = buffer.next();
        else break;
    }
    return ch;
}

改写buffer.peek()

public boolean peek(char ch) {
     if(peek() == ch) {
         next();
         return true;
     }
     return false;
}
// To
public boolean peek(char... chs) {
    char cur_ch = peek();
    for(char ch : chs) {
        if (cur_ch == ch) {
            next();
            return true;
        }
    }
    return false;
}
private void skipWhitespace() {
    while (buffer.peek(' ', '\t', '\n', '\r')) buffer.next();
}