Skip to content
This repository
tree: 38bc18b051
Fetching contributors…

Cannot retrieve contributors at this time

file 130 lines (109 sloc) 4.299 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
(*
Copyright © 2011 MLstate

This file is part of Opa.

Opa is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

Opa is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with Opa. If not, see <http://www.gnu.org/licenses/>.
*)
module B = Base
module O = Ocaml
module G = Grammar
module S = B.String
module L = B.List

exception Operation_after_statecall of string
exception NestedSubPatterns
exception LiteralRequired

let (><) = Option.map

let is_ident = function
  | G.Ident _ -> true
  | _ -> false

let consttype_to_string = function
  | O.TypeBool -> "bool"
  | O.TypeInt64 -> "int64"
  | O.TypeInt -> "int"
  | O.TypeFloat -> "float"
  | O.TypeUnit -> "unit"
  | _ -> "string"

let type_to_str = function
  | O.TypeConst a -> consttype_to_string a
  | O.TypeName (_,["int64"]) -> "int64"
  | O.TypeName (_,["bool"]) -> "bool"
  | t ->
      prerr_string "Tools.type_to_str: Unknown type="; OcamlPrint.Output.type_expr stderr t; prerr_newline ();
      assert false

let prefix p i = p ^ (string_of_int i)

let bruijnise (n,lst) = function
  | G.Ident s | G.SubPattern (s,_,_) -> succ n, (s, succ n) :: lst
  | G.Prevent _ -> n, lst
  | _ -> succ n, lst

let get_patconstr name tuple_list =
  O.PatConstructor([name], L.map (fun str -> O.PatVar (fst str)) tuple_list)

let types_of_tdefs pname lst =
  let rec prefix = function
    | O.TypeVar a when a = "msg" -> O.TypeVar ((S.capitalize pname) ^ ".msg")
    | O.TypeName (lst, n) -> O.TypeName (L.map prefix lst, n)
    | O.TypeTuple lst -> O.TypeTuple (L.map prefix lst)
    (*| O.TypePair (a, b) -> O.TypePair (prefix a, prefix b)*)
    | O.TypeRecord rl -> O.TypeRecord (L.map (fun (b,n,t) -> b, n, prefix t) rl)
    | O.TypeArrow (a, b) -> O.TypeArrow (prefix a, prefix b)
    | O.TypeConstructor lst ->
        O.TypeConstructor (L.map (fun (n, o) -> n, prefix >< o) lst)
    | otherwise -> otherwise in
  let aux = function
    | G.MType (a, b) -> O.Type [[], a, prefix b]
    | G.MVal (a, b) -> O.Val (Ident.source a, prefix b)
    | _ -> assert false
  in L.map aux lst

let tuple_of_var = function
  | G.GVar (a, b) -> a,b
  | _ -> assert false

let val_of_import = function
  | G.Import (G.GVar (n, t)) -> O.Val (Ident.source n, t)
  | _ -> assert false

let let_of_set = function
  | G.Set (G.GVar (n, _), value) -> O.make_Let (O.Pat (O.PatVar (Ident.source n))) (O.Verbatim value)
  | _ -> assert false


let add_suffix ?(force_split=false) name suffix =
  let split(us)=
    if suffix = "" then ""
    else if force_split
    then "_"
    else if us = 0
    then ""
    else "_" in
  match S.fold (fun (uc,lc,us) c ->
                       match c with
                       | 'a'..'z' -> (uc,lc+1,us)
                       | 'A'..'Z' -> (uc+1,lc,us)
                       | '_' -> (uc,lc,us+1)
                       | _ -> (uc,lc,us)) (0,0,0) name with
  | (0,0,0) -> name^suffix
  | (uc,0,us) -> name^(split(us))^(S.uppercase suffix)
  | (0,lc,us) -> name^(split(us))^(S.lowercase suffix)
  | (uc,lc,us) -> name^(split(us))^(S.capitalize suffix)

let add_prefix ?(force_split=false) name prefix =
  let split(us)=
    if prefix = "" then ""
    else if force_split
    then "_"
    else if us = 0
    then ""
    else "_" in
  match S.fold (fun (uc,lc,us) c ->
                       match c with
                       | 'a'..'z' -> (uc,lc+1,us)
                       | 'A'..'Z' -> (uc+1,lc,us)
                       | '_' -> (uc,lc,us+1)
                       | _ -> (uc,lc,us)) (0,0,0) name with
  | (0,0,0) -> prefix^name
  | (uc,0,us) -> (S.uppercase prefix)^(split(us))^name
  | (0,lc,us) -> (S.lowercase prefix)^(split(us))^name
  | (uc,lc,us) -> (S.capitalize prefix)^(split(us))^name

let str_of_type_expr te = let b = Buffer.create 1024 in (OcamlPrint.Buf.type_expr b te; Buffer.contents b)

let str_of_expr e = let b = Buffer.create 1024 in (OcamlPrint.Buf.expr b e; Buffer.contents b)
Something went wrong with that request. Please try again.