https://github.com/jmid/js_of_ocaml-xyz
An example of compiling a simple language processor in OCaml to JavaScript with js_of_ocaml
https://github.com/jmid/js_of_ocaml-xyz
Last synced: over 1 year ago
JSON representation
An example of compiling a simple language processor in OCaml to JavaScript with js_of_ocaml
- Host: GitHub
- URL: https://github.com/jmid/js_of_ocaml-xyz
- Owner: jmid
- License: unlicense
- Created: 2014-09-23T13:51:36.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2014-09-23T14:03:41.000Z (almost 12 years ago)
- Last Synced: 2025-01-29T11:27:25.287Z (over 1 year ago)
- Language: OCaml
- Size: 117 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
A js_of_ocaml example
=====================
This is a simple example of how a language processor written in OCaml
with ocamllex and [menhir](http://gallium.inria.fr/~fpottier/menhir/)
can be compiled to a client-side JavaScript application with
[js_of_ocaml](http://ocsigen.org/js_of_ocaml/).
Internally, it builds and processes an abstract syntax tree (AST) for
a simple language of arithmetic expressions. The AST is defined in
ast.ml. It is built recursively in the action statements of each
grammar production in parser.mly, and it is interpreted in eval.ml.
The supported syntax is:
exp ::= x | y | z
| exp + exp
| exp - exp
| exp * exp
| exp / exp
| ( exp )
Requirements: ocaml, ocamlfind, menhir, js_of_ocaml
To build:
---------
$ make
To run:
-------
Open index.html in a browser
Example 1:
- Press 'Evaluate' (with '(x + y) * z' still in the text area)
- the result 9 appears
(hence '(x + y) * z' evaluates to 9 when x=1, y=2, and z=3)
Example 2:
- Enter 'x + y--' (without the quotes) in the text area
- Press 'Evaluate'
- the result 'Parser.Error' appears
(hence the string 'x + y--' is rejected by the parser)
Example 3:
- Enter '2 + 2' (without the quotes) in the text area
- Press 'Evaluate'
- the result 'Failure in lexing: empty token' appears
(hence the string '2 + 2' is rejected by the scanner)