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

Add backend plugin support #213

Merged
merged 5 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions .github/workflows/run-make-all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,14 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
clean: false
- name: Re-initialize python dependencies
run: |
rm -rf french_law/python/env
./french_law/python/setup_env.sh
- name: Install dependencies
run: |
opam exec -- make dependencies
opam exec -- make dependencies pygments
- name: Check promoted files
run: |
rm -f bad-promote
opam exec -- make check-promoted > promotion.out 2>&1 || touch bad-promote
- name: Make all
run: |
Expand Down
7 changes: 7 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ To add support for a new language:
Feel free to open a pull request for discussion even if you couldn't go through
all these steps, the `lexer_xx.cppo.ml` file is the important part.

### Example: writing custom backends as plugins

Catala has support for dynamically-loaded plugins to use as alternative
backends. See `compiler/plugins` for examples, and [the
documentation](https://catala-lang.org/ocaml_docs/catala/plugins.html) for more
detail.

### Automatic formatting

Please ensure to submit commits formatted using the included `ocamlformat`
Expand Down
14 changes: 10 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ doc:
install:
dune build @install

#> plugins : Builds the demonstration plugins
plugins:
dune build compiler/plugins/
@echo "define CATALA_PLUGINS=_build/default/compiler/plugins to test the plugins"

##########################################
# Rules related to promoted files
##########################################
Expand All @@ -85,15 +90,15 @@ SYNTAX_HIGHLIGHTING_PL=${CURDIR}/syntax_highlighting/pl

pygmentize_fr: $(SYNTAX_HIGHLIGHTING_FR)/set_up_pygments.sh
chmod +x $<
sudo $<
$<

pygmentize_en: $(SYNTAX_HIGHLIGHTING_EN)/set_up_pygments.sh
chmod +x $<
sudo $<
$<

pygmentize_pl: $(SYNTAX_HIGHLIGHTING_PL)/set_up_pygments.sh
chmod +x $<
sudo $<
$<

#> pygments : Extends your pygmentize executable with Catala lexers
pygments: pygmentize_fr pygmentize_en pygmentize_pl
Expand Down Expand Up @@ -306,6 +311,7 @@ website-assets: doc js_build literate_examples grammar.html catala.html build_fr
all: \
build js_build doc website-assets\
tests \
plugins \
generate_french_law_library_ocaml build_french_law_library_ocaml \
tests_ocaml bench_ocaml \
build_french_law_library_js \
Expand Down Expand Up @@ -340,4 +346,4 @@ help_catala:
##########################################
.PHONY: inspect clean all literate_examples english allocations_familiales pygments \
install build_dev build doc format dependencies dependencies-ocaml \
catala.html help parser-messages
catala.html help parser-messages plugins
39 changes: 19 additions & 20 deletions build_system/clerk_driver.ml
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ type expected_output_descr = {
base_filename : string;
output_dir : string;
complete_filename : string;
backend : Cli.backend_option;
backend : string Cli.backend_option;
scope : string option;
}

Expand All @@ -156,18 +156,18 @@ let filename_to_expected_output_descr (output_dir : string) (filename : string)
let filename = Filename.remove_extension filename in
let backend =
match String.lowercase_ascii first_extension with
| ".dcalc" -> Some Cli.Dcalc
| ".d" -> Some Cli.Makefile
| ".html" -> Some Cli.Html
| ".interpret" -> Some Cli.Interpret
| ".lcalc" -> Some Cli.Lcalc
| ".ml" -> Some Cli.OCaml
| ".proof" -> Some Cli.Proof
| ".py" -> Some Cli.Python
| ".scalc" -> Some Cli.Scalc
| ".scopelang" -> Some Cli.Scopelang
| ".tex" -> Some Cli.Latex
| ".typecheck" -> Some Cli.Typecheck
| ".dcalc" -> Some `Dcalc
| ".d" -> Some `Makefile
| ".html" -> Some `Html
| ".interpret" -> Some `Interpret
| ".lcalc" -> Some `Lcalc
| ".ml" -> Some `OCaml
| ".proof" -> Some `Proof
| ".py" -> Some `Python
| ".scalc" -> Some `Scalc
| ".scopelang" -> Some `Scopelang
| ".tex" -> Some `Latex
| ".typecheck" -> Some `Typecheck
| _ -> None
in
match backend with
Expand Down Expand Up @@ -419,8 +419,7 @@ let collect_all_ninja_build
[
( "catala_cmd",
Nj.Expr.Lit
(Cli.catala_backend_option_to_string expected_output.backend)
);
(Cli.backend_option_to_string expected_output.backend) );
"tested_file", Nj.Expr.Lit tested_file;
( "expected_output",
Nj.Expr.Lit
Expand All @@ -429,7 +428,7 @@ let collect_all_ninja_build
]
and output_build_kind = if reset_test_outputs then "reset" else "test"
and catala_backend =
Cli.catala_backend_option_to_string expected_output.backend
Cli.backend_option_to_string expected_output.backend
in

let get_rule_infos ?(rule_postfix = "") :
Expand Down Expand Up @@ -465,14 +464,14 @@ let collect_all_ninja_build
in

match expected_output.backend with
| Cli.Interpret | Cli.Proof | Cli.Typecheck | Cli.Dcalc
| Cli.Scopelang | Cli.Scalc | Cli.Lcalc ->
| `Interpret | `Proof | `Typecheck | `Dcalc | `Scopelang | `Scalc
| `Lcalc ->
let rule_output, rule_name, rule_vars =
get_rule_infos expected_output.scope
in
let rule_vars =
match expected_output.backend with
| Cli.Proof ->
| `Proof ->
("extra_flags", Nj.Expr.Lit "--disable_counterexamples")
:: rule_vars
(* Counterexamples can be different at each call because of the
Expand All @@ -483,7 +482,7 @@ let collect_all_ninja_build
in
( ninja_add_new_rule rule_output rule_name rule_vars ninja,
test_names ^ " $\n " ^ rule_output )
| Cli.Python | Cli.OCaml | Cli.Latex | Cli.Html | Cli.Makefile ->
| `Python | `OCaml | `Latex | `Html | `Makefile | `Plugin _ ->
let tmp_file = Filename.temp_file "clerk_" ("_" ^ catala_backend) in
let rule_output, rule_name, rule_vars =
get_rule_infos ~rule_postfix:"_and_output" expected_output.scope
Expand Down
1 change: 1 addition & 0 deletions compiler/catala_web_interpreter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ let _ =
wrap_weaved_output = false;
avoid_exceptions = false;
backend = "Interpret";
plugins_dirs = [];
language = Some (Js.to_string language);
max_prec_digits = None;
closure_conversion = false;
Expand Down
Loading