Skip to content

Commit f99c8f3

Browse files
Fix: Dictionary indexing support for d[key] inside function calls like print.
1 parent 027c3b3 commit f99c8f3

3 files changed

Lines changed: 22 additions & 7 deletions

File tree

compiler/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ fn main() {
2929

3030
initialize_logger();
3131

32-
let source = "def add(a, b): return a + b\nresult = add(1, 2)";
33-
32+
let source = "d = {1: 'one', 2: True, 'x': 3.14}\nprint(d)\nprint(d[2])\nprint(d['x'])";
33+
3434
let chunk = modules::parser::Parser::new(source, modules::lexer::lexer(source)).parse();
3535

3636
// Instructions.

compiler/src/modules/parser.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,13 +316,13 @@ impl<'src, I: Iterator<Item = Token>> Parser<'src, I> {
316316
fn dict_literal(&mut self) {
317317
let mut pairs = 0u16;
318318
while !matches!(self.peek(), Some(TokenType::Rbrace) | None) {
319-
self.expr(); // clave
320-
if matches!(self.peek(), Some(TokenType::Colon)) { self.advance(); }
321-
self.expr(); // valor
319+
self.expr(); // clave
320+
self.eat(TokenType::Colon); // ← ahora OBLIGATORIO
321+
self.expr(); // valor
322322
pairs += 1;
323-
if matches!(self.peek(), Some(TokenType::Comma)) { self.advance(); }
323+
self.eat_if(TokenType::Comma);
324324
}
325-
self.advance(); // consume '}'
325+
self.advance(); // '}'
326326
self.chunk.emit(OpCode::BuildDict, pairs);
327327
}
328328

@@ -483,6 +483,14 @@ impl<'src, I: Iterator<Item = Token>> Parser<'src, I> {
483483
Some(TokenType::Lpar) => self.call(name),
484484
_ => self.emit_load_ssa(name),
485485
}
486+
487+
// NUEVO: soporte para d[expr], d["x"], etc. (funciona en cualquier sitio)
488+
while matches!(self.peek(), Some(TokenType::Lsqb)) {
489+
self.advance(); // consume [
490+
self.expr();
491+
self.eat(TokenType::Rsqb);
492+
self.chunk.emit(OpCode::GetItem, 0);
493+
}
486494
}
487495

488496
fn assign(&mut self, name: String) {

compiler/tests/cases/parser_cases.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,12 @@
7575
"names": ["x_1", "a_0", "x_2", "b_0", "x_3", "x_4", "x_5", "y_1"],
7676
"instructions": [["LoadConst",0], ["StoreName",0], ["PopTop",0], ["LoadName",1], ["JumpIfFalse",9], ["LoadConst",1], ["StoreName",2], ["PopTop",0], ["Jump",18], ["LoadName",3], ["JumpIfFalse",15], ["LoadConst",2], ["StoreName",4], ["PopTop",0], ["Jump",18], ["LoadConst",3], ["StoreName",5], ["PopTop",0], ["Phi",6], ["LoadName",6], ["StoreName",7], ["PopTop",0], ["ReturnValue",0]],
7777
"annotations": {}
78+
},
79+
{
80+
"src": "d = {1: 'one', 2: True, 'x': 3.14}\nprint(d)\nprint(d[2])\nprint(d['x'])",
81+
"constants": ["1", "one", "2", "true", "x", "3.14", "2", "x"],
82+
"names": ["d_1"],
83+
"instructions": [["LoadConst",0], ["LoadConst",1], ["LoadConst",2], ["LoadConst",3], ["LoadConst",4], ["LoadConst",5], ["BuildDict",3], ["StoreName",0], ["PopTop",0], ["LoadName",0], ["CallPrint",0], ["PopTop",0], ["LoadName",0], ["LoadConst",6], ["GetItem",0], ["CallPrint",0], ["PopTop",0], ["LoadName",0], ["LoadConst",7], ["GetItem",0], ["CallPrint",0], ["PopTop",0], ["ReturnValue",0]],
84+
"annotations": {}
7885
}
7986
]

0 commit comments

Comments
 (0)