Skip to content

Commit

Permalink
Add control array out of bound : alpha version
Browse files Browse the repository at this point in the history
  • Loading branch information
epatrizio committed Mar 21, 2022
1 parent 598a477 commit 43b32ff
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
7 changes: 5 additions & 2 deletions compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ let rec compile_expr ?(label = "") e env k li =
| Ederef i -> compile_expr (Eident i) env k li @ ["GETFIELD 0"] @ li
| Earray [] -> error "empty array"
| Earray l -> compile_array_expr (List.rev l) env k li @ ["MAKEBLOCK " ^ string_of_int (List.length l)] @ li
| Eaget (i,e) -> compile_expr e env k li @ ["PUSH"] @ compile_expr (Eident i) env (k+1) li @ ["GETVECTITEM"] @ li
| Eaget (i,e) ->
let tmp = "_tmp_" ^ string_of_int (counter ()) in
compile_stmt (Sassign (tmp, e, Sif (Ebinop (Bge, (Eident tmp), (Easize i)), Sexit, Sskip))) env li @
compile_expr e env k li @ ["PUSH"] @ compile_expr (Eident i) env (k+1) li @ ["GETVECTITEM"] @ li
| Easize i -> compile_expr (Eident i) env k li @ ["VECTLENGTH"] @ li

let rec compile_stmt ?(label = "") s env li =
and compile_stmt ?(label = "") s env li =
match s with
| Sassign(i,e,s) ->
if List.mem i env then error ("local var already bound: " ^ i);
Expand Down
2 changes: 2 additions & 0 deletions tests/t41-2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
let a = {1,2,3,4,5,6,7,8,9,10} in
print a[100]

0 comments on commit 43b32ff

Please sign in to comment.