Every test program in the compiler course is plain text in one interchange format, .uir. Before anything can be run or compiled, the text has to become a graph, and for tests and caches any graph has to print back in one canonical form.
Implement canonicalize(src): parse a .uir program and return its canonical text.
Reading the program
%name = <expr>. Everything after # is a comment, and blank lines are ignored.op operand ... attr ..., or a single operand. An operand is a %name reference, a number, true or false, a list [a b c] of numbers, or a parenthesized expression (op ...), which may nest.0x hex with an optional -; floats contain . or an exponent e.key=value tokens. They may follow a : separator, and directly after : a bare dtype (void, bool, i32, f32) means dtype= that dtype.%name = { opens a block that ends at a line holding only }. Names defined inside a block are local to it; the block's value is its own %result, and the block's name refers to that node. Blocks can nest, and a reference resolves to the innermost visible definition made on an earlier line.%name is an alias. A single literal becomes the node const <literal>. Defining a name again rebinds it for the lines that follow.%result, or else the last top-level statement.Hash-consing. Two nodes with the same op, the same operands (the same referenced nodes and the same literals) and the same attributes are one node, wherever and however they were written.
Canonical text
%0, %1, ... in printing order, so the value comes last.%k = op, then the operands, then the attributes, separated by single spaces. Node operands are written by name. Literals are written inline: integers in decimal, floats as Python's repr(float(x)), booleans as true/false, lists as [ elements ] with each element written the same way.key=value with no :, sorted by key, values exactly as written. Ops are lower case.Return the lines joined with newlines.