MatthewKosloski / torrey

The Torrey Programming Language

Home Page:https://torrey.xyz

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Make compiler passes pure

MatthewKosloski opened this issue · comments

Each compiler pass, before performing its actions, should first create a deep copy of its input and should return that modified deep copy instead of altering its input.

For example, consider the current frontend:

final TokenList tokens = lexicalAnalysis();
final Program dirtyAST = syntaxAnalysis(tokens);
final Program semanticAST = semanticAnalysis(dirtyAST);
ir = irGen(semanticAST);

The semanticAnalysis pass modifies doesn't create a copy of the input AST. Rather, it modifies it and returns the reference to the input. So, after semanticAnalysis() returns, dirtyAST and semanticAST are references to the same object in memory. It would be better if semanticAST was a whole new reference. This would make testing and debugging much easier.