diff --git a/compiler.ml b/compiler.ml index a9aad8d..1ba9b86 100644 --- a/compiler.ml +++ b/compiler.ml @@ -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); diff --git a/tests/t41-2.txt b/tests/t41-2.txt new file mode 100644 index 0000000..6729944 --- /dev/null +++ b/tests/t41-2.txt @@ -0,0 +1,2 @@ +let a = {1,2,3,4,5,6,7,8,9,10} in + print a[100] \ No newline at end of file