Skip to content

Commit

Permalink
Add separate expression kind types (FuelLabs#2507)
Browse files Browse the repository at this point in the history
* factor Expression into a Span and an ExpressionKind

* box large ExpressionKind variants

* fix clippy lint

* factor some functions out of expr_to_expression
  • Loading branch information
canndrew authored Aug 11, 2022
1 parent 87e0a48 commit 00b8d61
Show file tree
Hide file tree
Showing 15 changed files with 857 additions and 758 deletions.
5 changes: 3 additions & 2 deletions sway-ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub mod where_clause;
pub use crate::{
assignable::Assignable,
attribute::AttributeDecl,
brackets::{AngleBrackets, Braces},
brackets::{AngleBrackets, Braces, Parens},
dependency::Dependency,
expr::{
asm::{AsmBlock, AsmRegisterDeclaration},
Expand All @@ -44,11 +44,12 @@ pub use crate::{
item_use::{ItemUse, UseTree},
FnArg, FnArgs, FnSignature, Item, ItemKind, TypeField,
},
keywords::{DoubleColonToken, PubToken},
keywords::{CommaToken, DoubleColonToken, PubToken},
literal::{LitInt, LitIntType, Literal},
module::{Module, ModuleKind},
path::{PathExpr, PathExprSegment, PathType, PathTypeSegment, QualifiedPathRoot},
pattern::{Pattern, PatternStructField},
punctuated::Punctuated,
statement::{Statement, StatementLet},
ty::Ty,
where_clause::{WhereBound, WhereClause},
Expand Down
885 changes: 496 additions & 389 deletions sway-core/src/convert_parse_tree.rs

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion sway-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,11 @@ fn test_unary_ordering() {
} = &prog.root.tree.root_nodes[0]
{
if let AstNode {
content: AstNodeContent::Expression(Expression::LazyOperator { op, .. }),
content:
AstNodeContent::Expression(Expression {
kind: ExpressionKind::LazyOperator(LazyOperatorExpression { op, .. }),
..
}),
..
} = &body.contents[2]
{
Expand Down
8 changes: 1 addition & 7 deletions sway-core/src/parse_tree/declaration/reassignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,7 @@ pub struct Reassignment {
impl Reassignment {
pub fn lhs_span(&self) -> Span {
match &self.lhs {
ReassignmentTarget::VariableExpression(var) => match **var {
Expression::SubfieldExpression { ref span, .. } => span.clone(),
Expression::VariableExpression { ref name, .. } => name.span(),
_ => {
unreachable!("any other reassignment lhs is invalid and cannot be constructed.")
}
},
ReassignmentTarget::VariableExpression(var) => var.span.clone(),
ReassignmentTarget::StorageField(ref idents) => idents
.iter()
.fold(idents[0].span(), |acc, ident| Span::join(acc, ident.span())),
Expand Down
224 changes: 108 additions & 116 deletions sway-core/src/parse_tree/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,81 +17,114 @@ use sway_ast::intrinsics::Intrinsic;

/// Represents a parsed, but not yet type checked, [Expression](https://en.wikipedia.org/wiki/Expression_(computer_science)).
#[derive(Debug, Clone)]
pub enum Expression {
Literal {
value: Literal,
span: Span,
},
FunctionApplication {
call_path_binding: TypeBinding<CallPath>,
arguments: Vec<Expression>,
span: Span,
},
LazyOperator {
op: LazyOp,
lhs: Box<Expression>,
rhs: Box<Expression>,
span: Span,
},
VariableExpression {
name: Ident,
span: Span,
},
Tuple {
fields: Vec<Expression>,
span: Span,
},
TupleIndex {
prefix: Box<Expression>,
index: usize,
index_span: Span,
span: Span,
},
Array {
contents: Vec<Expression>,
span: Span,
},
StructExpression {
call_path_binding: TypeBinding<CallPath<(TypeInfo, Span)>>,
fields: Vec<StructExpressionField>,
span: Span,
},
CodeBlock {
contents: CodeBlock,
span: Span,
},
IfExp {
condition: Box<Expression>,
then: Box<Expression>,
r#else: Option<Box<Expression>>,
span: Span,
},
MatchExp {
value: Box<Expression>,
branches: Vec<MatchBranch>,
span: Span,
},
pub struct Expression {
pub kind: ExpressionKind,
pub span: Span,
}

#[derive(Debug, Clone)]
pub struct FunctionApplicationExpression {
pub call_path_binding: TypeBinding<CallPath>,
pub arguments: Vec<Expression>,
}

#[derive(Debug, Clone)]
pub struct LazyOperatorExpression {
pub op: LazyOp,
pub lhs: Box<Expression>,
pub rhs: Box<Expression>,
}

#[derive(Debug, Clone)]
pub struct TupleIndexExpression {
pub prefix: Box<Expression>,
pub index: usize,
pub index_span: Span,
}

#[derive(Debug, Clone)]
pub struct StructExpression {
pub call_path_binding: TypeBinding<CallPath<(TypeInfo, Span)>>,
pub fields: Vec<StructExpressionField>,
}

#[derive(Debug, Clone)]
pub struct IfExpression {
pub condition: Box<Expression>,
pub then: Box<Expression>,
pub r#else: Option<Box<Expression>>,
}

#[derive(Debug, Clone)]
pub struct MatchExpression {
pub value: Box<Expression>,
pub branches: Vec<MatchBranch>,
}

#[derive(Debug, Clone)]
pub struct MethodApplicationExpression {
pub method_name_binding: TypeBinding<MethodName>,
pub contract_call_params: Vec<StructExpressionField>,
pub arguments: Vec<Expression>,
}

#[derive(Debug, Clone)]
pub struct SubfieldExpression {
pub prefix: Box<Expression>,
pub field_to_access: Ident,
}

#[derive(Debug, Clone)]
pub struct DelineatedPathExpression {
pub call_path_binding: TypeBinding<CallPath>,
pub args: Vec<Expression>,
}

#[derive(Debug, Clone)]
pub struct AbiCastExpression {
pub abi_name: CallPath,
pub address: Box<Expression>,
}

#[derive(Debug, Clone)]
pub struct ArrayIndexExpression {
pub prefix: Box<Expression>,
pub index: Box<Expression>,
}

#[derive(Debug, Clone)]
pub struct StorageAccessExpression {
pub field_names: Vec<Ident>,
}

#[derive(Debug, Clone)]
pub struct IntrinsicFunctionExpression {
pub kind_binding: TypeBinding<Intrinsic>,
pub arguments: Vec<Expression>,
}

#[derive(Debug, Clone)]
pub enum ExpressionKind {
Literal(Literal),
FunctionApplication(Box<FunctionApplicationExpression>),
LazyOperator(LazyOperatorExpression),
Variable(Ident),
Tuple(Vec<Expression>),
TupleIndex(TupleIndexExpression),
Array(Vec<Expression>),
Struct(Box<StructExpression>),
CodeBlock(CodeBlock),
If(IfExpression),
Match(MatchExpression),
// separated into other struct for parsing reasons
AsmExpression {
span: Span,
asm: AsmExpression,
},
MethodApplication {
method_name_binding: TypeBinding<MethodName>,
contract_call_params: Vec<StructExpressionField>,
arguments: Vec<Expression>,
span: Span,
},
Asm(Box<AsmExpression>),
MethodApplication(Box<MethodApplicationExpression>),
/// A _subfield expression_ is anything of the form:
/// ```ignore
/// <ident>.<ident>
/// ```
///
SubfieldExpression {
prefix: Box<Expression>,
span: Span,
field_to_access: Ident,
},
Subfield(SubfieldExpression),
/// A _delineated path_ is anything of the form:
/// ```ignore
/// <ident>::<ident>
Expand All @@ -113,31 +146,12 @@ pub enum Expression {
///
/// MyEnum::Variant1
/// ```
DelineatedPath {
call_path_binding: TypeBinding<CallPath>,
args: Vec<Expression>,
span: Span,
},
DelineatedPath(Box<DelineatedPathExpression>),
/// A cast of a hash to an ABI for calling a contract.
AbiCast {
abi_name: CallPath,
address: Box<Expression>,
span: Span,
},
ArrayIndex {
prefix: Box<Expression>,
index: Box<Expression>,
span: Span,
},
StorageAccess {
field_names: Vec<Ident>,
span: Span,
},
IntrinsicFunction {
kind_binding: TypeBinding<Intrinsic>,
arguments: Vec<Expression>,
span: Span,
},
AbiCast(Box<AbiCastExpression>),
ArrayIndex(ArrayIndexExpression),
StorageAccess(StorageAccessExpression),
IntrinsicFunction(IntrinsicFunctionExpression),
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
Expand All @@ -155,29 +169,7 @@ pub struct StructExpressionField {

impl Spanned for Expression {
fn span(&self) -> Span {
use Expression::*;
(match self {
Literal { span, .. } => span,
FunctionApplication { span, .. } => span,
LazyOperator { span, .. } => span,
VariableExpression { span, .. } => span,
Tuple { span, .. } => span,
TupleIndex { span, .. } => span,
Array { span, .. } => span,
StructExpression { span, .. } => span,
CodeBlock { span, .. } => span,
IfExp { span, .. } => span,
MatchExp { span, .. } => span,
AsmExpression { span, .. } => span,
MethodApplication { span, .. } => span,
SubfieldExpression { span, .. } => span,
DelineatedPath { span, .. } => span,
AbiCast { span, .. } => span,
ArrayIndex { span, .. } => span,
StorageAccess { span, .. } => span,
IntrinsicFunction { span, .. } => span,
})
.clone()
self.span.clone()
}
}

Expand Down
Loading

0 comments on commit 00b8d61

Please sign in to comment.