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

Introduces a new syntax for invoking the finalize block. #2172

Merged
merged 15 commits into from
Nov 23, 2022
  •  
  •  
  •  
3 changes: 0 additions & 3 deletions compiler/ast/src/passes/consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ pub trait StatementConsumer {
Statement::Decrement(stmt) => self.consume_decrement(stmt),
Statement::Definition(stmt) => self.consume_definition(stmt),
Statement::Expression(stmt) => self.consume_expression_statement(stmt),
Statement::Finalize(stmt) => self.consume_finalize(stmt),
Statement::Increment(stmt) => self.consume_increment(stmt),
Statement::Iteration(stmt) => self.consume_iteration(*stmt),
Statement::Return(stmt) => self.consume_return(stmt),
Expand All @@ -98,8 +97,6 @@ pub trait StatementConsumer {

fn consume_expression_statement(&mut self, input: ExpressionStatement) -> Self::Output;

fn consume_finalize(&mut self, input: FinalizeStatement) -> Self::Output;

fn consume_increment(&mut self, input: IncrementStatement) -> Self::Output;

fn consume_iteration(&mut self, input: IterationStatement) -> Self::Output;
Expand Down
21 changes: 6 additions & 15 deletions compiler/ast/src/passes/reconstructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ pub trait StatementReconstructor: ExpressionReconstructor {
Statement::Decrement(stmt) => self.reconstruct_decrement(stmt),
Statement::Definition(stmt) => self.reconstruct_definition(stmt),
Statement::Expression(stmt) => self.reconstruct_expression_statement(stmt),
Statement::Finalize(stmt) => self.reconstruct_finalize(stmt),
Statement::Increment(stmt) => self.reconstruct_increment(stmt),
Statement::Iteration(stmt) => self.reconstruct_iteration(*stmt),
Statement::Return(stmt) => self.reconstruct_return(stmt),
Expand Down Expand Up @@ -270,20 +269,6 @@ pub trait StatementReconstructor: ExpressionReconstructor {
)
}

fn reconstruct_finalize(&mut self, input: FinalizeStatement) -> (Statement, Self::AdditionalOutput) {
(
Statement::Finalize(FinalizeStatement {
arguments: input
.arguments
.into_iter()
.map(|arg| self.reconstruct_expression(arg).0)
.collect(),
span: input.span,
}),
Default::default(),
)
}

fn reconstruct_increment(&mut self, input: IncrementStatement) -> (Statement, Self::AdditionalOutput) {
(
Statement::Increment(IncrementStatement {
Expand Down Expand Up @@ -317,6 +302,12 @@ pub trait StatementReconstructor: ExpressionReconstructor {
(
Statement::Return(ReturnStatement {
expression: self.reconstruct_expression(input.expression).0,
finalize_arguments: input.finalize_arguments.map(|arguments| {
arguments
.into_iter()
.map(|argument| self.reconstruct_expression(argument).0)
.collect()
}),
span: input.span,
}),
Default::default(),
Expand Down
12 changes: 5 additions & 7 deletions compiler/ast/src/passes/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ pub trait StatementVisitor<'a>: ExpressionVisitor<'a> {
Statement::Decrement(stmt) => self.visit_decrement(stmt),
Statement::Definition(stmt) => self.visit_definition(stmt),
Statement::Expression(stmt) => self.visit_expression_statement(stmt),
Statement::Finalize(stmt) => self.visit_finalize(stmt),
Statement::Increment(stmt) => self.visit_increment(stmt),
Statement::Iteration(stmt) => self.visit_iteration(stmt),
Statement::Return(stmt) => self.visit_return(stmt),
Expand Down Expand Up @@ -177,12 +176,6 @@ pub trait StatementVisitor<'a>: ExpressionVisitor<'a> {
self.visit_expression(&input.expression, &Default::default());
}

fn visit_finalize(&mut self, input: &'a FinalizeStatement) {
input.arguments.iter().for_each(|expr| {
self.visit_expression(expr, &Default::default());
});
}

fn visit_increment(&mut self, input: &'a IncrementStatement) {
self.visit_expression(&input.amount, &Default::default());
self.visit_expression(&input.index, &Default::default());
Expand All @@ -197,6 +190,11 @@ pub trait StatementVisitor<'a>: ExpressionVisitor<'a> {

fn visit_return(&mut self, input: &'a ReturnStatement) {
self.visit_expression(&input.expression, &Default::default());
if let Some(arguments) = &input.finalize_arguments {
arguments.iter().for_each(|argument| {
self.visit_expression(argument, &Default::default());
})
}
}
}

Expand Down
46 changes: 0 additions & 46 deletions compiler/ast/src/statement/finalize.rs

This file was deleted.

8 changes: 0 additions & 8 deletions compiler/ast/src/statement/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ pub use definition::*;
pub mod expression;
pub use expression::*;

pub mod finalize;
pub use finalize::*;

pub mod increment;
pub use increment::*;

Expand Down Expand Up @@ -71,8 +68,6 @@ pub enum Statement {
Definition(DefinitionStatement),
/// An expression statement
Expression(ExpressionStatement),
/// A finalize statement.
Finalize(FinalizeStatement),
/// An increment statement.
Increment(IncrementStatement),
/// A `for` statement.
Expand Down Expand Up @@ -101,7 +96,6 @@ impl fmt::Display for Statement {
Statement::Decrement(x) => x.fmt(f),
Statement::Definition(x) => x.fmt(f),
Statement::Expression(x) => x.fmt(f),
Statement::Finalize(x) => x.fmt(f),
Statement::Increment(x) => x.fmt(f),
Statement::Iteration(x) => x.fmt(f),
Statement::Return(x) => x.fmt(f),
Expand All @@ -120,7 +114,6 @@ impl Node for Statement {
Decrement(n) => n.span(),
Definition(n) => n.span(),
Expression(n) => n.span(),
Finalize(n) => n.span(),
Increment(n) => n.span(),
Iteration(n) => n.span(),
Return(n) => n.span(),
Expand All @@ -137,7 +130,6 @@ impl Node for Statement {
Decrement(n) => n.set_span(span),
Definition(n) => n.set_span(span),
Expression(n) => n.set_span(span),
Finalize(n) => n.set_span(span),
Increment(n) => n.set_span(span),
Iteration(n) => n.set_span(span),
Return(n) => n.set_span(span),
Expand Down
2 changes: 2 additions & 0 deletions compiler/ast/src/statement/return_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ use std::fmt;
pub struct ReturnStatement {
/// The expression to return to the function caller.
pub expression: Expression,
/// Arguments to the finalize block.
pub finalize_arguments: Option<Vec<Expression>>,
/// The span of `return expression` excluding the semicolon.
pub span: Span,
}
Expand Down
42 changes: 26 additions & 16 deletions compiler/parser/src/parser/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,15 @@ impl ParserContext<'_> {
pub(crate) fn parse_statement(&mut self) -> Result<Statement> {
match &self.token.token {
Token::Return => Ok(Statement::Return(self.parse_return_statement()?)),
Token::Async => Ok(Statement::Finalize(self.parse_finalize_statement()?)),
// If a finalize token is found without a preceding async token, return an error.
Token::Finalize => Err(ParserError::finalize_without_async(self.token.span).into()),
Token::Increment => Ok(Statement::Increment(self.parse_increment_statement()?)),
Token::Decrement => Ok(Statement::Decrement(self.parse_decrement_statement()?)),
Token::If => Ok(Statement::Conditional(self.parse_conditional_statement()?)),
Token::For => Ok(Statement::Iteration(Box::new(self.parse_loop_statement()?))),
Token::Console => Ok(Statement::Console(self.parse_console_statement()?)),
Token::Let | Token::Const => Ok(Statement::Definition(self.parse_definition_statement()?)),
Token::LeftCurly => Ok(Statement::Block(self.parse_block()?)),
Token::Async => Err(ParserError::async_finalize_is_deprecated(self.token.span).into()),
Token::Finalize => Err(ParserError::finalize_statements_are_deprecated(self.token.span).into()),
_ => Ok(self.parse_assign_statement()?),
}
}
Expand Down Expand Up @@ -117,25 +116,36 @@ impl ParserContext<'_> {
/// Returns a [`ReturnStatement`] AST node if the next tokens represent a return statement.
fn parse_return_statement(&mut self) -> Result<ReturnStatement> {
let start = self.expect(&Token::Return)?;

let expression = match self.token.token {
// If the next token is a semicolon, implicitly return a unit expression, `()`.
Token::Semicolon => Expression::Unit(UnitExpression { span: self.token.span }),
Token::Semicolon | Token::Then => Expression::Unit(UnitExpression { span: self.token.span }),
// Otherwise, attempt to parse an expression.
_ => self.parse_expression()?,
};
self.expect(&Token::Semicolon)?;
let span = start + expression.span();
Ok(ReturnStatement { span, expression })
}

/// Returns a [`FinalizeStatement`] AST node if the next tokens represent a finalize statement.
fn parse_finalize_statement(&mut self) -> Result<FinalizeStatement> {
self.expect(&Token::Async)?;
let start = self.expect(&Token::Finalize)?;
let (arguments, _, span) = self.parse_paren_comma_list(|p| p.parse_expression().map(Some))?;
self.expect(&Token::Semicolon)?;
let span = start + span;
Ok(FinalizeStatement { span, arguments })
let finalize_args = match self.token.token {
Token::Then => {
// Parse `then`.
self.expect(&Token::Then)?;
// Parse `finalize`.
self.expect(&Token::Finalize)?;
// Parse finalize arguments if they exist.
match self.token.token {
Token::Semicolon => Some(vec![]),
Token::LeftParen => Some(self.parse_paren_comma_list(|p| p.parse_expression().map(Some))?.0),
_ => Some(vec![self.parse_expression()?]),
}
}
_ => None,
};
let end = self.expect(&Token::Semicolon)?;
let span = start + end;
Ok(ReturnStatement {
span,
expression,
finalize_arguments: finalize_args,
})
}

/// Returns a [`DecrementStatement`] AST node if the next tokens represent a decrement statement.
Expand Down
1 change: 1 addition & 0 deletions compiler/parser/src/tokenizer/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ impl Token {
"self" => Token::SelfLower,
"string" => Token::String,
"struct" => Token::Struct,
"then" => Token::Then,
"transition" => Token::Transition,
"true" => Token::True,
"u8" => Token::U8,
Expand Down
3 changes: 2 additions & 1 deletion compiler/parser/src/tokenizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ mod tests {
string
struct
test
then
transition
true
u128
Expand Down Expand Up @@ -163,7 +164,7 @@ mod tests {

assert_eq!(
output,
r#""test" "test{}test" "test{}" "{}test" "test{" "test}" "test{test" "test}test" "te{{}}" test_ident 12345 address async bool const else false field finalize for function group i128 i64 i32 i16 i8 if in input let mut private program public return scalar self string struct test transition true u128 u64 u32 u16 u8 console ! != && ( ) * ** + , - -> => _ . .. / : ; < <= = == > >= [ ] { { } } || ? @ // test
r#""test" "test{}test" "test{}" "{}test" "test{" "test}" "test{test" "test}test" "te{{}}" test_ident 12345 address async bool const else false field finalize for function group i128 i64 i32 i16 i8 if in input let mut private program public return scalar self string struct test then transition true u128 u64 u32 u16 u8 console ! != && ( ) * ** + , - -> => _ . .. / : ; < <= = == > >= [ ] { { } } || ? @ // test
/* test */ // "#
);
});
Expand Down
4 changes: 4 additions & 0 deletions compiler/parser/src/tokenizer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ pub enum Token {
SelfLower,
Static,
Struct,
Then,
Transition,
// For imports.
Leo,
Expand Down Expand Up @@ -182,6 +183,7 @@ pub const KEYWORD_TOKENS: &[Token] = &[
Token::Static,
Token::String,
Token::Struct,
Token::Then,
Token::Transition,
Token::True,
Token::U8,
Expand Down Expand Up @@ -236,6 +238,7 @@ impl Token {
Token::Static => sym::Static,
Token::String => sym::string,
Token::Struct => sym::Struct,
Token::Then => sym::then,
Token::Transition => sym::transition,
Token::True => sym::True,
Token::U8 => sym::u8,
Expand Down Expand Up @@ -355,6 +358,7 @@ impl fmt::Display for Token {
SelfLower => write!(f, "self"),
Static => write!(f, "static"),
Struct => write!(f, "struct"),
Then => write!(f, "then"),
Transition => write!(f, "transition"),
Leo => write!(f, "leo"),
Eof => write!(f, "<eof>"),
Expand Down
38 changes: 20 additions & 18 deletions compiler/passes/src/code_generation/visit_statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use crate::CodeGenerator;

use leo_ast::{
AssignStatement, Block, ConditionalStatement, ConsoleFunction, ConsoleStatement, DecrementStatement,
DefinitionStatement, Expression, ExpressionStatement, FinalizeStatement, IncrementStatement, IterationStatement,
Mode, Output, ReturnStatement, Statement,
DefinitionStatement, Expression, ExpressionStatement, IncrementStatement, IterationStatement, Mode, Output,
ReturnStatement, Statement,
};

use itertools::Itertools;
Expand All @@ -35,15 +35,14 @@ impl<'a> CodeGenerator<'a> {
Statement::Decrement(stmt) => self.visit_decrement(stmt),
Statement::Definition(stmt) => self.visit_definition(stmt),
Statement::Expression(stmt) => self.visit_expression_statement(stmt),
Statement::Finalize(stmt) => self.visit_finalize(stmt),
Statement::Increment(stmt) => self.visit_increment(stmt),
Statement::Iteration(stmt) => self.visit_iteration(stmt),
Statement::Return(stmt) => self.visit_return(stmt),
}
}

fn visit_return(&mut self, input: &'a ReturnStatement) -> String {
match input.expression {
let mut instructions = match input.expression {
// Skip empty return statements.
Expression::Unit(_) => String::new(),
_ => {
Expand Down Expand Up @@ -100,7 +99,24 @@ impl<'a> CodeGenerator<'a> {

expression_instructions
}
};

// Output a finalize instruction if needed.
// TODO: Check formatting.
if let Some(arguments) = &input.finalize_arguments {
let mut finalize_instruction = "\n finalize".to_string();

for argument in arguments.iter() {
let (argument, argument_instructions) = self.visit_expression(argument);
write!(finalize_instruction, " {}", argument).expect("failed to write to string");
instructions.push_str(&argument_instructions);
}
writeln!(finalize_instruction, ";").expect("failed to write to string");

instructions.push_str(&finalize_instruction);
}

instructions
}

fn visit_definition(&mut self, _input: &'a DefinitionStatement) -> String {
Expand Down Expand Up @@ -137,20 +153,6 @@ impl<'a> CodeGenerator<'a> {
instructions
}

fn visit_finalize(&mut self, input: &'a FinalizeStatement) -> String {
let mut instructions = String::new();
let mut finalize_instruction = " finalize".to_string();

for argument in input.arguments.iter() {
let (argument, argument_instructions) = self.visit_expression(argument);
write!(finalize_instruction, " {argument}").expect("failed to write to string");
instructions.push_str(&argument_instructions);
}
writeln!(finalize_instruction, ";").expect("failed to write to string");

finalize_instruction
}

fn visit_assign(&mut self, input: &'a AssignStatement) -> String {
match (&input.place, &input.value) {
(Expression::Identifier(identifier), _) => {
Expand Down
Loading