Skip to content

Commit

Permalink
lint(all): cargo fix and rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
hanmindev committed Feb 21, 2024
1 parent 867d541 commit 86ba310
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 66 deletions.
2 changes: 1 addition & 1 deletion src/front/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::front::passes::types::TypeError;
pub enum PassError {
Unimplemented,
Generic(String),
Types(TypeError)
Types(TypeError),
}

pub type PassResult = Result<(), PassError>;
Expand Down
6 changes: 3 additions & 3 deletions src/front/passes/check_assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl Visitor<bool, ResolverError> for Option<NullCheck> {
arg.visit(self)?;
}
Ok((false, Some(false)))
},
}
AtomicExpression::Literal(_) => Ok((true, Some(false))),
}
}
Expand Down Expand Up @@ -316,7 +316,7 @@ mod tests {
&mut front_program,
&mut vec![Box::new(DisallowNullAssignment)],
)
.is_err());
.is_err());
}

#[test]
Expand All @@ -337,6 +337,6 @@ mod tests {
&mut front_program,
&mut vec![Box::new(DisallowNullAssignment)],
)
.is_err());
.is_err());
}
}
61 changes: 38 additions & 23 deletions src/front/passes/types.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
mod first_assignment_graph;
mod insert_types;
mod topological_sort;
mod type_expression;
mod var_def_table;
mod topological_sort;
mod insert_types;

use std::collections::HashMap;
use either::{Either};
use crate::front::exporter::export::FrontProgram;
use crate::front::passes::{Pass, PassError, PassResult};
use either::Either;
use std::collections::HashMap;

use crate::front::ast_types::visitor::{Visitable, Visitor};
use crate::front::passes::types::first_assignment_graph::create_first_assignment_graph;
use crate::front::passes::types::insert_types::{insert_types, ResolvedVarDefTable};
use crate::front::passes::types::topological_sort::topological_sort;
Expand All @@ -29,7 +28,6 @@ impl Pass for AnnotateTypes {
// topologically sort first assignment graph
let sorted = topological_sort(&table);


let mut new_table = ResolvedVarDefTable {
var_types: HashMap::new(),
};
Expand All @@ -38,9 +36,7 @@ impl Pass for AnnotateTypes {
for value in sorted {
let types = &table.var_types.get(&value).unwrap().types_;
let type_ = match &types {
Either::Left(l) => {
l.clone()
}
Either::Left(l) => l.clone(),
Either::Right(r) => {
if let Ok(type_) = r.tree.resolve_type(program, &table.var_types) {
type_
Expand All @@ -50,7 +46,12 @@ impl Pass for AnnotateTypes {
}
};

table.var_types.insert(value.clone(), VarTypeNode { types_: Either::Left(type_.clone()) });
table.var_types.insert(
value.clone(),
VarTypeNode {
types_: Either::Left(type_.clone()),
},
);
new_table.var_types.insert(value, type_);
}

Expand All @@ -62,15 +63,15 @@ impl Pass for AnnotateTypes {

#[cfg(test)]
mod tests {
use std::rc::Rc;
use crate::front::ast_types::Type;
use crate::front::ast_types::{GlobalResolvedName, Statement};
use crate::front::file_system::fs::FileSystem;
use crate::front::file_system::mock_fs::MockFileSystem;
use crate::front::mergers::program::ProgramMerger;
use crate::front::passes::pass;
use crate::front::passes::types::AnnotateTypes;
use camino::Utf8PathBuf;
use crate::front::ast_types::{GlobalResolvedName, Statement};
use crate::front::ast_types::Type;
use std::rc::Rc;

#[test]
fn test_type_annotation_simple() {
Expand All @@ -88,11 +89,18 @@ mod tests {

pass(&mut front_program, &mut vec![Box::new(AnnotateTypes)]);

if let Statement::VarDecl(x) = &front_program.definitions.function_definitions.get(&Rc::from(GlobalResolvedName {
package: Rc::from("pkg"),
module: Rc::from("/root"),
name: "0_main".to_string(),
})).unwrap().body.statements[0] {
if let Statement::VarDecl(x) = &front_program
.definitions
.function_definitions
.get(&Rc::from(GlobalResolvedName {
package: Rc::from("pkg"),
module: Rc::from("/root"),
name: "0_main".to_string(),
}))
.unwrap()
.body
.statements[0]
{
assert_eq!(x.var_def.type_.as_ref().unwrap(), &Type::Int);
} else {
panic!("Expected VarDecl");
Expand All @@ -115,11 +123,18 @@ mod tests {

pass(&mut front_program, &mut vec![Box::new(AnnotateTypes)]);

if let Statement::VarDecl(x) = &front_program.definitions.function_definitions.get(&Rc::from(GlobalResolvedName {
package: Rc::from("pkg"),
module: Rc::from("/root"),
name: "0_main".to_string(),
})).unwrap().body.statements[0] {
if let Statement::VarDecl(x) = &front_program
.definitions
.function_definitions
.get(&Rc::from(GlobalResolvedName {
package: Rc::from("pkg"),
module: Rc::from("/root"),
name: "0_main".to_string(),
}))
.unwrap()
.body
.statements[0]
{
assert_eq!(x.var_def.type_.as_ref().unwrap(), &Type::Int);
} else {
panic!("Expected VarDecl");
Expand Down
61 changes: 35 additions & 26 deletions src/front/passes/types/insert_types.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::collections::HashMap;
use std::rc::Rc;
use crate::front::ast_types::{AtomicExpression, ExpressionEnum, GlobalResolvedName, Type};
use crate::front::ast_types::visitor::{ASTNodeEnum, GenericResolveResult, Visitable, Visitor};
use crate::front::ast_types::{AtomicExpression, ExpressionEnum, GlobalResolvedName, Type};
use crate::front::exporter::export::FrontProgram;
use crate::front::passes::types::type_expression::{binop_type_resolver, literal_types, TypeError, unop_type_resolver};
use crate::front::passes::types::type_expression::{
binop_type_resolver, literal_types, unop_type_resolver, TypeError,
};
use std::collections::HashMap;
use std::rc::Rc;

#[derive(Debug, PartialEq)]
pub enum ResolverError {
Expand All @@ -16,7 +18,12 @@ impl Visitor<Type, ResolverError> for ResolvedVarDefTable {
fn apply(&mut self, ast_node: &mut ASTNodeEnum) -> ResolveResult<Type> {
match ast_node {
ASTNodeEnum::VarDef(&mut ref mut x) => {
x.type_ = Some(self.var_types.get(x.name.global_resolved.as_ref().unwrap()).unwrap().clone());
x.type_ = Some(
self.var_types
.get(x.name.global_resolved.as_ref().unwrap())
.unwrap()
.clone(),
);
}

ASTNodeEnum::VarDecl(&mut ref mut x) => {
Expand All @@ -29,31 +36,35 @@ impl Visitor<Type, ResolverError> for ResolvedVarDefTable {
}

ASTNodeEnum::VarAssign(&mut ref mut x) => {
if self.var_types.get(x.name_path.name.global_resolved.as_ref().unwrap()).unwrap().clone() != x.expr.visit(self)?.unwrap() {
if self
.var_types
.get(x.name_path.name.global_resolved.as_ref().unwrap())
.unwrap()
.clone()
!= x.expr.visit(self)?.unwrap()
{
return Err(ResolverError::TypeError(TypeError::MultipleTypes));
}
}

ASTNodeEnum::Expression(&mut ref mut x) => {
x.type_ = Some(match &mut x.expr {
ExpressionEnum::AtomicExpression(atomic) => {
match atomic {
AtomicExpression::Variable(name_path) => {
self.var_types.get(name_path.name.global_resolved.as_ref().unwrap()).unwrap().clone()
}
AtomicExpression::FnCall(fn_call) => {
self.var_types.get(fn_call.name.global_resolved.as_ref().unwrap()).unwrap().clone()
}
AtomicExpression::Literal(literal) => {
literal_types(literal)
}
}
}
ExpressionEnum::AtomicExpression(atomic) => match atomic {
AtomicExpression::Variable(name_path) => self
.var_types
.get(name_path.name.global_resolved.as_ref().unwrap())
.unwrap()
.clone(),
AtomicExpression::FnCall(fn_call) => self
.var_types
.get(fn_call.name.global_resolved.as_ref().unwrap())
.unwrap()
.clone(),
AtomicExpression::Literal(literal) => literal_types(literal),
},
ExpressionEnum::Unary(unop, x) => {
match unop_type_resolver(unop, &x.visit(self)?.unwrap()) {
Ok(type_) => {
type_
}
Ok(type_) => type_,
Err(type_error) => {
return Err(ResolverError::TypeError(type_error));
}
Expand All @@ -64,9 +75,7 @@ impl Visitor<Type, ResolverError> for ResolvedVarDefTable {
let t1 = e1.visit(self)?.unwrap();

match binop_type_resolver(binop, &t0, &t1) {
Ok(type_) => {
type_
}
Ok(type_) => type_,
Err(type_error) => {
return Err(ResolverError::TypeError(type_error));
}
Expand All @@ -86,7 +95,7 @@ impl Visitor<Type, ResolverError> for ResolvedVarDefTable {
| ASTNodeEnum::FnCall(_)
| ASTNodeEnum::AtomicExpression(_) => return Ok((true, None)),

| ASTNodeEnum::NamePath(_)
ASTNodeEnum::NamePath(_)
| ASTNodeEnum::Reference(_)
| ASTNodeEnum::StructDef(_)
| ASTNodeEnum::LiteralValue(_)
Expand Down
13 changes: 5 additions & 8 deletions src/front/passes/types/topological_sort.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::collections::HashSet;
use std::rc::Rc;
use either::Either;
use crate::front::ast_types::GlobalResolvedName;
use crate::front::passes::types::var_def_table::VarDefTable;

use either::Either;
use std::collections::HashSet;
use std::rc::Rc;

fn topological_sort_visit(
name: &Rc<GlobalResolvedName>,
Expand All @@ -26,9 +25,7 @@ fn topological_sort_visit(
result.push(Rc::clone(name));
}

pub fn topological_sort(
table: &VarDefTable,
) -> Vec<Rc<GlobalResolvedName>> {
pub fn topological_sort(table: &VarDefTable) -> Vec<Rc<GlobalResolvedName>> {
let mut result = Vec::new();
let mut visited = HashSet::new();

Expand All @@ -39,4 +36,4 @@ pub fn topological_sort(
}

result
}
}
2 changes: 1 addition & 1 deletion src/front/passes/types/type_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use crate::front::ast_types::{
UnOp,
};
use crate::front::exporter::export::FrontProgram;
use crate::front::passes::types::var_def_table::VarTypeNode;
use either::Left;
use std::collections::HashMap;
use std::rc::Rc;
use crate::front::passes::types::var_def_table::VarTypeNode;

struct BinOpNode {
pub op: BinOp,
Expand Down
8 changes: 4 additions & 4 deletions src/front/passes/types/var_def_table.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::collections::HashMap;
use std::rc::Rc;
use either::Either;
use crate::front::ast_types::{Expression, GlobalResolvedName, Type};
use crate::front::exporter::export::FrontProgram;
use crate::front::passes::types::type_expression::TypeDependency;
use either::Either;
use std::collections::HashMap;
use std::rc::Rc;

pub struct VarTypeNode {
pub types_: Either<Type, TypeDependency>,
Expand Down Expand Up @@ -65,4 +65,4 @@ impl VarDefTable {
);
}
}
}
}

0 comments on commit 86ba310

Please sign in to comment.