trav.ml 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. include Ast
  2. (* Default tree transformation
  3. * (node -> node) -> node -> node *)
  4. let rec transform visitor node =
  5. let trav = visitor in
  6. let trav_all nodes = List.map trav nodes in
  7. match node with
  8. | Program (decls) ->
  9. Program (trav_all decls)
  10. | FunDec (ret_type, name, params) ->
  11. FunDec (ret_type, name, trav_all params)
  12. | FunDef (export, ret_type, name, params, body) ->
  13. FunDef (export, ret_type, name, trav_all params, trav_all body)
  14. | GlobalDec (ctype, name) ->
  15. GlobalDec (ctype, name)
  16. | GlobalDef (export, ctype, name, Some init) ->
  17. GlobalDef (export, ctype, name, Some (trav init))
  18. | VarDec (ctype, name, Some init) ->
  19. VarDec (ctype, name, Some (trav init))
  20. | Assign (name, value) ->
  21. Assign (name, trav value)
  22. | Return (value) ->
  23. Return (trav value)
  24. | If (cond, body) ->
  25. If (trav cond, trav_all body)
  26. | IfElse (cond, true_body, false_body) ->
  27. IfElse (trav cond, trav_all true_body, trav_all false_body)
  28. | While (cond, body) ->
  29. While (trav cond, trav_all body)
  30. | DoWhile (cond, body) ->
  31. DoWhile (trav cond, trav_all body)
  32. | For (counter, start, stop, step, body) ->
  33. For (counter, trav start, trav stop, trav step, trav_all body)
  34. | Expr (value) ->
  35. Expr (trav value)
  36. | Monop (op, value) ->
  37. Monop (op, trav value)
  38. | Binop (op, left, right) ->
  39. Binop (op, trav left, trav right)
  40. | Cond (cond, true_expr, false_expr) ->
  41. Cond (trav cond, trav true_expr, trav false_expr)
  42. | TypeCast (ctype, value) ->
  43. TypeCast (ctype, trav value)
  44. | FunCall (name, args) ->
  45. FunCall (name, trav_all args)
  46. | _ -> node
  47. (*
  48. (* Visit
  49. * *)
  50. let rec visit visitor = function
  51. let trav = visit visitor in
  52. let trav_all nodes = List.map trav nodes in
  53. | Program (decls) -> List.map visitor decls;
  54. | Param (ctype, name) ->
  55. | FunDec (ret_type, name, params) ->
  56. | FunDef (export, ret_type, name, params, body) ->
  57. | GlobalDec (ctype, name) ->
  58. | GlobalDef (export, ctype, name, None) ->
  59. | GlobalDef (export, ctype, name, Some init) ->
  60. | VarDec (ctype, name, None) ->
  61. | VarDec (ctype, name, Some init) ->
  62. | Assign (name, value) ->
  63. | Return (value) ->
  64. | If (cond, body) ->
  65. | IfElse (cond, true_body, false_body) ->
  66. | While (cond, body) ->
  67. | DoWhile (cond, body) ->
  68. | For (counter, start, stop, step, body) ->
  69. | Expr (value) ->
  70. | BoolConst (value) ->
  71. | IntConst (value) ->
  72. | FloatConst (value) ->
  73. | Var (name) ->
  74. | Monop (op, value) ->
  75. | Binop (op, left, right) ->
  76. | Cond (cond, true_expr, false_expr) ->
  77. | TypeCast (ctype, value) ->
  78. | FunCall (name, args) ->
  79. *)