www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - closure wonkiness

Hello.

I have a function that starts off as follows. When peek is called for
the first time, f should be -2. It turns out to be garbage. f doesn't
get modified anywhere but peek and consume. peek doesn't get called
until lexer has returned. This is D 2.031. Is it me or the compiler?

public Token delegate() lexer(InputStream inpp, OutputStream outpp){
    char[] buffer = new char[buffsize];
    char[] line;
    int b = 0;
    int f = -2;
    InputStream inp = inpp;
    OutputStream outp = outpp;
    bool eof(){
	return f == -2 && inp.eof;
    }
    char consume(){
	if(eof) return char.init;
	if(f == -2){
	    f=-1;
	    line = inp.readLine(buffer);
	}
	if( f == line.length - 1){
	    f = -2;
	    return '\n';
	}
	return line[++f];
    }
    char peek(){
	if(eof()) return char.init;
	writeln(f);
	if(f == -2){
	    f = -1;
	    line = inp.readLine(buffer);
	}
	if(f == line.length - 1){
	    return '\n';
	}
	return line[f+1];
    }
Aug 31 2009