Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] Use the Consumer pattern for code generation. #2282

Open
wants to merge 24 commits into
base: testnet3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 0 additions & 40 deletions compiler/ast/src/access/associated_constant_access.rs

This file was deleted.

27 changes: 0 additions & 27 deletions compiler/ast/src/access/mod.rs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,17 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.

use crate::{access::*, Node};
pub mod associated_function_access;
pub use associated_function_access::*;

pub mod member_access;
pub use member_access::*;

pub mod tuple_access;
pub use tuple_access::*;

use crate::Node;

use leo_span::Span;

use serde::{Deserialize, Serialize};
Expand All @@ -27,8 +37,6 @@ pub enum AccessExpression {
// Array(ArrayAccess),
// /// An expression accessing a range of an array.
// ArrayRange(ArrayRangeAccess),
/// Access to an associated variable of a struct e.g `u8::MAX`.
AssociatedConstant(AssociatedConstant),
/// Access to an associated function of a struct e.g `Pedersen64::hash()`.
AssociatedFunction(AssociatedFunction),
/// An expression accessing a field in a structure, e.g., `struct_var.field`.
Expand All @@ -40,7 +48,6 @@ pub enum AccessExpression {
impl Node for AccessExpression {
fn span(&self) -> Span {
match self {
AccessExpression::AssociatedConstant(n) => n.span(),
AccessExpression::AssociatedFunction(n) => n.span(),
AccessExpression::Member(n) => n.span(),
AccessExpression::Tuple(n) => n.span(),
Expand All @@ -49,7 +56,6 @@ impl Node for AccessExpression {

fn set_span(&mut self, span: Span) {
match self {
AccessExpression::AssociatedConstant(n) => n.set_span(span),
AccessExpression::AssociatedFunction(n) => n.set_span(span),
AccessExpression::Member(n) => n.set_span(span),
AccessExpression::Tuple(n) => n.set_span(span),
Expand All @@ -62,7 +68,6 @@ impl fmt::Display for AccessExpression {
use AccessExpression::*;

match self {
AssociatedConstant(access) => access.fmt(f),
AssociatedFunction(access) => access.fmt(f),
Member(access) => access.fmt(f),
Tuple(access) => access.fmt(f),
Expand Down
12 changes: 6 additions & 6 deletions compiler/ast/src/expressions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.

use crate::{Identifier, Node};
use leo_span::Span;

use serde::{Deserialize, Serialize};
use std::fmt;

mod access;
pub use access::*;

Expand Down Expand Up @@ -50,6 +44,12 @@ pub use unit::*;
mod literal;
pub use literal::*;

use crate::{Identifier, Node};
use leo_span::Span;

use serde::{Deserialize, Serialize};
use std::fmt;

/// Expression that evaluates to a value.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum Expression {
Expand Down
3 changes: 0 additions & 3 deletions compiler/ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
#![doc = include_str!("../README.md")]
extern crate core;

pub mod access;
pub use self::access::*;

pub mod r#struct;
pub use self::r#struct::*;

Expand Down
14 changes: 13 additions & 1 deletion compiler/ast/src/passes/consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@ pub trait ExpressionConsumer {
}
}

fn consume_access(&mut self, _input: AccessExpression) -> Self::Output;
fn consume_access(&mut self, input: AccessExpression) -> Self::Output {
match input {
AccessExpression::AssociatedFunction(function) => self.consume_associated_function(function),
AccessExpression::Member(member) => self.consume_member_access(member),
AccessExpression::Tuple(tuple) => self.consume_tuple_access(tuple),
}
}

fn consume_associated_function(&mut self, _input: AssociatedFunction) -> Self::Output;

fn consume_binary(&mut self, _input: BinaryExpression) -> Self::Output;

Expand All @@ -55,10 +63,14 @@ pub trait ExpressionConsumer {

fn consume_literal(&mut self, _input: Literal) -> Self::Output;

fn consume_member_access(&mut self, _input: MemberAccess) -> Self::Output;

fn consume_ternary(&mut self, _input: TernaryExpression) -> Self::Output;

fn consume_tuple(&mut self, _input: TupleExpression) -> Self::Output;

fn consume_tuple_access(&mut self, _input: TupleAccess) -> Self::Output;

fn consume_unary(&mut self, _input: UnaryExpression) -> Self::Output;

fn consume_unit(&mut self, _input: UnitExpression) -> Self::Output;
Expand Down
65 changes: 40 additions & 25 deletions compiler/ast/src/passes/reconstructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,25 @@ pub trait ExpressionReconstructor {
}

fn reconstruct_access(&mut self, input: AccessExpression) -> (Expression, Self::AdditionalOutput) {
match input {
AccessExpression::AssociatedFunction(function) => self.reconstruct_associated_function(function),
AccessExpression::Member(member) => self.reconstruct_member_access(member),
AccessExpression::Tuple(tuple) => self.reconstruct_tuple_access(tuple),
}
}

fn reconstruct_associated_function(&mut self, input: AssociatedFunction) -> (Expression, Self::AdditionalOutput) {
(
Expression::Access(match input {
AccessExpression::AssociatedFunction(function) => {
AccessExpression::AssociatedFunction(AssociatedFunction {
ty: function.ty,
name: function.name,
args: function
.args
.into_iter()
.map(|arg| self.reconstruct_expression(arg).0)
.collect(),
span: function.span,
})
}
AccessExpression::Member(member) => AccessExpression::Member(MemberAccess {
inner: Box::new(self.reconstruct_expression(*member.inner).0),
name: member.name,
span: member.span,
}),
AccessExpression::Tuple(tuple) => AccessExpression::Tuple(TupleAccess {
tuple: Box::new(self.reconstruct_expression(*tuple.tuple).0),
index: tuple.index,
span: tuple.span,
}),
expr => expr,
}),
Expression::Access(AccessExpression::AssociatedFunction(AssociatedFunction {
ty: input.ty,
name: input.name,
args: input
.args
.into_iter()
.map(|arg| self.reconstruct_expression(arg).0)
.collect(),
span: input.span,
})),
Default::default(),
)
}
Expand Down Expand Up @@ -115,6 +108,17 @@ pub trait ExpressionReconstructor {
(Expression::Literal(input), Default::default())
}

fn reconstruct_member_access(&mut self, input: MemberAccess) -> (Expression, Self::AdditionalOutput) {
(
Expression::Access(AccessExpression::Member(MemberAccess {
inner: Box::new(self.reconstruct_expression(*input.inner).0),
name: input.name,
span: input.span,
})),
Default::default(),
)
}

fn reconstruct_ternary(&mut self, input: TernaryExpression) -> (Expression, Self::AdditionalOutput) {
(
Expression::Ternary(TernaryExpression {
Expand All @@ -141,6 +145,17 @@ pub trait ExpressionReconstructor {
)
}

fn reconstruct_tuple_access(&mut self, input: TupleAccess) -> (Expression, Self::AdditionalOutput) {
(
Expression::Access(AccessExpression::Tuple(TupleAccess {
tuple: Box::new(self.reconstruct_expression(*input.tuple).0),
index: input.index,
span: input.span,
})),
Default::default(),
)
}

fn reconstruct_unary(&mut self, input: UnaryExpression) -> (Expression, Self::AdditionalOutput) {
(
Expression::Unary(UnaryExpression {
Expand Down
37 changes: 24 additions & 13 deletions compiler/ast/src/passes/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,21 @@ pub trait ExpressionVisitor<'a> {

fn visit_access(&mut self, input: &'a AccessExpression, additional: &Self::AdditionalInput) -> Self::Output {
match input {
AccessExpression::AssociatedFunction(function) => {
function.args.iter().for_each(|arg| {
self.visit_expression(arg, &Default::default());
});
}
AccessExpression::Member(member) => {
self.visit_expression(&member.inner, additional);
}
AccessExpression::Tuple(tuple) => {
self.visit_expression(&tuple.tuple, additional);
}
_ => {}
}
AccessExpression::AssociatedFunction(function) => self.visit_associated_function(function, additional),
AccessExpression::Member(member) => self.visit_member_access(member, additional),
AccessExpression::Tuple(tuple) => self.visit_tuple_access(tuple, additional),
};
Default::default()
}

fn visit_associated_function(
&mut self,
input: &'a AssociatedFunction,
additional: &Self::AdditionalInput,
) -> Self::Output {
input.args.iter().for_each(|arg| {
self.visit_expression(arg, additional);
});
Default::default()
}

Expand Down Expand Up @@ -89,6 +90,11 @@ pub trait ExpressionVisitor<'a> {
Default::default()
}

fn visit_member_access(&mut self, input: &'a MemberAccess, additional: &Self::AdditionalInput) -> Self::Output {
self.visit_expression(&input.inner, additional);
Default::default()
}

fn visit_ternary(&mut self, input: &'a TernaryExpression, additional: &Self::AdditionalInput) -> Self::Output {
self.visit_expression(&input.condition, additional);
self.visit_expression(&input.if_true, additional);
Expand All @@ -103,6 +109,11 @@ pub trait ExpressionVisitor<'a> {
Default::default()
}

fn visit_tuple_access(&mut self, input: &'a TupleAccess, additional: &Self::AdditionalInput) -> Self::Output {
self.visit_expression(&input.tuple, additional);
Default::default()
}

fn visit_unary(&mut self, input: &'a UnaryExpression, additional: &Self::AdditionalInput) -> Self::Output {
self.visit_expression(&input.receiver, additional);
Default::default()
Expand Down
3 changes: 2 additions & 1 deletion compiler/compiler/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ impl<'a> Compiler<'a> {
self.parse_program()?;
let (symbol_table, struct_graph, call_graph) = self.compiler_stages()?;

let bytecode = CodeGenerator::do_pass((&self.ast, &symbol_table, &struct_graph, &call_graph))?;
let bytecode =
CodeGenerator::do_pass((std::mem::take(&mut self.ast), &symbol_table, &struct_graph, &call_graph))?;

Ok((symbol_table, bytecode))
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/compiler/tests/utilities/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ pub fn compile_and_process<'a>(parsed: &'a mut Compiler<'a>) -> Result<String, L
let _ = parsed.function_inlining_pass(&call_graph, assigner)?;

// Compile Leo program to bytecode.
let bytecode = CodeGenerator::do_pass((&parsed.ast, &st, &struct_graph, &call_graph))?;
let bytecode = CodeGenerator::do_pass((std::mem::take(&mut parsed.ast), &st, &struct_graph, &call_graph))?;

Ok(bytecode)
}
27 changes: 13 additions & 14 deletions compiler/parser/src/parser/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,25 +319,24 @@ impl ParserContext<'_> {
let member_name = self.expect_identifier()?;

// Check if there are arguments.
Ok(Expression::Access(if self.check(&Token::LeftParen) {
if self.check(&Token::LeftParen) {
// Parse the arguments
let (args, _, end) = self.parse_expr_tuple()?;

// Return the struct function.
AccessExpression::AssociatedFunction(AssociatedFunction {
span: module_name.span() + end,
ty: type_,
name: member_name,
args,
})
Ok(Expression::Access(AccessExpression::AssociatedFunction(
AssociatedFunction {
span: module_name.span() + end,
ty: type_,
name: member_name,
args,
},
)))
} else {
// Return the struct constant.
AccessExpression::AssociatedConstant(AssociatedConstant {
span: module_name.span() + member_name.span(),
ty: type_,
name: member_name,
})
}))
// Attempted to parse an associated constant, e.g `Foo::MAX`.
// These are not supported in the Leo language.
Err(ParserError::invalid_associated_access(&module_name, module_name.span()).into())
}
}

/// Parses a tuple of `Expression` AST nodes.
Expand Down
Loading