Starting to translate expressions [skip ci]

This commit is contained in:
Denis Merigoux 2022-03-01 10:15:44 +01:00
parent 171e8932bc
commit 5a0719b25d
No known key found for this signature in database
GPG Key ID: EE99DCFA365C3EE3

View File

@ -16,6 +16,51 @@
open Utils
(** {1 Expression translation}*)
type target_scope_vars =
| WholeVar of Scopelang.Ast.ScopeVar.t
| States of (Desugared.Ast.StateName.t * Scopelang.Ast.ScopeVar.t) list
type ctx = {
scope_var_mapping : target_scope_vars Desugared.Ast.ScopeVarMap.t;
var_mapping : Scopelang.Ast.Var.t Desugared.Ast.VarMap.t;
}
let rec translate_expr (ctx : ctx) (e : Desugared.Ast.expr Pos.marked) :
Scopelang.Ast.expr Pos.marked =
Pos.map_under_mark
(fun e ->
match e with
| Desugared.Ast.ELocation (SubScopeVar (s_name, ss_name, s_var)) ->
(* When referring to a subscope variable in an expression, we are referring to the output,
hence we take the last state. *)
let new_s_var =
match Desugared.Ast.ScopeVarMap.find (Pos.unmark s_var) ctx.scope_var_mapping with
| WholeVar new_s_var -> Pos.same_pos_as new_s_var s_var
| States states -> Pos.same_pos_as (snd (List.hd (List.rev states))) s_var
in
Scopelang.Ast.ELocation (SubScopeVar (s_name, ss_name, new_s_var))
| Desugared.Ast.ELocation (ScopeVar (s_var, None)) ->
Scopelang.Ast.ELocation
(ScopeVar
(match Desugared.Ast.ScopeVarMap.find (Pos.unmark s_var) ctx.scope_var_mapping with
| WholeVar new_s_var -> Pos.same_pos_as new_s_var s_var
| States _ -> failwith "should not happen"))
| Desugared.Ast.ELocation (ScopeVar (s_var, Some state)) ->
Scopelang.Ast.ELocation
(ScopeVar
(match Desugared.Ast.ScopeVarMap.find (Pos.unmark s_var) ctx.scope_var_mapping with
| WholeVar _ -> failwith "should not happen"
| States states -> Pos.same_pos_as (List.assoc state states) s_var))
| Desugared.Ast.EVar v ->
Scopelang.Ast.EVar
(Pos.same_pos_as (Desugared.Ast.VarMap.find (Pos.unmark v) ctx.var_mapping) v)
| EStruct (s_name, fields) ->
EStruct (s_name, Scopelang.Ast.StructFieldMap.map (translate_expr ctx) fields)
| _ -> assert false)
e
(** {1 Rule tree construction} *)
(** Intermediate representation for the exception tree of rules for a particular scope definition. *)