Skip to content

Commit 5340c01

Browse files
committed
refactor compile_array_index_lvalue
1 parent e64cd5b commit 5340c01

File tree

3 files changed

+55
-29
lines changed

3 files changed

+55
-29
lines changed

compiler/src/expressions.rs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ impl Compiler {
137137
array_index_assign: ArrayIndexAssign,
138138
) -> *mut gcc_jit_rvalue {
139139
let (lvalue, _) = self.access_identifier_values(Rc::clone(&scope), array_index_assign.from_package);
140-
let array_index_lvalue = self.array_dimension_as_lvalue(Rc::clone(&scope), lvalue, array_index_assign.dimensions);
140+
let array_index_lvalue =
141+
self.array_dimension_as_lvalue(Rc::clone(&scope), lvalue, array_index_assign.dimensions);
141142

142143
let block_func = self.block_func_ref.lock().unwrap();
143144
if let Some(block) = block_func.block {
@@ -170,11 +171,12 @@ impl Compiler {
170171
}
171172
_ => {
172173
let rvalue_type = unsafe { gcc_jit_rvalue_get_type(gcc_jit_lvalue_as_rvalue(array_index_lvalue)) };
174+
let expr = array_index_assign.expr.clone();
173175
return self.safe_assign_lvalue(
174176
Rc::clone(&scope),
175177
array_index_lvalue,
176178
rvalue_type,
177-
array_index_assign.expr.clone(),
179+
expr,
178180
array_index_assign.loc,
179181
);
180182
}
@@ -190,12 +192,19 @@ impl Compiler {
190192
variable: *mut gcc_jit_lvalue,
191193
dimensions: Vec<Expression>,
192194
) -> *mut gcc_jit_lvalue {
195+
if dimensions.len() == 0 {
196+
compiler_error!("You are trying to access an array item lvalue with empty dimension.")
197+
}
198+
193199
let mut result: *mut gcc_jit_lvalue = variable;
194200

195201
for dim in dimensions {
196202
if let Expression::Array(index_expr) = dim {
197-
if let Expression::Array(value) = index_expr.elements[0].clone() {
198-
let idx = self.compile_expression(Rc::clone(&scope), value.elements[0].clone());
203+
if let Expression::Literal(Literal::Integer(integer_literal)) = index_expr.elements[0].clone() {
204+
let idx = self.compile_expression(
205+
Rc::clone(&scope),
206+
Expression::Literal(Literal::Integer(integer_literal)),
207+
);
199208

200209
let lvalue = unsafe {
201210
gcc_jit_context_new_array_access(
@@ -207,10 +216,16 @@ impl Compiler {
207216
};
208217

209218
result = lvalue;
219+
} else {
220+
compiler_error!("Unable to access array lvalue through a non-integer literal.")
210221
}
211222
}
212223
}
213224

225+
if result == variable {
226+
compiler_error!("Unexpected behavior when trying to compile array_dimension_as_lvalue.")
227+
}
228+
214229
result
215230
}
216231

@@ -304,6 +319,7 @@ impl Compiler {
304319
compiler_error!("Incorrect usage of the assignment. Assignments must be performed inside a valid block.");
305320
}
306321
}
322+
307323
fn compile_assignment(&mut self, scope: ScopeRef, assignment: Assignment) -> *mut gcc_jit_rvalue {
308324
let (lvalue, rvalue) = self.access_identifier_values(Rc::clone(&scope), assignment.identifier);
309325
let rvalue_type = unsafe { gcc_jit_rvalue_get_type(rvalue) };
@@ -521,10 +537,18 @@ impl Compiler {
521537
},
522538
Literal::Float(float_literal) => match float_literal {
523539
FloatLiteral::Float(value) => unsafe {
524-
gcc_jit_context_new_rvalue_from_double(self.context, Compiler::float_type(self.context), value as f64)
540+
gcc_jit_context_new_rvalue_from_double(
541+
self.context,
542+
Compiler::float_type(self.context),
543+
value as f64,
544+
)
525545
},
526546
FloatLiteral::Double(value) => unsafe {
527-
gcc_jit_context_new_rvalue_from_double(self.context, Compiler::double_type(self.context), value as f64)
547+
gcc_jit_context_new_rvalue_from_double(
548+
self.context,
549+
Compiler::double_type(self.context),
550+
value as f64,
551+
)
528552
},
529553
},
530554
Literal::Bool(bool_literal) => {

examples/main.cyr

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import std::io;
22

33
pub fn main() {
4-
for (#i = 0; i < 10; i++) {
5-
std::io::printf("%d\n", i);
6-
}
4+
#a: i32[3] = [10, 2];
5+
6+
a = [5, 6];
7+
8+
std::io::printf("result: %d\n", a[0]);
79
}

parser/src/expressions.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -442,25 +442,6 @@ impl<'a> Parser<'a> {
442442
}
443443
}
444444

445-
pub fn parse_array_index_assign(&mut self, array_index: ArrayIndex) -> Result<Expression, ParseError> {
446-
self.next_token(); // consume right bracket
447-
448-
self.expect_current(TokenKind::Assign)?;
449-
450-
let expr = self.parse_expression(Precedence::Lowest)?.0;
451-
452-
Ok(Expression::ArrayIndexAssign(Box::new(ArrayIndexAssign {
453-
from_package: array_index.from_package,
454-
dimensions: array_index.dimensions,
455-
span: Span {
456-
start: array_index.span.start,
457-
end: self.current_token.span.end,
458-
},
459-
loc: self.current_location(),
460-
expr,
461-
})))
462-
}
463-
464445
pub fn parse_struct_member(&mut self, object_expr: Box<Expression>) -> Result<Expression, ParseError> {
465446
let start = self.current_token.span.start.clone();
466447

@@ -698,6 +679,25 @@ impl<'a> Parser<'a> {
698679
})))
699680
}
700681

682+
pub fn parse_array_index_assign(&mut self, array_index: ArrayIndex) -> Result<Expression, ParseError> {
683+
self.next_token(); // consume right bracket
684+
685+
self.expect_current(TokenKind::Assign)?;
686+
687+
let expr = self.parse_expression(Precedence::Lowest)?.0;
688+
689+
Ok(Expression::ArrayIndexAssign(Box::new(ArrayIndexAssign {
690+
from_package: array_index.from_package,
691+
dimensions: array_index.dimensions,
692+
span: Span {
693+
start: array_index.span.start,
694+
end: self.current_token.span.end,
695+
},
696+
loc: self.current_location(),
697+
expr,
698+
})))
699+
}
700+
701701
pub fn parse_array_index(&mut self, from_package: FromPackage) -> Result<ArrayIndex, ParseError> {
702702
let start = self.current_token.span.start;
703703

@@ -711,7 +711,7 @@ impl<'a> Parser<'a> {
711711
let end = self.current_token.span.end;
712712

713713
Ok(ArrayIndex {
714-
dimensions: vec![],
714+
dimensions,
715715
span: Span { start, end },
716716
from_package: from_package,
717717
loc: self.current_location(),

0 commit comments

Comments
 (0)