Skip to content

Commit

Permalink
Merge pull request #198 from Glyphack/add-instance-attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
Glyphack authored Nov 3, 2023
2 parents 79e6e84 + 4ef6c96 commit 3f3aad0
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 4 deletions.
29 changes: 29 additions & 0 deletions typechecker/src/semantic_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,9 +503,37 @@ impl TraversalVisitor for SemanticAnalyzer {
);
}
let mut methods = vec![];
let mut attributes = HashMap::new();
for stmt in &c.body {
match stmt {
parser::ast::Statement::FunctionDef(f) => {
if f.name == "__init__" {
for stmt in &f.body {
match stmt {
parser::ast::Statement::AssignStatement(assign) => {
for target in &assign.targets {
match target {
parser::ast::Expression::Attribute(attr) => {
match *attr.value {
parser::ast::Expression::Name(ref n) => {
if n.id == "self" {
attributes.insert(
attr.attr.clone(),
assign.value.clone(),
);
}
}
_ => (),
}
}
_ => (),
}
}
}
_ => (),
}
}
}
methods.push(f.name.clone());
}
_ => (),
Expand All @@ -517,6 +545,7 @@ impl TraversalVisitor for SemanticAnalyzer {

let class_declaration = Declaration::Class(Class {
declaration_path,
attributes,
methods,
});
self.create_symbol(c.name.clone(), class_declaration);
Expand Down
4 changes: 4 additions & 0 deletions typechecker/src/symbol_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ pub struct Class {
// Method names, can be used to look up the function in the symbol table
// of the class
pub methods: Vec<String>,
// instance attibutes that are defined in the __init__ method
// if the attribute is referencing another symbol we need to look up that symbol in the
// __init__ method
pub attributes: HashMap<String, ast::Expression>,
}

#[derive(Debug, Clone)]
Expand Down
2 changes: 2 additions & 0 deletions typechecker/test_data/inputs/symbol_table/class_definition.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
class c:
def __init__(self):
a = 1
b = a
self.c = b
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: typechecker/src/build.rs
description: "class c:\n def __init__(self):\n a = 1\n"
description: "class c:\n def __init__(self):\n a = 1\n b = a\n self.c = b\n"
expression: result
input_file: typechecker/test_data/inputs/symbol_table/class_definition.py
---
Expand All @@ -14,12 +14,23 @@ c
module_name: [REDACTED]",
node: Node {
start: 0,
end: 47,
end: 80,
},
},
methods: [
"__init__",
],
attributes: {
"c": Name(
Name {
node: Node {
start: 78,
end: 79,
},
id: "b",
},
),
},
}

all scopes:
Expand Down Expand Up @@ -49,6 +60,31 @@ a
),
is_constant: false,
}
b
- Declarations:
--: Variable {
declaration_path: DeclarationPath {
module_name: [REDACTED]",
node: Node {
start: 55,
end: 60,
},
},
scope: Global,
type_annotation: None,
inferred_type_source: Some(
Name(
Name {
node: Node {
start: 59,
end: 60,
},
id: "a",
},
),
),
is_constant: false,
}
self
- Declarations:
--: Paramter {
Expand Down Expand Up @@ -79,13 +115,13 @@ __init__
module_name: [REDACTED]",
node: Node {
start: 13,
end: 47,
end: 80,
},
},
function_node: FunctionDef {
node: Node {
start: 13,
end: 47,
end: 80,
},
name: "__init__",
args: Arguments {
Expand Down Expand Up @@ -139,6 +175,71 @@ __init__
),
},
),
AssignStatement(
Assign {
node: Node {
start: 55,
end: 60,
},
targets: [
Name(
Name {
node: Node {
start: 55,
end: 56,
},
id: "b",
},
),
],
value: Name(
Name {
node: Node {
start: 59,
end: 60,
},
id: "a",
},
),
},
),
AssignStatement(
Assign {
node: Node {
start: 69,
end: 79,
},
targets: [
Attribute(
Attribute {
node: Node {
start: 69,
end: 75,
},
value: Name(
Name {
node: Node {
start: 69,
end: 73,
},
id: "self",
},
),
attr: "c",
},
),
],
value: Name(
Name {
node: Node {
start: 78,
end: 79,
},
id: "b",
},
),
},
),
],
decorator_list: [],
returns: None,
Expand Down

0 comments on commit 3f3aad0

Please sign in to comment.