views:

58

answers:

2

Hello, maybe I'm just failing in a really simple thing but I'm developing an intepreter written in OCaml with standard tools as ocamllex and ocamlyacc and I have this situation:

  • iparser.mly contains parser spec
  • ilexer.mll contains lexer spec
  • impossible.ml contains the vm that executes the code and all the types needed

The instruction type that defines various instructions is in impossible.ml and it is needed by the parser but impossible.ml uses the parser defined in iparser.mly so they both need each other to compile correctly.

Is there a way to produce just the .cmi file for my impossible.ml? In this way the parser would know about types defined in impossible.ml and it will allow me to compile impossible.cmo/.cmi and later compile also impossible.cmo. Then I can link all of them together.

So far my compiling script is:

ocamlyacc iparser.mly
ocamlc -c iparser.mli
ocamllex ilexer.mll
ocamlc -c ilexer.ml
ocamlc -c iparser.ml
ocamlc -c impossible.ml
ocamlc -o imp.exe ilexer.cmo iparser.cmo impossible.cmo

but this doesn't work because ocamlc -c iparser.ml needs at least impossible.cmi to know the types.

Any suggestions? Thanks in advance..

+3  A: 

You need to create an impossible.mli and compile that. That will produce the impossible.cmi and only the .cmi.

Alternatively:

ocamlc -i impossible.ml

will print the mli to stdout. You could do something like this:

ocamlc -i impossible.ml > impossible.mli
ocamlc -c impossible.mli
Niki Yoshiuchi
Can't I just build the normal inferred .cmi? From what I understood using just a .ml file will generate a standard .mli file that publishes everything of the module.. am I right?
Jack
Yeah, you are correct. I'm just not sure if there is a way to output just the mli/cmi without also compiling the cmo file. The closest I can find at the moment is ocamlc -i impossible.ml which will print the default mli to stdout. You could add something to the build process to do that, redirect the output to a file and then compile that.
Niki Yoshiuchi
I tried to figure it out but ofcourse calling `ocamlc -i impossible.ml` returns in __Error: Unbound value Iparser.first__ because of the same problem exposed in my question. At this point I don't think there are way around writing my own .mli file
Jack
Too bad. It makes sense though, the default mli makes pretty much everything public. Your mli can only contain interfaces that don't reference the parser code.
Niki Yoshiuchi
+1  A: 

IMHO, you cannot legitimately compile recursively-dependant modules this way. Either factor out the interdependencies in the third module (usually easy), or pass them as parameters (or mutable initialization references - ugly), or use recursive modules.

ygrek