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

Refactoring, updating code to latest OCaml and build system and Javascript cross compiling #22

Merged
merged 13 commits into from
Jan 9, 2024
Merged
35 changes: 6 additions & 29 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

## =========================
## Polygen Compiled Grammars
## =========================
Expand All @@ -14,38 +13,16 @@
## ==============================
notepad_session
notepad_workspace
Session.vim
*.code-workspace

# ------------------------------------------------------------------------------
# OCaml Files Exclusion
# OCaml Files Exclusion
# ------------------------------------------------------------------------------
# Created by https://www.gitignore.io/api/ocaml

### OCaml ###
*.annot
*.cmo
*.cma
*.cmi
*.a
*.o
*.cmx
*.cmxs
*.cmxa

# ocamlbuild working directory
# dune working directory
_build/

# ocamlbuild targets
*.byte
*.native

# oasis generated files
setup.data
setup.log

# End of https://www.gitignore.io/api/ocaml
src/lexer.ml
src/parser.ml
src/parser.mli
src/polygen
src/ver.ml
src/.depend
# opam local switch
_opam/
2 changes: 2 additions & 0 deletions dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(lang dune 2.8)
(name polygen)
22 changes: 22 additions & 0 deletions polygen.opam
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
opam-version: "2.0"
name: "polygen"
version: "1.0.6"
synopsis: "Polygen"
description: "Polygen — a first effort towards satyre in computer science."
maintainer: "Paolo Donadeo <p.donadeo@gmail.com>"
authors: [ "Alvise Spanò <spano.alvise@gmail.com>" ]
build: [
["dune" "build" "-p" name "-j" jobs]
]
dev-repo: "git+https://github.com/alvisespano/Polygen.git"
post-messages: [
"Polygen installed in %{bin}%/polygen"
{success & !user-setup:installed}
]
depends: [
"js_of_ocaml" { >= "3.9.0"}
"js_of_ocaml-ppx" { >= "3.9.0"}
"dune" { build & >= "2.8.2"}
pdonadeo marked this conversation as resolved.
Show resolved Hide resolved
]
homepage: "http://www.polygen.org/"
bug-reports: "https://github.com/alvisespano/Polygen/issues"
14 changes: 0 additions & 14 deletions src/.depend

This file was deleted.

62 changes: 0 additions & 62 deletions src/README

This file was deleted.

121 changes: 121 additions & 0 deletions src/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# Polygen: build instructions

## Table of contents

* [Requirements](#requirements)
+ [Portability](#portability)
* [Building](#building)
+ [Installing opam](#installing-opam)
+ [Building Polygen](#building-polygen)
* [Using Polygen](#using-polygen)
+ [Command line program](#command-line-program)
+ [Polygen as a library](#polygen-as-a-library)
+ [Polygen inside the browser](#polygen-inside-the-browser)

## Requirements

Polygen is written in OCaml, a highly efficient dialect of the functional language ML - a serious language with a serious paradigm for serious programmers.

Thus, in order to compile it by yourself, you need the following:

- OCaml bytecode (ocamlc) or native code (ocamlopt) compiler
- OCaml lexer (ocamllex) and parser (ocamlyacc) generators
- [`dune`](https://dune.readthedocs.io/en/stable/), the _de facto_ standard build system of the OCaml ecosystem

For instructions to install all of these components see [building](#building) below.


Yuo can learn more about OCaml browsing the [official site](https://ocaml.org/).


### Portability

OCaml is a platform-independent language, since its standard library abstracts from the underlying operating system.

Polygen uses just few I/O functions, all the rest being pure owner code: it is therefore 100% portable to any platform supported by an (even poor) OCaml implementation.

## Building

To build Polygen you have to install the OCaml standard toolchain (compiler, build system, standard libraries, etc.). There are many ways to achive this task, but we think the easiest way is using the [opam](https://opam.ocaml.org/) package manager.

### Installing opam

First of all you need opam to be installed on your computer. Follow the official documentation about [How to install opam](https://opam.ocaml.org/doc/Install.html). Most probably opam is available in your Linux distrubution using the [distro package manager](https://opam.ocaml.org/doc/Install.html#Using-your-distribution-39-s-package-system).

### Building Polygen

Having opam installed it's easy to build and install Polygen.

```bash
$ opam init # answer YES to both the questions
$ eval $(opam env)
$ git clone --recursive https://github.com/alvisespano/Polygen.git
$ cd Polygen
$ opam install .
$ polygen
Polygen (type 2) v1.0.6 build 20180122 - http://www.polygen.org
Manta/Spinning Kids alias Alvise Spano' anno MMII ac insequenti fecit.

usage: polygen [OPTION]... SOURCES...

SOURCE source file(s) providing grammar definition

OPTION
-eof STR use string STR as end-of-file (default: STR = "\n")
-help display this help message
-info alias for '-S I'
-l LABEL add LABEL to initial active label environment
-o DEST output to DEST file
-pedantic set warning level to maximum
-pre output preprocessed source grammar
-seed N pass unsigned integer N as random seed
-S SYM use SYM as starting non-terminal symbol (default: SYM = S)
-t check source grammar and output inferred global label groups
-v be verbose
-X N iterate generation for N times (default: N = 1)
-W N set warning pedantry at level N (default: N = 1)
--help Display this list of options
```

Done!

The `polygen` executable is installed in `$HOME/.opam/default/bin/polygen`: since it's a statically linked program you can move it in the location you prefer in your `$PATH` and remove all the OCaml environment, which is as simple as:

```bash
$ rm -Rf $HOME/.opam
```

## Using Polygen

### Command line program

Polygen first use is via command line: `polygen --help` gives you the complete set of available options.
### Polygen as a library

Polygen is also a library: if you want to include functions from Polygen in your OCaml program the complete library is exposed in the current [opam switch](https://opam.ocaml.org/doc/Usage.html#opam-switch) as `polygen`. If you use `dune` as a build system you have to include the library in the list of used libraries. This is an example of `dune` file:

```
(executable
(name my_program)
(public_name my_program)
(libraries polygen another_library)
(modules my_program another_module_in_my_source)
)
```

Inside your program you have access to the library via the `Polygen_lib` module.


### Polygen inside the browser

The generative subset of the Polygen library is also available in the **browser** because the whole library is also transcompiled into Javascript.

The relevant file is installed in `HOME/.opam/default/lib/polygen/polygen.js`: copy that file where you prefer and include the following line into your HTML:

```html
<script src="polygen.js"></script>
```

From any Javascript program you can now access the global `Polygen` object in which a single function is present: `generate`.

A minimal working example is present in the `html` filder.
38 changes: 38 additions & 0 deletions src/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
(executable
(name polygen)
(public_name polygen)
(package polygen)
(flags (-w -27))
(libraries polygen)
(modules polygen)
)

(library
(name polygen_lib)
(public_name polygen)
;(package polygen)
(flags (-w -27))
(libraries unix)
(modules
polygen_lib
absyn check env err fake gen io lexer
load parser polygen_lib prelude pre ver)
)

(executable
(name polygen_js)
(modes js)
(flags (-w -27))
(libraries polygen)
(modules polygen_js)
(preprocess (pps js_of_ocaml-ppx)))

(install
(files (polygen_js.bc.js as polygen.js))
(section lib)
(package polygen))

(rule
(target ver.ml)
(deps ver.ml.template DATE VERSION gen_ver.sh)
(action (with-stdout-to %{target} (run ./gen_ver.sh))))
2 changes: 1 addition & 1 deletion src/gen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module Gen =
[] -> ""
| B.Epsilon :: l' -> post cap spc l'
| B.Concat :: l' -> post cap "" l'
| B.Capitalize :: l' -> post String.capitalize spc l'
| B.Capitalize :: l' -> post String.capitalize_ascii spc l'
| B.Term s :: l' -> spc ^ (cap s) ^ (post (fun s -> s) " " l')


Expand Down
7 changes: 7 additions & 0 deletions src/gen_ver.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh


VERSION=$(cat VERSION)
DATE=$(cat DATE)

sed -e "s/%VERSION%/${VERSION}/" -e "s/%DATE%/${DATE}/" ver.ml.template
Loading