Created
October 4, 2012 00:12
-
-
Save darkf/3830719 to your computer and use it in GitHub Desktop.
AST to Postfix string representation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| open Printf | |
| type node = | |
| | Call of string * node list | |
| | Int of int | |
| let rec toString = function | |
| | Call (fn, args) -> sprintf "(%s %s)" fn (String.concat " " (List.map toString args)) | |
| | Int i -> sprintf "%d" i | |
| let rec toPostfix = function | |
| | Call (fn, args) -> sprintf "%s %s" (String.concat " " (List.map toPostfix args)) fn | |
| | Int i -> sprintf "%d" i | |
| let () = | |
| let ast = Call ("print", [ | |
| Call ("+", [Int 2; Int 3]) | |
| ]) in | |
| let r = toPostfix ast in | |
| printf "%s\n" r |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment