diff --git a/nac3core/src/codegen/expr.rs b/nac3core/src/codegen/expr.rs index 1c989e69..04790e42 100644 --- a/nac3core/src/codegen/expr.rs +++ b/nac3core/src/codegen/expr.rs @@ -14,7 +14,7 @@ use inkwell::{ use itertools::{chain, izip, zip, Itertools}; use rustpython_parser::ast::{self, Boolop, Constant, Expr, ExprKind, Operator}; -fn assert_int_val<'ctx>(val: BasicValueEnum<'ctx>) -> IntValue<'ctx> { +pub fn assert_int_val<'ctx>(val: BasicValueEnum<'ctx>) -> IntValue<'ctx> { if let BasicValueEnum::IntValue(v) = val { v } else { @@ -22,7 +22,7 @@ fn assert_int_val<'ctx>(val: BasicValueEnum<'ctx>) -> IntValue<'ctx> { } } -fn assert_pointer_val<'ctx>(val: BasicValueEnum<'ctx>) -> PointerValue<'ctx> { +pub fn assert_pointer_val<'ctx>(val: BasicValueEnum<'ctx>) -> PointerValue<'ctx> { if let BasicValueEnum::PointerValue(v) = val { v } else { @@ -556,7 +556,6 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> { let arr_ptr = assert_pointer_val(self.builder.build_load(ptr_to_arr, "loadptr")); let ptr = self.builder.build_gep(arr_ptr, &[index], "loadarrgep"); - println!("building element pointer"); self.builder.build_load(ptr, "loadarr") } } diff --git a/nac3core/src/codegen/stmt.rs b/nac3core/src/codegen/stmt.rs index 6c920ba6..66209b93 100644 --- a/nac3core/src/codegen/stmt.rs +++ b/nac3core/src/codegen/stmt.rs @@ -1,4 +1,7 @@ -use super::CodeGenContext; +use super::{ + expr::{assert_int_val, assert_pointer_val}, + CodeGenContext, +}; use crate::typecheck::typedef::Type; use inkwell::values::{BasicValue, BasicValueEnum, PointerValue}; use rustpython_parser::ast::{Expr, ExprKind, Stmt, StmtKind}; @@ -45,7 +48,21 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> { ) } } - ExprKind::Subscript { .. } => unimplemented!(), + ExprKind::Subscript { value, slice, .. } => { + let i32_type = self.ctx.i32_type(); + let v = assert_pointer_val(self.gen_expr(value).unwrap()); + let index = assert_int_val(self.gen_expr(slice).unwrap()); + unsafe { + let ptr_to_arr = self.builder.build_in_bounds_gep( + v, + &[i32_type.const_zero(), i32_type.const_int(1, false)], + "ptr_to_arr", + ); + let arr_ptr = + assert_pointer_val(self.builder.build_load(ptr_to_arr, "loadptr")); + self.builder.build_gep(arr_ptr, &[index], "loadarrgep") + } + } _ => unreachable!(), } }