Skip to content

Commit

Permalink
Merge pull request #155 from Glyphack/binder-function-def
Browse files Browse the repository at this point in the history
  • Loading branch information
Glyphack authored Aug 6, 2023
2 parents 719d1d8 + cc768f1 commit aa8a0b3
Show file tree
Hide file tree
Showing 10 changed files with 449 additions and 256 deletions.
1 change: 1 addition & 0 deletions parser/src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub struct Module {
pub body: Vec<Statement>,
}

// Use box to reduce the enum size
#[derive(Debug, Clone)]
pub enum Statement {
AssignStatement(Assign),
Expand Down
4 changes: 4 additions & 0 deletions typechecker/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ mod tests {
"c,d = 1,2",
"a: int = 1",
"a += b",
"def f():
a = 1
return
",
];
for source in sources {
let path = write_temp_source(source);
Expand Down
53 changes: 44 additions & 9 deletions typechecker/src/semantic_analyzer.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::ops::Deref;
use std::collections::HashMap;

use parser::ast::Expression;

use crate::{
ast_visitor::TraversalVisitor,
nodes::EnderpyFile,
symbol_table::{
Declaration, DeclarationPath, SymbolScope, SymbolTable, SymbolTableNode, Variable,
Declaration, DeclarationPath, Function, SymbolScope, SymbolTable, SymbolTableNode,
SymbolTableScope, SymbolTableType, Variable,
},
};

Expand All @@ -15,6 +16,8 @@ pub struct SemanticAnalyzer {
// TODO: Replace errors with another type
file: Box<EnderpyFile>,
errors: Vec<String>,

// TOD: Not needed?
scope: SymbolScope,
}

Expand All @@ -36,7 +39,6 @@ impl SemanticAnalyzer {
module_public: false,
module_hidden: false,
implicit: false,
scope: self.scope,
};
self.globals.add_symbol(symbol_node)
}
Expand All @@ -46,8 +48,12 @@ impl SemanticAnalyzer {
.push(String::from(format!("cannot resolve reference {}", "")))
}

fn current_scope(&self) -> SymbolScope {
SymbolScope::Global
fn current_scope(&self) -> &SymbolTableType {
return self.globals.current_scope_type();
}

fn is_inside_class(&self) -> bool {
return matches!(self.current_scope(), SymbolTableType::Class);
}

fn create_variable_declaration_symbol(
Expand All @@ -61,7 +67,8 @@ impl SemanticAnalyzer {
Expression::Name(n) => {
let decl = Declaration::Variable(Box::new(Variable {
declaration_path,
scope: self.current_scope(),
// TODO: Hacky way
scope: SymbolScope::Global,
type_annotation,
inferred_type_source: value,
is_constant: false,
Expand Down Expand Up @@ -224,9 +231,39 @@ impl TraversalVisitor for SemanticAnalyzer {
}

fn visit_function_def(&mut self, f: &parser::ast::FunctionDef) {
let declaration_path = DeclarationPath {
module_name: self.file.module_name.clone(),
node: f.node,
};
self.globals.enter_scope(SymbolTableScope::new(
crate::symbol_table::SymbolTableType::Function,
));
let mut return_statements = vec![];
let mut yeild_statements = vec![];
let mut raise_statements = vec![];
for stmt in &f.body {
self.visit_stmt(&stmt);
match &stmt {
parser::ast::Statement::Raise(r) => raise_statements.push(r.clone()),
parser::ast::Statement::Return(r) => return_statements.push(r.clone()),
parser::ast::Statement::ExpressionStatement(e) => match e {
parser::ast::Expression::Yield(y) => yeild_statements.push(*y.clone()),
_ => (),
},
_ => (),
}
}
self.globals.exit_scope();

let function_declaration = Declaration::Function(Box::new(Function {
declaration_path,
is_method: self.is_inside_class(),
is_generator: !yeild_statements.is_empty(),
return_statements,
yeild_statements,
raise_statements,
}));
self.create_symbol(f.name.clone(), function_declaration);
}

fn visit_class_def(&mut self, c: &parser::ast::ClassDef) {
Expand Down Expand Up @@ -407,9 +444,7 @@ impl TraversalVisitor for SemanticAnalyzer {
todo!()
}

fn visit_return(&mut self, r: &parser::ast::Return) {
todo!()
}
fn visit_return(&mut self, r: &parser::ast::Return) {}

fn visit_raise(&mut self, r: &parser::ast::Raise) {
todo!()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,65 @@ source: typechecker/src/build.rs
description: b = a + 1
---
SymbolTable {
symbol_table_type: Module,
symbols: {
"b": SymbolTableNode {
name: "b",
declarations: [
Variable(
Variable {
declaration_path: DeclarationPath {
module_name: "test",
node: Node {
start: 0,
end: 9,
},
},
scope: Global,
type_annotation: None,
inferred_type_source: Some(
BinOp(
BinOp {
scopes: [
SymbolTableScope {
symbol_table_type: Module,
symbols: {
"b": SymbolTableNode {
name: "b",
declarations: [
Variable(
Variable {
declaration_path: DeclarationPath {
module_name: "test",
node: Node {
start: 4,
start: 0,
end: 9,
},
op: Add,
left: Name(
Name {
},
scope: Global,
type_annotation: None,
inferred_type_source: Some(
BinOp(
BinOp {
node: Node {
start: 4,
end: 5,
},
id: "a",
},
),
right: Constant(
Constant {
node: Node {
start: 8,
end: 9,
},
value: Int(
"1",
op: Add,
left: Name(
Name {
node: Node {
start: 4,
end: 5,
},
id: "a",
},
),
right: Constant(
Constant {
node: Node {
start: 8,
end: 9,
},
value: Int(
"1",
),
},
),
},
),
},
),
),
is_constant: false,
},
),
is_constant: false,
},
),
],
module_public: false,
module_hidden: false,
implicit: false,
scope: Global,
],
module_public: false,
module_hidden: false,
implicit: false,
},
},
},
},
start_line_number: 0,
],
all_scopes: [],
}
Loading

0 comments on commit aa8a0b3

Please sign in to comment.