12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697(**************************************************************************)(* This file is part of the Codex semantics library. *)(* *)(* Copyright (C) 2013-2025 *)(* CEA (Commissariat à l'énergie atomique et aux énergies *)(* alternatives) *)(* *)(* you can redistribute it and/or modify it under the terms of the GNU *)(* Lesser General Public License as published by the Free Software *)(* Foundation, version 2.1. *)(* *)(* It is distributed in the hope that it will be useful, *)(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)(* GNU Lesser General Public License for more details. *)(* *)(* See the GNU Lesser General Public License version 2.1 *)(* for more details (enclosed in the file LICENSE). *)(* *)(**************************************************************************)(* $MDX part-begin=variables *)moduleVar=structtypet={name:string;id:int}letuniq=Hashtbl.create117;;letid_count=ref0;;letof_stringname=tryHashtbl.finduniqnamewithNot_found->letid=!id_countinincrid_count;letvar={name;id}inHashtbl.replaceuniqnamevar;varletto_intx=x.idletcomparexy=Int.comparex.idy.idletppfmtx=Format.pp_print_stringfmtx.nameend(* $MDX part-end *)(* Arithmetic expressions *)(* $MDX part-begin=aexpressions *)typeaexp=|Intofint(* integer constant *)|VarofVar.t(* variable *)|Addofaexp*aexp(* addition *)|Subofaexp*aexp(* subtraction *)|Mulofaexp*aexp(* multiplication *)(* $MDX part-end *)(* This does not handle parentheses well *)letrecpp_aexpfmt=function|Inti->Format.pp_print_intfmti|Varv->Var.ppfmtv|Add(l,r)->Format.fprintffmt"%a + %a"pp_aexplpp_aexpr|Sub(l,r)->Format.fprintffmt"%a - %a"pp_aexplpp_aexpr|Mul(l,r)->Format.fprintffmt"%a * %a"pp_aexplpp_aexpr(* Boolean expressions *)(* $MDX part-begin=bexpressions *)typebexp=|True|False|Eqofaexp*aexp(* equality *)|Leofaexp*aexp(* less than or equal *)|Gtofaexp*aexp(* strictly greater than *)|Notofbexp(* negation *)|Andofbexp*bexp(* conjunction *)(* $MDX part-end *)letrecpp_bexpfmt=function|True->Format.fprintffmt"True"|False->Format.fprintffmt"False"|Eq(l,r)->Format.fprintffmt"%a == %a"pp_aexplpp_aexpr|Le(l,r)->Format.fprintffmt"%a <= %a"pp_aexplpp_aexpr|Gt(l,r)->Format.fprintffmt"%a > %a"pp_aexplpp_aexpr|Notb->Format.fprintffmt"!(%a)"pp_bexpb|And(l,r)->Format.fprintffmt"%a && %a"pp_bexplpp_bexpr(* Statements *)(* $MDX part-begin=statements *)typestmt=|Skip(* do nothing *)|AssignofVar.t*aexp(* assignment *)|Seqofstmt*stmt(* sequence of statements *)|Ifofbexp*stmt*stmt(* conditional *)|Whileofbexp*stmt(* while loop *)(* $MDX part-end *)letpp_stmtfmt=function|Skip->Format.fprintffmt"Skip"|Assign(v,e)->Format.fprintffmt"%a := %a"Var.ppvpp_aexpe|Seq_->Format.fprintffmt"Seq"|If(b,_,_)->Format.fprintffmt"If %a"pp_bexpb|While(b,_)->Format.fprintffmt"While %a"pp_bexpb