Created
September 1, 2022 05:40
-
-
Save mjambon/6dee3cf344d2a9672357630993208935 to your computer and use it in GitHub Desktop.
Deriving language-specific ASTs and conversions to generic AST
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Generic = struct | |
(* generic expr type used for matching *) | |
type expr = | |
| A | |
| B of expr * expr [@lang a] | |
| C of expr [@lang b c] | |
| Unsupported of string [@fallback] | |
end | |
(*** Generated from the above ***) | |
module A = struct | |
(* expr is a subtype of Generic.expr used for parsing and printing *) | |
type expr = | |
| A | |
| B of expr * expr | |
| Unsupported of string | |
let rec to_generic_expr (x : expr) : Generic.expr = | |
match x with | |
| A -> Generic.A | |
| B (v1, v2) -> Generic.B (to_generic_expr v1, to_generic_expr v2) | |
| Unsupported x -> Unsupported x | |
let rec of_generic_expr (x : Generic.expr) : expr = | |
match x with | |
| Generic.A -> A | |
| Generic.B (v1, v2) -> B (of_generic_expr v1, of_generic_expr v2) | |
| Generic.C _ -> Unsupported "C" | |
| Generic.Unsupported x -> Unsupported x | |
end | |
module B = struct | |
(* expr is a subtype of Generic.expr used for parsing and printing *) | |
type expr = | |
| A | |
| C of expr | |
| Unsupported of string | |
let rec to_generic_expr (x : expr) : Generic.expr = | |
match x with | |
| A -> Generic.A | |
| C x -> Generic.C (to_generic_expr x) | |
| Unsupported x -> Generic.Unsupported x | |
let rec of_generic_expr (x : Generic.expr) : expr = | |
match x with | |
| Generic.A -> A | |
| Generic.B _ -> Unsupported "B" | |
| Generic.C x -> C (of_generic_expr x) | |
| Generic.Unsupported x -> Unsupported x | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment