Skip to content

Commit b4cc2d7

Browse files
committed
adapted grammer to full support, most test are passing now, esp. all invalids are passed. Cleaned up in symboltable
1 parent 17008ef commit b4cc2d7

3 files changed

Lines changed: 26 additions & 33 deletions

File tree

Lib/test/test_named_expression.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,27 @@
66

77
class NamedExpressionInvalidTest(unittest.TestCase):
88

9-
@unittest.expectedFailure # TODO RustPython
109
def test_named_expression_invalid_01(self):
1110
code = """x := 0"""
1211

1312
#with self.assertRaisesRegex(SyntaxError, "invalid syntax"):
1413
with self.assertRaises(SyntaxError): # TODO RustPython
1514
exec(code, {}, {})
1615

17-
@unittest.expectedFailure # TODO RustPython
1816
def test_named_expression_invalid_02(self):
1917
code = """x = y := 0"""
2018

2119
#with self.assertRaisesRegex(SyntaxError, "invalid syntax"):
2220
with self.assertRaises(SyntaxError): # TODO RustPython
2321
exec(code, {}, {})
2422

25-
@unittest.expectedFailure # TODO RustPython
2623
def test_named_expression_invalid_03(self):
2724
code = """y := f(x)"""
2825

2926
#with self.assertRaisesRegex(SyntaxError, "invalid syntax"):
3027
with self.assertRaises(SyntaxError): # TODO RustPython
3128
exec(code, {}, {})
3229

33-
@unittest.expectedFailure # TODO RustPython
3430
def test_named_expression_invalid_04(self):
3531
code = """y0 = y1 := f(x)"""
3632

compiler/src/symboltable.rs

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,10 @@ impl<'a> SymbolTableAnalyzer<'a> {
202202
}
203203
let (symbols, st_typ) = self.tables.pop().unwrap();
204204

205-
//println!("\n\n\n\n\nbefore analyze scope {:?} of type {:?}", symbols, st_typ.to_string());
206205
// Analyze symbols:
207206
for symbol in symbols.values_mut() {
208207
self.analyze_symbol(symbol, st_typ)?;
209208
}
210-
211-
//println!("\n\n\nafter analyze scope {:?} of type {:?}", symbols, st_typ.to_string());
212-
213209
Ok(())
214210
}
215211

@@ -218,7 +214,6 @@ impl<'a> SymbolTableAnalyzer<'a> {
218214
symbol: &mut Symbol,
219215
curr_st_typ: SymbolTableType,
220216
) -> SymbolTableResult {
221-
//assert!(!symbol.is_assign_namedexpr_in_comprehension || curr_st_typ==SymbolTableType::Comprehension);
222217
if symbol.is_assign_namedexpr_in_comprehension
223218
&& curr_st_typ == SymbolTableType::Comprehension
224219
{
@@ -293,9 +288,6 @@ impl<'a> SymbolTableAnalyzer<'a> {
293288
parent_offset: usize,
294289
) -> SymbolTableResult {
295290
// when this is called, we expect to be in the direct parent scope of the scope that contains 'symbol'
296-
//println!(" analyze symbol {:?}", symbol);
297-
298-
//let mut last=self.tables.last_mut().unwrap();
299291
let offs = self.tables.len() - 1 - parent_offset;
300292
let last = self.tables.get_mut(offs).unwrap();
301293
let symbols = &mut last.0;
@@ -306,18 +298,17 @@ impl<'a> SymbolTableAnalyzer<'a> {
306298
symbol.scope = SymbolScope::Global;
307299
}
308300
SymbolTableType::Class => {}
309-
SymbolTableType::Function => match symbols.get_mut(&symbol.name) {
310-
Some(parent_symbol) => {
301+
SymbolTableType::Function => {
302+
if let Some(parent_symbol) = symbols.get_mut(&symbol.name) {
311303
match parent_symbol.scope {
312304
// possibly we can omit this check?
313305
SymbolScope::Unknown => {
314-
parent_symbol.is_assigned = true; // this information is new, as it was
306+
parent_symbol.is_assigned = true; // this information is new, as the asignment is done in inner scope
315307
self.analyze_unknown_symbol(symbol);
316308
}
317309
_ => {}
318310
}
319311

320-
println!(" symbol in parent scope contained");
321312
match symbol.scope {
322313
SymbolScope::Global => {
323314
symbol.scope = SymbolScope::Global;
@@ -327,25 +318,13 @@ impl<'a> SymbolTableAnalyzer<'a> {
327318
}
328319
}
329320
}
330-
None => {
331-
//println!(" adding {:?} to next outer scope", symbol.name);
332-
/*
333-
//symbol.scope = SymbolScope::Nonlocal;
334-
let mut sym_cloned = symbol.clone();
335-
sym_cloned.scope=SymbolScope::Local;
336-
symbol.scope = SymbolScope::Nonlocal;
337-
last.0.insert(sym_cloned.name.to_owned(),sym_cloned);*/
338-
339-
//assert!(false); // I guess this shall not happen, but if so we find it quickly to replace with proper handling
340-
//println!("Found undefined symbol {:?} in scope {:?}", symbol.name, table_type.to_string());
341-
}
342321
},
343322
SymbolTableType::Comprehension => {
344323
// TODO check for conflicts
345324

346325
match symbols.get_mut(&symbol.name) {
347326
Some(parent_symbol) => {
348-
parent_symbol.is_assigned = true; // more checks are reauired
327+
parent_symbol.is_assigned = true; // more checks are required
349328
}
350329
None => {
351330
let cloned_sym = symbol.clone();

parser/src/python.lalrpop

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,25 @@ TestOrStarExprList: ast::Expression = {
148148
}
149149
};
150150

151+
TestOrStarNamedExprList: ast::Expression = {
152+
<location:@L> <elements:OneOrMore<TestOrStarNamedExpr>> <comma:","?> => {
153+
if elements.len() == 1 && comma.is_none() {
154+
elements.into_iter().next().unwrap()
155+
} else {
156+
ast::Expression {
157+
location,
158+
node: ast::ExpressionType::Tuple { elements }
159+
}
160+
}
161+
}
162+
};
163+
151164
TestOrStarExpr: ast::Expression = {
165+
Test,
166+
StarExpr,
167+
};
168+
169+
TestOrStarNamedExpr: ast::Expression = {
152170
NamedExpression_Test,
153171
StarExpr,
154172
};
@@ -932,7 +950,7 @@ Atom: ast::Expression = {
932950
node: ast::ExpressionType::List { elements }
933951
}
934952
},
935-
<location:@L> "[" <element:TestOrStarExpr> <generators:CompFor> "]" => {
953+
<location:@L> "[" <element:TestOrStarNamedExpr> <generators:CompFor> "]" => {
936954
ast::Expression {
937955
location,
938956
node: ast::ExpressionType::Comprehension {
@@ -941,7 +959,7 @@ Atom: ast::Expression = {
941959
}
942960
}
943961
},
944-
<location:@L> "(" <elements:TestOrStarExprList?> ")" => {
962+
<location:@L> "(" <elements:TestOrStarNamedExprList?> ")" => {
945963
elements.unwrap_or(ast::Expression {
946964
location,
947965
node: ast::ExpressionType::Tuple { elements: Vec::new() }
@@ -990,7 +1008,7 @@ Atom: ast::Expression = {
9901008
};
9911009

9921010
ListLiteralValues: Vec<ast::Expression> = {
993-
<e:OneOrMore<TestOrStarExpr>> ","? => e,
1011+
<e:OneOrMore<TestOrStarNamedExpr>> ","? => e,
9941012
};
9951013

9961014
DictLiteralValues: Vec<(Option<ast::Expression>, ast::Expression)> = {
@@ -1007,7 +1025,7 @@ DictElement: (Option<ast::Expression>, ast::Expression) = {
10071025
};
10081026

10091027
SetLiteralValues: Vec<ast::Expression> = {
1010-
<e1:OneOrMore<TestOrStarExpr>> ","? => e1
1028+
<e1:OneOrMore<TestOrStarNamedExpr>> ","? => e1
10111029
};
10121030

10131031
ExpressionOrStarExpression = {

0 commit comments

Comments
 (0)