1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (C) 2019-2021 Aleo Systems Inc.
// This file is part of the Leo library.

// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// 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/>.

//! This module defines a statement node in an asg.
//!
//! Ast statement nodes can be directly converted into asg nodes with no major differences.

mod assign;
pub use assign::*;

mod block;
pub use block::*;

mod conditional;
pub use conditional::*;

mod console;
pub use console::*;

mod definition;
pub use definition::*;

mod expression;
pub use expression::*;

mod iteration;
pub use iteration::*;

mod return_;
pub use return_::*;

use crate::{AsgConvertError, FromAst, Node, PartialType, Scope, Span};

use std::sync::Arc;

#[derive(Debug)]
pub enum Statement {
    Return(ReturnStatement),
    Definition(DefinitionStatement),
    Assign(AssignStatement),
    Conditional(ConditionalStatement),
    Iteration(IterationStatement),
    Console(ConsoleStatement),
    Expression(ExpressionStatement),
    Block(BlockStatement),
}

impl Node for Statement {
    fn span(&self) -> Option<&Span> {
        use Statement::*;
        match self {
            Return(s) => s.span(),
            Definition(s) => s.span(),
            Assign(s) => s.span(),
            Conditional(s) => s.span(),
            Iteration(s) => s.span(),
            Console(s) => s.span(),
            Expression(s) => s.span(),
            Block(s) => s.span(),
        }
    }
}

impl FromAst<leo_ast::Statement> for Arc<Statement> {
    fn from_ast(
        scope: &Scope,
        value: &leo_ast::Statement,
        _expected_type: Option<PartialType>,
    ) -> Result<Arc<Statement>, AsgConvertError> {
        use leo_ast::Statement::*;
        Ok(match value {
            Return(statement) => Arc::new(Statement::Return(ReturnStatement::from_ast(scope, statement, None)?)),
            Definition(statement) => Arc::<Statement>::from_ast(scope, statement, None)?,
            Assign(statement) => Arc::<Statement>::from_ast(scope, statement, None)?,
            Conditional(statement) => Arc::new(Statement::Conditional(ConditionalStatement::from_ast(
                scope, statement, None,
            )?)),
            Iteration(statement) => Arc::<Statement>::from_ast(scope, statement, None)?,
            Console(statement) => Arc::new(Statement::Console(ConsoleStatement::from_ast(scope, statement, None)?)),
            Expression(statement) => Arc::new(Statement::Expression(ExpressionStatement::from_ast(
                scope, statement, None,
            )?)),
            Block(statement) => Arc::new(Statement::Block(BlockStatement::from_ast(scope, statement, None)?)),
        })
    }
}

impl Into<leo_ast::Statement> for &Statement {
    fn into(self) -> leo_ast::Statement {
        use Statement::*;
        match self {
            Return(statement) => leo_ast::Statement::Return(statement.into()),
            Definition(statement) => leo_ast::Statement::Definition(statement.into()),
            Assign(statement) => leo_ast::Statement::Assign(statement.into()),
            Conditional(statement) => leo_ast::Statement::Conditional(statement.into()),
            Iteration(statement) => leo_ast::Statement::Iteration(statement.into()),
            Console(statement) => leo_ast::Statement::Console(statement.into()),
            Expression(statement) => leo_ast::Statement::Expression(statement.into()),
            Block(statement) => leo_ast::Statement::Block(statement.into()),
        }
    }
}