q_symbol.ml 1.07 KB
Newer Older
1 2 3
let symbols = ref []

let define s =
4 5 6 7 8 9
  let i = 
    try String.index s '=' 
    with Not_found -> failwith ("Invalid symbol definition :" ^ s) in
  symbols := 
  (String.sub s 0 i, String.sub s (i + 1) (String.length s - i - 1)) ::
    !symbols
10

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
EXTEND
  GLOBAL: Pcaml.str_item;

  Pcaml.str_item: FIRST
    [ [ "ifdef"; c = UIDENT; "then"; e1 = SELF;
        "else"; e2 = SELF ->
          if List.mem_assoc c !symbols then e1 else e2
      | "ifdef"; c = UIDENT; "then"; e1 = SELF ->
          if List.mem_assoc c !symbols then e1 else <:str_item< declare end >>
      | "ifndef"; c = UIDENT; "then"; e1 = SELF;
        "else"; e2 = SELF ->
          if List.mem_assoc c !symbols then e2 else e1
      | "ifndef"; c = UIDENT; "then"; e1 = SELF ->
          if List.mem_assoc c !symbols then <:str_item< declare end >> else e1
      ] ];
END

28 29
let expr _ s =
  try List.assoc s !symbols
30
  with Not_found -> failwith ("No definition for symbol " ^ s)
31 32 33

let _ =
  Quotation.add "symbol" (Quotation.ExStr expr);
34
  Pcaml.add_option "-symbol" (Arg.String define) 
35
    "<symbol=value> Define a symbol"