Skip to content

Commit f919ed0

Browse files
committed
Extend symtable module.
1 parent ae018d5 commit f919ed0

6 files changed

Lines changed: 32 additions & 14 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ license = "MIT"
88
edition = "2018"
99

1010
[dependencies]
11+
indexmap = "1.0"
1112
rustpython-bytecode = { path = "../bytecode", version = "0.1.0" }
1213
rustpython-parser = { path = "../parser", version = "0.1.0" }
1314
num-complex = { version = "0.2", features = ["serde"] }

compiler/src/symboltable.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ Inspirational file: https://github.com/python/cpython/blob/master/Python/symtabl
88
*/
99

1010
use crate::error::{CompileError, CompileErrorType};
11+
use indexmap::map::IndexMap;
1112
use rustpython_parser::ast;
1213
use rustpython_parser::location::Location;
13-
use std::collections::HashMap;
1414

1515
pub fn make_symbol_table(program: &ast::Program) -> Result<SymbolScope, SymbolTableError> {
1616
let mut builder: SymbolTableBuilder = Default::default();
@@ -48,7 +48,7 @@ pub enum SymbolRole {
4848
#[derive(Clone)]
4949
pub struct SymbolScope {
5050
/// A set of symbols present on this scope level.
51-
pub symbols: HashMap<String, SymbolRole>,
51+
pub symbols: IndexMap<String, SymbolRole>,
5252

5353
/// A list of subscopes in the order as found in the
5454
/// AST nodes.
@@ -81,8 +81,8 @@ impl SymbolScope {
8181
impl Default for SymbolScope {
8282
fn default() -> Self {
8383
SymbolScope {
84-
symbols: HashMap::new(),
85-
sub_scopes: vec![],
84+
symbols: Default::default(),
85+
sub_scopes: Default::default(),
8686
}
8787
}
8888
}

crawl_sourcecode.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
""" This script can be used to test the equivalence in parsing between
2+
rustpython and cpython.
3+
4+
Usage example:
5+
6+
$ python crawl_sourcecode.py crawl_sourcecode.py > cpython.txt
7+
$ cargo run crawl_sourcecode.py crawl_sourcecode.py > rustpython.txt
8+
$ diff cpython.txt rustpython.txt
9+
"""
10+
111

212
import ast
313
import sys
@@ -36,7 +46,7 @@ def print_table(table, indent=0):
3646
print(' '*indent, 'table:', table.get_name())
3747
print(' '*indent, ' ', 'Syms:')
3848
for sym in table.get_symbols():
39-
print(' '*indent, ' ', sym)
49+
print(' '*indent, ' ', sym.get_name(), 'is_referenced=', sym.is_referenced(), 'is_assigned=', sym.is_assigned())
4050
print(' '*indent, ' ', 'Child tables:')
4151
for child in table.get_children():
4252
print_table(child, indent=indent+shift)

vm/src/eval.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@ pub fn eval(vm: &VirtualMachine, source: &str, scope: Scope, source_path: &str)
1414
}
1515

1616
pub fn get_compile_mode(vm: &VirtualMachine, mode: &str) -> PyResult<compile::Mode> {
17-
if mode == "exec" {
18-
Ok(compile::Mode::Exec)
19-
} else if mode == "eval" {
20-
Ok(compile::Mode::Eval)
21-
} else if mode == "single" {
22-
Ok(compile::Mode::Single)
23-
} else {
24-
Err(vm.new_value_error("compile() mode must be 'exec', 'eval' or single'".to_string()))
17+
match mode {
18+
"exec" => Ok(compile::Mode::Exec),
19+
"eval" => Ok(compile::Mode::Eval),
20+
"single" => Ok(compile::Mode::Single),
21+
_ => {
22+
Err(vm.new_value_error("compile() mode must be 'exec', 'eval' or 'single'".to_string()))
23+
}
2524
}
2625
}
2726

vm/src/stdlib/symtable.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,14 @@ impl PySymbolTable {
107107
.symtable
108108
.symbols
109109
.iter()
110-
.map(|s| vm.ctx.new_str(s.0.clone()))
110+
.map(|s| {
111+
(PySymbol {
112+
name: s.0.clone(),
113+
role: s.1.clone(),
114+
})
115+
.into_ref(vm)
116+
.into_object()
117+
})
111118
.collect();
112119
Ok(vm.ctx.new_list(symbols))
113120
}

0 commit comments

Comments
 (0)