home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Caml Light 0.61 / Source / src / compiler / syntax.ml < prev    next >
Encoding:
Text File  |  1993-09-24  |  4.1 KB  |  140 lines  |  [TEXT/MPS ]

  1. (* The abstract syntax for the language *)
  2.  
  3. #open "const";;
  4. #open "location";;
  5. #open "globals";;
  6.  
  7. type type_expression =
  8.     Typexp of type_expression_desc * location
  9. and type_expression_desc =
  10.     Ztypevar of string
  11.   | Ztypearrow of type_expression * type_expression
  12.   | Ztypetuple of type_expression list
  13.   | Ztypeconstr of global_reference * type_expression list
  14. ;;
  15.  
  16. type pattern =
  17.     Pat of pattern_desc * location
  18. and pattern_desc =
  19.     Zwildpat
  20.   | Zvarpat of string
  21.   | Zaliaspat of pattern * string
  22.   | Zconstantpat of atomic_constant
  23.   | Ztuplepat of pattern list
  24.   | Zconstruct0pat of constr_desc global
  25.   | Zconstruct1pat of constr_desc global * pattern
  26.   | Zorpat of pattern * pattern
  27.   | Zconstraintpat of pattern * type_expression
  28.   | Zrecordpat of (label_desc global * pattern) list
  29. ;;
  30.  
  31. type expression =
  32.     Expr of expression_desc * location
  33. and expression_desc =
  34.     Zident of expr_ident ref
  35.   | Zconstant of struct_constant
  36.   | Ztuple of expression list
  37.   | Zconstruct0 of constr_desc global
  38.   | Zconstruct1 of constr_desc global * expression
  39.   | Zapply of expression * expression list
  40.   | Zlet of bool * (pattern * expression) list * expression
  41.   | Zfunction of (pattern list * expression) list
  42.   | Ztrywith of expression * (pattern * expression) list
  43.   | Zsequence of expression * expression
  44.   | Zcondition of expression * expression * expression
  45.   | Zwhile of expression * expression
  46.   | Zfor of string * expression * expression * bool * expression
  47.   | Zsequand of expression * expression
  48.   | Zsequor of expression * expression
  49.   | Zconstraint of expression * type_expression
  50.   | Zvector of expression list
  51.   | Zassign of string * expression
  52.   | Zrecord of (label_desc global * expression) list
  53.   | Zrecord_access of expression * label_desc global
  54.   | Zrecord_update of expression * label_desc global * expression
  55.   | Zstream of stream_component list
  56.   | Zparser of (stream_pattern list * expression) list
  57.  
  58. and expr_ident =
  59.     Zglobal of value_desc global
  60.   | Zlocal of string
  61.  
  62. and stream_component =
  63.     Zterm of expression
  64.   | Znonterm of expression
  65.  
  66. and stream_pattern =
  67.     Ztermpat of pattern
  68.   | Znontermpat of expression * pattern
  69.   | Zstreampat of string
  70. ;;
  71.  
  72. type type_decl =
  73.     Zabstract_type of mutable_flag
  74.   | Zvariant_type of constr_decl list
  75.   | Zrecord_type of (string * type_expression * mutable_flag) list
  76.   | Zabbrev_type of type_expression
  77.  
  78. and constr_decl =
  79.     Zconstr0decl of string
  80.   | Zconstr1decl of string * type_expression * mutable_flag
  81. ;;
  82.  
  83. type directiveu =
  84.     Zdir of string * string
  85. ;;
  86.  
  87. type impl_phrase =
  88.     Impl of impl_desc * location
  89. and impl_desc =
  90.     Zexpr of expression
  91.   | Zletdef of bool * (pattern * expression) list
  92.   | Ztypedef of (string * string list * type_decl) list
  93.   | Zexcdef of constr_decl list
  94.   | Zimpldirective of directiveu
  95. ;;
  96.  
  97. type intf_phrase =
  98.     Intf of intf_desc * location
  99. and intf_desc =
  100.     Zvaluedecl of (string * type_expression * prim_desc) list
  101.   | Ztypedecl of (string * string list * type_decl) list
  102.   | Zexcdecl of constr_decl list
  103.   | Zintfdirective of directiveu
  104. ;;
  105.  
  106. let rec free_vars_of_pat (Pat(desc, _)) =
  107.   match desc with
  108.     Zwildpat -> []
  109.   | Zvarpat v -> [v]
  110.   | Zaliaspat(pat,v) -> v :: free_vars_of_pat pat
  111.   | Zconstantpat _ -> []
  112.   | Ztuplepat patl -> flat_map free_vars_of_pat patl
  113.   | Zconstruct0pat(_) -> []
  114.   | Zconstruct1pat(_, pat) -> free_vars_of_pat pat
  115.   | Zorpat(pat1, pat2) -> free_vars_of_pat pat1 @ free_vars_of_pat pat2
  116.   | Zconstraintpat(pat, _) -> free_vars_of_pat pat
  117.   | Zrecordpat lbl_pat_list ->
  118.       flat_map (fun (lbl,pat) -> free_vars_of_pat pat) lbl_pat_list
  119. ;;    
  120.  
  121. let rec expr_is_pure (Expr(desc,loc)) =
  122.   match desc with
  123.     Zident _ -> true
  124.   | Zconstant _ -> true
  125.   | Ztuple el -> for_all expr_is_pure el
  126.   | Zconstruct0 cstr -> true
  127.   | Zconstruct1(cstr,arg) -> expr_is_pure arg
  128.   | Zfunction _ -> true
  129.   | Zconstraint(expr, ty) -> expr_is_pure expr
  130.   | Zvector el -> for_all expr_is_pure el
  131.   | Zrecord lbl_expr_list ->
  132.       for_all (fun (lbl,e) -> expr_is_pure e) lbl_expr_list
  133.   | Zparser _ -> true
  134.   | _ -> false
  135. ;;
  136.  
  137. let letdef_is_pure pat_expr_list =
  138.   for_all (fun (pat,expr) -> expr_is_pure expr) pat_expr_list
  139. ;;
  140.