I've got some code like this:
type boolean = T | F
type bexp = Const of boolean
| Var of variable
| Bop of bop * bexp * bexp
| Not of bexp
and bop = And | Or | Xor
and variable = { name: string; mutable value: boolean }
Later on if I want to create a variable I have to do:
let full = Var({name ="full"; value = F});;
I'd rather not have to repeat the 'full' twice and would like to come up with some introspective way to get the name as a string. I'm thinking camlp4 could work for this, but have no idea where to start.
So ultimately I'd like to be able to do something like:
let full = Var({name = :letname:; value = F});;
Where :letname: would fill in the current let binding as a string in place of (in this case "full"). (the syntax :letname: is just a suggestion, other ideas for syntax that won't clash with OCaml's syntax?)
A more concise syntax like this would probably be preferable:
var full = F
Which would then expand to:
let full = Var({name = "full"; value = F});;
Is this possible to do with camlp4 and if so, how would I go about it?
(upon further consideration, the :letname: syntax or something similar would be more general-purpose and useful in more areas)
Try the following. In a separate file, such as test.ml
Next, compile with
On the top level
Then
In order to compile with the extension create a different file such as test2.ml
Then, compile with