Fun thing I wanted to make for awhile… definitely going to do some doodles of this in my sketchbook…
This code is implementing a form of symbolic computation using a transformational language. This language has four primary symbols: “E”, “M”, “3”, and “W”. Each of these symbols is associated with a specific angle: 0, 90, 180, and 270 degrees, respectively.
There are also special symbols, which act as operators that perform certain transformations on the primary symbols:
- “)”: Represents a rotation 90 degrees clockwise.
- “(“: Represents a rotation 90 degrees counter-clockwise.
- “|”: Represents a mirror operation, which reverses the direction. However, “M” and “W” are unaffected by this operation.
- “.”: This operator sums up all previous primary symbols (by their respective degree values), resulting in a new symbol. The degree value of the new symbol is the sum of the previous degrees modulo 360.
The function emw(prog)
interprets a string prog
as a sequence of symbols and operations. It reads each character in the string and based on whether it’s a primary symbol, operator, or a “.”, it either adds the symbol to an array or performs an operation on the existing symbols in the array.
The expr(prog)
function splits the input string prog
into space-separated substrings. It interprets each substring as a sequence in the transformational language, using the emw
function. It then joins the results together with a space and prepends the original input and an equals sign to give a string representation of the transformations.
The final section of the code generates a series of examples of expressions in this language, evaluates them using the expr
function, and adds the results to the HTML body of a webpage in a formatted manner. This gives the user a way to see the transformations in action.