Skip to content

Commit c9df7e4

Browse files
Fix: Sub-type anotation.
1 parent 7145d4e commit c9df7e4

3 files changed

Lines changed: 42 additions & 12 deletions

File tree

compiler/src/modules/parser/stmt.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -281,18 +281,24 @@ impl<'src, I: Iterator<Item = Token>> Parser<'src, I> {
281281
pub(super) fn name_stmt(&mut self, t: Token) -> bool {
282282
let name = self.lexeme(&t).to_string();
283283

284-
if self.eat_if(TokenType::Colon) {
285-
if matches!(self.peek(), Some(TokenType::Name)) {
286-
let ann = {
287-
let t = self.advance();
288-
self.lexeme(&t).to_string()
289-
};
290-
self.chunk.annotations.insert(name.clone(), ann);
291-
}
292-
if !matches!(self.peek(), Some(TokenType::Equal)) {
293-
return false;
294-
}
284+
if self.eat_if(TokenType::Colon) {
285+
if matches!(self.peek(), Some(TokenType::Name)) {
286+
let ann = {
287+
let t = self.advance();
288+
self.lexeme(&t).to_string()
289+
};
290+
self.chunk.annotations.insert(name.clone(), ann);
295291
}
292+
while !matches!(
293+
self.peek(),
294+
Some(TokenType::Equal | TokenType::Dedent | TokenType::Endmarker) | None
295+
) {
296+
self.advance();
297+
}
298+
if !matches!(self.peek(), Some(TokenType::Equal)) {
299+
return false;
300+
}
301+
}
296302

297303
match self.peek() {
298304
Some(TokenType::Equal) => {

compiler/tests/cases/parser_cases.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,5 +1625,26 @@
16251625
"names": ["a_0"],
16261626
"instructions": [["LoadName",0], ["LoadConst",0], ["Dup2",0], ["GetItem",0], ["LoadConst",1], ["Add",0], ["StoreItem",0], ["ReturnValue",0]],
16271627
"annotations": {}
1628+
},
1629+
{
1630+
"src": "x: list[int] = [1, 2]",
1631+
"constants": ["1", "2"],
1632+
"names": ["x_1"],
1633+
"instructions": [["LoadConst", 0], ["LoadConst", 1], ["BuildList", 2], ["StoreName", 0], ["ReturnValue", 0]],
1634+
"annotations": {"x": "list"}
1635+
},
1636+
{
1637+
"src": "x: dict[str, int] = {}",
1638+
"constants": [],
1639+
"names": ["x_1"],
1640+
"instructions": [["BuildDict", 0], ["StoreName", 0], ["ReturnValue", 0]],
1641+
"annotations": {"x": "dict"}
1642+
},
1643+
{
1644+
"src": "x: tuple[int, str, float] = [1]",
1645+
"constants": ["1"],
1646+
"names": ["x_1"],
1647+
"instructions": [["LoadConst", 0], ["BuildList", 1], ["StoreName", 0], ["ReturnValue", 0]],
1648+
"annotations": {"x": "tuple"}
16281649
}
16291650
]

compiler/tests/cases/vm_cases.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,5 +213,8 @@
213213
{"src": "x = 99999999999999999999\nprint(x)", "output": ["99999999999999999999"], "result": "None"},
214214
{"src": "scale = 1\nfor _ in range(110):\n scale = scale * 10\nterm_a = scale // 5\nprint(term_a)", "output": ["20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"], "result": "None"},
215215
{"src": "x = 42\ndef f():\n return x\nprint(f())", "output": ["42"], "result": "None"},
216-
{"src": "def pi(n):\n S_num = 0\n for k in range(n):\n if k % 2 == 0:\n S_num = S_num + 1/(2*k + 1)\n else:\n S_num = S_num - 1/(2*k + 1)\n return 4 * S_num\nprint(pi(10))", "output": ["3.0418396189294032"], "result": "None"}
216+
{"src": "def pi(n):\n S_num = 0\n for k in range(n):\n if k % 2 == 0:\n S_num = S_num + 1/(2*k + 1)\n else:\n S_num = S_num - 1/(2*k + 1)\n return 4 * S_num\nprint(pi(10))", "output": ["3.0418396189294032"], "result": "None"},
217+
{"src": "a: list[int] = [1, 2, 3]\nprint(a[0])", "output": ["1"], "result": "None"},
218+
{"src": "def add(x: int, y: int) -> int:\n return x + y\nprint(add(3, 4))", "output": ["7"], "result": "None"},
219+
{"src": "def greet(name: str) -> str:\n return 'Hello ' + name\nprint(greet('world'))", "output": ["Hello world"], "result": "None"}
217220
]

0 commit comments

Comments
 (0)