diff --git a/ast/src/leo.pest b/ast/src/leo.pest index 49980f8aba..d4f2ca7af1 100644 --- a/ast/src/leo.pest +++ b/ast/src/leo.pest @@ -362,7 +362,7 @@ formatted_string = { // Declared in macros/formatted_container.rs formatted_container = { "{" ~ "}"} -// Declared in marcros/formatted_parameter.rs +// Declared in macros/formatted_parameter.rs formatted_parameter = { expression } // Declared in macros/macro_symbol.rs diff --git a/ast/src/macros/formatted_parameter.rs b/ast/src/macros/formatted_parameter.rs index f88e333cde..d69359638c 100644 --- a/ast/src/macros/formatted_parameter.rs +++ b/ast/src/macros/formatted_parameter.rs @@ -7,7 +7,7 @@ use std::fmt; #[derive(Clone, Debug, FromPest, PartialEq)] #[pest_ast(rule(Rule::formatted_parameter))] pub struct FormattedParameter<'ast> { - expression: Expression<'ast>, + pub expression: Expression<'ast>, #[pest_ast(outer())] pub span: Span<'ast>, } diff --git a/ast/src/statements/assert_statement.rs b/ast/src/statements/macro_statement.rs similarity index 100% rename from ast/src/statements/assert_statement.rs rename to ast/src/statements/macro_statement.rs diff --git a/ast/src/statements/mod.rs b/ast/src/statements/mod.rs index 1b6670e273..9cd2273adb 100644 --- a/ast/src/statements/mod.rs +++ b/ast/src/statements/mod.rs @@ -1,5 +1,5 @@ -pub mod assert_statement; -pub use assert_statement::*; +pub mod macro_statement; +pub use macro_statement::*; pub mod assign_statement; pub use assign_statement::*; diff --git a/compiler/src/statement/statement.rs b/compiler/src/statement/statement.rs index 853c9ea3ea..6a4851042c 100644 --- a/compiler/src/statement/statement.rs +++ b/compiler/src/statement/statement.rs @@ -78,6 +78,9 @@ impl> ConstrainedProgram { self.enforce_assert_eq_statement(cs, indicator, &resolved_left, &resolved_right, span)?; } + Statement::Macro(macro_) => { + println!("{}", macro_); + } Statement::Expression(expression, span) => { let expression_string = expression.to_string(); let value = self.enforce_expression(cs, file_scope, function_scope, &vec![], expression)?; diff --git a/types/src/lib.rs b/types/src/lib.rs index 02ec446817..4aea3db985 100644 --- a/types/src/lib.rs +++ b/types/src/lib.rs @@ -19,6 +19,9 @@ pub use self::imports::*; pub mod inputs; pub use self::inputs::*; +pub mod macros; +pub use self::macros::*; + pub mod program; pub use self::program::*; diff --git a/types/src/macros/debug.rs b/types/src/macros/debug.rs new file mode 100644 index 0000000000..acda9bfed5 --- /dev/null +++ b/types/src/macros/debug.rs @@ -0,0 +1,19 @@ +use leo_ast::macros::Debug as AstDebug; + +use serde::{Deserialize, Serialize}; +use std::fmt; + +#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct Debug {} + +impl<'ast> From> for Debug { + fn from(_debug: AstDebug<'ast>) -> Self { + Self {} + } +} + +impl fmt::Display for Debug { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "debug") + } +} diff --git a/types/src/macros/formatted_container.rs b/types/src/macros/formatted_container.rs new file mode 100644 index 0000000000..7be6537ac9 --- /dev/null +++ b/types/src/macros/formatted_container.rs @@ -0,0 +1,24 @@ +use crate::Span; +use leo_ast::macros::FormattedContainer as AstFormattedContainer; + +use serde::{Deserialize, Serialize}; +use std::fmt; + +#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct FormattedContainer { + pub span: Span, +} + +impl<'ast> From> for FormattedContainer { + fn from(container: AstFormattedContainer<'ast>) -> Self { + Self { + span: Span::from(container.span), + } + } +} + +impl fmt::Display for FormattedContainer { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{{}}") + } +} diff --git a/types/src/macros/formatted_error.rs b/types/src/macros/formatted_error.rs new file mode 100644 index 0000000000..e2fb0a242c --- /dev/null +++ b/types/src/macros/formatted_error.rs @@ -0,0 +1,19 @@ +use leo_ast::macros::Error as AstError; + +use serde::{Deserialize, Serialize}; +use std::fmt; + +#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct FormattedError {} + +impl<'ast> From> for FormattedError { + fn from(_error: AstError<'ast>) -> Self { + Self {} + } +} + +impl fmt::Display for FormattedError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "error") + } +} diff --git a/types/src/macros/formatted_macro.rs b/types/src/macros/formatted_macro.rs new file mode 100644 index 0000000000..c66e118cf0 --- /dev/null +++ b/types/src/macros/formatted_macro.rs @@ -0,0 +1,33 @@ +use crate::{FormattedString, MacroName, Span}; +use leo_ast::macros::FormattedMacro as AstFormattedMacro; + +use serde::{Deserialize, Serialize}; +use std::fmt; + +#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct FormattedMacro { + pub name: MacroName, + pub string: Option, + pub span: Span, +} + +impl<'ast> From> for FormattedMacro { + fn from(formatted: AstFormattedMacro<'ast>) -> Self { + Self { + name: MacroName::from(formatted.name), + string: formatted.string.map(|string| FormattedString::from(string)), + span: Span::from(formatted.span), + } + } +} + +impl fmt::Display for FormattedMacro { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!( + f, + "{}!({});", + self.name, + self.string.as_ref().map(|s| s.to_string()).unwrap_or("".to_string()), + ) + } +} diff --git a/types/src/macros/formatted_parameter.rs b/types/src/macros/formatted_parameter.rs new file mode 100644 index 0000000000..baa192b8ba --- /dev/null +++ b/types/src/macros/formatted_parameter.rs @@ -0,0 +1,26 @@ +use crate::{Expression, Span}; +use leo_ast::macros::FormattedParameter as AstFormattedParameter; + +use serde::{Deserialize, Serialize}; +use std::fmt; + +#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct FormattedParameter { + pub expression: Expression, + pub span: Span, +} + +impl<'ast> From> for FormattedParameter { + fn from(parameter: AstFormattedParameter<'ast>) -> Self { + Self { + expression: Expression::from(parameter.expression), + span: Span::from(parameter.span), + } + } +} + +impl fmt::Display for FormattedParameter { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.expression) + } +} diff --git a/types/src/macros/formatted_string.rs b/types/src/macros/formatted_string.rs new file mode 100644 index 0000000000..61bb48e8ec --- /dev/null +++ b/types/src/macros/formatted_string.rs @@ -0,0 +1,43 @@ +use crate::{FormattedContainer, FormattedParameter, Span}; +use leo_ast::macros::FormattedString as AstFormattedString; + +use serde::{Deserialize, Serialize}; +use std::fmt; + +#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct FormattedString { + pub string: String, + pub containers: Vec, + pub parameters: Vec, + pub span: Span, +} + +impl<'ast> From> for FormattedString { + fn from(formatted: AstFormattedString<'ast>) -> Self { + let string = formatted.string; + let span = Span::from(formatted.span); + let containers = formatted + .containers + .into_iter() + .map(|container| FormattedContainer::from(container)) + .collect(); + let parameters = formatted + .parameters + .into_iter() + .map(|parameter| FormattedParameter::from(parameter)) + .collect(); + + Self { + string, + containers, + parameters, + span, + } + } +} + +impl fmt::Display for FormattedString { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.string) + } +} diff --git a/types/src/macros/macro_name.rs b/types/src/macros/macro_name.rs new file mode 100644 index 0000000000..771e05c4fe --- /dev/null +++ b/types/src/macros/macro_name.rs @@ -0,0 +1,32 @@ +use crate::{Debug, FormattedError, PrintLine}; +use leo_ast::macros::MacroName as AstMacroName; + +use serde::{Deserialize, Serialize}; +use std::fmt; + +#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)] +pub enum MacroName { + Debug(Debug), + Error(FormattedError), + PrintLine(PrintLine), +} + +impl<'ast> From> for MacroName { + fn from(name: AstMacroName<'ast>) -> Self { + match name { + AstMacroName::Debug(debug) => MacroName::Debug(Debug::from(debug)), + AstMacroName::Error(error) => MacroName::Error(FormattedError::from(error)), + AstMacroName::PrintLine(print_line) => MacroName::PrintLine(PrintLine::from(print_line)), + } + } +} + +impl fmt::Display for MacroName { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + MacroName::Debug(ref debug) => write!(f, "{}", debug), + MacroName::Error(ref error) => write!(f, "{}", error), + MacroName::PrintLine(ref print_line) => write!(f, "{}", print_line), + } + } +} diff --git a/types/src/macros/mod.rs b/types/src/macros/mod.rs new file mode 100644 index 0000000000..288bf4b031 --- /dev/null +++ b/types/src/macros/mod.rs @@ -0,0 +1,23 @@ +pub mod debug; +pub use debug::*; + +pub mod formatted_container; +pub use formatted_container::*; + +pub mod formatted_error; +pub use formatted_error::*; + +pub mod formatted_macro; +pub use formatted_macro::*; + +pub mod formatted_string; +pub use formatted_string::*; + +pub mod formatted_parameter; +pub use formatted_parameter::*; + +pub mod macro_name; +pub use macro_name::*; + +pub mod print_line; +pub use print_line::*; diff --git a/types/src/macros/print_line.rs b/types/src/macros/print_line.rs new file mode 100644 index 0000000000..257dc41001 --- /dev/null +++ b/types/src/macros/print_line.rs @@ -0,0 +1,19 @@ +use leo_ast::macros::PrintLine as AstPrintLine; + +use serde::{Deserialize, Serialize}; +use std::fmt; + +#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct PrintLine {} + +impl<'ast> From> for PrintLine { + fn from(_print_line: AstPrintLine<'ast>) -> Self { + Self {} + } +} + +impl fmt::Display for PrintLine { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "println") + } +} diff --git a/types/src/statements/statement.rs b/types/src/statements/statement.rs index 7e83891c29..399c8f3441 100644 --- a/types/src/statements/statement.rs +++ b/types/src/statements/statement.rs @@ -1,4 +1,4 @@ -use crate::{Assignee, ConditionalStatement, Declare, Expression, Identifier, Span, Variable}; +use crate::{Assignee, ConditionalStatement, Declare, Expression, FormattedMacro, Identifier, Span, Variable}; use leo_ast::{ common::Return, operations::AssignOperation, @@ -27,6 +27,7 @@ pub enum Statement { Conditional(ConditionalStatement, Span), Iteration(Identifier, Expression, Expression, Vec, Span), AssertEq(Expression, Expression, Span), + Macro(FormattedMacro), Expression(Expression, Span), } @@ -170,15 +171,14 @@ impl<'ast> From> for Statement { impl<'ast> From> for Statement { fn from(statement: MacroStatement<'ast>) -> Self { - println!("{}", statement); - // match statement { - // MacroStatement::AssertEq(assert_eq) => Statement::AssertEq( - // Expression::from(assert_eq.left), - // Expression::from(assert_eq.right), - // Span::from(assert_eq.span), - // ), - // } - unimplemented!() + match statement { + MacroStatement::AssertEq(assert_eq) => Statement::AssertEq( + Expression::from(assert_eq.left), + Expression::from(assert_eq.right), + Span::from(assert_eq.span), + ), + MacroStatement::Formatted(formatted) => Statement::Macro(FormattedMacro::from(formatted)), + } } } @@ -247,6 +247,7 @@ impl fmt::Display for Statement { write!(f, "\t}}") } Statement::AssertEq(ref left, ref right, ref _span) => write!(f, "assert_eq({}, {});", left, right), + Statement::Macro(ref macro_) => write!(f, "{}", macro_), Statement::Expression(ref expression, ref _span) => write!(f, "{};", expression), } }