caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Very strange camlp4 error when using records
@ 2005-05-13 19:54 Richard Jones
  2005-05-13 22:10 ` [Caml-list] " Martin Jambon
  0 siblings, 1 reply; 2+ messages in thread
From: Richard Jones @ 2005-05-13 19:54 UTC (permalink / raw)
  To: caml-list

I have a strange case where the generated code looks completely
correct - indeed, I can pretty-print the code, save it to a file, and
compile it using ocamlc just fine.  However when using the camlp4
preprocessor directly (ocamlc -pp "camlp4o ./pa_test2.cmo" ...), it
fails to compile.

$ ocamlc -pp "camlp4o pa_extend.cmo q_MLast.cmo" -I +camlp4 -c pa_test2.ml
$ ocamlc -pp "camlp4o ./pa_test2.cmo" -I +camlp4 -c test2.ml
File "test2.ml", line 1, characters 11-18:
Unbound value t.a
$ camlp4o ./pa_test2.cmo pr_o.cmo test2.ml
type t = { a : int }
let rec t t = Array.iter print_endline [| string_of_int t.a |]
      (* generated code above compiles just fine in toplevel or ocamlc *)

-- pa_test2.ml -----------------------------------------------
let rec generate loc name = function
  | <:ctyp< int >> ->
      <:expr< string_of_int $lid:name$ >>
  | <:ctyp< { $list:fields$ } >> ->
      let fields = List.map (
	fun (loc, fname, mut, tdef) ->
	  let name = name ^ "." ^ fname in
	  generate loc name tdef
      ) fields in
      <:expr< Array.iter print_endline [| $list:fields$ |] >>
  | _ -> assert false

DELETE_RULE
  Pcaml.str_item: "type"; LIST1 Pcaml.type_declaration SEP "and"
END

EXTEND
  GLOBAL: Pcaml.str_item;

  Pcaml.str_item:
    [[ "type"; tds = LIST1 Pcaml.type_declaration SEP "and" ->
	 let typedefs = <:str_item< type $list:tds$ >> in
	 let values = List.map (
	   fun ((loc, name), tparams, tdef, cl) ->
	     let body = generate loc name tdef in
	     <:patt< $lid:name$ >>, <:expr< fun $lid:name$ -> $body$ >>
	 ) tds in
	 <:str_item<
	   declare
	     $typedefs$;
	     value rec $list:values$;
           end
         >> ]];
END
--------------------------------------------------------------

-- test2.ml --------------------------------------------------
type t = { a : int }
--------------------------------------------------------------

Ideas?

Rich.

-- 
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Team Notepad - intranets and extranets for business - http://team-notepad.com


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2005-05-13 22:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-05-13 19:54 Very strange camlp4 error when using records Richard Jones
2005-05-13 22:10 ` [Caml-list] " Martin Jambon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).