8000 Add location to expressions. Change symboltable to use flags for symb… · rmliddle/RustPython@d6c19c1 · GitHub
[go: up one dir, main page]

Skip to content

Commit d6c19c1

Browse files
committed
Add location to expressions. Change symboltable to use flags for symbols.
1 parent 1b48ade commit d6c19c1

File tree

10 files changed

+932
-825
lines changed

10 files changed

+932
-825
lines changed

compiler/src/compile.rs

Lines changed: 100 additions & 96 deletions
Large diffs are not rendered by default.

compiler/src/symboltable.rs

Lines changed: 152 additions & 118 deletions
Large diffs are not rendered by default.

crawl_sourcecode.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
shift = 3
2727
def print_node(node, indent=0):
2828
if isinstance(node, ast.AST):
29-
print(' '*indent, "NODE", node.__class__.__name__)
29+
lineno = 'row={}'.format(node.lineno) if hasattr(node, 'lineno') else ''
30+
print(' '*indent, "NODE", node.__class__.__name__, lineno)
3031
for field in node._fields:
3132
print(' '*indent,'-', field)
3233
f = getattr(node, field)
@@ -41,12 +42,25 @@ def print_node(node, indent=0):
4142
print_node(t)
4243

4344
# print(ast.dump(t))
45+
flag_names = [
46+
'is_referenced',
47+
'is_assigned',
48+
'is_global',
49+
'is_local',
50+
'is_parameter',
51+
'is_free',
52+
]
4453

4554
def print_table(table, indent=0):
4655
print(' '*indent, 'table:', table.get_name())
4756
print(' '*indent, ' ', 'Syms:')
4857
for sym in table.get_symbols():
49-
print(' '*indent, ' ', sym.get_name(), 'is_referenced=', sym.is_referenced(), 'is_assigned=', sym.is_assigned())
58+
flags = []
59+
for flag_name in flag_names:
60+
func = getattr(sym, flag_name)
61+
if func():
62+
flags.append(flag_name)
63+
print(' '*indent, ' sym:', sym.get_name(), 'flags:', ' '.join(flags))
5064
print(' '*indent, ' ', 'Child tables:')
5165
for child in table.get_children():
5266
print_table(child, indent=indent+shift)

parser/src/ast.rs

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ pub struct Node {
1717
#[derive(Debug, PartialEq)]
1818
pub enum Top {
1919
Program(Program),
20-
Statement(Vec<LocatedStatement>),
20+
Statement(Vec<Statement>),
2121
Expression(Expression),
2222
}
2323

2424
#[derive(Debug, PartialEq)]
2525
pub struct Program {
26-
pub statements: Vec<LocatedStatement>,
26+
pub statements: Vec<Statement>,
2727
}
2828

2929
#[derive(Debug, PartialEq)]
@@ -38,12 +38,12 @@ pub struct Located<T> {
3838
pub node: T,
3939
}
4040

41-
pub type LocatedStatement = Located<Statement>;
41+
pub type Statement = Located<StatementType>;
4242

4343
/// Abstract syntax tree nodes for python statements.
4444
#[allow(clippy::large_enum_variant)]
4545
#[derive(Debug, PartialEq)]
46-
pub enum Statement {
46+
pub enum StatementType {
4747
Break,
4848
Continue,
4949
Return {
@@ -85,58 +85,58 @@ pub enum Statement {
8585
},
8686
If {
8787
test: Expression,
88-
body: Vec<LocatedStatement>,
89-
orelse: Option<Vec<LocatedStatement>>,
88+
body: Vec<Statement>,
89+
orelse: Option<Vec<Statement>>,
9090
},
9191
While {
9292
test: Expression,
93-
body: Vec<LocatedStatement>,
94-
orelse: Option<Vec<LocatedStatement>>,
93+
body: Vec<Statement>,
94+
orelse: Option<Vec<Statement>>,
9595
},
9696
With {
9797
items: Vec<WithItem>,
98-
body: Vec<LocatedStatement>,
98+
body: Vec<Statement>,
9999
},
100100
For {
101101
target: Expression,
102102
iter: Expression,
103-
body: Vec<LocatedStatement>,
104-
orelse: Option<Vec<LocatedStatement>>,
103+
body: Vec<Statement>,
104+
orelse: Option<Vec<Statement>>,
105105
},
106106
AsyncFor {
107107
target: Expression,
108108
iter: Expression,
109-
body: Vec<LocatedStatement>,
110-
orelse: Option<Vec<LocatedStatement>>,
109+
body: Vec<Statement>,
110+
orelse: Option<Vec<Statement>>,
111111
},
112112
Raise {
113113
exception: Option<Expression>,
114114
cause: Option<Expression>,
115115
},
116116
Try {
117-
body: Vec<LocatedStatement>,
117+
body: Vec<Statement>,
118118
handlers: Vec<ExceptHandler>,
119-
orelse: Option<Vec<LocatedStatement>>,
120-
finalbody: Option<Vec<LocatedStatement>>,
119+
orelse: Option<Vec<Statement>>,
120+
finalbody: Option<Vec<Statement>>,
121121
},
122122
ClassDef {
123123
name: String,
124-
body: Vec<LocatedStatement>,
124+
body: Vec<Statement>,
125125
bases: Vec<Expression>,
126126
keywords: Vec<Keyword>,
127127
decorator_list: Vec<Expression>,
128128
},
129129
FunctionDef {
130130
name: String,
131131
args: Parameters,
132-
body: Vec<LocatedStatement>,
132+
body: Vec<Statement>,
133133
decorator_list: Vec<Expression>,
134134
returns: Option<Expression>,
135135
},
136136
AsyncFunctionDef {
137137
name: String,
138138
args: Parameters,
139-
body: Vec<LocatedStatement>,
139+
body: Vec<Statement>,
140140
decorator_list: Vec<Expression>,
141141
returns: Option<Expression>,
142142
},
@@ -148,8 +148,10 @@ pub struct WithItem {
148148
pub optional_vars: Option<Expression>,
149149
}
150150

151+
pub type Expression = Located<ExpressionType>;
152+
151153
#[derive(Debug, PartialEq)]
152-
pub enum Expression {
154+
pub enum ExpressionType {
153155
BoolOp {
154156
a: Box<Expression>,
155157
op: BooleanOperator,
@@ -242,10 +244,10 @@ pub enum Expression {
242244
impl Expression {
243245
/// Returns a short name for the node suitable for use in error messages.
244246
pub fn name(&self) -> &'static str {
245-
use self::Expression::*;
247+
use self::ExpressionType::*;
246248
use self::StringGroup::*;
247249

248-
match self {
250+
match &self.node {
249251
BoolOp { .. } | Binop { .. } | Unop { .. } => "operator",
250252
Subscript { .. } => "subscript",
251253
Await { .. } => "await expression",
@@ -330,7 +332,7 @@ pub struct Keyword {
330332
pub struct ExceptHandler {
331333
pub typ: Option<Expression>,
332334
pub name: Option<String>,
333-
pub body: Vec<LocatedStatement>,
335+
pub body: Vec<Statement>,
334336
}
335337

336338
#[derive(Debug, PartialEq)]

parser/src/fstring.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,12 @@ mod tests {
173173

174174
use super::*;
175175

176-
fn mk_ident(name: &str) -> ast::Expression {
177-
ast::Expression::Identifier {
178-
name: name.to_owned(),
176+
fn mk_ident(name: &str, row: usize, col: usize) -> ast::Expression {
177+
ast::Expression {
178+
location: ast::Location::new(row, col),
179+
node: ast::ExpressionType::Identifier {
180+
name: name.to_owned(),
181+
},
179182
}
180183
}
181184

@@ -189,12 +192,12 @@ mod tests {
189192
Joined {
190193
values: vec![
191194
FormattedValue {
192-
value: Box::new(mk_ident("a")),
195+
value: Box::new(mk_ident("a", 1, 1)),
193196
conversion: None,
194197
spec: String::new(),
195198
},
196199
FormattedValue {
197-
value: Box::new(mk_ident("b")),
200+
value: Box::new(mk_ident("b", 1, 1)),
198201
conversion: None,
199202
spec: String::new(),
200203
},

0 commit comments

Comments
 (0)
0