File: README.md

package info (click to toggle)
optcomp 1.6-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 372 kB
  • ctags: 717
  • sloc: ml: 6,731; makefile: 62
file content (106 lines) | stat: -rw-r--r-- 2,498 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
optcomp - optional compilation with cpp-like directives
=======================================================

Optcomp is a syntax extension which handles `#if`, `#else`,
... directives in ocaml source files.

For example, to switch between two pieces of code according to the
ocaml compiler version, one can write:


    #if ocaml_version < (3, 10)
    let x = 1
    #else
    let x = 2
    #endif

### What the difference between cpp and optcomp ?

Optcomp is more OCaml-friendly than cpp:

* it does not interpret `//`, `/*`, and `*/` as comment delimiters
* it does not complains about missing `'`
* it is easier to integrate in the build process when using other
  camlp4 syntax extensions

By the way optcomp does not do macro expansion while cpp does.

### What the difference between pa_macro and optcomp ?

Optcomp does not require code that will be dropped to be valid caml
code. So for example this code will be rejected by
camlp4(<3.13)+pa_macro:

    IFDEF HAVE_GADTS THEN
    type 'a t =
      | Int : int t
      | String : string t
    ENDIF

But this one will be accepted by camlp4+optcomp:

    #if HAVE_GADTS
    type 'a t =
      | Int : int t
      | String : string t
    #endif

Installation
------------

To build and install optcomp:

    $ ./configure
    $ make
    $ make install

### Documentation _(optional)_

To build the documentation:

    $ make doc

It will then be installed by `make install`.

### Tests _(optionnal)_

To build and execute tests:

    $ ./configure --enable-tests
    $ make test

Usage
-----

### As a syntax extension

You can use optcomp as a regular syntax extension with camlp4. If you
have ocamlfind installed, you can use:

    $ ocamlfind ocamlc -syntax camlp4o -pakcage camlp4,optcomp file.ml

You can also embed `pa_optcomp.ml` in your project sources.

#### As a preprocessor

Optcomp can be used as a preprocessor, for that there is the two
executable optcomp-o and optcomp-r:

* `optcomp-o` is for when directives are written using original syntax
* `optcomp-r` is for when directives are written using revised syntax

To use them:

    $ ocamlc -pp optcomp-o <file.ml>
    $ ocamlc -pp optcomp-r <file.ml>

The preprocessor version is especially usefull for .mli because it
does not modify the layout of your code, which is important for
ocamldoc.

Hacking
-------

To add support to more expressions, you can modify the eval function
of pa_optcomp.ml. It takes a camlp4 expression ast and must return
something of type value.