Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature-wish: camlp4 quotation #8155

Closed
vicuna opened this issue May 26, 2003 · 1 comment
Closed

feature-wish: camlp4 quotation #8155

vicuna opened this issue May 26, 2003 · 1 comment

Comments

@vicuna
Copy link

vicuna commented May 26, 2003

Original bug ID: 1696
Reporter: @alainfrisch
Assigned to: ertai
Status: closed (set by @xavierleroy on 2010-04-29T12:31:22Z)
Resolution: fixed
Priority: normal
Severity: feature
Fixed in version: 3.11+dev
Category: -for Camlp4 use https://github.com/ocaml/camlp4/issues

Bug description

Bonjour,

Sauf erreur de ma part, il n'y a pas de moyen simple de passer des valeurs
calculées à la compilation à un programme Caml. Typiquement, je veux faire
qu'un module connaisse sa date de compilation, ou le compilateur utilisé
(ocamlc ou ocamlopt). La solution de créer un fichier externe (genre
utils/config.ml) n'est pas toujours agréable à mettre en oeuvre dans un
Makefile (par exemple, pour savoir quel est le compilateur utilisé, car il
faut regenerer le fichier entre un make et un make opt).

Je propose la quotation camlp4 ci-dessous, qui permet de faire des choses
du genre:

-pp 'camlp4o q_symbol.cmo -symbol version="0.0.9"'

(pour les messages d'erreur, c'est plus joli que -pp 'sed ...')

Je pense que ça aurait sa place dans le Camlp4 de la distribution d'OCaml.

-- Alain


let symbols = ref []

let define s =
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

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

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

@vicuna
Copy link
Author

vicuna commented Oct 6, 2008

Comment author: ertai

Recent extensions of the camlp4 macro system (-D option) provides the same feature.

Example:
$ camlp4o -parser Camlp4MacroParser -DA=42 -str 'let x = A'
let x = 42

camlp4of is shorter

$ camlp4of -DA='(1, "foo", '"'a'"', 3.4, [])' -str 'A'
(1, "foo", 'a', 3.4, [])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant