I am currently working on a toy-compiler to learn about Rust.
Aaaaand the problems with the borrow- and lifetime-checker have already started!
GitHub Repo: https://github.com/Robbepop/cionc/tree/master/src/parser
In my case I have a Lexer that streams its output as Tokens.
Later I am planning to implement a Parser that it transforming these Tokens into an abstract syntax tree etc. just like a default compiler would work these days.
Problems I am currently facing:
Tokens shall store
&strwith important information, e.g. an Identifier should store here its name and a number should store its textual value. The
&strshall be a reference to a String object stored within the so-called StringTable which is a thin wrapper around a HashMap<String,String> (better would be HashSet but this currently doesn’t work in the standard). The problem is that I can not have references outside of the StringTable to its contents (in the Tokens) and mutating the StringTable simultaneously and I do need that in my current design.
The second problem is that I wanted to have a central management unit that is called the CompileContext which stores several helper utilities requires in different stages of compilation like the StringTable, an ErrorHandler or the SymbolTable etc. Lexer and Parser would then store a reference to this CompileContext entity and would have access to all of its members. This however, is critical since both, Lexer and Parser, may co-exist and thus may borrow things from the CompileContext at the same time.
My final question is: How can I fix these problem in the way Rust is meant to work?