nast.ml 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. (*
  2. * Neko AST for OCaml
  3. * Copyright (c)2005 Nicolas Cannasse
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  18. *)
  19. type pos = {
  20. psource : string;
  21. pline : int;
  22. }
  23. type constant =
  24. | True
  25. | False
  26. | Null
  27. | This
  28. | Int of int
  29. | Float of string
  30. | String of string
  31. | Builtin of string
  32. | Ident of string
  33. | Int32 of int32
  34. type while_flag =
  35. | NormalWhile
  36. | DoWhile
  37. type expr_decl =
  38. | EConst of constant
  39. | EBlock of expr list
  40. | EParenthesis of expr
  41. | EField of expr * string
  42. | ECall of expr * expr list
  43. | EArray of expr * expr
  44. | EVars of (string * expr option) list
  45. | EWhile of expr * expr * while_flag
  46. | EIf of expr * expr * expr option
  47. | ETry of expr * string * expr
  48. | EFunction of string list * expr
  49. | EBinop of string * expr * expr
  50. | EReturn of expr option
  51. | EBreak of expr option
  52. | EContinue
  53. | ENext of expr * expr
  54. | EObject of (string * expr) list
  55. | ELabel of string
  56. | ESwitch of expr * (expr * expr) list * expr option
  57. | ENeko of string
  58. and expr = expr_decl * pos
  59. let pos = snd
  60. let null_pos = { pline = 0; psource = "<null pos>" }
  61. let mk_call v args p = ECall (v,args) , p
  62. let mk_call0 v p = ECall (v,[]) , p
  63. let mk_call1 v a p = ECall (v,[a]) , p
  64. let mk_ident i p = EConst (Ident i) , p
  65. let mk_builtin b p = EConst (Builtin b) , p
  66. let mk_int i p = EConst (Int i) , p
  67. let mk_string s p = EConst (String s) , p
  68. let mk_binop op e1 e2 p = EBinop (op,e1,e2) , p
  69. let map f (e,p) =
  70. (match e with
  71. | EBlock el -> EBlock (List.map f el)
  72. | EParenthesis e -> EParenthesis (f e)
  73. | EField (e,s) -> EField (f e, s)
  74. | ECall (e,el) -> ECall (f e, List.map f el)
  75. | EArray (e1,e2) -> EArray (f e1, f e2)
  76. | EVars vl -> EVars (List.map (fun (v,e) -> v , match e with None -> None | Some e -> Some (f e)) vl)
  77. | EWhile (e1,e2,flag) -> EWhile (f e1, f e2, flag)
  78. | EIf (e,e1,e2) -> EIf (f e, f e1, match e2 with None -> None | Some e -> Some (f e))
  79. | ETry (e,ident,e2) -> ETry (f e, ident, f e2)
  80. | EFunction (params,e) -> EFunction (params, f e)
  81. | EBinop (op,e1,e2) -> EBinop (op, f e1, f e2)
  82. | EReturn (Some e) -> EReturn (Some (f e))
  83. | EBreak (Some e) -> EBreak (Some (f e))
  84. | ENext (e1,e2) -> ENext (f e1,f e2)
  85. | EObject fl -> EObject (List.map (fun (s,e) -> s , f e) fl)
  86. | ESwitch (e,cases,def) -> ESwitch (f e,List.map (fun(e1,e2) -> f e1, f e2) cases,match def with None -> None | Some e -> Some (f e))
  87. | EReturn None
  88. | EBreak None
  89. | EContinue
  90. | ENeko _
  91. | ELabel _
  92. | EConst _ as x -> x) , p
  93. let iter f (e,p) =
  94. match e with
  95. | EBlock el -> List.iter f el
  96. | EParenthesis e -> f e
  97. | EField (e,s) -> f e
  98. | ECall (e,el) -> f e; List.iter f el
  99. | EArray (e1,e2) -> f e1; f e2
  100. | EVars vl -> List.iter (fun (_,e) -> match e with None -> () | Some e -> f e) vl
  101. | EWhile (e1,e2,_) -> f e1; f e2
  102. | EIf (e,e1,e2) -> f e; f e1; (match e2 with None -> () | Some e -> f e)
  103. | ETry (e1,_,e2) -> f e1; f e2
  104. | EFunction (_,e) -> f e
  105. | EBinop (_,e1,e2) -> f e1; f e2
  106. | EReturn (Some e) -> f e
  107. | EBreak (Some e) -> f e
  108. | ENext (e1,e2) -> f e1; f e2
  109. | EObject fl -> List.iter (fun (_,e) -> f e) fl
  110. | ESwitch (e,cases,def) -> f e; List.iter (fun(e1,e2) -> f e1; f e2) cases; (match def with None -> () | Some e -> f e)
  111. | EReturn None
  112. | EBreak None
  113. | EContinue
  114. | ENeko _
  115. | ELabel _
  116. | EConst _ -> ()
  117. let is_printable c = c >= '\032' && c <= '\126'
  118. let escape s =
  119. let b = Buffer.create (String.length s) in
  120. for i = 0 to (String.length s) - 1 do
  121. match s.[i] with
  122. | '\n' -> Buffer.add_string b "\\n"
  123. | '\t' -> Buffer.add_string b "\\t"
  124. | '\r' -> Buffer.add_string b "\\r"
  125. | '\\' -> Buffer.add_string b "\\\\"
  126. | c when c == '"' || not (is_printable c) -> Buffer.add_string b (Printf.sprintf "\\%.3d" (int_of_char c))
  127. | c -> Buffer.add_char b c
  128. done;
  129. Buffer.contents b
  130. let s_constant = function
  131. | True -> "true"
  132. | False -> "false"
  133. | Null -> "null"
  134. | This -> "this"
  135. | Int i -> string_of_int i
  136. | Float s -> s
  137. | String s -> "\"" ^ escape s ^ "\""
  138. | Builtin s -> "$" ^ s
  139. | Ident s -> s
  140. | Int32 i -> Int32.to_string i