diff --git a/src/passes.ml b/src/passes.ml new file mode 100644 index 0000000..155dd04 --- /dev/null +++ b/src/passes.ml @@ -0,0 +1,67 @@ +(** This file contains simplification passes for our Lustre-like AST *) + +open Ast + +(** [node_pass] is an auxiliary function used to write passes: it will iterate + * the function passed as argument on all the nodes of the program *) +let node_pass f ast: t_nodelist option = + Utils.list_map_option f ast + +(** [equation_pass] is an auxiliary function used to write passes: it will + * iterate the function passed as argument on all the equations of the + * program *) +let equation_pass f ast: t_nodelist option = + let aux (node: t_node): t_node option = + match Utils.list_map_option f node.n_equations with + | None -> None + | Some eqs -> Some {n_name = node.n_name; + n_inputs = node.n_inputs; + n_outputs = node.n_outputs; + n_local_vars = node.n_local_vars; + n_equations = eqs; + n_inputs_type = node.n_inputs_type; + n_outputs_type = node.n_outputs_type; + } + in + node_pass aux ast + +let expression_pass f: t_nodelist -> t_nodelist option = + let aux (patt, expr) = + match f expr with + | None -> None + | Some expr -> Some (patt, expr) + in + equation_pass aux + +let pre2vars = + let rec aux = function + | EVar (ty, v) -> EVar (ty, v) + | EMonOp (ty, mop, expr) -> + let expr = aux expr in EMonOp (ty, mop, expr) + | EBinOp (ty, bop, expr, expr') -> + let expr = aux expr in let expr' = aux expr' in + EBinOp (ty, bop, expr, expr') + | ETriOp (ty, top, expr, expr', expr'') -> + let expr = aux expr in let expr' = aux expr' in + let expr'' = aux expr'' in + ETriOp (ty, top, expr, expr', expr'') + | EComp (ty, cop, expr, expr') -> + let expr = aux expr in let expr' = aux expr' in + EComp (ty, cop, expr, expr') + | EWhen (ty, expr, expr') -> + let expr = aux expr in let expr' = aux expr' in + EWhen (ty, expr, expr') + | EReset (ty, expr, expr') -> + let expr = aux expr in let expr' = aux expr' in + EReset (ty, expr, expr') + | EConst (ty, c) -> EConst (ty, c) + | ETuple (ty, elist) -> + let elist = + List.fold_right (fun expr acc -> (aux expr) :: acc) elist [] in + ETuple (ty, elist) + | EApp (ty, node, arg) -> + let arg = aux arg in + EApp (ty, node, arg) + in + expression_pass (Utils.somify aux) +