Skip to content

Instantly share code, notes, and snippets.

@darkf
Created October 4, 2012 00:12
Show Gist options
  • Select an option

  • Save darkf/3830719 to your computer and use it in GitHub Desktop.

Select an option

Save darkf/3830719 to your computer and use it in GitHub Desktop.
AST to Postfix string representation
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