Skip to content

Commit

Permalink
Adjust to pull request #146 generic-table-access
Browse files Browse the repository at this point in the history
  • Loading branch information
mdurero committed Mar 30, 2023
1 parent 3468636 commit 0b0fed1
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions src/mlang/backend_compilers/bir_to_ocaml.ml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ let rec generate_ocaml_expr (e : Bir.expression Pos.marked) :
(Pos.unmark var.mir_var.name),
[] )
| LocalVar _lvar -> ("localvar", []) (*TODO*)
| GenericTableIndex -> ("generic table index", []) (* TODO *)
| Error -> assert false (* should not happen *)
| LocalLet (lvar, e1, e2) ->
let _, local1 = generate_ocaml_expr e1 in
Expand All @@ -98,6 +97,16 @@ let format_tgv_set (variable_expression : string) (oc : Format.formatter)
Format.fprintf oc "Array.set tgv %d %s;@," variable_position
variable_expression

let format_tgv_set_with_offset (variable_position : int) (offset_tgv_variable : Bir.variable) (oc : Format.formatter)
(variable_expression : string): unit =
Format.fprintf oc
"Array.set tgv (%d + (((*%s*) Array.get tgv %d).value |> int_of_float)) \
%s;@,"
variable_position
(Pos.unmark offset_tgv_variable.mir_var.name)
(get_var_pos offset_tgv_variable)
variable_expression

let format_local_defs (oc : Format.formatter)
(defs : (Mir.LocalVariable.t * Bir.expression Pos.marked) list) : unit =
Format.pp_print_list
Expand Down Expand Up @@ -126,22 +135,15 @@ let generate_var_def (variable : Bir.variable) (vdata : Bir.variable_data)
(format_tgv_set tgv_expression)
(get_var_pos variable |> ( + ) i)))
es
| TableVar (size, IndexGeneric e) ->
| TableVar (_size, IndexGeneric (v, e)) ->
let tgv_expression, local_defs = generate_ocaml_expr e in
let list_pos =
let rec integer_range first_item last_item =
if first_item > last_item then []
else first_item :: integer_range (first_item + 1) last_item
in
integer_range (get_var_pos variable) (get_var_pos variable + size)
in
let aux_print_list (list_pos : int list) (oc : Format.formatter)
(tgv_expression : string) =
List.iter (format_tgv_set tgv_expression oc) list_pos
in
Format.fprintf oc "%a(*Table %s*)@,%a" format_local_defs local_defs
Format.fprintf oc
"if (Array.get tgv %d (*%s*)).undefined then %a(*Table %s*)@,%a"
(get_var_pos v)
(Pos.unmark v.mir_var.name)
format_local_defs local_defs
(Pos.unmark variable.mir_var.name)
(aux_print_list list_pos) tgv_expression
(format_tgv_set_with_offset (get_var_pos variable) v) tgv_expression
| InputVar -> assert false

let rec generate_stmts (program : Bir.program) (oc : Format.formatter)
Expand Down

0 comments on commit 0b0fed1

Please sign in to comment.