blynn / nex

Lexer for Go
http://cs.stanford.edu/~blynn/nex/
GNU General Public License v3.0
416 stars 47 forks source link

Getting the AST without a global... #43

Closed purpleidea closed 7 years ago

purpleidea commented 7 years ago

Is this possible? Currently I have to do something like:

in parser.y:

top:    prog
    {
        langGlobal = $1.prog
    }
;

in main.go:

    lexer := NewLexer(os.Stdin)
    yyParse(lexer) // writes the result to langGlobal

    log.Printf("behold, the AST: %+v\n", langGlobal)

I know it's not directly nex related, but I was hoping someone here might know! Thanks :)

wolfkdy commented 7 years ago

yes, ofcourse, you may derive a new class of Lexer, just call it type NewLexer struct { *yyLexer; myAST interface{}, }

pass the NewLexer instance into yyParse

so, in the parser.y, you should write lines like below: top: prog { myLexer, _ := yylexer.(*NewLexer) myLexer.myAST = $1.prog }

purpleidea commented 7 years ago

@wolfkdy Thanks for your response. I tried this sort of thing, but due my stupidity, I seem to have failed. Is there a full working example somewhere that I can try? I couldn't get the above integrated and compiling.

Thanks!

purpleidea commented 7 years ago

@wolfkdy Still haven't figured this out, since while I could define a new struct, how do I pass in the stdin, etc...

purpleidea commented 7 years ago

Okay, figured it out. Will post a full solution if I remember to shortly. There's a broken example in https://github.com/blynn/nex/tree/master/tacky which I modified to use .parseResult instead of .p in my code, and got it all working. I assume that @blynn didn't test his code before committing the example, but it looks as if the parseResult is meant for this usage, since it isn't used elsewhere.