util.ml 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. open Printf
  2. open Lexing
  3. open Ast
  4. let var_counter = ref 0
  5. let fresh_var prefix =
  6. var_counter := !var_counter + 1;
  7. prefix ^ "$" ^ string_of_int !var_counter
  8. let loc_from_lexpos pstart pend =
  9. let (fname, ystart, yend, xstart, xend) = (
  10. pstart.pos_fname,
  11. pstart.pos_lnum,
  12. pend.pos_lnum,
  13. (pstart.pos_cnum - pstart.pos_bol + 1),
  14. (pend.pos_cnum - pend.pos_bol)
  15. ) in
  16. if ystart = yend && xend < xstart then
  17. (fname, ystart, yend, xstart, xstart)
  18. else
  19. (fname, ystart, yend, xstart, xend)
  20. (* Default tree transformation
  21. * (node -> node) -> node -> node *)
  22. let transform_children trav node =
  23. let trav_all nodes = List.map trav nodes in
  24. match node with
  25. | Program (decls, loc) ->
  26. Program (trav_all decls, loc)
  27. | FunDec (ret_type, name, params, loc) ->
  28. FunDec (ret_type, name, trav_all params, loc)
  29. | FunDef (export, ret_type, name, params, body, loc) ->
  30. FunDef (export, ret_type, name, trav_all params, trav body, loc)
  31. | GlobalDec (ctype, name, loc) ->
  32. GlobalDec (ctype, name, loc)
  33. | GlobalDef (export, ctype, name, Some init, loc) ->
  34. GlobalDef (export, ctype, name, Some (trav init), loc)
  35. | VarDec (ctype, name, Some init, loc) ->
  36. VarDec (ctype, name, Some (trav init), loc)
  37. | Assign (name, value, loc) ->
  38. Assign (name, trav value, loc)
  39. | Return (value, loc) ->
  40. Return (trav value, loc)
  41. | If (cond, body, loc) ->
  42. If (trav cond, trav body, loc)
  43. | IfElse (cond, true_body, false_body, loc) ->
  44. IfElse (trav cond, trav true_body, trav false_body, loc)
  45. | While (cond, body, loc) ->
  46. While (trav cond, trav body, loc)
  47. | DoWhile (cond, body, loc) ->
  48. DoWhile (trav cond, trav body, loc)
  49. | For (counter, start, stop, step, body, loc) ->
  50. For (counter, trav start, trav stop, trav step, trav body, loc)
  51. | Expr value ->
  52. Expr (trav value)
  53. | Monop (op, value, loc) ->
  54. Monop (op, trav value, loc)
  55. | Binop (op, left, right, loc) ->
  56. Binop (op, trav left, trav right, loc)
  57. | Cond (cond, true_expr, false_expr, loc) ->
  58. Cond (trav cond, trav true_expr, trav false_expr, loc)
  59. | TypeCast (ctype, value, loc) ->
  60. TypeCast (ctype, trav value, loc)
  61. | FunCall (name, args, loc) ->
  62. FunCall (name, trav_all args, loc)
  63. | Block (body) ->
  64. Block (trav_all body)
  65. | VarUse (var, def, depth) ->
  66. VarUse (trav var, def, depth)
  67. | _ -> node
  68. let rec locof = function
  69. | Program (_, loc)
  70. | Param (_, _, loc)
  71. | FunDec (_, _, _, loc)
  72. | FunDef (_, _, _, _, _, loc)
  73. | GlobalDec (_, _, loc)
  74. | GlobalDef (_, _, _, _, loc)
  75. | VarDec (_, _, _, loc)
  76. | Assign (_, _, loc)
  77. | Return (_, loc)
  78. | If (_, _, loc)
  79. | IfElse (_, _, _, loc)
  80. | While (_, _, loc)
  81. | DoWhile (_, _, loc)
  82. | For (_, _, _, _, _, loc)
  83. | Allocate (_, _, loc)
  84. | BoolConst (_, loc)
  85. | IntConst (_, loc)
  86. | FloatConst (_, loc)
  87. | ArrayConst (_, loc)
  88. | ArrayScalar (_, loc)
  89. | Var (_, loc)
  90. | Deref (_, _, loc)
  91. | Monop (_, _, loc)
  92. | Binop (_, _, _, loc)
  93. | Cond (_, _, _, loc)
  94. | TypeCast (_, _, loc)
  95. | FunCall (_, _, loc) -> loc
  96. | Expr value | VarUse (value, _, _) -> locof value
  97. | Block _ | Type _ -> noloc
  98. let prerr_loc (fname, ystart, yend, xstart, xend) =
  99. let file = open_in fname in
  100. (* skip lines until the first matched line *)
  101. for i = 1 to ystart - 1 do input_line file done;
  102. (* for each line in `loc`, print the source line with an underline *)
  103. for l = ystart to yend do
  104. let line = input_line file in
  105. let linewidth = String.length line in
  106. let left = if l = ystart then xstart else 1 in
  107. let right = if l = yend then xend else linewidth in
  108. if linewidth > 0 then (
  109. prerr_endline line;
  110. for i = 1 to left - 1 do prerr_char ' ' done;
  111. for i = left to right do prerr_char '^' done;
  112. prerr_endline "";
  113. )
  114. done;
  115. ()
  116. let prerr_loc_msg loc msg verbose =
  117. let (fname, ystart, yend, xstart, xend) = loc in
  118. let line_s = if yend != ystart
  119. then sprintf "lines %d-%d" ystart yend
  120. else sprintf "line %d" ystart
  121. in
  122. let char_s = if xend != xstart || yend != ystart
  123. then sprintf "characters %d-%d" xstart xend
  124. else sprintf "character %d" xstart
  125. in
  126. eprintf "File \"%s\", %s, %s:\n" fname line_s char_s;
  127. eprintf "%s\n" msg;
  128. if verbose >= 2 then prerr_loc loc;
  129. ()