Skip to content

Commit

Permalink
Rename semantics to semantic_analysis and move return statements to t…
Browse files Browse the repository at this point in the history
…he proper module (FuelLabs#36)

* rename semantics module to semantic_analysis

* move return_statements module to the correct module

Co-authored-by: Alexander Hansen <[email protected]>
  • Loading branch information
sezna and Alexander Hansen authored May 9, 2021
1 parent 1227f76 commit 6874712
Show file tree
Hide file tree
Showing 36 changed files with 98 additions and 86 deletions.
2 changes: 1 addition & 1 deletion core_lang/src/asm_generation/declaration/reassignment.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;
use crate::{
asm_generation::{convert_expression_to_asm, AsmNamespace, RegisterSequencer},
semantics::ast_node::TypedReassignment,
semantic_analysis::ast_node::TypedReassignment,
};

pub(crate) fn convert_reassignment_to_asm<'sc>(
Expand Down
2 changes: 1 addition & 1 deletion core_lang/src/asm_generation/declaration/var_decl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
asm_generation::{convert_expression_to_asm, AsmNamespace, RegisterSequencer},
asm_lang::Op,
error::*,
semantics::ast_node::TypedVariableDeclaration,
semantic_analysis::ast_node::TypedVariableDeclaration,
};

/// Provisions a register to put a variable in, and then adds the assembly used to initialize the
Expand Down
4 changes: 2 additions & 2 deletions core_lang/src/asm_generation/expression/enum_instantiation.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::asm_generation::{convert_expression_to_asm, AsmNamespace, RegisterSequencer};
use crate::asm_lang::{ConstantRegister, Op, Opcode, RegisterId};
use crate::error::*;
use crate::semantics::ast_node::TypedEnumDeclaration;
use crate::semantics::TypedExpression;
use crate::semantic_analysis::ast_node::TypedEnumDeclaration;
use crate::semantic_analysis::TypedExpression;
use crate::Literal;
use crate::{CompileResult, Ident};
use std::convert::TryFrom;
Expand Down
2 changes: 1 addition & 1 deletion core_lang/src/asm_generation/expression/if_exp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::asm_generation::{convert_expression_to_asm, AsmNamespace, RegisterSeq
use crate::asm_lang::{ConstantRegister, Op, RegisterId};
use crate::error::*;

use crate::semantics::TypedExpression;
use crate::semantic_analysis::TypedExpression;

use crate::CompileResult;

Expand Down
2 changes: 1 addition & 1 deletion core_lang/src/asm_generation/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::*;
use crate::{asm_lang::*, parse_tree::CallPath};
use crate::{
parse_tree::Literal,
semantics::{
semantic_analysis::{
ast_node::{TypedAsmRegisterDeclaration, TypedCodeBlock, TypedExpressionVariant},
TypedExpression,
},
Expand Down
2 changes: 1 addition & 1 deletion core_lang/src/asm_generation/expression/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
asm_generation::{convert_expression_to_asm, AsmNamespace, RegisterSequencer},
asm_lang::{ConstantRegister, Op, RegisterId},
error::*,
semantics::ast_node::TypedStructExpressionField,
semantic_analysis::ast_node::TypedStructExpressionField,
CompileResult, Ident,
};
use std::convert::TryInto;
Expand Down
2 changes: 1 addition & 1 deletion core_lang/src/asm_generation/expression/subfield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
};
use crate::{
parse_tree::Literal,
semantics::{
semantic_analysis::{
ast_node::{TypedAsmRegisterDeclaration, TypedCodeBlock, TypedExpressionVariant},
TypedExpression,
},
Expand Down
2 changes: 1 addition & 1 deletion core_lang/src/asm_generation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
asm_lang::{Label, Op, OrganizationalOp, RegisterId},
error::*,
parse_tree::Literal,
semantics::{TypedAstNode, TypedAstNodeContent, TypedParseTree},
semantic_analysis::{TypedAstNode, TypedAstNodeContent, TypedParseTree},
Ident,
};
use either::Either;
Expand Down
2 changes: 1 addition & 1 deletion core_lang/src/asm_generation/while_loop.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;
use crate::asm_lang::{ConstantRegister, Op};
use crate::semantics::ast_node::TypedWhileLoop;
use crate::semantic_analysis::ast_node::TypedWhileLoop;
pub(super) fn convert_while_loop_to_asm<'sc>(
r#loop: &TypedWhileLoop<'sc>,
namespace: &mut AsmNamespace<'sc>,
Expand Down
4 changes: 2 additions & 2 deletions core_lang/src/control_flow_analysis/analyze_return_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use super::*;
use super::{ControlFlowGraph, EntryPoint, ExitPoint, Graph};
use crate::semantics::{
use crate::semantic_analysis::{
ast_node::{
TypedCodeBlock, TypedDeclaration, TypedExpression, TypedFunctionDeclaration,
TypedReassignment, TypedVariableDeclaration, TypedWhileLoop,
Expand All @@ -12,7 +12,7 @@ use crate::semantics::{
};
use crate::types::ResolvedType;
use crate::Ident;
use crate::{error::*, semantics::TypedParseTree};
use crate::{error::*, semantic_analysis::TypedParseTree};
use pest::Span;
use petgraph::prelude::NodeIndex;

Expand Down
8 changes: 5 additions & 3 deletions core_lang/src/control_flow_analysis/dead_code_analysis.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use super::*;

use crate::semantics::ast_node::TypedStructExpressionField;
use crate::semantic_analysis::ast_node::TypedStructExpressionField;
use crate::types::ResolvedType;
use crate::{
parse_tree::Visibility,
semantics::ast_node::{TypedExpressionVariant, TypedStructDeclaration, TypedTraitDeclaration},
semantic_analysis::ast_node::{
TypedExpressionVariant, TypedStructDeclaration, TypedTraitDeclaration,
},
CompileError, Ident, TreeType,
};
use crate::{
semantics::{
semantic_analysis::{
ast_node::{
TypedCodeBlock, TypedDeclaration, TypedEnumDeclaration, TypedExpression,
TypedFunctionDeclaration, TypedReassignment, TypedVariableDeclaration, TypedWhileLoop,
Expand Down
4 changes: 2 additions & 2 deletions core_lang/src/control_flow_analysis/flow_graph/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! This is the flow graph, a graph which contains edges that represent possible steps of program
//! execution.
use crate::semantics::{ast_node::TypedStructField, TypedAstNode};
use crate::{semantics::ast_node::TypedEnumVariant, Ident};
use crate::semantic_analysis::{ast_node::TypedStructField, TypedAstNode};
use crate::{semantic_analysis::ast_node::TypedEnumVariant, Ident};
use pest::Span;

use petgraph::{graph::EdgeIndex, prelude::NodeIndex};
Expand Down
2 changes: 1 addition & 1 deletion core_lang/src/control_flow_analysis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
//!
//! # # Terms
//! # # # Node
//! A node is any [crate::semantics::TypedAstNode], with some [crate::semantics::TypedAstNodeContent].
//! A node is any [crate::semantic_analysis::TypedAstNode], with some [crate::semantic_analysis::TypedAstNodeContent].
//! # # # Dominating nodes
//! A dominating node is a node which all previous nodes pass through. These are what we are
//! concerned about in control flow analysis. More formally,
Expand Down
38 changes: 3 additions & 35 deletions core_lang/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod control_flow_analysis;
mod ident;
mod parse_tree;
mod parser;
mod semantics;
mod semantic_analysis;

use crate::parse_tree::*;
use crate::parser::{HllParser, Rule};
Expand All @@ -18,7 +18,7 @@ pub use asm_generation::{AbstractInstructionSet, FinalizedAsm, HllAsmSet};
use control_flow_analysis::ControlFlowGraph;
use pest::iterators::Pair;
use pest::Parser;
use semantics::{TreeType, TypedParseTree};
use semantic_analysis::{TreeType, TypedParseTree};
use std::collections::HashMap;

pub(crate) mod types;
Expand All @@ -28,7 +28,7 @@ pub(crate) use crate::parse_tree::{Expression, UseStatement, WhileLoop};
pub use error::{CompileError, CompileResult, CompileWarning};
pub use ident::Ident;
pub use pest::Span;
pub use semantics::{Namespace, TypedDeclaration, TypedFunctionDeclaration};
pub use semantic_analysis::{Namespace, TypedDeclaration, TypedFunctionDeclaration};
pub use types::TypeInfo;

// todo rename to language name
Expand Down Expand Up @@ -79,38 +79,6 @@ pub(crate) enum AstNodeContent<'sc> {
WhileLoop(WhileLoop<'sc>),
}

#[derive(Debug, Clone)]
struct ReturnStatement<'sc> {
expr: Expression<'sc>,
}

impl<'sc> ReturnStatement<'sc> {
fn parse_from_pair(pair: Pair<'sc, Rule>) -> CompileResult<'sc, Self> {
let span = pair.as_span();
let mut warnings = Vec::new();
let mut errors = Vec::new();
let mut inner = pair.into_inner();
let _ret_keyword = inner.next();
let expr = inner.next();
let res = match expr {
None => ReturnStatement {
expr: Expression::Unit { span },
},
Some(expr_pair) => {
let expr = eval!(
Expression::parse_from_pair,
warnings,
errors,
expr_pair,
Expression::Unit { span }
);
ReturnStatement { expr }
}
};
ok(res, warnings, errors)
}
}

impl<'sc> ParseTree<'sc> {
pub(crate) fn new(span: Span<'sc>) -> Self {
ParseTree {
Expand Down
4 changes: 3 additions & 1 deletion core_lang/src/parse_tree/code_block.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use super::WhileLoop;
use crate::parser::Rule;
use crate::{
error::*, parse_tree::Expression, AstNode, AstNodeContent, Declaration, ReturnStatement,
error::*,
parse_tree::{Expression, ReturnStatement},
AstNode, AstNodeContent, Declaration,
};
use pest::iterators::Pair;
use pest::Span;
Expand Down
6 changes: 4 additions & 2 deletions core_lang/src/parse_tree/declaration/enum_declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ use crate::parser::Rule;
use crate::types::TypeInfo;
use crate::Ident;
use crate::Namespace;
use crate::{error::*, semantics::ast_node::TypedEnumDeclaration};
use crate::{parse_tree::declaration::TypeParameter, semantics::ast_node::TypedEnumVariant};
use crate::{error::*, semantic_analysis::ast_node::TypedEnumDeclaration};
use crate::{
parse_tree::declaration::TypeParameter, semantic_analysis::ast_node::TypedEnumVariant,
};
use inflector::cases::classcase::is_class_case;
use pest::iterators::Pair;
use pest::Span;
Expand Down
2 changes: 2 additions & 0 deletions core_lang/src/parse_tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod code_block;
mod declaration;
mod expression;
mod literal;
mod return_statement;
mod use_statement;
mod while_loop;

Expand All @@ -11,5 +12,6 @@ pub(crate) use code_block::*;
pub(crate) use declaration::*;
pub(crate) use expression::*;
pub(crate) use literal::Literal;
pub(crate) use return_statement::*;
pub(crate) use use_statement::{ImportType, UseStatement};
pub(crate) use while_loop::WhileLoop;
36 changes: 36 additions & 0 deletions core_lang/src/parse_tree/return_statement.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use crate::error::ok;
use crate::parser::Rule;
use crate::{CompileResult, Expression};
use pest::iterators::Pair;

#[derive(Debug, Clone)]
pub(crate) struct ReturnStatement<'sc> {
pub(crate) expr: Expression<'sc>,
}

impl<'sc> ReturnStatement<'sc> {
pub(crate) fn parse_from_pair(pair: Pair<'sc, Rule>) -> CompileResult<'sc, Self> {
let span = pair.as_span();
let mut warnings = Vec::new();
let mut errors = Vec::new();
let mut inner = pair.into_inner();
let _ret_keyword = inner.next();
let expr = inner.next();
let res = match expr {
None => ReturnStatement {
expr: Expression::Unit { span },
},
Some(expr_pair) => {
let expr = eval!(
Expression::parse_from_pair,
warnings,
errors,
expr_pair,
Expression::Unit { span }
);
ReturnStatement { expr }
}
};
ok(res, warnings, errors)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::{
IsConstant, TypedCodeBlock, TypedExpression, TypedExpressionVariant, TypedReturnStatement,
};
use crate::parse_tree::*;
use crate::semantics::Namespace;
use crate::semantic_analysis::Namespace;
use crate::{error::*, types::ResolvedType, Ident};
use pest::Span;

Expand Down Expand Up @@ -360,24 +360,24 @@ impl<'sc> TypedFunctionDeclaration<'sc> {
}
}
// handle the return statement(s)
let return_statements: Vec<(&TypedExpression, &pest::Span<'sc>)> =
body.contents
.iter()
.filter_map(|x| {
if let crate::semantics::TypedAstNode {
content:
crate::semantics::TypedAstNodeContent::ReturnStatement(
TypedReturnStatement { ref expr },
),
span,
} = x
{
Some((expr, span))
} else {
None
}
})
.collect();
let return_statements: Vec<(&TypedExpression, &pest::Span<'sc>)> = body
.contents
.iter()
.filter_map(|x| {
if let crate::semantic_analysis::TypedAstNode {
content:
crate::semantic_analysis::TypedAstNodeContent::ReturnStatement(
TypedReturnStatement { ref expr },
),
span,
} = x
{
Some((expr, span))
} else {
None
}
})
.collect();
for (stmt, span) in return_statements {
let convertability = stmt.return_type.is_convertible(
&return_type,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::error::*;
use crate::semantics::ast_node::*;
use crate::semantic_analysis::ast_node::*;

/// Given an enum declaration and the instantiation expression/type arguments, construct a valid
/// [TypedExpression].
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::*;
use crate::semantics::ast_node::TypedCodeBlock;
use crate::semantic_analysis::ast_node::TypedCodeBlock;
use either::Either;

#[allow(dead_code)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::semantics::TypedExpression;
use crate::semantic_analysis::TypedExpression;

#[allow(dead_code)]
#[derive(Clone, Debug)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::semantics::TypedExpression;
use crate::semantic_analysis::TypedExpression;
use crate::Ident;

#[derive(Clone, Debug)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::*;
use crate::semantics::ast_node::*;
use crate::semantic_analysis::ast_node::*;
use crate::types::{IntegerBits, ResolvedType};
use either::Either;

Expand Down Expand Up @@ -698,7 +698,7 @@ impl<'sc> TypedExpression<'sc> {
}
}
a => {
println!("Unimplemented semantics for expression: {:?}", a);
println!("Unimplemented semantic_analysis for expression: {:?}", a);
errors.push(CompileError::Unimplemented(
"Unimplemented expression",
a.span(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;
use crate::parse_tree::AsmOp;
use crate::semantics::ast_node::*;
use crate::semantic_analysis::ast_node::*;
use crate::Ident;
#[derive(Clone, Debug)]
pub(crate) enum TypedExpressionVariant<'sc> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::{
ERROR_RECOVERY_DECLARATION,
};
use crate::parse_tree::ImplTrait;
use crate::semantics::{Namespace, TypedDeclaration, TypedFunctionDeclaration};
use crate::semantic_analysis::{Namespace, TypedDeclaration, TypedFunctionDeclaration};
use crate::{error::*, types::ResolvedType, Ident};

pub(crate) fn implementation_of_trait<'sc>(
Expand Down
Loading

0 comments on commit 6874712

Please sign in to comment.