Skip to content

Commit

Permalink
Merge branch 'main' into symbol-table-parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Glyphack authored Aug 20, 2023
2 parents 640cbf4 + 795cb0b commit 9817423
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 16 deletions.
2 changes: 1 addition & 1 deletion scripts/print_symbol_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def print_symbol_table_info(symbol_table: symtable.SymbolTable, indent=0):

# Example usage:
code = """
class A(B): pass
a = 1
"""
symbol_table = symtable.symtable(code, "example", "exec")
print_symbol_table_info(symbol_table)
Expand Down
5 changes: 5 additions & 0 deletions typechecker/src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,9 @@ impl<'a> TraversalVisitor for EnderpyFile {
let func = f.clone();
self.defs.push(Statement::FunctionDef(func));
}

fn visit_class_def(&mut self, c: &parser::ast::ClassDef) {
let class = c.clone();
self.defs.push(Statement::ClassDef(class));
}
}
29 changes: 26 additions & 3 deletions typechecker/src/semantic_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ impl SemanticAnalyzer {
let symbol_node = SymbolTableNode {
name,
declarations: vec![decl],
module_public: false,
module_hidden: false,
implicit: false,
};
self.globals.add_symbol(symbol_node)
}
Expand Down Expand Up @@ -85,6 +82,9 @@ impl SemanticAnalyzer {
)
}
}
Expression::Attribute(_) => print!(
"Ignoring attribute assingment. See https://github.com/Glyphack/enderpy/issues/157"
),
_ => panic!("cannot assign to {:?} is not supported", target),
}
}
Expand Down Expand Up @@ -340,6 +340,7 @@ impl TraversalVisitor for SemanticAnalyzer {
};
self.globals.enter_scope(SymbolTableScope::new(
crate::symbol_table::SymbolTableType::Function,
f.name.clone(),
));

self.add_arguments_definitions(&f.args);
Expand Down Expand Up @@ -374,9 +375,31 @@ impl TraversalVisitor for SemanticAnalyzer {
}

fn visit_class_def(&mut self, c: &parser::ast::ClassDef) {
let declaration_path = DeclarationPath {
module_name: self.file.module_name.clone(),
node: c.node,
};
self.globals.enter_scope(SymbolTableScope::new(
SymbolTableType::Class,
c.name.clone(),
));
let mut methods = vec![];
for stmt in &c.body {
match stmt {
parser::ast::Statement::FunctionDef(f) => {
methods.push(f.name.clone());
}
_ => (),
}
self.visit_stmt(&stmt);
}
self.globals.exit_scope();

let class_declaration = Declaration::Class(Box::new(Class {
declaration_path,
methods,
}));
self.create_symbol(c.name.clone(), class_declaration);
}

fn visit_match(&mut self, m: &parser::ast::Match) {
Expand Down
4 changes: 0 additions & 4 deletions typechecker/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,11 @@ pub struct Settings {
impl Settings {
pub fn new() -> Result<Self, ConfigError> {
let run_mode = env::var("RUN_MODE").unwrap_or_else(|_| "development".into());

let s = Config::builder()
// Start off by merging in the "default" configuration file
.add_source(File::with_name("examples/hierarchical-env/config/default"))
.build()?;

// Now that we're done, let's access our configuration
println!("debug: {:?}", s.get_bool("debug"));

s.try_deserialize()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ SymbolTable {
SymbolTableScope {
symbol_table_type: Module,
symbols: {
"d": SymbolTableNode {
name: "d",
"c": SymbolTableNode {
name: "c",
declarations: [
Variable(
Variable {
Expand Down Expand Up @@ -63,8 +63,8 @@ SymbolTable {
module_hidden: false,
implicit: false,
},
"c": SymbolTableNode {
name: "c",
"d": SymbolTableNode {
name: "d",
declarations: [
Variable(
Variable {
Expand Down
100 changes: 100 additions & 0 deletions typechecker/src/snapshots/typechecker__build__tests__class_def.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
---
source: typechecker/src/build.rs
description: "class c:\n def __init__(self):\n b = 1\n"
---
SymbolTable {
scopes: [
SymbolTableScope {
symbol_table_type: Module,
name: "global",
symbols: {
"c": SymbolTableNode {
name: "c",
declarations: [
Class(
Class {
declaration_path: DeclarationPath {
module_name: "test",
node: Node {
start: 0,
end: 46,
},
},
methods: [
"__init__",
],
},
),
],
},
},
},
],
all_scopes: [
SymbolTableScope {
symbol_table_type: Function,
name: "__init__",
symbols: {
"b": SymbolTableNode {
name: "b",
declarations: [
Variable(
Variable {
declaration_path: DeclarationPath {
module_name: "test",
node: Node {
start: 40,
end: 45,
},
},
scope: Global,
type_annotation: None,
inferred_type_source: Some(
Constant(
Constant {
node: Node {
start: 44,
end: 45,
},
value: Int(
"1",
),
},
),
),
is_constant: false,
},
),
],
},
},
},
SymbolTableScope {
symbol_table_type: Class,
name: "c",
symbols: {
"__init__": SymbolTableNode {
name: "__init__",
declarations: [
Function(
Function {
declaration_path: DeclarationPath {
module_name: "test",
node: Node {
start: 12,
end: 46,
},
},
is_method: true,
is_generator: false,
return_statements: [],
yeild_statements: [],
raise_statements: [],
},
),
],
},
},
},
],
}
9 changes: 5 additions & 4 deletions typechecker/src/symbol_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ pub struct SymbolTable {
#[derive(Debug)]
pub struct SymbolTableScope {
pub symbol_table_type: SymbolTableType,
pub name: String,
symbols: HashMap<String, SymbolTableNode>,
}

impl SymbolTableScope {
pub fn new(symbol_table_type: SymbolTableType) -> Self {
pub fn new(symbol_table_type: SymbolTableType, name: String) -> Self {
SymbolTableScope {
symbol_table_type,
name,
symbols: HashMap::new(),
}
}
Expand All @@ -35,9 +37,6 @@ pub enum SymbolTableType {
pub struct SymbolTableNode {
pub name: String,
pub declarations: Vec<Declaration>,
pub module_public: bool,
pub module_hidden: bool,
pub implicit: bool,
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -108,6 +107,7 @@ impl SymbolTable {
let global_scope = SymbolTableScope {
symbol_table_type,
symbols: HashMap::new(),
name: String::from("global"),
};
SymbolTable {
scopes: vec![global_scope],
Expand Down Expand Up @@ -142,6 +142,7 @@ impl SymbolTable {
Some(scope) => self.all_scopes.push(scope),
None => panic!("tried to exit non-existent scope"),
}

}

pub fn add_symbol(&mut self, symbol_node: SymbolTableNode) {
Expand Down

0 comments on commit 9817423

Please sign in to comment.