Ruslan Popov
It is not heavily tested.
Infix to prefix functionality does not count proper precedence and associativity.
I’m not sure if unary operators are parsed correctly.
In order to convert a prefix expressions to infix you need to create a table of operators (by make-ops-table ).
Then you should call the prefix->infix function with an expression to parse and your operators table. From the library, a default operators table is available for addition, subtraction, multiplication and division.
Inner implementation:
Firstly, expression is converted to a binary tree. That means all forms like (+ 1 2 3) is converted to (+ 1 (+ 2 3)). WARNING: There is a bug, this works correctly for left associative expressions only.
Then, binary expression are convert to prefix form. This form does not count associativity and precedence, instead it wraps all in parenthesis.
Finally, groupings are removed according to operators table were they are unnecessary.
In order to convert an infix expression to prefix you need to define a special parser with define-infix->prefix-parser . You should supply name and operators that you need to parse.
Then you can call that parser by name and supply an expression.
Inner implementation:
The define-infix->prefix-parser form uses match. It searches for operators inside an expression, splits expression into left and right part, and then calls itself recursively and makes a prefix expression.
procedure
( prefix->infix exprops)→any/c
expr:any/cops:hash?
syntax
( make-ops-table clause...)
clause = (operatorprecedenceassociativity)
syntax
( define-infix->prefix-parser nameop...)